Skip to content

Commit

Permalink
Tmp add token
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ondejka authored and MartinOndejka committed Aug 14, 2023
1 parent fd2f903 commit dfe824e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/ExampleToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { PublicKey, SmartContract, UInt64, method } from 'snarkyjs';
import {
AccountUpdate,
Provable,
PublicKey,
SmartContract,
UInt64,
method,
} from 'snarkyjs';

export class ExampleToken extends SmartContract {
@method mintTokens(receiverAddress: PublicKey, amount: UInt64) {
Expand All @@ -18,5 +25,25 @@ export class ExampleToken extends SmartContract {
from: senderAddress,
amount,
});

return this.self;
}

@method approveTransfer(from: AccountUpdate, to: AccountUpdate) {
Provable.log(from);
Provable.log(to);

from.body.balanceChange.sgn.isPositive().assertFalse();
to.body.balanceChange.sgn.isPositive().assertTrue();

from.body.balanceChange.magnitude.assertEquals(
to.body.balanceChange.magnitude
);

from.body.tokenId.assertEquals(this.token.id);
to.body.tokenId.assertEquals(this.token.id);

from.approve(from, AccountUpdate.Layout.NoChildren);
to.approve(from, AccountUpdate.Layout.NoChildren);
}
}
42 changes: 41 additions & 1 deletion src/ZekoBridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ describe('ZekoBridge', () => {
requests = [];
pendingActions = [];

return;
await ActionsRecursiveReducer.compile();
});

it('deploys the `ZekoBridge` zkapp', async () => {
const deployTx = await Mina.transaction(senderAddress, () => {
AccountUpdate.fundNewAccount(senderAddress, 2);
AccountUpdate.fundNewAccount(senderAddress, 3);
zkapp.deploy({ zkappKey });
tokenZkapp.deploy({ zkappKey: tokenKey });

tokenZkapp.mintTokens(senderAddress, UInt64.from(100 * MINA));
});

await deployTx.prove();
Expand All @@ -79,6 +82,43 @@ describe('ZekoBridge', () => {
expect(zkapp.actionState.get()).toEqual(Reducer.initialActionState);
});

it('dispatches token request', async () => {
// Arrange
const amount = UInt64.from(5 * MINA);

pendingActions.push(
new WrappingRequestAction({
amount,
tokenId: tokenZkapp.token.id,
receiver: senderAddress,
})
);

// Act
const tx = await Mina.transaction(senderAddress, () => {
AccountUpdate.fundNewAccount(senderAddress);
const accUpdate = tokenZkapp.sendTokens(
senderAddress,
zkappAddress,
amount
);
zkapp.createTokenWrappingRequest(pendingActions[0], accUpdate);
});

console.log(tx.toPretty());

await tx.prove();
await tx.sign([senderKey, zkappKey, tokenKey]).send();

// Assert

// Check that the zkapp's balance has increased by the amount of the three
// wrapping requests.
expect(Mina.getBalance(zkappAddress, tokenZkapp.token.id)).toEqual(amount);
});

return;

it('dispatches three wrapping requests', async () => {
// Arrange
const amount = UInt64.from(5 * MINA);
Expand Down
44 changes: 44 additions & 0 deletions src/ZekoBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,50 @@ export class ZekoBridge extends SmartContract {
this.reducer.dispatch(request);
}

@method createTokenWrappingRequest(
request: WrappingRequestAction,
accUpdate: AccountUpdate
) {
// Assert the request is for a custom token
request.tokenId.assertNotEquals(TokenId.default);

const [au1, au2] = accUpdate.children.accountUpdates;

const senderUpdate = Provable.switch(
[
au1.body.publicKey.equals(this.sender),
au2.body.publicKey.equals(this.sender),
],
AccountUpdate,
[au1, au2]
);
senderUpdate.publicKey.assertEquals(this.sender);
senderUpdate.tokenId.assertEquals(request.tokenId);

const receiverUpdate = Provable.switch(
[
au1.body.publicKey.equals(this.address),
au2.body.publicKey.equals(this.address),
],
AccountUpdate,
[au1, au2]
);
receiverUpdate.publicKey.assertEquals(this.address);
receiverUpdate.tokenId.assertEquals(request.tokenId);

request.amount.assertGreaterThan(UInt64.zero);
request.amount.assertEquals(senderUpdate.body.balanceChange.magnitude);
request.amount.assertEquals(receiverUpdate.body.balanceChange.magnitude);

senderUpdate.body.balanceChange.sgn.isPositive().assertFalse();
receiverUpdate.body.balanceChange.sgn.isPositive().assertTrue();

this.approve(accUpdate, AccountUpdate.Layout.StaticChildren(2));

// Dispatch action
this.reducer.dispatch(request);
}

@method rollupRequests(batch: ActionsBatch) {
const treeRoot = this.treeRoot.getAndAssertEquals();
const counter = this.counter.getAndAssertEquals();
Expand Down

0 comments on commit dfe824e

Please sign in to comment.