github.com/ethereum-optimism/optimism@v1.7.2/packages/sdk/test/utils/coercion.spec.ts (about)

     1  import { Provider } from '@ethersproject/abstract-provider'
     2  import { Contract } from 'ethers'
     3  import { ethers } from 'hardhat'
     4  
     5  import { expect } from '../setup'
     6  import { toSignerOrProvider, toTransactionHash } from '../../src'
     7  
     8  describe('type coercion utils', () => {
     9    describe('toSignerOrProvider', () => {
    10      it('should convert a string to a JsonRpcProvider', () => {
    11        const provider = toSignerOrProvider('http://localhost:8545')
    12        expect(Provider.isProvider(provider)).to.be.true
    13      })
    14  
    15      it('should not do anything with a provider', () => {
    16        const provider = toSignerOrProvider(ethers.provider)
    17        expect(provider).to.deep.equal(ethers.provider)
    18      })
    19    })
    20  
    21    describe('toTransactionHash', () => {
    22      describe('string inputs', () => {
    23        it('should return the input if the input is a valid transaction hash', () => {
    24          const input = '0x' + '11'.repeat(32)
    25          expect(toTransactionHash(input)).to.equal(input)
    26        })
    27  
    28        it('should throw an error if the input is a hex string but not a transaction hash', () => {
    29          const input = '0x' + '11'.repeat(31)
    30          expect(() => toTransactionHash(input)).to.throw(
    31            'Invalid transaction hash'
    32          )
    33        })
    34  
    35        it('should throw an error if the input is not a hex string', () => {
    36          const input = 'hi mom look at me go'
    37          expect(() => toTransactionHash(input)).to.throw(
    38            'Invalid transaction hash'
    39          )
    40        })
    41      })
    42  
    43      describe('transaction inputs', () => {
    44        let AbsolutelyNothing: Contract
    45        before(async () => {
    46          AbsolutelyNothing = (await (
    47            await ethers.getContractFactory('AbsolutelyNothing')
    48          ).deploy()) as any
    49        })
    50  
    51        it('should return the transaction hash if the input is a transaction response', async () => {
    52          const tx = await AbsolutelyNothing.doAbsolutelyNothing()
    53          expect(toTransactionHash(tx)).to.equal(tx.hash)
    54        })
    55  
    56        it('should return the transaction hash if the input is a transaction receipt', async () => {
    57          const tx = await AbsolutelyNothing.doAbsolutelyNothing()
    58          const receipt = await tx.wait()
    59          expect(toTransactionHash(receipt)).to.equal(receipt.transactionHash)
    60        })
    61      })
    62  
    63      describe('other types', () => {
    64        it('should throw if given a number as an input', () => {
    65          expect(() => toTransactionHash(1234 as any)).to.throw(
    66            'Invalid transaction'
    67          )
    68        })
    69  
    70        it('should throw if given a function as an input', () => {
    71          expect(() =>
    72            toTransactionHash((() => {
    73              return 1234
    74            }) as any)
    75          ).to.throw('Invalid transaction')
    76        })
    77      })
    78    })
    79  })