-
Notifications
You must be signed in to change notification settings - Fork 189
/
happy-path.ts
171 lines (133 loc) · 3.71 KB
/
happy-path.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import chai from "chai";
import { ethers } from "hardhat";
import { solidity } from "ethereum-waffle";
import { deployContracts } from "../test-utils";
import {
getSignerAddresses,
makeCheckpoint,
signHash,
examplePowers
} from "../test-utils/pure";
chai.use(solidity);
const { expect } = chai;
describe("Gravity happy path valset update + batch submit", function () {
it("Happy path", async function () {
// DEPLOY CONTRACTS
// ================
const signers = await ethers.getSigners();
const gravityId = ethers.utils.formatBytes32String("foo");
const valset0 = {
// This is the power distribution on the Cosmos hub as of 7/14/2020
powers: examplePowers(),
validators: signers.slice(0, examplePowers().length),
nonce: 0
}
const powerThreshold = 6666;
const {
gravity,
testERC20,
checkpoint: deployCheckpoint
} = await deployContracts(gravityId, valset0.validators, valset0.powers, powerThreshold);
// UDPATEVALSET
// ============
const valset1 = (() => {
// Make new valset by modifying some powers
let powers = examplePowers();
powers[0] -= 3;
powers[1] += 3;
let validators = signers.slice(0, powers.length);
return {
powers: powers,
validators: validators,
nonce: 1
}
})()
const checkpoint1 = makeCheckpoint(
await getSignerAddresses(valset1.validators),
valset1.powers,
valset1.nonce,
gravityId
);
let sigs1 = await signHash(valset0.validators, checkpoint1);
await gravity.updateValset(
await getSignerAddresses(valset1.validators),
valset1.powers,
valset1.nonce,
await getSignerAddresses(valset0.validators),
valset0.powers,
valset0.nonce,
sigs1.v,
sigs1.r,
sigs1.s
);
expect((await gravity.functions.state_lastValsetCheckpoint())[0]).to.equal(checkpoint1);
// SUBMITBATCH
// ==========================
// Transfer out to Cosmos, locking coins
await testERC20.functions.approve(gravity.address, 1000);
await gravity.functions.sendToCosmos(
testERC20.address,
ethers.utils.formatBytes32String("myCosmosAddress"),
1000
);
// Transferring into ERC20 from Cosmos
const numTxs = 100;
const txDestinationsInt = new Array(numTxs);
const txFees = new Array(numTxs);
const txAmounts = new Array(numTxs);
for (let i = 0; i < numTxs; i++) {
txFees[i] = 1;
txAmounts[i] = 1;
txDestinationsInt[i] = signers[i + 5];
}
const txDestinations = await getSignerAddresses(txDestinationsInt);
const batchNonce = 1
const batchTimeout = 10000000
const methodName = ethers.utils.formatBytes32String(
"transactionBatch"
);
let abiEncoded = ethers.utils.defaultAbiCoder.encode(
[
"bytes32",
"bytes32",
"uint256[]",
"address[]",
"uint256[]",
"uint256",
"address",
"uint256"
],
[
gravityId,
methodName,
txAmounts,
txDestinations,
txFees,
batchNonce,
testERC20.address,
batchTimeout
]
);
let digest = ethers.utils.keccak256(abiEncoded);
let sigs = await signHash(valset1.validators, digest);
await gravity.submitBatch(
await getSignerAddresses(valset1.validators),
valset1.powers,
valset1.nonce,
sigs.v,
sigs.r,
sigs.s,
txAmounts,
txDestinations,
txFees,
batchNonce,
testERC20.address,
batchTimeout
);
expect(
await (
await testERC20.functions.balanceOf(await signers[6].getAddress())
)[0].toNumber()
).to.equal(1);
});
});