wireBridges.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const CONFIG = require("../constants/config.json")
  2. const { getDeploymentAddresses } = require("../utils/readDeployments")
  3. const { getEndpointId } = require("../utils/network")
  4. const { getEndpointIdByName } = require("@layerzerolabs/lz-sdk")
  5. task("wireBridges", "connect the local stargate to a remote stargate by configuring the remote bridge")
  6. .addParam("targetNetworks", "the remote Stargate instance named by network")
  7. .setAction(async (taskArgs, hre) => {
  8. let accounts = await ethers.getSigners()
  9. let owner = accounts[0] // me
  10. // console.log(`owner: ${owner.address}`);
  11. const Bridge = await ethers.getContractFactory("Bridge")
  12. const bridgeAddr = (await hre.deployments.get("Bridge")).address
  13. const bridge = await Bridge.attach(bridgeAddr)
  14. let targetNetworks = taskArgs.targetNetworks.split(",")
  15. console.log(`${hre.network.name}: setting local functionType gas and remote bridge...`)
  16. for (let targetNetwork of targetNetworks) {
  17. let targetNetworkAddrs = getDeploymentAddresses(targetNetwork)
  18. // console.log(targetNetworkAddrs);
  19. let chainId = getEndpointIdByName(targetNetwork)
  20. let numFunctionTypes = Object.keys(CONFIG.gasAmounts).length
  21. for (let functionType = 1; functionType <= numFunctionTypes; ++functionType) {
  22. let gasAmount = CONFIG.gasAmounts[functionType]
  23. let currentAmount = await bridge.gasLookup(chainId, functionType)
  24. if (currentAmount.eq(gasAmount)) {
  25. console.log(
  26. ` ✅ ${hre.network.name} > bridge.setGasAmount(chainId:${chainId}, functionType:${functionType}, gasAmount:${gasAmount}) | *already set*`
  27. )
  28. } else {
  29. await (await bridge.setGasAmount(chainId, functionType, gasAmount)).wait()
  30. console.log(
  31. ` ✅ ${hre.network.name} > bridge.setGasAmount(chainId:${chainId}, functionType:${functionType}, gasAmount:${gasAmount})`
  32. )
  33. }
  34. }
  35. let currBridge = await bridge.bridgeLookup(chainId)
  36. let targetBridgeAddr = ethers.utils.getAddress(targetNetworkAddrs["Bridge"]) // cast to standardized address
  37. if (currBridge !== "0x" && ethers.utils.getAddress(currBridge) == targetBridgeAddr) {
  38. // its nto a bridge
  39. console.log(`✅ ${hre.network.name} > setBridge(${chainId}, ${targetBridgeAddr}) | *already set*`)
  40. } else {
  41. // setBridge , 1-time only call. better do it right!
  42. let tx = await (await bridge.setBridge(chainId, targetBridgeAddr)).wait()
  43. console.log(` ✅ ${hre.network.name} > setBridge(${chainId}, ${targetBridgeAddr}) | tx: ${tx.transactionHash}`)
  44. }
  45. }
  46. })