Skip to content

Commit 8047313

Browse files
committed
🤑 Fund test zkApps at initialization.
1 parent 1d1a9e7 commit 8047313

File tree

1 file changed

+74
-102
lines changed

1 file changed

+74
-102
lines changed

‎mina/examples/Airdrop.test.ts

+74-102
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,113 @@
1-
import {
2-
AccountUpdate,
3-
Field,
4-
MerkleTree,
5-
Mina,
6-
PrivateKey,
7-
UInt64
8-
} from "o1js";
1+
import { Field, MerkleTree, Mina, PrivateKey, PublicKey, UInt64 } from "o1js";
92
import { HumanIDWitness } from "../humanIDv1";
103
import { signHumanIDv1, truncateHumanIDv1 } from "../humanIDv1.test";
114
import { Airdrop } from "./Airdrop";
125

13-
describe('Example Airdrop zkApp', () => {
14-
const deployerKey = PrivateKey.random();
15-
const deployer = deployerKey.toPublicKey();
16-
const senderKey = PrivateKey.random();
17-
const sender = senderKey.toPublicKey();
18-
const appKey = PrivateKey.random();
19-
const appAddr = appKey.toPublicKey();
6+
describe("Example Airdrop zkApp", () => {
207
let tree: MerkleTree;
8+
let senderKey: PrivateKey;
9+
let appKey: PrivateKey;
10+
let sender: PublicKey;
2111
let app: Airdrop;
2212

2313
beforeAll(() => Airdrop.compile());
2414

25-
beforeEach(() => Mina.LocalBlockchain({ proofsEnabled: true })
26-
.then((local) => {
15+
beforeEach(() =>
16+
Mina.LocalBlockchain({ proofsEnabled: true }).then((local) => {
2717
tree = new MerkleTree(33);
2818
Mina.setActiveInstance(local);
29-
app = new Airdrop(appAddr);
30-
local.addAccount(deployer, "100000000000");
31-
local.addAccount(sender, "100000000000");
32-
}));
19+
senderKey = local.testAccounts[0].key;
20+
sender = senderKey.toPublicKey();
21+
appKey = local.testAccounts[1].key;
22+
app = new Airdrop(appKey.toPublicKey());
23+
24+
const deployerKey = local.testAccounts[2].key;
25+
const deployer = deployerKey.toPublicKey();
26+
return Mina.transaction(deployer, () => app.deploy())
27+
.prove()
28+
.sign([appKey, deployerKey])
29+
.send();
30+
})
31+
);
3332

3433
const getWitnessAndInsert = (humanIDv1Key: bigint) => {
3534
const truncated = truncateHumanIDv1(humanIDv1Key);
3635
const witness = new HumanIDWitness(tree.getWitness(truncated));
3736
tree.setLeaf(truncated, Field(1));
3837
return witness;
39-
}
38+
};
4039

41-
const fundZkApp = () => Mina.transaction(sender, async () => {
42-
let senderUpdate = AccountUpdate.create(sender);
43-
senderUpdate.requireSignature();
44-
senderUpdate.send({ to: appAddr, amount: 100 * 1e9 });
45-
}).then((txn) => txn.prove())
46-
.then((txn) => txn.sign([senderKey]).send());
40+
it("should deploy the app", () =>
41+
console.log("Deployed HumanIDs contract at", app.address));
4742

48-
const deploy = () => Mina.transaction(deployer, () => {
49-
AccountUpdate.fundNewAccount(deployer);
50-
return app.deploy()
51-
}).then((txn) => txn.prove())
52-
.then((txn) => txn.sign([deployerKey, appKey]).send())
53-
54-
it('should deploy the app and fund it', async () => {
55-
await deploy();
56-
await fundZkApp();
57-
console.log('Deployed HumanIDs contract at', app.address);
58-
});
59-
60-
it('should let people claimReward()', async () => {
61-
await deploy();
62-
await fundZkApp()
63-
64-
await Mina.transaction(sender, () => {
65-
return app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n));
66-
}).then((txn) => txn.prove())
67-
.then((txn) => txn.sign([senderKey]).send());
68-
});
69-
70-
it('should let 2 people claimReward()', async () => {
71-
await deploy();
72-
await fundZkApp();
43+
it("should let people claimReward()", () =>
44+
Mina.transaction(sender, () =>
45+
app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n))
46+
)
47+
.prove()
48+
.sign([senderKey])
49+
.send());
7350

51+
it("should let 2 people claimReward()", async () => {
7452
const id1 = 123123123123123123123123123123n;
75-
await Mina.transaction(
76-
sender,
77-
() => app.claimReward(...signHumanIDv1(id1, sender), getWitnessAndInsert(id1))
53+
await Mina.transaction(sender, () =>
54+
app.claimReward(...signHumanIDv1(id1, sender), getWitnessAndInsert(id1))
7855
)
79-
.then((txn) => txn.prove())
80-
.then((txn) => txn.sign([senderKey]).send());
56+
.prove()
57+
.sign([senderKey])
58+
.send();
8159

8260
const id2 = 123123123123123123123123123124n;
83-
await Mina.transaction(
84-
sender,
85-
() => app.claimReward(...signHumanIDv1(id2, sender), getWitnessAndInsert(id2))
61+
await Mina.transaction(sender, () =>
62+
app.claimReward(...signHumanIDv1(id2, sender), getWitnessAndInsert(id2))
8663
)
87-
.then((txn) => txn.prove())
88-
.then((txn) => txn.sign([senderKey]).send());
64+
.prove()
65+
.sign([senderKey])
66+
.send();
8967
});
9068

91-
it('should reject inconsistent witness', async () => {
92-
await deploy();
93-
await fundZkApp();
94-
69+
it("should reject inconsistent witness", async () => {
9570
const id = 123123123123123123123123123124n;
96-
expect(() => Mina.transaction(
97-
sender,
98-
() => app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(100n))
99-
)
100-
.then((txn) => txn.prove())
101-
.then((txn) => txn.sign([senderKey]).send())).rejects.toThrow(/does not match/);
102-
})
103-
104-
it('should not let double claimReward()', async () => {
105-
await deploy();
106-
await fundZkApp();
71+
expect(() =>
72+
Mina.transaction(sender, () =>
73+
app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(100n))
74+
)
75+
.prove()
76+
.sign([senderKey])
77+
.send()
78+
).rejects.toThrow(/does not match/);
79+
});
10780

81+
it("should not let double claimReward()", async () => {
10882
const id = 123123123123123123123123123123n;
109-
await Mina.transaction(
110-
sender,
111-
() => app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id))
112-
)
113-
.then((txn) => txn.prove())
114-
.then((txn) => txn.sign([senderKey]).send());
115-
116-
expect(() => Mina.transaction(
117-
sender,
118-
() => app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id))
83+
await Mina.transaction(sender, () =>
84+
app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id))
11985
)
120-
.then((txn) => txn.prove())
121-
.then((txn) => txn.sign([senderKey]).send())).rejects.toThrow(/already exists/);
122-
})
123-
124-
it('should send the reciepient 10 MINA', async () => {
125-
await deploy();
126-
await fundZkApp();
86+
.prove()
87+
.sign([senderKey])
88+
.send();
89+
90+
expect(() =>
91+
Mina.transaction(sender, () =>
92+
app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id))
93+
)
94+
.prove()
95+
.sign([senderKey])
96+
.send()
97+
).rejects.toThrow(/already exists/);
98+
});
12799

100+
it("should send the reciepient 10 MINA", async () => {
128101
let firstBalance = Mina.getBalance(sender);
129102

130-
await Mina.transaction(
131-
sender,
132-
() => app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n))
103+
await Mina.transaction(sender, () =>
104+
app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n))
133105
)
134-
.then((txn) => txn.prove())
135-
.then((txn) => txn.sign([senderKey]).send());
106+
.prove()
107+
.sign([senderKey])
108+
.send();
136109

137110
let secondBalance = Mina.getBalance(sender);
138-
139111
expect(secondBalance.sub(firstBalance)).toEqual(UInt64.from(10 * 1e9));
140-
})
112+
});
141113
});

0 commit comments

Comments
 (0)