contracts.spec.ts 12.7 KB
Newer Older
1
/* eslint-disable @typescript-eslint/no-empty-function */
2 3 4
import { Signer } from 'ethers'
import { ethers } from 'hardhat'

5 6 7 8 9 10
import { expect } from '../setup'
import {
  getOEContract,
  getAllOEContracts,
  CONTRACT_ADDRESSES,
  DEFAULT_L2_CONTRACT_ADDRESSES,
11
  L2ChainID,
12 13 14 15 16 17 18 19 20 21 22 23
} from '../../src'

describe('contract connection utils', () => {
  let signers: Signer[]
  before(async () => {
    signers = (await ethers.getSigners()) as any
  })

  describe('getOEContract', () => {
    describe('when given a known chain ID', () => {
      describe('when not given an address override', () => {
        it('should use the address for the given contract name and chain ID', () => {
24
          const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
25 26 27 28
          for (const [contractName, contractAddress] of [
            ...Object.entries(addresses.l1),
            ...Object.entries(addresses.l2),
          ]) {
29 30 31 32
            const contract = getOEContract(
              contractName as any,
              L2ChainID.OPTIMISM
            )
33 34 35 36 37 38 39
            expect(contract.address).to.equal(contractAddress)
          }
        })
      })

      describe('when given an address override', () => {
        it('should use the custom address', () => {
40
          const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
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
          for (const contractName of [
            ...Object.keys(addresses.l1),
            ...Object.keys(addresses.l2),
          ]) {
            const address = '0x' + '11'.repeat(20)
            const contract = getOEContract(contractName as any, 1, {
              address,
            })
            expect(contract.address).to.equal(address)
          }
        })
      })
    })

    describe('when given an unknown chain ID', () => {
      describe('when not given an address override', () => {
        it('should throw an error', () => {
          expect(() => getOEContract('L1CrossDomainMessenger', 3)).to.throw()
        })
      })

      describe('when given an address override', () => {
        it('should use the custom address', () => {
          const address = '0x' + '11'.repeat(20)
          const contract = getOEContract('L1CrossDomainMessenger', 3, {
            address,
          })
          expect(contract.address).to.equal(address)
        })
      })
    })

    describe('when connected to a valid address', () => {
      it('should have the correct interface for the contract name', () => {
75 76 77 78
        const contract = getOEContract(
          'L1CrossDomainMessenger',
          L2ChainID.OPTIMISM
        )
79 80 81 82 83
        expect(contract.sendMessage).to.not.be.undefined
      })

      describe('when not given a signer or provider', () => {
        it('should not have a signer or provider', () => {
84 85 86 87
          const contract = getOEContract(
            'L1CrossDomainMessenger',
            L2ChainID.OPTIMISM
          )
88 89 90 91 92 93 94
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        })
      })

      describe('when given a signer', () => {
        it('should attach the given signer', () => {
95 96 97 98 99 100 101
          const contract = getOEContract(
            'L1CrossDomainMessenger',
            L2ChainID.OPTIMISM,
            {
              signerOrProvider: signers[0],
            }
          )
102 103 104 105 106 107
          expect(contract.signer).to.deep.equal(signers[0])
        })
      })

      describe('when given a provider', () => {
        it('should attach the given provider', () => {
108 109 110 111 112 113 114
          const contract = getOEContract(
            'L1CrossDomainMessenger',
            L2ChainID.OPTIMISM,
            {
              signerOrProvider: ethers.provider as any,
            }
          )
115 116 117 118 119 120 121 122 123 124 125
          expect(contract.signer).to.be.null
          expect(contract.provider).to.deep.equal(ethers.provider)
        })
      })
    })
  })

  describe('getAllOEContracts', () => {
    describe('when given a known chain ID', () => {
      describe('when not given any address overrides', () => {
        it('should return all contracts connected to the default addresses', () => {
126 127
          const contracts = getAllOEContracts(L2ChainID.OPTIMISM)
          const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
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
          for (const [contractName, contractAddress] of Object.entries(
            addresses.l1
          )) {
            const contract = contracts.l1[contractName]
            expect(contract.address).to.equal(contractAddress)
          }
          for (const [contractName, contractAddress] of Object.entries(
            addresses.l2
          )) {
            const contract = contracts.l2[contractName]
            expect(contract.address).to.equal(contractAddress)
          }
        })
      })

      describe('when given address overrides', () => {
        it('should return contracts connected to the overridden addresses where given', () => {
          const overrides = {
            l1: {
              L1CrossDomainMessenger: '0x' + '11'.repeat(20),
            },
            l2: {
              L2CrossDomainMessenger: '0x' + '22'.repeat(20),
            },
          }
153 154
          const contracts = getAllOEContracts(L2ChainID.OPTIMISM, { overrides })
          const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
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
          for (const [contractName, contractAddress] of Object.entries(
            addresses.l1
          )) {
            const contract = contracts.l1[contractName]
            if (overrides.l1[contractName]) {
              expect(contract.address).to.equal(overrides.l1[contractName])
            } else {
              expect(contract.address).to.equal(contractAddress)
            }
          }
          for (const [contractName, contractAddress] of Object.entries(
            addresses.l2
          )) {
            const contract = contracts.l2[contractName]
            if (overrides.l2[contractName]) {
              expect(contract.address).to.equal(overrides.l2[contractName])
            } else {
              expect(contract.address).to.equal(contractAddress)
            }
          }
        })
      })
    })

    describe('when given an unknown chain ID', () => {
      describe('when given address overrides for all L1 contracts', () => {
        describe('when given address overrides for L2 contracts', () => {
          it('should return contracts connected to the overridden addresses where given', () => {
            const l1Overrides = {}
184 185 186
            for (const contractName of Object.keys(
              CONTRACT_ADDRESSES[L2ChainID.OPTIMISM].l1
            )) {
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
              l1Overrides[contractName] = '0x' + '11'.repeat(20)
            }

            const contracts = getAllOEContracts(3, {
              overrides: {
                l1: l1Overrides as any,
                l2: {
                  L2CrossDomainMessenger: '0x' + '22'.repeat(20),
                },
              },
            })

            for (const [contractName, contract] of Object.entries(
              contracts.l1
            )) {
              expect(contract.address).to.equal(l1Overrides[contractName])
            }

            expect(contracts.l2.L2CrossDomainMessenger.address).to.equal(
              '0x' + '22'.repeat(20)
            )
          })
        })

        describe('when not given address overrides for L2 contracts', () => {
          it('should return contracts connected to the default L2 addresses and custom L1 addresses', () => {
            const l1Overrides = {}
214 215 216
            for (const contractName of Object.keys(
              CONTRACT_ADDRESSES[L2ChainID.OPTIMISM].l1
            )) {
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
              l1Overrides[contractName] = '0x' + '11'.repeat(20)
            }

            const contracts = getAllOEContracts(3, {
              overrides: {
                l1: l1Overrides as any,
              },
            })

            for (const [contractName, contract] of Object.entries(
              contracts.l1
            )) {
              expect(contract.address).to.equal(l1Overrides[contractName])
            }

            for (const [contractName, contract] of Object.entries(
              contracts.l2
            )) {
              expect(contract.address).to.equal(
                DEFAULT_L2_CONTRACT_ADDRESSES[contractName]
              )
            }
          })
        })
      })

      describe('when given address overrides for some L1 contracts', () => {
        it('should throw an error', () => {
          expect(() =>
            getAllOEContracts(3, {
              overrides: {
                l1: {
                  L1CrossDomainMessenger: '0x' + '11'.repeat(20),
                },
              },
            })
          ).to.throw()
        })
      })

      describe('when given address overrides for no L1 contracts', () => {
        it('should throw an error', () => {
          expect(() => getAllOEContracts(3)).to.throw()
        })
      })
    })

    describe('when not given a signer or provider', () => {
      it('should not attach a signer or provider to any contracts', () => {
266
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM)
267 268 269 270 271 272 273 274 275 276 277 278 279
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        }
      })
    })

    describe('when given an L1 signer', () => {
      it('should attach the signer to the L1 contracts only', () => {
280
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
281 282 283 284 285 286 287 288 289 290 291 292 293 294
          l1SignerOrProvider: signers[0],
        })
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.deep.equal(signers[0])
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        }
      })
    })

    describe('when given an L2 signer', () => {
      it('should attach the signer to the L2 contracts only', () => {
295
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
296 297 298 299 300 301 302 303 304 305 306 307 308 309
          l2SignerOrProvider: signers[0],
        })
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.deep.equal(signers[0])
        }
      })
    })

    describe('when given an L1 signer and an L2 signer', () => {
      it('should attach the signer to both sets of contracts', () => {
310
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
311 312 313 314 315 316 317 318 319 320 321 322 323 324
          l1SignerOrProvider: signers[0],
          l2SignerOrProvider: signers[1],
        })
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.deep.equal(signers[0])
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.deep.equal(signers[1])
        }
      })
    })

    describe('when given an L1 provider', () => {
      it('should attach the provider to the L1 contracts only', () => {
325
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
          l1SignerOrProvider: ethers.provider as any,
        })
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.deep.equal(ethers.provider)
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        }
      })
    })

    describe('when given an L2 provider', () => {
      it('should attach the provider to the L2 contracts only', () => {
341
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
          l2SignerOrProvider: ethers.provider as any,
        })
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.be.null
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.deep.equal(ethers.provider)
        }
      })
    })

    describe('when given an L1 provider and an L2 provider', () => {
      it('should attach the provider to both sets of contracts', () => {
357
        const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
          l1SignerOrProvider: ethers.provider as any,
          l2SignerOrProvider: ethers.provider as any,
        })
        for (const contract of Object.values(contracts.l1)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.deep.equal(ethers.provider)
        }
        for (const contract of Object.values(contracts.l2)) {
          expect(contract.signer).to.be.null
          expect(contract.provider).to.deep.equal(ethers.provider)
        }
      })
    })
  })
})