Skip to content

Commit

Permalink
use events in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Jul 1, 2022
1 parent 95f7129 commit e893fa2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/examples/simple_zkapp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ class SimpleZkapp extends SmartContract {
super(address);
this.x = State();
}

events = {
update: Field,
};

deploy(args) {
super.deploy(args);
this.x.set(initialState);
}
update(y) {
this.emitEvent('update', y);
this.emitEvent('update', y);
this.account.balance.assertEquals(this.account.balance.get());
let x = this.x.get();
this.x.assertEquals(x);
Expand Down
26 changes: 21 additions & 5 deletions src/examples/simple_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ import {
DeployArgs,
UInt32,
Bool,
PublicKey,
} from 'snarkyjs';

await isReady;

class SimpleZkapp extends SmartContract {
@state(Field) x = State<Field>();

events = {
update: Field,
payout: UInt64,
payoutReceiver: PublicKey,
};

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
Expand All @@ -31,6 +38,7 @@ class SimpleZkapp extends SmartContract {
}

@method update(y: Field) {
this.emitEvent('update', y);
let x = this.x.get();
this.x.assertEquals(x);
this.x.set(x.add(y));
Expand All @@ -56,6 +64,10 @@ class SimpleZkapp extends SmartContract {
let halfBalance = balance.div(2);
this.balance.subInPlace(halfBalance);
callerParty.balance.addInPlace(halfBalance);

// emit some events
this.emitEvent('payoutReceiver', callerAddress);
this.emitEvent('payout', halfBalance);
}
}

Expand All @@ -78,7 +90,7 @@ let initialState = Field(1);
let zkapp = new SimpleZkapp(zkappAddress);

console.log('deploy');
let tx = await Local.transaction(feePayer, () => {
let tx = await Mina.transaction(feePayer, () => {
Party.fundNewAccount(feePayer, { initialBalance });
zkapp.deploy({ zkappKey });
});
Expand All @@ -88,24 +100,28 @@ console.log('initial state: ' + zkapp.x.get());
console.log(`initial balance: ${zkapp.account.balance.get().div(1e9)} MINA`);

console.log('update');
tx = await Local.transaction(feePayer, () => {
tx = await Mina.transaction(feePayer, () => {
zkapp.update(Field(3));
zkapp.sign(zkappKey);
});
tx.send();

console.log(tx.toJSON());

console.log('payout');
tx = await Local.transaction(feePayer, () => {
tx = await Mina.transaction(feePayer, () => {
zkapp.payout(privilegedKey);
zkapp.sign(zkappKey);
});
tx.send();

console.log(tx.toJSON());

console.log('final state: ' + zkapp.x.get());
console.log(`final balance: ${zkapp.account.balance.get().div(1e9)} MINA`);

console.log('try to payout a second time..');
tx = await Local.transaction(feePayer, () => {
tx = await Mina.transaction(feePayer, () => {
zkapp.payout(privilegedKey);
zkapp.sign(zkappKey);
});
Expand All @@ -117,7 +133,7 @@ try {

console.log('try to payout to a different account..');
try {
tx = await Local.transaction(feePayer, () => {
tx = await Mina.transaction(feePayer, () => {
zkapp.payout(Local.testAccounts[2].privateKey);
zkapp.sign(zkappKey);
});
Expand Down

0 comments on commit e893fa2

Please sign in to comment.