Skip to content

Commit

Permalink
Add a method that calls the reducer and updates circulating
Browse files Browse the repository at this point in the history
  • Loading branch information
kantp committed May 8, 2024
1 parent 2c9669b commit 8542c20
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
21 changes: 21 additions & 0 deletions FungibleToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ describe("token integration", () => {
)
})

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

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

tx.sign([sender.key])
await tx.prove()
await tx.send()

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

it("should burn tokens for the sender account", async () => {
const initialBalance = (await tokenAContract.getBalanceOf(sender))
.toBigInt()
Expand Down
38 changes: 32 additions & 6 deletions FungibleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DeployArgs,
Field,
Int64,
MerkleList,
method,
PublicKey,
Reducer,
Expand Down Expand Up @@ -115,12 +116,11 @@ export class FungibleToken extends TokenContract implements FungibleTokenLike {
return balance
}

@method.returns(UInt64)
async getCirculating(): Promise<UInt64> {
let oldCirculating = this.circulating.getAndRequireEquals()
let actionState = this.actionState.getAndRequireEquals()
let pendingActions = this.reducer.getActions({ fromActionState: actionState })

/** This function is used to fold the actions from minting/burning */
private calculateCirculating(
oldCirculating: UInt64,
pendingActions: MerkleList<MerkleList<Int64>>,
): UInt64 {
let newCirculating: Int64 = this.reducer.reduce(
pendingActions,
Int64,
Expand All @@ -134,6 +134,32 @@ export class FungibleToken extends TokenContract implements FungibleTokenLike {
return newCirculating.magnitude
}

/** Reports the current circulating supply
* This does take into account currently unreduced actions.
*/
@method.returns(UInt64)
async getCirculating(): Promise<UInt64> {
let oldCirculating = this.circulating.getAndRequireEquals()
let actionState = this.actionState.getAndRequireEquals()
let pendingActions = this.reducer.getActions({ fromActionState: actionState })

let newCirculating = this.calculateCirculating(oldCirculating, pendingActions)
return newCirculating
}

/** Aggregate actions from minting and burning to update the circulating supply */
@method
async updateCirculating() {
let oldCirculating = this.circulating.getAndRequireEquals()
let actionState = this.actionState.getAndRequireEquals()
let pendingActions = this.reducer.getActions({ fromActionState: actionState })

let newCirculating = this.calculateCirculating(oldCirculating, pendingActions)

this.circulating.set(newCirculating)
this.actionState.set(pendingActions.hash)
}

@method.returns(UInt64)
async getDecimals() {
return this.decimals
Expand Down

0 comments on commit 8542c20

Please sign in to comment.