Skip to content

Commit 4f8130a

Browse files
authored
Merge pull request #33 from webb-tools/drew-update-scripts
Update scripts - link anchor scripts
2 parents 26e7be0 + e65d0f8 commit 4f8130a

File tree

9 files changed

+253
-34
lines changed

9 files changed

+253
-34
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
"build:circuits": "./scripts/bash/compile_circom.sh",
1212
"build:hashers": "ts-node scripts/ts/build_hashers.ts",
1313
"build:ptau": "./scripts/bash/generate_phase1_ptau.sh",
14-
"build:contracts:types": "typechain --target ethers-v5 --outDir './types/ethers' './artifacts/**/*.json'",
14+
"build:contracts:types": "typechain --target ethers-v5 --out './typechain' './artifacts/**/*.json'",
15+
"run:deploy-webb": "ts-node ./scripts/evm/webbSystemDeployment.ts",
16+
"run:inspect-webb": "ts-node ./scripts/evm/inspectWebbInstance.ts",
17+
"run:link-webbs": "ts-node ./scripts/evm/linkAnchors.ts",
1518
"setup:poseidon-bridge2": "node scripts/js/generatePoseidonBridge2Input.js && ./scripts/bash/phase2_poseidon_bridge2.sh",
1619
"setup:poseidon2-preimage": "node scripts/js/generatePoseidon2Input.js && ./scripts/bash/phase2_poseidon.sh",
1720
"setup:poseidon3-preimage": "node scripts/js/generatePoseidon3Input.js && ./scripts/bash/phase2_poseidon3.sh",

scripts/evm/depositAnchor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export async function depositAnchor(anchorAddress: string, tokenAddress: string,
2626

2727
// There is enough to make the call.
2828
console.log(`depositing with arg: ${helpers.toFixedHex(deposit.commitment)} `);
29-
let tx = await anchor.deposit(helpers.toFixedHex(deposit.commitment), { from: walletAddress, gasLimit: '0x989680' });
29+
let tx = await anchor.deposit(helpers.toFixedHex(deposit.commitment), { gasLimit: '0x5B8D80' });
3030
let receipt = await tx.wait();
3131
console.log(receipt);
3232

33-
return deposit;
33+
return receipt;
3434
}

scripts/evm/json/PoseidonT3.json

Lines changed: 30 additions & 0 deletions
Large diffs are not rendered by default.

scripts/evm/json/VerifierPoseidonBridge.json

Lines changed: 45 additions & 0 deletions
Large diffs are not rendered by default.

