Skip to content

Commit

Permalink
Merge pull request #59 from consenlabs/feature/fix_types
Browse files Browse the repository at this point in the history
fix: adding order types
  • Loading branch information
BenjaminLu authored Sep 5, 2023
2 parents 4d93505 + 61935b7 commit d2236ce
Show file tree
Hide file tree
Showing 18 changed files with 790 additions and 99 deletions.
73 changes: 67 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,87 @@ Program setup,
- types.WalletType.ERC1271
- types.WalletType.EOA
- SIGNING_URL, If you wanna sign orders in your own service instead of the mmsk,
please set the SIGNING_URL to your service endpoint. the mmsk would post every unsigned PMM/RFQV1/RFQV2 orders to your service. Remember to set the WALLET_ADDRESS as well. An example RFQV1 request is shown below:
please set the SIGNING_URL to your service endpoint. the mmsk would post every unsigned PMM/RFQV1/RFQV2 orders to your service. Remember to set the WALLET_ADDRESS as well. Example PMMV5/RFQV1/RFQV2 requests are shown below:

PMMV5:
```
{
quoteId: '0x123',
protocol: 'PMMV5',
pmmOrder: {
makerAddress: '0x86b9f429c3ef44c599eb560eb531a0e3f2e36f64',
makerAssetAmount: '100000000',
makerAssetData: '0xf47261b0000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7',
makerFee: '0',
takerAddress: '0x7bd7d025d4231aad1233967b527ffd7416410257',
takerAssetAmount: '1000000000000000000',
takerAssetData: '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
takerFee: '0',
senderAddress: '0x7bd7d025d4231aad1233967b527ffd7416410257',
feeRecipientAddress: '0x8fba2dd6968ddc51aea563091008d8451fec4db6',
expirationTimeSeconds: '1620444917',
exchangeAddress: '0x86b9f429c3ef44c599eb560eb531a0e3f2e36f64',
salt: '22685491128062564230891640495451214097'
},
feeFactor: 30,
orderHash: '0x9f9bb186d77c19a763266f54978eef923f3e6ebd5ac6d2c687b1323abe91d8b5',
orderSignDigest: '0x6e95144f3539f8679b94e858a0bcd755e8b17a4011e2fdf025387e4523a9b0fe',
userAddr: '0x8fba2dd6968ddc51aea563091008d8451fec4db6',
chainId: 1,
pmmAddr: '0x7bd7d025D4231aAD1233967b527FFd7416410257'
}
```
RFQV1:
```
{
quoteId: '0x123',
protocol: 'RFQV1',
rfqOrder: {
takerAddr: '0x87fca7135c1c54876a62dc4922da3ce45f38debf',
makerAddr: '0x86B9F429C3Ef44c599EB560Eb531A0E3f2E36f64',
takerAddr: '0xcabfea3a7f41452a9c8e475a53b30c43fbff6683',
makerAddr: '0x86b9f429c3ef44c599eb560eb531a0e3f2e36f64',
takerAssetAddr: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
makerAssetAddr: '0xdac17f958d2ee523a2206206994597c13d831ec7',
takerAssetAmount: '1000000000000000000',
makerAssetAmount: '100000000',
deadline: 1620444917,
feeFactor: 30,
salt: '54987026777386128963216107663301166813737035846370728350988439404382800511006'
salt: '7719472615821079694904732333912527190235994441565629342017219118620679208990'
},
userAddr: '0x87fca7135c1c54876a62dc4922da3ce45f38debf',
feeFactor: 30,
orderHash: '0x77eda617afe88090a34cf031470b0968c1754a474f573dfb5c5b5c67cf8167ce',
orderSignDigest: '0xdd785ffa1a694db8af972523ee115fc95580929e23ac5c46a62692b0f6600fc5',
userAddr: '0xcabfea3a7f41452a9c8e475a53b30c43fbff6683',
chainId: 1,
rfqAddr: '0xfD6C2d2499b1331101726A8AC68CCc9Da3fAB54F'
rfqAddr: '0x117CAf73eB142eDC431E707DC33D4dfeF7c5BAd0'
}
```
RFQV2:
```
{
quoteId: '0x123',
protocol: 'RFQV2',
rfqOrder: {
taker: '0x8fda8bc038af1c426838248718eb2fd5425882cc',
maker: '0x86b9f429c3ef44c599eb560eb531a0e3f2e36f64',
takerToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
takerTokenAmount: '1000000000000000000',
makerToken: '0xdac17f958d2ee523a2206206994597c13d831ec7',
makerTokenAmount: '100000000',
feeFactor: '30',
expiry: '1620444917',
salt: '0x11111111111111111111111111111111'
},
feeFactor: 30,
orderHash: '0xd431d9a453b67d76244e6ea1244895fa4c2e874eee03bc2af1f6e171afa938ae',
orderSignDigest: '0x9573a91c47d9bdad67354a5a677d778eff3ccb94866bfe3c2907827db7866c91',
userAddr: '0x8fda8bc038af1c426838248718eb2fd5425882cc',
chainId: 1,
rfqAddr: '0xaE5FDd548E5B107C54E5c0D36952fB8a089f10C7'
}
```
An example response the signing service should return
```
{
Expand Down
2 changes: 2 additions & 0 deletions app/mmConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const types = require('../lib/signer/types')
const dotenv = require('dotenv')
dotenv.config()

module.exports = {
// Tokenlon server address
Expand Down
47 changes: 45 additions & 2 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@consenlabs/tokenlon-mmsk",
"version": "5.3.1",
"version": "5.3.2",
"description": "Tokenlon market maker server kit",
"author": "imToken PTE. LTD.",
"license": "MIT",
Expand All @@ -26,7 +26,7 @@
"check": "node ./app/check.js",
"clean": "rm -rf ./lib",
"start": "node ./app/start.js",
"test": "chainId=5 npx hardhat --network hardhat test",
"test": "chainId=1 npx hardhat --network hardhat test",
"watch": "tsc -w"
},
"devDependencies": {
Expand All @@ -46,6 +46,7 @@
"ethereum-types": "^3.2.0",
"hardhat": "^2.9.3",
"hardhat-gas-reporter": "1.0.4",
"nock": "^13.3.3",
"npm-run-all": "4.1.5",
"prettier": "^2.0.5",
"ts-node": "8.10.2",
Expand Down
1 change: 1 addition & 0 deletions src/handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { getOrderState } from './getOrderState'
export { dealOrder } from './dealOrder'
export { exceptionOrder } from './exceptionOrder'
export { version } from './version'
export { signOrder } from './signOrder'
52 changes: 33 additions & 19 deletions src/handler/newOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Protocol, QueryInterface } from '../types'
import { validateNewOrderRequest, validateRequest } from '../validations'
import { ValidationError } from './errors'
import { addQuoteIdPrefix, constructQuoteResponse, preprocessQuote } from '../quoting'

import { assetDataUtils } from '0x-v2-order-utils'
import { buildSignedOrder as buildRFQV1SignedOrder } from '../signer/rfqv1'
import { buildSignedOrder as buildRFQV2SignedOrder } from '../signer/rfqv2'
Expand All @@ -22,27 +21,28 @@ import {
getWethAddrIfIsEth,
getTimestamp,
} from '../utils'
import { ExtendedZXOrder } from '../signer/types'

type NumberOrString = number | string

interface Order {
export interface Order {
// quoteId is from market maker backend quoter.
quoteId: string
quoteId: string | number
// protocol represents the order type as enum, [PMMV4, PMMV5, AMMV1, RFQV1, AMMV2].
protocol: Protocol

// Common fields
makerAddress: string
makerAssetAmount: string
makerAssetAmount: string | BigNumber
makerAssetAddress: string
takerAddress: string
takerAssetAmount: string
takerAssetAmount: string | BigNumber
takerAssetAddress: string
expirationTimeSeconds: string
expirationTimeSeconds: BigNumber | string
// feeFactor is tokenlon protocol field, works like BPS, should <= 10000.
feeFactor: number
// salt represents the uniqueness of order, is to prevent replay attack.
salt: string
salt?: BigNumber | string

// 0x protocol specific fields
makerAssetData: string
Expand All @@ -53,14 +53,14 @@ interface Order {
exchangeAddress: string

// makerFee and takerFee are not used, but keep to make 0x order signature.
makerFee: string
takerFee: string
makerFee: BigNumber | string
takerFee: BigNumber | string

// PMM/RFQ market maker signature
makerWalletSignature: string
makerWalletSignature?: string

// Extra data
payload: string
payload?: string
}

interface Response {
Expand Down Expand Up @@ -97,7 +97,10 @@ function extractAssetAmounts(
side,
rate: number | string,
amountBN: BigNumber
) {
): {
makerAssetAmount: BigNumber
takerAssetAmount: BigNumber
} {
let makerAssetAmount, takerAssetAmount
if (side === 'BUY') {
makerAssetAmount = fromUnitToDecimalBN(
Expand All @@ -121,15 +124,21 @@ function extractAssetAmounts(
return { makerAssetAmount, takerAssetAmount }
}

function getOrderAndFeeFactor(query: QueryInterface, rate, tokenList, tokenConfigs, config) {
function getOrderAndFeeFactor(
query: QueryInterface,
rate,
tokenList,
tokenConfigs,
config
): ExtendedZXOrder {
const { side, amount, feefactor } = query
const baseToken = getTokenByAddress(tokenList, query.baseAddress)
const quoteToken = getTokenByAddress(tokenList, query.quoteAddress)
const makerToken = side === 'BUY' ? baseToken : quoteToken
const takerToken = side === 'BUY' ? quoteToken : baseToken
const foundTokenConfig = tokenConfigs.find((t) => t.symbol === makerToken.symbol)

let fFactor = config.feeFactor || 10
let fFactor: number = Number(config.feeFactor) || 10
if (foundTokenConfig?.feeFactor) {
// console.log('set fee factor from token config', { factor: foundTokenConfig.feeFactor })
fFactor = foundTokenConfig.feeFactor
Expand All @@ -154,32 +163,37 @@ function getOrderAndFeeFactor(query: QueryInterface, rate, tokenList, tokenConfi
)

// ETH -> WETH
const makerAssetAddress = getWethAddrIfIsEth(
const makerAssetAddress: string = getWethAddrIfIsEth(
makerToken.contractAddress,
config.wethContractAddress
)
// ETH -> WETH
let takerAssetAddress = getWethAddrIfIsEth(takerToken.contractAddress, config.wethContractAddress)
let takerAssetAddress: string = getWethAddrIfIsEth(
takerToken.contractAddress,
config.wethContractAddress
)
if (Protocol.RFQV2 === query.protocol) {
takerAssetAddress = takerToken.contractAddress
}
return {
protocol: query.protocol,
quoteId: query.uniqId,
makerAddress: config.mmProxyContractAddress.toLowerCase(),
makerAssetAmount,
makerAssetAddress: makerAssetAddress,
makerAssetData: assetDataUtils.encodeERC20AssetData(makerAssetAddress),
makerFee: toBN(0),

takerAddress: config.userProxyContractAddress,
takerAddress: config.userProxyContractAddress as string,
takerAssetAmount,
takerAssetAddress: takerAssetAddress,
takerAssetData: assetDataUtils.encodeERC20AssetData(takerAssetAddress),
takerFee: toBN(0),

senderAddress: config.tokenlonExchangeContractAddress.toLowerCase(),
feeRecipientAddress: FEE_RECIPIENT_ADDRESS,
feeRecipientAddress: FEE_RECIPIENT_ADDRESS.toLowerCase(),
expirationTimeSeconds: toBN(getTimestamp() + +config.orderExpirationSeconds),
exchangeAddress: config.exchangeContractAddress,
exchangeAddress: config.exchangeContractAddress.toLowerCase() as string,

feeFactor: fFactor,
}
Expand Down
Loading

0 comments on commit d2236ce

Please sign in to comment.