-
Notifications
You must be signed in to change notification settings - Fork 143
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
Add send
API to zkApp and Party classes
#325
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
034a5a4
Add send method to Party and zkApp
MartinMinkov 7e44c01
Fix tokens test
MartinMinkov 6eb3b73
Add example script
MartinMinkov ee23a87
Make receiverParty a child of zkApp
MartinMinkov 2fce81b
Move payment script under examples/zkApps dir
MartinMinkov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { | ||
isReady, | ||
method, | ||
Mina, | ||
Party, | ||
PrivateKey, | ||
SmartContract, | ||
PublicKey, | ||
UInt64, | ||
shutdown, | ||
DeployArgs, | ||
Permissions, | ||
} from 'snarkyjs'; | ||
|
||
await isReady; | ||
|
||
class SendMINAExample extends SmartContract { | ||
deploy(args: DeployArgs) { | ||
super.deploy(args); | ||
this.setPermissions({ | ||
...Permissions.default(), | ||
editState: Permissions.proofOrSignature(), | ||
editSequenceState: Permissions.proofOrSignature(), | ||
}); | ||
this.balance.addInPlace(UInt64.fromNumber(initialBalance)); | ||
} | ||
|
||
@method sendMINA(receiverAddress: PublicKey, amount: UInt64) { | ||
this.send({ | ||
to: receiverAddress, | ||
amount, | ||
}); | ||
} | ||
} | ||
|
||
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.random(); | ||
let zkappAddress = zkappKey.toPublicKey(); | ||
|
||
let account1Key = PrivateKey.random(); | ||
let account1Address = account1Key.toPublicKey(); | ||
|
||
let account2Key = PrivateKey.random(); | ||
let account2Address = account2Key.toPublicKey(); | ||
|
||
let zkapp = new SendMINAExample(zkappAddress); | ||
let initialBalance = 10_000_000_000; | ||
let tx; | ||
|
||
console.log('deploy'); | ||
tx = await Mina.transaction(feePayer, () => { | ||
Party.fundNewAccount(feePayer, { initialBalance }); | ||
zkapp.deploy({ zkappKey }); | ||
}); | ||
tx.send(); | ||
|
||
console.log(`zkApp balance: ${Mina.getBalance(zkappAddress)} MINA`); | ||
|
||
console.log('----------MINA sending----------'); | ||
tx = await Local.transaction(feePayer, () => { | ||
Party.fundNewAccount(feePayer); | ||
zkapp.sendMINA(account1Address, UInt64.from(1_000_000)); | ||
zkapp.sign(zkappKey); | ||
}); | ||
tx.send(); | ||
|
||
console.log(`zkApp balance: ${Mina.getBalance(zkappAddress)} MINA`); | ||
console.log( | ||
`account1Address balance: ${Mina.getBalance(account1Address)} MINA` | ||
); | ||
|
||
console.log('----------MINA sending (with signed)----------'); | ||
tx = await Local.transaction(feePayer, () => { | ||
Party.fundNewAccount(feePayer); | ||
let party = Party.createSigned(zkappKey); | ||
party.send({ to: account2Address, amount: UInt64.from(1_000_000) }); | ||
zkapp.sign(zkappKey); | ||
zkapp.account.nonce.assertEquals(zkapp.account.nonce.get().add(1)); | ||
}); | ||
tx.send(); | ||
|
||
console.log(`zkApp balance: ${Mina.getBalance(zkappAddress)} MINA`); | ||
console.log( | ||
`account1Address balance: ${Mina.getBalance(account1Address)} MINA` | ||
); | ||
console.log( | ||
`account2Address balance: ${Mina.getBalance(account2Address)} MINA` | ||
); | ||
|
||
console.log('----------MINA sending (with unsigned)----------'); | ||
tx = await Local.transaction(feePayer, () => { | ||
let party = Party.createUnsigned(zkappAddress); | ||
party.signInPlace(zkappKey); | ||
party.send({ to: account2Address, amount: UInt64.from(1_000_000) }); | ||
zkapp.sign(zkappKey); | ||
zkapp.account.nonce.assertEquals(zkapp.account.nonce.get().add(1)); | ||
}); | ||
tx.send(); | ||
|
||
console.log(`zkApp balance: ${Mina.getBalance(zkappAddress)} MINA`); | ||
console.log( | ||
`account1Address balance: ${Mina.getBalance(account1Address)} MINA` | ||
); | ||
console.log( | ||
`account2Address balance: ${Mina.getBalance(account2Address)} MINA` | ||
); | ||
|
||
console.log('----------MINA sending (with proof)----------'); | ||
tx = await Local.transaction(feePayer, () => { | ||
let party = Party.createSigned(zkappKey); | ||
party.send({ to: account2Address, amount: UInt64.from(1_000_000) }); | ||
zkapp.account.nonce.assertEquals(zkapp.account.nonce.get().add(1)); | ||
}); | ||
await tx.prove(); | ||
tx.send(); | ||
|
||
console.log(`zkApp balance: ${Mina.getBalance(zkappAddress)} MINA`); | ||
console.log( | ||
`account1Address balance: ${Mina.getBalance(account1Address)} MINA` | ||
); | ||
console.log( | ||
`account2Address balance: ${Mina.getBalance(account2Address)} MINA` | ||
); | ||
|
||
shutdown(); |
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
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.
@MartinMinkov @mitschabaude do you have an opinion if we should be adding examples like these in the
src/examples/zkapps
directory or keep them insrc/examples
? I noticed apps likecomposability
andsimple_and_counter
were added to the zkapps directory and others to examples. It's not a big deal either way as long as we are in agreement.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.
good point, I think we'll accumulate more & more of those, so I'd add most of them to
/src/examples/zkapps
and reserve/src/examples
for the most fundamental examples, to not overwhelm a user who explores theseThere 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.
Sure thing, I'll move it over