Skip to content

Commit

Permalink
Restore old API for getCirculating() / updateCirculating()
Browse files Browse the repository at this point in the history
We can reduce the code duplication without changing the API.
  • Loading branch information
kantp committed Jul 3, 2024
1 parent 9f4f908 commit ecf1497
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
42 changes: 21 additions & 21 deletions FungibleToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("token integration", async () => {
it("should mint for the sender account", async () => {
const initialBalance = (await tokenAContract.getBalanceOf(sender))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
Expand All @@ -152,19 +152,19 @@ describe("token integration", async () => {
initialBalance + mintAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating + mintAmount.toBigInt(),
)
})

it("calling the reducer should not change the reported circulating supply", async () => {
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
fee: 1e8,
}, async () => {
await tokenAContract.getCirculating(Bool(true))
await tokenAContract.updateCirculating()
})

tx.sign([sender.key])
Expand All @@ -173,15 +173,15 @@ describe("token integration", async () => {

localChain.incrementGlobalSlot(1)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating,
)
})

it("should burn tokens for the sender account", async () => {
const initialBalance = (await tokenAContract.getBalanceOf(sender))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
Expand All @@ -199,7 +199,7 @@ describe("token integration", async () => {
initialBalance - burnAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating - burnAmount.toBigInt(),
)
})
Expand Down Expand Up @@ -274,7 +274,7 @@ describe("token integration", async () => {
.toBigInt()
const initialBalanceReceiver = (await tokenAContract.getBalanceOf(receiver))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
Expand All @@ -301,7 +301,7 @@ describe("token integration", async () => {
initialBalanceReceiver + sendAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating,
)
})
Expand All @@ -322,7 +322,7 @@ describe("token integration", async () => {
.toBigInt()
const initialBalanceReceiver = (await tokenAContract.getBalanceOf(receiver))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const updateSend = AccountUpdate.createSigned(
sender,
Expand Down Expand Up @@ -353,7 +353,7 @@ describe("token integration", async () => {
initialBalanceReceiver + sendAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating,
)
})
Expand Down Expand Up @@ -460,7 +460,7 @@ describe("token integration", async () => {
.toBigInt()
const initialBalanceReceiver = (await tokenAContract.getBalanceOf(receiver))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
Expand All @@ -486,7 +486,7 @@ describe("token integration", async () => {
initialBalanceReceiver + sendAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating,
)
})
Expand All @@ -498,7 +498,7 @@ describe("token integration", async () => {
it("should deposit from the user to the token account of the third party", async () => {
const initialBalance = (await tokenAContract.getBalanceOf(sender))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const tokenId = tokenAContract.deriveTokenId()

Expand Down Expand Up @@ -533,7 +533,7 @@ describe("token integration", async () => {
initialBalance - depositAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating,
)
})
Expand All @@ -543,7 +543,7 @@ describe("token integration", async () => {
.toBigInt()
const initialBalance2 = (await tokenAContract.getBalanceOf(thirdPartyB))
.toBigInt()
const initialCirculating = (await tokenAContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenAContract.getCirculating()).toBigInt()

const transferAmount = UInt64.from(1)
const updateWithdraw = await thirdPartyAContract.withdraw(transferAmount)
Expand Down Expand Up @@ -571,7 +571,7 @@ describe("token integration", async () => {
initialBalance2 + transferAmount.toBigInt(),
)
equal(
(await tokenAContract.getCirculating(Bool(false))).toBigInt(),
(await tokenAContract.getCirculating()).toBigInt(),
initialCirculating,
)
})
Expand Down Expand Up @@ -606,7 +606,7 @@ describe("token integration", async () => {
FungibleToken.adminContract = CustomTokenAdmin
const initialBalance = (await tokenBContract.getBalanceOf(sender))
.toBigInt()
const initialCirculating = (await tokenBContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenBContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
Expand All @@ -625,7 +625,7 @@ describe("token integration", async () => {
initialBalance + mintAmount.toBigInt(),
)
equal(
(await tokenBContract.getCirculating(Bool(false))).toBigInt(),
(await tokenBContract.getCirculating()).toBigInt(),
initialCirculating + mintAmount.toBigInt(),
)
FungibleToken.adminContract = FungibleTokenAdmin
Expand All @@ -636,7 +636,7 @@ describe("token integration", async () => {
.toBigInt()
const initialBalanceReceiver = (await tokenBContract.getBalanceOf(receiver))
.toBigInt()
const initialCirculating = (await tokenBContract.getCirculating(Bool(false))).toBigInt()
const initialCirculating = (await tokenBContract.getCirculating()).toBigInt()

const tx = await Mina.transaction({
sender: sender,
Expand All @@ -663,7 +663,7 @@ describe("token integration", async () => {
initialBalanceReceiver + sendAmount.toBigInt(),
)
equal(
(await tokenBContract.getCirculating(Bool(false))).toBigInt(),
(await tokenBContract.getCirculating()).toBigInt(),
initialCirculating,
)
})
Expand Down
14 changes: 10 additions & 4 deletions FungibleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,18 @@ export class FungibleToken extends TokenContract {

/** Reports the current circulating supply
* This does take into account currently unreduced actions.
*
* If @param updateContractState is true, also updates the circulating supply in the contract state.
*/
async getCirculating(): Promise<UInt64> {
return this.getOrUpdateCirculating(Bool(false))
}

/** Aggregate actions from minting and burning to update the circulating supply */
async updateCirculating() {
this.getOrUpdateCirculating(Bool(true))
}

@method.returns(UInt64)
async getCirculating(updateContractState: Bool): Promise<UInt64> {
async getOrUpdateCirculating(updateContractState: Bool): Promise<UInt64> {
let oldCirculating = this.circulating.getAndRequireEquals()
let actionState = this.actionState.getAndRequireEquals()
let pendingActions = this.reducer.getActions({ fromActionState: actionState })
Expand All @@ -205,7 +212,6 @@ export class FungibleToken extends TokenContract {

this.circulating.set(contractCirculating)
this.actionState.set(contractActionState)

return newCirculating
}

Expand Down
6 changes: 3 additions & 3 deletions documentation/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ The user facing methods of `FungibleToken` are
@method async transfer(from: PublicKey, to: PublicKey, amount: UInt64)
@method async approveBase(updates: AccountUpdateForest): Promise<void>
@method.returns(UInt64) async getBalanceOf(address: PublicKey): Promise<UInt64>
@method.returns(UInt64) async getCirculating(updateContractState: Bool): Promise<UInt64>
@method.returns(UInt64) async getCirculating(): Promise<UInt64>
@method async updateCirculating()
@method.returns(UInt8) async getDecimals(): Promise<UInt8>
```

Expand All @@ -76,8 +77,7 @@ In order to allow multiple minting/burning transactions in a single block, we us
actions/reducer model of MINA. The `mint` and `burn` methods will modify the token balance in the
specified account. But instead of directly modifying the value of `circulating` in the contract
state, they will instead dispatches an action that instructs the reducer to modify the state. The
method `getCirculating(updateContractState:Bool)`, when supplied with
`updateContractState:Bool(true)`, collects all the actions and updates the state of the contract.
method `calculateCirculating` collects all the actions and updates the state of the contract.

## Events

Expand Down

0 comments on commit ecf1497

Please sign in to comment.