-
Notifications
You must be signed in to change notification settings - Fork 551
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
Tokens #11449
Merged
Merged
Tokens #11449
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0dfeb89
[snarkyjs] - Create token account and token id
MartinMinkov 6b0c0e8
[snarkyjs] - Add tokens test script
MartinMinkov 2c0b7f1
[snarkyjs] - Refactor existing token functions, add tokenId to accoun…
MartinMinkov 7c5c137
[snarkyjs] - Add tokenSymbol to ocaml bindings and test script
MartinMinkov dea7b89
[snarkyjs] - Add support for Checked versions of token helpers
MartinMinkov 0280361
[snarkyjs] - Bump SnarkyJS submodule
MartinMinkov 8679fe5
[snarkyjs] compute zkapp public input with calls
mitschabaude 712e64c
[snarkyjs] rename hash_party & remove stuff
mitschabaude 2b22852
bump snarkyjs
mitschabaude 39d0e81
Merge branch 'develop' into test/tokens-playground
mitschabaude a91ec12
[snarkyjs] fix intg test
mitschabaude d1d490d
bump snarkyjs
mitschabaude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Submodule snarkyjs
updated
from a20146 to c0035b
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
199 changes: 199 additions & 0 deletions
199
src/lib/snarky_js_bindings/test_module/simple-zkapp-token.js
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 |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import { | ||
Field, | ||
declareState, | ||
declareMethods, | ||
State, | ||
PublicKey, | ||
PrivateKey, | ||
SmartContract, | ||
isReady, | ||
shutdown, | ||
Mina, | ||
Permissions, | ||
Party, | ||
UInt64, | ||
Ledger, | ||
Token, | ||
getDefaultTokenId, | ||
} from "snarkyjs"; | ||
|
||
function sendTransaction(tx) { | ||
// console.log("DEBUG -- TXN\n", JSON.stringify(partiesToJson(tx.transaction))); | ||
tx.send(); | ||
} | ||
|
||
await isReady; | ||
|
||
// declare the zkapp | ||
class SimpleZkapp extends SmartContract { | ||
constructor(address) { | ||
super(address); | ||
this.x = State(); | ||
} | ||
|
||
deploy(args) { | ||
super.deploy(args); | ||
this.setPermissions({ | ||
...Permissions.default(), | ||
editState: Permissions.proofOrSignature(), | ||
}); | ||
this.balance.addInPlace(UInt64.fromNumber(initialBalance)); | ||
this.x.set(initialState); | ||
this.tokenSymbol.set("TEST_TOKEN"); | ||
console.log( | ||
"Current tokenId while deploying: ", | ||
Ledger.fieldToBase58(this.tokenId) | ||
); | ||
} | ||
|
||
update(y) { | ||
let x = this.x.get(); | ||
this.x.set(x.add(y)); | ||
} | ||
|
||
initialize() { | ||
this.x.set(initialState); | ||
} | ||
|
||
mint(receiverAddress) { | ||
let amount = UInt64.from(1_000_000); | ||
this.token().mint({ | ||
address: receiverAddress, | ||
amount, | ||
}); | ||
console.log(`Minting ${amount} to ${receiverAddress.toBase58()}`); | ||
} | ||
|
||
burn(receiverAddress) { | ||
let amount = UInt64.from(1_000); | ||
this.token().burn({ | ||
address: receiverAddress, | ||
amount, | ||
}); | ||
console.log(`Burning ${amount} to ${receiverAddress.toBase58()}`); | ||
} | ||
|
||
send(senderAddress, receiverAddress) { | ||
let amount = UInt64.from(1_000); | ||
this.token().send({ | ||
from: senderAddress, | ||
to: receiverAddress, | ||
amount, | ||
}); | ||
console.log(`Sending ${amount} to ${receiverAddress.toBase58()}`); | ||
} | ||
} | ||
// note: this is our non-typescript way of doing what our decorators do | ||
declareState(SimpleZkapp, { x: Field }); | ||
declareMethods(SimpleZkapp, { | ||
initialize: [], | ||
update: [Field], | ||
send: [PublicKey, PublicKey], | ||
mint: [PublicKey], | ||
burn: [PublicKey], | ||
}); | ||
|
||
let Local = Mina.LocalBlockchain(); | ||
Mina.setActiveInstance(Local); | ||
|
||
// a test account that pays all the fees, and puts additional funds into the zkapp | ||
let feePayer = Local.testAccounts[0].privateKey; | ||
|
||
// the zkapp account | ||
let zkappKey = PrivateKey.fromBase58( | ||
"EKEfEZpMctKoyon4nxhqFBiKyUsCyyZReF9fbs21nDrrTgGMTcok" | ||
); | ||
let zkappAddress = zkappKey.toPublicKey(); | ||
|
||
let tokenAccount1Key = Local.testAccounts[1].privateKey; | ||
let tokenAccount1 = tokenAccount1Key.toPublicKey(); | ||
|
||
let tokenAccount2Key = Local.testAccounts[2].privateKey; | ||
let tokenAccount2 = tokenAccount2Key.toPublicKey(); | ||
|
||
let initialBalance = 10_000_000_000; | ||
let initialState = Field(1); | ||
let zkapp = new SimpleZkapp(zkappAddress); | ||
let tx; | ||
|
||
console.log("deploy"); | ||
tx = await Local.transaction(feePayer, () => { | ||
Party.fundNewAccount(feePayer, { initialBalance }); | ||
zkapp.deploy({ zkappKey }); | ||
}); | ||
sendTransaction(tx); | ||
|
||
console.log(`initial balance: ${zkapp.account.balance.get().div(1e9)} MINA`); | ||
|
||
// Log custom token info | ||
const customToken = new Token({ tokenOwner: zkappAddress }); | ||
console.log("---FEE PAYER", feePayer.toPublicKey().toBase58()); | ||
console.log("---TOKEN OWNER", zkappAddress.toBase58()); | ||
console.log("---CUSTOM TOKEN", Ledger.fieldToBase58(customToken.id)); | ||
console.log(`---TOKEN SYMBOL ${Mina.getAccount(zkappAddress).tokenSymbol}`); | ||
console.log("---TOKEN ACCOUNT1", tokenAccount1.toBase58()); | ||
console.log("---TOKEN ACCOUNT2", tokenAccount2.toBase58()); | ||
console.log( | ||
"---CUSTOM TOKEN CHECKED", | ||
Ledger.fieldToBase58( | ||
Ledger.customTokenIdChecked(zkappAddress, getDefaultTokenId()) | ||
) | ||
); | ||
console.log( | ||
"---CUSTOM TOKEN UNCHECKED", | ||
Ledger.fieldToBase58(Ledger.customTokenId(zkappAddress, getDefaultTokenId())) | ||
); | ||
|
||
console.log("----------token minting----------"); | ||
tx = await Local.transaction(feePayer, () => { | ||
Party.fundNewAccount(feePayer); | ||
zkapp.mint(tokenAccount1); | ||
zkapp.sign(zkappKey); | ||
}); | ||
sendTransaction(tx); | ||
|
||
console.log( | ||
`tokenAccount1 balance: ${Mina.getBalance( | ||
tokenAccount1, | ||
customToken.id | ||
)} custom tokens` | ||
); | ||
|
||
console.log("----------token burning----------"); | ||
tx = await Local.transaction(feePayer, () => { | ||
zkapp.burn(tokenAccount1); | ||
zkapp.sign(zkappKey); | ||
}); | ||
tx = tx.sign([tokenAccount1Key]); | ||
sendTransaction(tx); | ||
|
||
console.log( | ||
`tokenAccount1 balance: ${Mina.getBalance( | ||
tokenAccount1, | ||
customToken.id | ||
)} custom tokens` | ||
); | ||
|
||
console.log("----------token transfer----------"); | ||
tx = await Local.transaction(feePayer, () => { | ||
Party.fundNewAccount(feePayer); | ||
zkapp.send(tokenAccount1, tokenAccount2); | ||
zkapp.sign(zkappKey); | ||
}); | ||
tx = tx.sign([tokenAccount1Key, tokenAccount2Key]); | ||
sendTransaction(tx); | ||
|
||
console.log( | ||
`tokenAccount1 balance: ${Mina.getBalance( | ||
tokenAccount1, | ||
customToken.id | ||
)} custom tokens` | ||
); | ||
console.log( | ||
`tokenAccount2 balance: ${Mina.getBalance( | ||
tokenAccount2, | ||
customToken.id | ||
)} custom tokens` | ||
); | ||
|
||
shutdown(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels super hacky, does this work? I need a way to convert a
Mina_base.Token_id.Checked.t
to aField.t
. This type checks but I'm pretty sure the logic is wrong. If there is a better way, can someone advise how to do so?cc @mitschabaude @bkase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the right approach 👍🏻 Would've done the same