add exchanges data structure

fix tests
This commit is contained in:
Noah Zinsmeister
2019-11-19 18:17:11 -05:00
parent c338bd4e5a
commit 43dfc8c567
10 changed files with 43 additions and 43 deletions

View File

@ -6,7 +6,6 @@ import { AddressZero, MaxUint256 } from 'ethers/constants'
import { bigNumberify, hexlify } from 'ethers/utils'
import { ecsign } from 'ethereumjs-util'
import { CHAIN_ID } from './shared/constants'
import { expandTo18Decimals, getApprovalDigest } from './shared/utilities'
import ERC20 from '../build/GenericERC20.json'
@ -32,17 +31,15 @@ describe('ERC20', () => {
TOKEN_DETAILS.name,
TOKEN_DETAILS.symbol,
TOKEN_DETAILS.decimals,
TOKEN_DETAILS.totalSupply,
CHAIN_ID
TOKEN_DETAILS.totalSupply
])
})
it('name, symbol, decimals, totalSupply, chainId', async () => {
it('name, symbol, decimals, totalSupply', async () => {
expect(await token.name()).to.eq(TOKEN_DETAILS.name)
expect(await token.symbol()).to.eq(TOKEN_DETAILS.symbol)
expect(await token.decimals()).to.eq(TOKEN_DETAILS.decimals)
expect(await token.totalSupply()).to.eq(TOKEN_DETAILS.totalSupply)
expect(await token.chainId()).to.eq(CHAIN_ID)
})
it('transfer', async () => {

View File

@ -4,7 +4,6 @@ import { solidity, createMockProvider, getWallets, createFixtureLoader } from 'e
import { Contract } from 'ethers'
import { BigNumber, bigNumberify } from 'ethers/utils'
import { CHAIN_ID } from './shared/constants'
import { expandTo18Decimals } from './shared/utilities'
import { exchangeFixture, ExchangeFixture } from './shared/fixtures'
@ -28,12 +27,6 @@ describe('UniswapV2', () => {
exchange = _exchange
})
it('initialize:fail', async () => {
await expect(exchange.connect(wallet).initialize(token0.address, token1.address, CHAIN_ID)).to.be.revertedWith(
'UniswapV2: ALREADY_INITIALIZED'
)
})
it('getAmountOutput', async () => {
const testCases: BigNumber[][] = [
[1, 5, 10],
@ -73,7 +66,15 @@ describe('UniswapV2', () => {
await token1.transfer(exchange.address, token1Amount)
await expect(exchange.connect(wallet).mintLiquidity(wallet.address))
.to.emit(exchange, 'LiquidityMinted')
.withArgs(wallet.address, wallet.address, expectedLiquidity, token0Amount, token1Amount)
.withArgs(
wallet.address,
wallet.address,
token0Amount,
token1Amount,
token0Amount,
token1Amount,
expectedLiquidity
)
expect(await exchange.totalSupply()).to.eq(expectedLiquidity)
expect(await exchange.balanceOf(wallet.address)).to.eq(expectedLiquidity)
@ -122,7 +123,15 @@ describe('UniswapV2', () => {
expect(await exchange.swap)
await expect(exchange.connect(wallet).swap(token0.address, wallet.address))
.to.emit(exchange, 'Swap')
.withArgs(wallet.address, wallet.address, token0.address, swapAmount, expectedOutputAmount)
.withArgs(
wallet.address,
wallet.address,
swapAmount,
expectedOutputAmount,
token0Amount.add(swapAmount),
token1Amount.sub(expectedOutputAmount),
token0.address
)
expect(await token0.balanceOf(exchange.address)).to.eq(token0Amount.add(swapAmount))
expect(await token1.balanceOf(exchange.address)).to.eq(token1Amount.sub(expectedOutputAmount))
@ -140,7 +149,7 @@ describe('UniswapV2', () => {
await exchange.connect(wallet).transfer(exchange.address, liquidity)
await expect(exchange.connect(wallet).burnLiquidity(wallet.address))
.to.emit(exchange, 'LiquidityBurned')
.withArgs(wallet.address, wallet.address, liquidity, token0Amount, token1Amount)
.withArgs(wallet.address, wallet.address, token0Amount, token1Amount, bigNumberify(0), bigNumberify(0), liquidity)
expect(await exchange.balanceOf(wallet.address)).to.eq(0)
expect(await token0.balanceOf(exchange.address)).to.eq(0)

View File

@ -4,7 +4,6 @@ import { solidity, createMockProvider, getWallets, createFixtureLoader } from 'e
import { Contract } from 'ethers'
import { bigNumberify } from 'ethers/utils'
import { CHAIN_ID } from './shared/constants'
import { getCreate2Address } from './shared/utilities'
import { factoryFixture, FactoryFixture } from './shared/fixtures'
@ -31,10 +30,9 @@ describe('UniswapV2Factory', () => {
factory = _factory
})
it('exchangeBytecode, chainId, exchangeCount', async () => {
it('exchangeBytecode', async () => {
expect(await factory.exchangeBytecode()).to.eq(bytecode)
expect(await factory.chainId()).to.eq(CHAIN_ID)
expect(await factory.exchangeCount()).to.eq(0)
expect(await factory.getExchangesLength()).to.eq(0)
})
async function createExchange(tokens: string[]) {
@ -42,13 +40,13 @@ describe('UniswapV2Factory', () => {
await expect(factory.createExchange(...tokens))
.to.emit(factory, 'ExchangeCreated')
.withArgs(TEST_ADDRESSES.token0, TEST_ADDRESSES.token1, create2Address, bigNumberify(0))
.withArgs(TEST_ADDRESSES.token0, TEST_ADDRESSES.token1, create2Address, bigNumberify(1))
await expect(factory.createExchange(...tokens)).to.be.revertedWith('UniswapV2Factory: EXCHANGE_EXISTS')
await expect(factory.createExchange(...tokens.slice().reverse())).to.be.revertedWith(
'UniswapV2Factory: EXCHANGE_EXISTS'
)
expect(await factory.exchangeCount()).to.eq(1)
expect(await factory.getExchangesLength()).to.eq(1)
expect(await factory.getTokens(create2Address)).to.deep.eq([TEST_ADDRESSES.token0, TEST_ADDRESSES.token1])
expect(await factory.getExchange(...tokens)).to.eq(create2Address)
expect(await factory.getExchange(...tokens.slice().reverse())).to.eq(create2Address)

View File

@ -1,4 +0,0 @@
import { expandTo18Decimals } from './utilities'
export const CHAIN_ID = 1
export const TOTAL_SUPPLY = expandTo18Decimals(1000)

View File

@ -1,7 +1,6 @@
import { providers, Wallet, Contract } from 'ethers'
import { deployContract } from 'ethereum-waffle'
import { CHAIN_ID } from './constants'
import { expandTo18Decimals } from './utilities'
import ERC20 from '../../build/GenericERC20.json'
@ -15,7 +14,7 @@ export interface FactoryFixture {
export async function factoryFixture(provider: providers.Web3Provider, [wallet]: Wallet[]): Promise<FactoryFixture> {
const bytecode = `0x${UniswapV2.evm.bytecode.object}`
const factory = await deployContract(wallet, UniswapV2Factory, [bytecode, CHAIN_ID], {
const factory = await deployContract(wallet, UniswapV2Factory, [bytecode], {
gasLimit: (provider._web3Provider as any).options.gasLimit
})
@ -31,8 +30,8 @@ export interface ExchangeFixture extends FactoryFixture {
export async function exchangeFixture(provider: providers.Web3Provider, [wallet]: Wallet[]): Promise<ExchangeFixture> {
const { bytecode, factory } = await factoryFixture(provider, [wallet])
const tokenA = await deployContract(wallet, ERC20, ['Test Token A', 'TESTA', 18, expandTo18Decimals(1000), CHAIN_ID])
const tokenB = await deployContract(wallet, ERC20, ['Test Token B', 'TESTB', 18, expandTo18Decimals(1000), CHAIN_ID])
const tokenA = await deployContract(wallet, ERC20, ['Test Token A', 'TESTA', 18, expandTo18Decimals(1000)])
const tokenB = await deployContract(wallet, ERC20, ['Test Token B', 'TESTB', 18, expandTo18Decimals(1000)])
await factory.createExchange(tokenA.address, tokenB.address)
const exchangeAddress = await factory.getExchange(tokenA.address, tokenB.address)

View File

@ -9,8 +9,6 @@ import {
solidityPack
} from 'ethers/utils'
import { CHAIN_ID } from './constants'
const APPROVE_TYPEHASH = keccak256(
toUtf8Bytes('Approve(address owner,address spender,uint256 value,uint256 nonce,uint256 expiration)')
)
@ -24,7 +22,7 @@ const GET_DOMAIN_SEPARATOR = async (token: Contract) => {
keccak256(toUtf8Bytes('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)')),
keccak256(toUtf8Bytes(name)),
keccak256(toUtf8Bytes('1')),
CHAIN_ID,
1,
token.address
]
)