setup.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const { deployNew } = require("./helpers")
  2. const { CHAIN_ID_TO_NAME, POOL_ID_TO_NAME } = require("./constants")
  3. setup = async (numOfChains, numOfTokens, random = false) => {
  4. const config = generateConfig(numOfChains, numOfTokens, random)
  5. // deploy the stargate instances on each "chain"
  6. let endpoints = Object.fromEntries(
  7. await Promise.all(Object.values(config).map(async (endpoint) => [endpoint.chainId, await deployStargateEndpoint(endpoint)]))
  8. )
  9. await bridgeEndpoints(endpoints)
  10. await deployPoolsOnChains(endpoints)
  11. await createChainPaths(endpoints)
  12. await activateChainPaths(endpoints)
  13. return endpoints
  14. }
  15. generateConfig = (numOfChains, numOfTokens, random) => {
  16. const endpoints = {}
  17. const lds = {}
  18. for (let chainId = 1; chainId <= numOfChains; chainId++) {
  19. const config = { chainId, name: CHAIN_ID_TO_NAME[chainId], pools: {} }
  20. for (let tokenId = 11; tokenId <= numOfTokens * 11; tokenId += 11) {
  21. const ld = random ? Math.floor(Math.random() * 18) : 18
  22. if (lds[tokenId]) {
  23. lds[tokenId].push(ld)
  24. } else {
  25. lds[tokenId] = [ld]
  26. }
  27. const pool = { id: tokenId, chainId, name: POOL_ID_TO_NAME[tokenId], ld, dstChainWeights: {} }
  28. for (let dstChainId = 1; dstChainId <= numOfChains; dstChainId++) {
  29. if (dstChainId !== chainId) {
  30. pool.dstChainWeights[dstChainId] = {}
  31. for (let dstTokenId = 11; dstTokenId <= numOfTokens * 11; dstTokenId += 11) {
  32. pool.dstChainWeights[dstChainId][dstTokenId] = random ? Math.floor(Math.random() * 99) + 1 : 1
  33. }
  34. }
  35. }
  36. config.pools[tokenId] = pool
  37. }
  38. endpoints[chainId] = config
  39. }
  40. for (const [_tokenId, _lds] of Object.entries(lds)) {
  41. lds[_tokenId] = Math.min(..._lds)
  42. }
  43. for (const endpoint of Object.values(endpoints)) {
  44. for (const pool of Object.values(endpoint.pools)) {
  45. pool["sd"] = lds[pool.id]
  46. }
  47. }
  48. return endpoints
  49. }
  50. deployStargateEndpoint = async (endpoint) => {
  51. const lzEndpoint = await deployNew("LZEndpointMock", [endpoint.chainId])
  52. const router = await deployNew("Router")
  53. const bridge = await deployNew("Bridge", [lzEndpoint.address, router.address])
  54. const factory = await deployNew("Factory", [router.address])
  55. const feeLibrary = await deployNew("StargateFeeLibraryV02", [factory.address])
  56. // set deploy params
  57. await factory.setDefaultFeeLibrary(feeLibrary.address)
  58. await router.setBridgeAndFactory(bridge.address, factory.address)
  59. return { factory, router, bridge, lzEndpoint, feeLibrary, ...endpoint }
  60. }
  61. bridgeEndpoints = async (endpoints) => {
  62. for (const src of Object.values(endpoints)) {
  63. for (const dst of Object.values(endpoints)) {
  64. await src.bridge.setBridge(dst.chainId, dst.bridge.address)
  65. await src.lzEndpoint.setDestLzEndpoint(dst.bridge.address, dst.lzEndpoint.address)
  66. }
  67. }
  68. }
  69. deployPoolsOnChains = async (endpoints) => {
  70. for (const endpoint of Object.values(endpoints)) {
  71. endpoint.pools = Object.fromEntries(
  72. await Promise.all(
  73. Object.values(endpoint.pools).map(async (pool) => {
  74. const poolObj = {
  75. ...pool,
  76. lzEndpoint: endpoint.lzEndpoint,
  77. router: endpoint.router,
  78. bridge: endpoint.bridge,
  79. dstChainWeights: pool.dstChainWeights,
  80. ...(await deployPool(endpoint, pool.name, pool.ld, pool.sd, pool.id)),
  81. }
  82. return [pool.id, poolObj]
  83. })
  84. )
  85. )
  86. }
  87. }
  88. deployPool = async (sgEndpoint, name, ld, sd, id) => {
  89. let tokenName = `${name}-${sgEndpoint.name}`
  90. const token = await deployNew("MockToken", [tokenName, tokenName, ld])
  91. await sgEndpoint.router.createPool(id, token.address, sd, ld, "x", "x*")
  92. let poolAddress = await sgEndpoint.factory.getPool(id)
  93. const Pool = await ethers.getContractFactory("Pool")
  94. let pool = await Pool.attach(poolAddress)
  95. return { token, pool, name: tokenName, id, ld, sd, chainPaths: {} }
  96. }
  97. createChainPaths = async (endpoints) => {
  98. for (const endpoint of Object.values(endpoints)) {
  99. for (const pool of Object.values(endpoint.pools)) {
  100. pool.chainPaths = {}
  101. for (const [chainId, pathWeights] of Object.entries(pool.dstChainWeights)) {
  102. pool.chainPaths[chainId] = {}
  103. for (const [tokenId, weight] of Object.entries(pathWeights)) {
  104. await endpoint.router.createChainPath(pool.id, chainId, tokenId, weight)
  105. pool.chainPaths[chainId][tokenId] = false
  106. }
  107. }
  108. }
  109. }
  110. }
  111. activateChainPaths = async (endpoints) => {
  112. for (const endpoint of Object.values(endpoints)) {
  113. for (const pool of Object.values(endpoint.pools)) {
  114. for (const [chainId, chainPaths] of Object.entries(pool.chainPaths)) {
  115. for (const tokenId of Object.keys(chainPaths)) {
  116. await endpoint.router.activateChainPath(pool.id, chainId, tokenId)
  117. pool.chainPaths[chainId][tokenId] = true
  118. }
  119. }
  120. }
  121. }
  122. }
  123. module.exports = {
  124. setup,
  125. deployPool,
  126. }