scripts/evm/linkAnchors.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
require('dotenv').config();
2+
import { ethers } from 'ethers';
3+
4+
import { WEBB__factory } from '../../typechain/factories/WEBB__factory';
5+
import { WEBBAnchor2__factory } from '../../typechain/factories/WEBBAnchor2__factory';
6+
import { Bridge__factory } from '../../typechain/factories/Bridge__factory';
7+
import { WEBB } from '../../typechain/WEBB';
8+
import { depositAnchor } from './depositAnchor';
9+
import { AnchorHandler__factory } from '../../typechain/factories/AnchorHandler__factory';
10+
const helpers = require('../../test/helpers');
11+
12+
async function getAllContracts(originWallet: any, destWallet: any) {
13+
return {
14+
chain1Anchor: WEBBAnchor2__factory.connect(process.env.CHAIN_1_WEBBAnchor!, originWallet),
15+
chain1WebbToken: WEBB__factory.connect(process.env.CHAIN_1_WEBB!, originWallet),
16+
chain1Bridge: Bridge__factory.connect(process.env.CHAIN_1_Bridge!, originWallet),
17+
chain1AnchorHandler: AnchorHandler__factory.connect(process.env.CHAIN_1_AnchorHandler!, originWallet),
18+
chain2Anchor: WEBBAnchor2__factory.connect(process.env.CHAIN_2_WEBBAnchor!, destWallet),
19+
chain2WebbToken: WEBB__factory.connect(process.env.CHAIN_2_WEBB!, destWallet),
20+
chain2Bridge: Bridge__factory.connect(process.env.CHAIN_2_Bridge!, destWallet),
21+
chain2AnchorHandler: AnchorHandler__factory.connect(process.env.CHAIN_2_AnchorHandler!, destWallet),
22+
};
23+
}
24+
25+
interface Options {
26+
originAnchor: ethers.Contract;
27+
originToken: ethers.Contract;
28+
originAnchorHandler: ethers.Contract;
29+
originWallet: ethers.Wallet;
30+
destAnchor: ethers.Contract;
31+
destBridge: ethers.Contract;
32+
destAnchorHandler: ethers.Contract;
33+
destWallet: ethers.Wallet;
34+
}
35+
36+
async function depositAndProposeAndExecute({
37+
originAnchor,
38+
originToken,
39+
originAnchorHandler,
40+
originWallet,
41+
destAnchor,
42+
destBridge,
43+
destAnchorHandler,
44+
destWallet,
45+
}: Options) {
46+
let receipt = await depositAnchor(originAnchor.address, originToken.address, originWallet);
47+
// @ts-ignore
48+
const data = receipt.events[2].args;
49+
// @ts-ignore
50+
const updateNonce = data[1];
51+
const originBlockHeight = receipt.blockNumber;
52+
53+
const originChainID = await originWallet.getChainId();
54+
const originMerkleRoot = await originAnchor.getLastRoot();
55+
// create correct update proposal data for the deposit on origin chain
56+
const updateData = helpers.createUpdateProposalData(originChainID, originBlockHeight, originMerkleRoot);
57+
console.log('Created update data w/ args', originChainID, originBlockHeight, originMerkleRoot, updateData)
58+
const dataHash = ethers.utils.keccak256(destAnchorHandler.address + updateData.substr(2));
59+
// create destination resourceID to create proposals to update against
60+
const destChainId = await destWallet.getChainId();
61+
const resourceId = helpers.createResourceID(destAnchor.address, destChainId);
62+
let tx = await destBridge.voteProposal(
63+
originChainID,
64+
updateNonce,
65+
resourceId,
66+
dataHash,
67+
{ gasLimit: '0x5B8D80' },
68+
);
69+
let result = await tx.wait();
70+
tx = await destBridge.executeProposal(
71+
originChainID,
72+
updateNonce,
73+
updateData,
74+
resourceId,
75+
{ gasLimit: '0x5B8D80' },
76+
);
77+
result = await tx.wait();
78+
console.log('origin merkle root', originMerkleRoot);
79+
console.log('destination neighbor roots', await destAnchor.getLatestNeighborRoots());
80+
}
81+
82+
async function run() {
83+
const chain1Provider = new ethers.providers.JsonRpcProvider(`${process.env.CHAIN_1_ENDPOINT}`);
84+
const chain2Provider = new ethers.providers.JsonRpcProvider(`${process.env.CHAIN_2_ENDPOINT}`);
85+
const chain1Wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, chain1Provider);
86+
const chain2Wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, chain2Provider);
87+
const options = await getAllContracts(chain1Wallet, chain2Wallet);
88+
await depositAndProposeAndExecute({
89+
originAnchor: options.chain1Anchor,
90+
originToken: options.chain1WebbToken,
91+
originAnchorHandler: options.chain1AnchorHandler,
92+
originWallet: chain1Wallet,
93+
destAnchor: options.chain2Anchor,
94+
destBridge: options.chain2Bridge,
95+
destAnchorHandler: options.chain2AnchorHandler,
96+
destWallet: chain2Wallet,
97+
});
98+
99+
await depositAndProposeAndExecute({
100+
originAnchor: options.chain2Anchor,
101+
originToken: options.chain2WebbToken,
102+
originAnchorHandler: options.chain2AnchorHandler,
103+
originWallet: chain2Wallet,
104+
destAnchor: options.chain1Anchor,
105+
destBridge: options.chain1Bridge,
106+
destAnchorHandler: options.chain1AnchorHandler,
107+
destWallet: chain1Wallet,
108+
});
109+
}
110+
111+
run();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require('dotenv').config();
2+
import { ethers } from 'ethers';
3+
4+
import { WEBB__factory } from '../../../typechain/factories/WEBB__factory';
5+
import { WEBBAnchor2__factory } from '../../../typechain/factories/WEBBAnchor2__factory';
6+
7+
let provider = new ethers.providers.JsonRpcProvider(`${process.env.ENDPOINT}`);
8+
9+
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
10+
11+
async function run() {
12+
// @ts-ignore
13+
const WEBB = WEBB__factory.connect(process.env.WEBB, wallet.provider);
14+
// @ts-ignore
15+
const WEBBAnchor = WEBBAnchor2__factory.connect(process.env.WEBBAnchor, wallet.provider);
16+
const root = await WEBBAnchor.getLastRoot();
17+
console.log(root);
18+
}
19+
20+
run();

