1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
75
76
77
78
79
import { Provider } from '@ethersproject/abstract-provider'
import { Contract } from 'ethers'
import { ethers } from 'hardhat'
import { expect } from '../setup'
import { toSignerOrProvider, toTransactionHash } from '../../src'
describe('type coercion utils', () => {
describe('toSignerOrProvider', () => {
it('should convert a string to a JsonRpcProvider', () => {
const provider = toSignerOrProvider('http://localhost:8545')
expect(Provider.isProvider(provider)).to.be.true
})
it('should not do anything with a provider', () => {
const provider = toSignerOrProvider(ethers.provider)
expect(provider).to.deep.equal(ethers.provider)
})
})
describe('toTransactionHash', () => {
describe('string inputs', () => {
it('should return the input if the input is a valid transaction hash', () => {
const input = '0x' + '11'.repeat(32)
expect(toTransactionHash(input)).to.equal(input)
})
it('should throw an error if the input is a hex string but not a transaction hash', () => {
const input = '0x' + '11'.repeat(31)
expect(() => toTransactionHash(input)).to.throw(
'Invalid transaction hash'
)
})
it('should throw an error if the input is not a hex string', () => {
const input = 'hi mom look at me go'
expect(() => toTransactionHash(input)).to.throw(
'Invalid transaction hash'
)
})
})
describe('transaction inputs', () => {
let AbsolutelyNothing: Contract
before(async () => {
AbsolutelyNothing = (await (
await ethers.getContractFactory('AbsolutelyNothing')
).deploy()) as any
})
it('should return the transaction hash if the input is a transaction response', async () => {
const tx = await AbsolutelyNothing.doAbsolutelyNothing()
expect(toTransactionHash(tx)).to.equal(tx.hash)
})
it('should return the transaction hash if the input is a transaction receipt', async () => {
const tx = await AbsolutelyNothing.doAbsolutelyNothing()
const receipt = await tx.wait()
expect(toTransactionHash(receipt)).to.equal(receipt.transactionHash)
})
})
describe('other types', () => {
it('should throw if given a number as an input', () => {
expect(() => toTransactionHash(1234 as any)).to.throw(
'Invalid transaction'
)
})
it('should throw if given a function as an input', () => {
expect(() =>
toTransactionHash((() => {
return 1234
}) as any)
).to.throw('Invalid transaction')
})
})
})
})