Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit events for all transfers #88

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions FungibleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Int64,
MerkleList,
method,
Option,
Permissions,
Provable,
PublicKey,
Expand Down Expand Up @@ -60,7 +61,7 @@ export class FungibleToken extends TokenContract {
Pause: PauseEvent,
Mint: MintEvent,
Burn: BurnEvent,
Transfer: TransferEvent,
BalanceChange: BalanceChangeEvent,
}

// We use actions and reducers for changing the circulating supply. That is to allow multiple mints/burns in a single block, which would not work if those would alter the contract state directly.
Expand Down Expand Up @@ -149,7 +150,6 @@ export class FungibleToken extends TokenContract {
async transfer(from: PublicKey, to: PublicKey, amount: UInt64) {
this.paused.getAndRequireEquals().assertFalse()
this.internal.send({ from, to, amount })
this.emitEvent("Transfer", new TransferEvent({ from, to, amount }))
}

private checkPermissionsUpdate(update: AccountUpdate) {
Expand All @@ -169,6 +169,11 @@ export class FungibleToken extends TokenContract {
let totalBalance = Int64.from(0)
this.forEachUpdate(updates, (update, usesToken) => {
this.checkPermissionsUpdate(update)
this.emitEventIf(
usesToken,
"BalanceChange",
new BalanceChangeEvent({ address: update.publicKey, amount: update.balanceChange }),
)
totalBalance = Provable.if(usesToken, totalBalance.add(update.balanceChange), totalBalance)
totalBalance.isPositiveV2().assertFalse(
"Flash-minting detected. Please make sure that your `AccountUpdate`s are ordered properly, so that tokens are not received before they are sent.",
Expand Down Expand Up @@ -254,8 +259,7 @@ export class BurnEvent extends Struct({
amount: UInt64,
}) {}

export class TransferEvent extends Struct({
from: PublicKey,
to: PublicKey,
amount: UInt64,
export class BalanceChangeEvent extends Struct({
address: PublicKey,
amount: Int64,
}) {}
16 changes: 8 additions & 8 deletions documentation/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ events = {
Pause: PauseEvent,
Mint: MintEvent,
Burn: BurnEvent,
Transfer: TransferEvent,
BalanceChange: BalanceChangeEvent,
}

export class SetAdminEvent extends Struct({
Expand All @@ -115,13 +115,13 @@ class BurnEvent extends Struct({
amount: UInt64,
}) {}

class TransferEvent extends Struct({
from: PublicKey,
to: PublicKey,
amount: UInt64,
export class BalanceChangeEvent extends Struct({
address: PublicKey,
amount: Int64,
}) {}
```

Note that `approveBase` does not emit an event. Thus, transfers where the account updates have been
constructed externally to `FungibleToken` will not have an event emitted by the `FungibleToken`
contract.
Note that `MintEvent`, `BurnEvent`, and `BalanceChangeEvent` each signal that the balance of an
account changes. The difference is that `MintEvent` and `BurnEvent` are emitted when tokens are
minted/burned, and `BalanceChangeEvent` is emitted when a transaction takes tokens from some
addresses, and sends them to others.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"check": "tsc --noEmit"
},
"peerDependencies": {
"o1js": "https://pkg.pr.new/o1-labs/o1js@9549719"
"o1js": "https://pkg.pr.new/o1-labs/o1js@8d95222"
},
"devDependencies": {
"ts-node": "^10.9.2",
Expand Down