scripts/evm/webbSystemDeployment.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
require('dotenv').config({ path: '../.env' });
1+
require('dotenv').config();
22
const helpers = require('../../test/helpers');
33
import { ethers } from 'ethers';
44

55
import { WEBB__factory } from '../../typechain/factories/WEBB__factory';
6-
import { TimelockHarness__factory } from '../../typechain/factories/TimelockHarness__factory';
7-
import { GovernorBravoImmutable__factory } from '../../typechain/factories/GovernorBravoImmutable__factory';
8-
import { PoseidonT3__factory } from '../../typechain/factories/PoseidonT3__factory';
9-
import { VerifierPoseidonBridge__factory } from '../../typechain/factories/VerifierPoseidonBridge__factory';
6+
107

118
import { setLinkableAnchorBridge } from './setLinkableAnchorBridge';
129
import { setLinkableAnchorHandler } from './setLinkableAnchorHandler';
@@ -16,49 +13,62 @@ import { deployWebbBridge } from './deployments/deployWebbBridge';
1613
import { deployAnchorHandler } from './deployments/deployAnchorHandler';
1714
import { setResourceId } from './setResourceId';
1815

16+
const HasherContract = require('./json/PoseidonT3.json');
17+
const VerifierContract = require('./json/VerifierPoseidonBridge.json');
18+
1919
let provider = new ethers.providers.JsonRpcProvider(`${process.env.ENDPOINT}`);
2020

2121
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
2222

