This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo-chain-setup.ts
183 lines (163 loc) · 5.67 KB
/
two-chain-setup.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
172
173
174
175
176
177
178
179
180
181
182
183
import * as _ from 'lodash';
import { Contract, ethers } from 'ethers';
import ganache = require('ganache-core');
import chalk = require('chalk');
import { LEFT_CHAIN_ID, RIGHT_CHAIN_ID } from '../constants';
const executorWallet = ethers.Wallet.createRandom();
const responderWallet = ethers.Wallet.createRandom();
const deployerWallet = ethers.Wallet.createRandom(); // to deploy contracts
export function spinUpChains() {
const left = {
gasPrice: ethers.constants.One.toHexString(),
port: 9001,
_chainId: LEFT_CHAIN_ID,
_chainIdRpc: LEFT_CHAIN_ID,
accounts: [
{
secretKey: executorWallet.privateKey,
balance: ethers.constants.WeiPerEther.mul(800).toHexString(),
},
{
secretKey: deployerWallet.privateKey,
balance: ethers.constants.WeiPerEther.mul(800).toHexString(),
},
],
};
const leftServer = (ganache as any).server(left);
leftServer.listen(left.port, async (err) => {
if (err) throw err;
console.log(`ganache listening on port ${left.port}...`);
});
const leftChain = new ethers.providers.JsonRpcProvider(
`http://localhost:${left.port}`
);
const right = {
gasPrice: ethers.constants.One.toHexString(),
port: 9002,
_chainId: RIGHT_CHAIN_ID,
_chainIdRpc: RIGHT_CHAIN_ID,
accounts: [
{
secretKey: responderWallet.privateKey,
balance: ethers.constants.WeiPerEther.mul(800).toHexString(),
},
{
secretKey: deployerWallet.privateKey,
balance: ethers.constants.WeiPerEther.mul(800).toHexString(),
},
],
};
const rightServer = (ganache as any).server(right);
rightServer.listen(right.port, async (err) => {
if (err) throw err;
console.log(`ganache listening on port ${right.port}...`);
});
const rightChain = new ethers.providers.JsonRpcProvider(
`http://localhost:${right.port}`
);
async function tearDownChains() {
await leftServer.close();
await rightServer.close();
}
return {
leftChain,
rightChain,
tearDownChains,
};
}
export class Actor {
prompt: '> ' | '< ' = '> ';
color: string = 'black';
log(s: string) {
console.log(chalk.keyword(this.color)(this.prompt + s));
}
gasSpent: number = 0;
async getLeftBalance() {
return this.leftToken.balanceOf(this.signingWallet.address);
}
async getRightBalance() {
return this.rightToken.balanceOf(this.signingWallet.address);
}
async logBalances() {
this.log(
`I have ${(
await this.getLeftBalance()
).toString()} tokens on the left chain`
);
this.log(
`I have ${(
await this.getRightBalance()
).toString()} tokens on the right chain`
);
}
constructor(
public signingWallet: ethers.Wallet,
public leftToken: Contract, // TODO allow this to be undefined and fall back on an ETH swap
public rightToken: Contract
) {}
}
export class Executor extends Actor {
prompt: '> ' | '< ' = '> ';
color: string = 'orangered';
constructor(leftToken: Contract, rightToken: Contract) {
super(executorWallet, leftToken, rightToken);
}
}
export class Responder extends Actor {
prompt: '> ' | '< ' = '< ';
color: string = 'gray';
constructor(leftToken: Contract, rightToken: Contract) {
super(responderWallet, leftToken, rightToken);
}
}
export async function logBalances(...actors: Actor[]) {
for await (const actor of actors) {
await actor.logBalances();
}
}
export async function logTotalGasSpentByAll(...actors: Actor[]) {
let total = 0;
for (const actor of actors) {
total += actor.gasSpent;
}
console.log('Total gas spent by all parties: ' + total);
}
// Copied from https://github.com/connext/vector/blob/c8a8deed53cfa7caa58a6466d82fe5d22c208622/modules/contracts/src.ts/utils.ts#L29
export async function advanceBlocktime(
provider: ethers.providers.JsonRpcProvider,
seconds: number
): Promise<void> {
const { timestamp: currTime } = await provider.getBlock('latest');
await provider.send('evm_increaseTime', [seconds]);
await provider.send('evm_mine', []);
const { timestamp: finalTime } = await provider.getBlock('latest');
const desired = currTime + seconds;
if (finalTime < desired) {
const diff = finalTime - desired;
await provider.send('evm_increaseTime', [diff]);
}
}
export async function parseTransaction(chain, tx, action: string) {
const { gasUsed, transactionHash } = await tx.wait();
console.log(`Gas used for ${action}: ${gasUsed}`);
const costPerOpcode = await aggregatedCostPerOpcode(chain, transactionHash);
const bigSpenders = costPerOpcode.filter((item) => item.gas >= 200);
const refunds = costPerOpcode.filter((item) => item.gas < 0);
console.log(_.concat(bigSpenders, ['...'], refunds));
return gasUsed;
}
async function aggregatedCostPerOpcode(
chain: ethers.providers.JsonRpcProvider,
txHash: string
) {
const trace = await chain.send('debug_traceTransaction', [txHash, {}]);
const ops = _.groupBy(trace.structLogs, (step) => step.op);
const sum = (a, b) => a + b;
const summary = _.mapValues(ops, (items) => {
const gas = items.map((o) => o.gasCost).reduce(sum);
const count = items.length;
const [{ op }] = items;
return { count, gas, op };
});
return _.sortBy(_.values(summary), (row) => -row.gas);
}