-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
145 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,119 @@ | ||
import { | ||
AccountUpdate, | ||
DeployArgs, | ||
Experimental, | ||
Field, | ||
Int64, | ||
Permissions, | ||
PublicKey, | ||
SmartContract, | ||
State, | ||
UInt64, | ||
method, | ||
state | ||
} from "o1js" | ||
|
||
interface CustomDeployArgs { | ||
symbol: Field | ||
fixedSupply: UInt64 | ||
} | ||
state, | ||
} from 'o1js' | ||
|
||
/** | ||
* | ||
* | ||
* # `Token` Smart Contract | ||
* | ||
* | ||
* The smart contract for tokens on Xane. | ||
* | ||
* **Note:** `deploy` method requires two account funding. | ||
* | ||
* | ||
* **Note:** `deploy` method requires two account funding. | ||
* | ||
* # Usage | ||
* | ||
* | ||
* ```ts | ||
* // Import `Token` smart contract. | ||
* import { Token } from 'xane' | ||
* | ||
* | ||
* // Create an instance of `Token` contract. | ||
* const token = new Token(zkAppPublicKey) | ||
* | ||
* // Deploy it with given symbol and supply. | ||
* token.deploy({ verificationKey, zkappKey, symbol, fixedSupply }) | ||
* | ||
* // Transfer tokens. | ||
* token.transfer(receiver, amount) | ||
* | ||
* | ||
* // Deploy it. | ||
* token.deploy({ verificationKey, zkappKey }) | ||
* | ||
* | ||
* ``` | ||
* | ||
* | ||
*/ | ||
export class Token extends SmartContract { | ||
@state(Field) symbol = State<Field>() | ||
enum TokenError { | ||
MaxSupplyCannotBeExceeded = 'TOKEN: Max supply cannot be exceeded.', | ||
} | ||
|
||
@state(UInt64) fixedSupply = State<UInt64>() | ||
export class Token extends SmartContract { | ||
@state(UInt64) symbol = State<Field>() | ||
@state(UInt64) decimals = State<UInt64>() | ||
@state(UInt64) maxSupply = State<UInt64>() | ||
@state(UInt64) circulatingSupply = State<UInt64>() | ||
|
||
deploy(args: DeployArgs & CustomDeployArgs) { | ||
deploy(args: DeployArgs) { | ||
super.deploy(args) | ||
|
||
this.account.permissions.set({ | ||
...Permissions.default(), | ||
editState: Permissions.proof(), | ||
setTokenSymbol: Permissions.proof(), | ||
send: Permissions.none(), | ||
receive: Permissions.proof(), | ||
access: Permissions.proof() | ||
send: Permissions.proof(), | ||
access: Permissions.proof(), | ||
}) | ||
} | ||
|
||
this.symbol.set(args.symbol) | ||
@method initialize(symbol: Field, decimals: UInt64, maxSupply: UInt64) { | ||
this.symbol.set(symbol) | ||
this.decimals.set(decimals) | ||
this.maxSupply.set(maxSupply) | ||
this.circulatingSupply.set(UInt64.from(0)) | ||
} | ||
|
||
@method mint(receiver: PublicKey, amount: UInt64) { | ||
const maxSupply = this.maxSupply.getAndRequireEquals() | ||
const circulatingSupply = this.circulatingSupply.getAndRequireEquals() | ||
|
||
const newCirculatingSupply = circulatingSupply.add(amount) | ||
|
||
newCirculatingSupply.assertLessThanOrEqual(maxSupply, TokenError.MaxSupplyCannotBeExceeded) | ||
|
||
this.token.mint({ | ||
address: this.sender, | ||
amount: args.fixedSupply | ||
address: receiver, | ||
amount, | ||
}) | ||
this.fixedSupply.set(args.fixedSupply) | ||
|
||
this.circulatingSupply.set(newCirculatingSupply) | ||
} | ||
|
||
@method transfer(from: PublicKey, to: PublicKey, amount: UInt64) { | ||
this.token.send({ | ||
from, | ||
to, | ||
amount | ||
@method burn(burner: PublicKey, amount: UInt64) { | ||
const circulatingSupply = this.circulatingSupply.getAndRequireEquals() | ||
|
||
const newCirculatingSupply = circulatingSupply.sub(amount) | ||
|
||
this.token.burn({ | ||
address: burner, | ||
amount, | ||
}) | ||
|
||
this.circulatingSupply.set(newCirculatingSupply) | ||
} | ||
|
||
@method transfer(sender: PublicKey, receiver: PublicKey, amount: UInt64) { | ||
this.token.send({ from: sender, to: receiver, amount }) | ||
} | ||
} | ||
|
||
@method transferWithCallback( | ||
sender: PublicKey, | ||
receiver: PublicKey, | ||
amount: UInt64, | ||
callback: Experimental.Callback<any> | ||
) { | ||
const tokenId = this.token.id | ||
|
||
const senderAccountUpdate = this.approve(callback, AccountUpdate.Layout.AnyChildren) | ||
senderAccountUpdate.body.tokenId.assertEquals(tokenId) | ||
senderAccountUpdate.body.publicKey.assertEquals(sender) | ||
|
||
const negativeAmount = Int64.fromObject(senderAccountUpdate.body.balanceChange) | ||
negativeAmount.assertEquals(Int64.from(amount).neg()) | ||
|
||
const receiverAccountUpdate = Experimental.createChildAccountUpdate(this.self, receiver, tokenId) | ||
receiverAccountUpdate.balance.addInPlace(amount) | ||
} | ||
} |