23-
async function run() {
23+
async function getHasherFactory(wallet: ethers.Signer): Promise<ethers.ContractFactory> {
24+
const hasherContractRaw = {
25+
contractName: 'PoseidonT3',
26+
abi: HasherContract.abi,
27+
bytecode: HasherContract.bytecode,
28+
};
29+
30+
const hasherFactory = new ethers.ContractFactory(hasherContractRaw.abi, hasherContractRaw.bytecode, wallet);
31+
return hasherFactory;
32+
};
33+
34+
async function getVerifierFactory(wallet: ethers.Signer): Promise<ethers.ContractFactory> {
35+
const VerifierContractRaw = {
36+
contractName: 'Verifier',
37+
abi: VerifierContract.abi,
38+
bytecode: VerifierContract.bytecode,
39+
};
40+
41+
const verifierFactory = new ethers.ContractFactory(VerifierContractRaw.abi, VerifierContractRaw.bytecode, wallet);
42+
return verifierFactory;
43+
};
44+
45+
export async function run() {
46+
const denomination = ethers.BigNumber.from('100000000000000000');
47+
// WARNING: ENSURE THIS MATCHES CIRCUIT HEIGHT
48+
const merkleTreeHeight = 30;
49+
2450
const chainId = await wallet.getChainId();
2551

2652
// deploy WEBB gov token first and then add to anchor
2753
const WebbFactory = new WEBB__factory(wallet);
2854
const WEBB = await WebbFactory.deploy('Webb Governance Token', 'WEBB');
2955
await WEBB.deployed();
3056
console.log(`Deployed WEBB: ${WEBB.address}`);
31-
// deploy timelockHarness
32-
const TimelockHarnessFactory = new TimelockHarness__factory(wallet);
33-
const TimelockHarness = await TimelockHarnessFactory.deploy(wallet.address, '345600');
34-
await TimelockHarness.deployed();
35-
console.log(`Deployed TimelockHarness: ${TimelockHarness.address}`);
36-
// deploy gov bravo
37-
const GovBravoFactory = new GovernorBravoImmutable__factory(wallet);
38-
const GovBravo = await GovBravoFactory.deploy(
39-
TimelockHarness.address,
40-
WEBB.address,
41-
wallet.address,
42-
'10',
43-
'1',
44-
'100000000000000000000000'
45-
);
46-
await GovBravo.deployed();
47-
console.log(`Deployed GovBravo: ${GovBravo.address}`);
57+
await WEBB.mint(wallet.address, '1000000000000000000000000', {
58+
gasLimit: '0x5B8D80',
59+
});
4860

49-
const hasherFactory = new PoseidonT3__factory(wallet);
61+
// deploy the Hasher
62+
const hasherFactory = await getHasherFactory(wallet);
5063
let hasherInstance = await hasherFactory.deploy({ gasLimit: '0x5B8D80' });
5164
await hasherInstance.deployed();
5265
console.log(`Deployed Hasher: ${hasherInstance.address}`);
5366

54-
const verifierFactory = new VerifierPoseidonBridge__factory(wallet);
67+
const verifierFactory = await getVerifierFactory(wallet);
5568
let verifierInstance = await verifierFactory.deploy({ gasLimit: '0x5B8D80' });
5669
await verifierInstance.deployed();
5770
console.log(`Deployed Verifier: ${verifierInstance.address}`);
5871

59-
const denomination = ethers.BigNumber.from('100000000000000000');
60-
const merkleTreeHeight = 20;
61-
6272
const webbAnchor = await deployWEBBAnchor(
6373
verifierInstance.address,
6474
hasherInstance.address,
@@ -77,7 +87,7 @@ async function run() {
7787
await tx.wait();
7888

7989
// Create the bridge
80-
const webbBridge = await deployWebbBridge(chainId, [wallet.address], 1, '1000', 30, wallet);
90+
const webbBridge = await deployWebbBridge(chainId, [wallet.address], 1, '0', 30, wallet);
8191

8292
let resourceID = helpers.createResourceID(webbAnchor.address, chainId);
8393

test/helpers/bridge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const advanceBlock = () => {
8282
}
8383

8484
const createResourceID = (contractAddress, chainID) => {
85-
return toHex(contractAddress + toHex(chainID, 1).substr(2), 32)
85+
return toHex(contractAddress + toHex(chainID, 4).substr(2), 32)
8686
};
8787

8888
const assertObjectsMatch = (expectedObj, actualObj) => {

test/integration/simpleWithdrawals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ contract('E2E LinkableAnchors - Cross chain withdrawals', async accounts => {
153153
// minting Tokens
154154
await originChainToken.mint(sender, initialTokenMintAmount);
155155
// increasing allowance of anchors
156-
await originChainToken.approve(OriginChainAnchorInstance.address, initialTokenMintAmount, { from: sender }),
156+
await originChainToken.approve(OriginChainAnchorInstance.address, initialTokenMintAmount, { from: sender });
157157
// generate deposit commitment targeting withdrawal on destination chain
158158
originDeposit = helpers.generateDeposit(destChainID);
159159
// deposit on origin chain and define nonce

0 commit comments

Comments
 (0)