|
| 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(); |
0 commit comments