improve code quality of tests
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { providers, Wallet, Contract } from 'ethers'
|
||||
import { Contract, Wallet } from 'ethers'
|
||||
import { Web3Provider } from 'ethers/providers'
|
||||
import { deployContract } from 'ethereum-waffle'
|
||||
|
||||
import { expandTo18Decimals } from './utilities'
|
||||
@ -7,22 +8,22 @@ import GenericERC20 from '../../build/GenericERC20.json'
|
||||
import UniswapV2Factory from '../../build/UniswapV2Factory.json'
|
||||
import UniswapV2Exchange from '../../build/UniswapV2Exchange.json'
|
||||
|
||||
export interface FactoryFixture {
|
||||
interface FactoryFixture {
|
||||
factory: Contract
|
||||
}
|
||||
|
||||
export async function factoryFixture(_: providers.Web3Provider, [wallet]: Wallet[]): Promise<FactoryFixture> {
|
||||
export async function factoryFixture(_: Web3Provider, [wallet]: Wallet[]): Promise<FactoryFixture> {
|
||||
const factory = await deployContract(wallet, UniswapV2Factory, [wallet.address])
|
||||
return { factory }
|
||||
}
|
||||
|
||||
export interface ExchangeFixture extends FactoryFixture {
|
||||
interface ExchangeFixture extends FactoryFixture {
|
||||
token0: Contract
|
||||
token1: Contract
|
||||
exchange: Contract
|
||||
}
|
||||
|
||||
export async function exchangeFixture(provider: providers.Web3Provider, [wallet]: Wallet[]): Promise<ExchangeFixture> {
|
||||
export async function exchangeFixture(provider: Web3Provider, [wallet]: Wallet[]): Promise<ExchangeFixture> {
|
||||
const { factory } = await factoryFixture(provider, [wallet])
|
||||
|
||||
const tokenA = await deployContract(wallet, GenericERC20, [expandTo18Decimals(10000)])
|
||||
@ -30,7 +31,7 @@ export async function exchangeFixture(provider: providers.Web3Provider, [wallet]
|
||||
|
||||
await factory.createExchange(tokenA.address, tokenB.address)
|
||||
const exchangeAddress = await factory.getExchange(tokenA.address, tokenB.address)
|
||||
const exchange = new Contract(exchangeAddress, JSON.stringify(UniswapV2Exchange.abi), provider)
|
||||
const exchange = new Contract(exchangeAddress, JSON.stringify(UniswapV2Exchange.abi), provider).connect(wallet)
|
||||
|
||||
const token0Address = (await exchange.token0()).address
|
||||
const token0 = tokenA.address === token0Address ? tokenA : tokenB
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { providers, Contract } from 'ethers'
|
||||
import { Contract } from 'ethers'
|
||||
import { Web3Provider } from 'ethers/providers'
|
||||
import {
|
||||
BigNumber,
|
||||
bigNumberify,
|
||||
@ -9,16 +10,15 @@ import {
|
||||
solidityPack
|
||||
} from 'ethers/utils'
|
||||
|
||||
export function expandTo18Decimals(n: number): BigNumber {
|
||||
return bigNumberify(n).mul(bigNumberify(10).pow(18))
|
||||
}
|
||||
|
||||
const PERMIT_TYPEHASH = keccak256(
|
||||
toUtf8Bytes('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)')
|
||||
)
|
||||
|
||||
const GET_DOMAIN_SEPARATOR = async (token: Contract) => {
|
||||
const name = await token.name()
|
||||
export function expandTo18Decimals(n: number): BigNumber {
|
||||
return bigNumberify(n).mul(bigNumberify(10).pow(18))
|
||||
}
|
||||
|
||||
function getDomainSeparator(name: string, tokenAddress: string) {
|
||||
return keccak256(
|
||||
defaultAbiCoder.encode(
|
||||
['bytes32', 'bytes32', 'bytes32', 'uint256', 'address'],
|
||||
@ -27,7 +27,7 @@ const GET_DOMAIN_SEPARATOR = async (token: Contract) => {
|
||||
keccak256(toUtf8Bytes(name)),
|
||||
keccak256(toUtf8Bytes('1')),
|
||||
1,
|
||||
token.address
|
||||
tokenAddress
|
||||
]
|
||||
)
|
||||
)
|
||||
@ -45,24 +45,22 @@ export function getCreate2Address(
|
||||
keccak256(solidityPack(['address', 'address'], [token0Address, token1Address])),
|
||||
keccak256(bytecode)
|
||||
]
|
||||
|
||||
const sanitizedInputs = `0x${create2Inputs.map(i => i.slice(2)).join('')}`
|
||||
|
||||
return getAddress(`0x${keccak256(sanitizedInputs).slice(-40)}`)
|
||||
}
|
||||
|
||||
interface Approve {
|
||||
owner: string
|
||||
spender: string
|
||||
value: BigNumber
|
||||
}
|
||||
export async function getApprovalDigest(
|
||||
token: Contract,
|
||||
approve: Approve,
|
||||
approve: {
|
||||
owner: string
|
||||
spender: string
|
||||
value: BigNumber
|
||||
},
|
||||
nonce: BigNumber,
|
||||
expiration: BigNumber
|
||||
): Promise<string> {
|
||||
const DOMAIN_SEPARATOR = await GET_DOMAIN_SEPARATOR(token)
|
||||
const name = await token.name()
|
||||
const DOMAIN_SEPARATOR = getDomainSeparator(name, token.address)
|
||||
return keccak256(
|
||||
solidityPack(
|
||||
['bytes1', 'bytes1', 'bytes32', 'bytes32'],
|
||||
@ -81,7 +79,7 @@ export async function getApprovalDigest(
|
||||
)
|
||||
}
|
||||
|
||||
async function mineBlock(provider: providers.Web3Provider, timestamp?: number): Promise<void> {
|
||||
async function mineBlock(provider: Web3Provider, timestamp?: number): Promise<void> {
|
||||
await new Promise((resolve, reject) => {
|
||||
;(provider._web3Provider.sendAsync as any)(
|
||||
{ jsonrpc: '2.0', method: 'evm_mine', params: timestamp ? [timestamp] : [] },
|
||||
@ -96,11 +94,7 @@ async function mineBlock(provider: providers.Web3Provider, timestamp?: number):
|
||||
})
|
||||
}
|
||||
|
||||
export async function mineBlocks(
|
||||
provider: providers.Web3Provider,
|
||||
numberOfBlocks: number,
|
||||
timestamp?: number
|
||||
): Promise<void> {
|
||||
export async function mineBlocks(provider: Web3Provider, numberOfBlocks: number, timestamp?: number): Promise<void> {
|
||||
await Promise.all([...Array(numberOfBlocks - 1)].map(() => mineBlock(provider)))
|
||||
await mineBlock(provider, timestamp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user