Skip to content

Commit

Permalink
Merge pull request #62 from consenlabs/test-sig-using-on-chain-contracts
Browse files Browse the repository at this point in the history
fix: refactoring tests
  • Loading branch information
BenjaminLu authored Sep 15, 2023
2 parents 4b5b230 + 9980b10 commit 40d203c
Show file tree
Hide file tree
Showing 16 changed files with 1,227 additions and 1,499 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@consenlabs/tokenlon-mmsk",
"version": "5.3.2",
"version": "5.3.3",
"description": "Tokenlon market maker server kit",
"author": "imToken PTE. LTD.",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/handler/newOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface Order {
payload?: string
}

interface Response {
export interface Response {
rate: NumberOrString
minAmount: NumberOrString
maxAmount: NumberOrString
Expand Down Expand Up @@ -205,7 +205,7 @@ const _getBaseTokenByAddress = (baseTokenAddr, tokenList) => {

const getBaseTokenByAddress = memoize(_getBaseTokenByAddress)

export const newOrder = async (ctx) => {
export const newOrder = async (ctx): Promise<Response> => {
const { quoter, signer, chainID, walletType, signingUrl, permitType } = ctx
const req: QueryInterface = {
protocol: Protocol.PMMV5, // by default is v2 protocol
Expand Down
2 changes: 1 addition & 1 deletion src/handler/version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VERSION = '5.3.2'
export const VERSION = '5.3.3'

export const version = (ctx) => {
ctx.body = {
Expand Down
2 changes: 1 addition & 1 deletion src/signer/pmmv5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Protocol } from '../types'
import { ExtendedZXOrder, RemoteSigningPMMV5Request } from './types'
import { Order as ZXOrder } from '0x-v2-order-utils'

const EIP712_ORDER_SCHEMA = {
export const EIP712_ORDER_SCHEMA = {
name: 'Order',
parameters: [
{ name: 'makerAddress', type: EIP712Types.Address },
Expand Down
2 changes: 0 additions & 2 deletions src/signer/rfqv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ export const buildSignedOrder = async (
const orderHash = getOfferHash(rfqOrder)
console.log(`orderHash: ${orderHash}`)
const orderSignDigest = getOfferSignDigest(rfqOrder, chainId, rfqAddr)
console.log(`chainId: ${chainId}`)
console.log(`rfqAddr: ${rfqAddr}`)
console.log(`orderSignDigest: ${orderSignDigest}`)
let makerWalletSignature
if (!signingUrl) {
Expand Down
133 changes: 133 additions & 0 deletions test/amm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { ethers, network } from 'hardhat'
import { Wallet, utils } from 'ethers'
import { Protocol } from '../src/types'
import { WalletType } from '../src/signer/types'
import * as ethUtils from 'ethereumjs-util'
import { WETH, ZERO } from '@tokenlon/sdk'
import { expect } from 'chai'
import { generateSaltWithFeeFactor } from '../src/signer/pmmv5'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { USDT_ADDRESS, callNewOrder, expectOrder, getMarketMakingInfo, init } from './utils'
import { assetDataUtils } from '0x-v2-order-utils'
import { FEE_RECIPIENT_ADDRESS } from '../src/constants'

describe('AMM NewOrder', function () {
const chainId: number = network.config.chainId!
let signer: SignerWithAddress
before(async () => {
const signers = await ethers.getSigners()
signer = signers[0]
})
beforeEach(() => {
init(chainId, signer)
})
it('should create ammv1 order by uniswap v2', async function () {
const marketMakingInfo = getMarketMakingInfo()
const ammAddr = '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852'
const order = await callNewOrder({
chainId: chainId,
base: 'ETH',
quote: 'USDT',
side: 'SELL',
amount: 0.1,
signer,
userAddr: Wallet.createRandom().address.toLowerCase(),
protocol: Protocol.AMMV1,
makerAddress: ammAddr,
})
expectOrder({
order: order,
expectedProtocol: Protocol.AMMV1,
expectedTakerAddress: marketMakingInfo.userProxyContractAddress,
expectedMakerAddress: ammAddr,
expectedTakerAssetAddress: ZERO[chainId],
expectedMakerAssetAddress: USDT_ADDRESS[chainId],
expectedTakerAssetAmount: utils.parseEther('0.1').toString(),
expectedMakerAssetAmount: utils.parseUnits('0.1', 6).toString(),
expectedFeeRecipient: FEE_RECIPIENT_ADDRESS,
expectedSenderAddress: marketMakingInfo.tokenlonExchangeContractAddress,
})
})
it('should create ammv2 order by uniswap v2', async function () {
const marketMakingInfo = getMarketMakingInfo()
const ammAddr = '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852'
const payload = Buffer.from(
JSON.stringify({
path: [WETH[chainId].toLowerCase(), USDT_ADDRESS[chainId].toLowerCase()],
})
).toString('base64')
const order = await callNewOrder({
chainId: chainId,
base: 'ETH',
quote: 'USDT',
side: 'SELL',
amount: 0.1,
signer,
userAddr: Wallet.createRandom().address.toLowerCase(),
protocol: Protocol.AMMV2,
makerAddress: ammAddr,
payload: payload,
})
expectOrder({
order: order,
expectedProtocol: Protocol.AMMV2,
expectedTakerAddress: marketMakingInfo.userProxyContractAddress,
expectedMakerAddress: ammAddr,
expectedTakerAssetAddress: ZERO[chainId],
expectedMakerAssetAddress: USDT_ADDRESS[chainId],
expectedTakerAssetAmount: utils.parseEther('0.1').toString(),
expectedMakerAssetAmount: utils.parseUnits('0.1', 6).toString(),
expectedFeeRecipient: FEE_RECIPIENT_ADDRESS,
expectedSenderAddress: marketMakingInfo.tokenlonExchangeContractAddress,
})
expect(order.payload).eq(payload)
})
describe('handle token precision and decimals', () => {
it('should format taker asset amount', async function () {
const order = await callNewOrder({
chainId: chainId,
base: 'ETH',
quote: 'USDT',
side: 'BUY',
amount: 0.1111,
walletType: WalletType.MMP_VERSION_4,
signer: Wallet.createRandom(),
userAddr: Wallet.createRandom().address.toLowerCase(),
protocol: Protocol.PMMV5,
})
expect(order).is.not.null
expect(order.quoteId).eq('1--echo-testing-8888')
expect(order.makerWalletSignature?.toString().slice(-1)).eq('4')
expect(order.takerAssetData).eq(assetDataUtils.encodeERC20AssetData(USDT_ADDRESS[chainId]))
expect(order.takerAssetAmount).eq(utils.parseUnits('0.1114', 6).toString())
expect(order.makerAssetAmount).eq(utils.parseEther('0.1114').toString())
})
it('should format maker asset amount', async function () {
const order = await callNewOrder({
chainId: chainId,
base: 'ETH',
quote: 'USDT',
side: 'SELL',
amount: 0.1111,
walletType: WalletType.MMP_VERSION_4,
signer: Wallet.createRandom(),
userAddr: Wallet.createRandom().address.toLowerCase(),
protocol: Protocol.PMMV5,
})
expect(order).is.not.null
expect(order.quoteId).eq('1--echo-testing-8888')
expect(order.makerWalletSignature?.slice(-1)).eq('4')
expect(order.takerAssetAmount).eq(utils.parseEther('0.1111').toString())
expect(order.makerAssetAmount).eq(utils.parseUnits('0.1111', 6).toString())
})
})
it('Should generate correct salt', async () => {
const givenPrefixSalt = generateSaltWithFeeFactor(30, '0x11111111111111111111111111111111')
const salt = generateSaltWithFeeFactor(30)
console.log(givenPrefixSalt.toString(16))
console.log(ethUtils.toBuffer('0x' + salt.toString(16)).length)
console.log(salt.toString(16))
expect(ethUtils.toBuffer('0x' + givenPrefixSalt.toString(16)).length).is.eq(32)
expect(ethUtils.toBuffer('0x' + salt.toString(16)).length).is.eq(32)
})
})
Loading

0 comments on commit 40d203c

Please sign in to comment.