Skip to content

Commit

Permalink
Merge pull request #23 from consenlabs/fix/rfq-sign-eoa
Browse files Browse the repository at this point in the history
  • Loading branch information
kaichen authored Jun 22, 2021
2 parents 1797093 + f632b36 commit faf4919
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
14 changes: 7 additions & 7 deletions benchmark/MockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ function createRPCHandler(): JsonRPC {
forwarderContractAddress: '0xd85e2fa7e7e252b27b01bf0d65c946959d2f45b8',
zrxContractAddress: '0x2002d3812f58e35f0ea1ffbf80a75a38c32175fa',
tokenlonExchangeContractAddress: '0xd489f1684cf5e78d933e254bd7ac8a9a6a70d491',
wethContractAddress: '0xd0a1e359811322d97991e03f863a0c30c2cf029c',
wethContractAddress: '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6',
userProxyContractAddress: '0x25657705a6be20511687D483f2fCCfb2d92f6033',
orderExpirationSeconds: 600,
mmProxyContractAddress: '0xf79e851C12160E9F207E54C3B65c9Be9c956b2d3',
mmProxyContractAddress: '0xd7Fe4409973a032a4B19E3b4aE3C2B6E6559D12E',
feeFactor: 30,
addressBookV5: {
Tokenlon: '0x4A234aA24A1911Fa256162Dd34395B676f5DbdA6',
PMM: '0xD6ec1bAd089241207C640CaE45Fec5576C3D72d5',
AllowanceTarget: '0x595d711189e48e766cc0cc011e85e40702764288',
AllowanceTarget: '0x6F1eE33E23a33Ef690C6C0CD9B7Dc4a666e072E5',
AMMQuoter: '0x75a4f88deeed0ace489285d1695323ef49dbc2ab',
AMMWrapper: '0xcf011536f10e85e376e70905eed4ca9ea8cded34',
RFQ: '0xfD474E4809e690626C67ECb7A908de4b9c464b99',
Expand All @@ -87,15 +87,15 @@ function createRPCHandler(): JsonRPC {
},
{
symbol: 'WETH',
contractAddress: '0xd0a1e359811322d97991e03f863a0c30c2cf029c',
contractAddress: '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6',
decimal: 18,
precision: 6,
minTradeAmount: 0.1,
maxTradeAmount: 1e1,
},
{
symbol: 'DAI',
contractAddress: '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa',
symbol: 'LON',
contractAddress: '0x6dA0e6ABd44175f50C563cd8b860DD988A7C3433',
decimal: 18,
precision: 6,
minTradeAmount: 0.001,
Expand All @@ -111,7 +111,7 @@ function createRPCHandler(): JsonRPC {
},
{
symbol: 'USDT',
contractAddress: '0xd85476c906b5301e8e9eb58d174a6f96b9dfc5ee',
contractAddress: '0xa93Ef9215b907c19e739E2214e1AA5412a0401B5',
decimal: 6,
precision: 4,
minTradeAmount: 5,
Expand Down
4 changes: 2 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.2.0-alpha.0",
"version": "5.2.0-alpha.1",
"description": "Tokenlon market maker server kit",
"author": "imToken PTE. LTD.",
"license": "MIT",
Expand Down Expand Up @@ -64,7 +64,7 @@
"crypto-random-string": "^3.3.0",
"dotenv": "^8.2.0",
"ethereumjs-util": "^7.0.4",
"ethers": "^5.0.5",
"ethers": "^5.2.0",
"keythereum": "1.0.4",
"koa": "2.5.3",
"koa-bodyparser": "4.2.1",
Expand Down
23 changes: 15 additions & 8 deletions src/handler/newOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,28 @@ function getOrderAndFeeFactor(simpleOrder, rate, tokenList, tokenConfigs, config
toBN(amount)
)

// ETH -> WETH
const makerAssetAddress = getWethAddrIfIsEth(
makerToken.contractAddress,
config.wethContractAddress
)
// ETH -> WETH
const takerAssetAddress = getWethAddrIfIsEth(
takerToken.contractAddress,
config.wethContractAddress
)

return {
makerAddress: config.mmProxyContractAddress.toLowerCase(),
makerAssetAmount,
makerAssetAddress: makerToken.contractAddress,
makerAssetData: assetDataUtils.encodeERC20AssetData(
getWethAddrIfIsEth(makerToken.contractAddress, config.wethContractAddress)
),
makerAssetAddress: makerAssetAddress,
makerAssetData: assetDataUtils.encodeERC20AssetData(makerAssetAddress),
makerFee: toBN(0),

takerAddress: config.userProxyContractAddress,
takerAssetAmount,
takerAssetAddress: takerToken.contractAddress,
takerAssetData: assetDataUtils.encodeERC20AssetData(
getWethAddrIfIsEth(takerToken.contractAddress, config.wethContractAddress)
),
takerAssetAddress: takerAssetAddress,
takerAssetData: assetDataUtils.encodeERC20AssetData(takerAssetAddress),
takerFee: toBN(0),

senderAddress: config.tokenlonExchangeContractAddress.toLowerCase(),
Expand Down
2 changes: 1 addition & 1 deletion src/handler/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const version = (ctx) => {
ctx.body = {
result: true,
version: '5.2.0-alpha.0',
version: '5.2.0-alpha.1',
}
}
6 changes: 2 additions & 4 deletions src/signer/ammv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ export const buildSignedOrder = (order, makerAddress, wethAddress) => {
order.makerAddress = makerAddress
// 2. convert weth to eth
if (makerAssetAddress === wethAddress.toLowerCase()) {
order.makerAssetAddress = NULL_ADDRESS
order.makerAssetData = assetDataUtils.encodeERC20AssetData(NULL_ADDRESS)
} else {
order.makerAssetData = assetDataUtils.encodeERC20AssetData(makerAssetAddress)
}

if (takerAssetAddress === wethAddress.toLowerCase()) {
order.takerAssetAddress = NULL_ADDRESS
order.takerAssetData = assetDataUtils.encodeERC20AssetData(NULL_ADDRESS)
} else {
order.takerAssetData = assetDataUtils.encodeERC20AssetData(takerAssetAddress)
}
// NOTE: for AMM order we don't do signing here
const signedOrder = {
Expand Down
5 changes: 3 additions & 2 deletions src/signer/rfqv1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Wallet } from 'ethers'
import { Wallet, utils } from 'ethers'
import { orderBNToString, BigNumber } from '../utils'
import { generateSaltWithFeeFactor, signWithUserAndFee } from './pmmv5'
import { getOrderSignDigest } from './orderHash'
Expand Down Expand Up @@ -28,7 +28,8 @@ export enum SignatureType {
// +------|---------|---------|-------------------|---------+
export async function signByEOA(orderHash: string, wallet: Wallet): Promise<string> {
// signature: R+S+V
let signature = await wallet.signMessage(orderHash)
const hashArray = utils.arrayify(orderHash)
let signature = await wallet.signMessage(hashArray)
var signatureBuffer = Buffer.concat([
ethUtils.toBuffer(signature),
ethUtils.toBuffer('0x' + '00'.repeat(32)),
Expand Down
14 changes: 9 additions & 5 deletions test/new_order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('NewOrder', function () {
)
assert.equal(order.takerAddress, '0x7bd7d025d4231aad1233967b527ffd7416410257')
assert.equal(order.takerAssetAmount, '100000000000000000')
assert.equal(order.takerAssetAddress, '0x0000000000000000000000000000000000000000')
assert.equal(order.takerAssetAddress, '0xd0a1e359811322d97991e03f863a0c30c2cf029c')
assert.equal(
order.takerAssetData,
'0xf47261b0000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c'
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('NewOrder', function () {
)
assert.equal(order.takerAddress, '0x7bd7d025d4231aad1233967b527ffd7416410257')
assert.equal(order.takerAssetAmount, '100000000000000000')
assert.equal(order.takerAssetAddress, '0x0000000000000000000000000000000000000000')
assert.equal(order.takerAssetAddress, '0xd0a1e359811322d97991e03f863a0c30c2cf029c')
assert.equal(
order.takerAssetData,
'0xf47261b0000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c'
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('NewOrder', function () {
)
assert.equal(order.takerAddress, userAddr)
assert.equal(order.takerAssetAmount, '100000000000000000')
assert.equal(order.takerAssetAddress, '0x0000000000000000000000000000000000000000')
assert.equal(order.takerAssetAddress, '0xd0a1e359811322d97991e03f863a0c30c2cf029c')
assert.equal(
order.takerAssetData,
'0xf47261b0000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c'
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('NewOrder', function () {
)
assert.equal(order.takerAddress, userAddr)
assert.equal(order.takerAssetAmount, '100000000000000000')
assert.equal(order.takerAssetAddress, '0x0000000000000000000000000000000000000000')
assert.equal(order.takerAssetAddress, '0xd0a1e359811322d97991e03f863a0c30c2cf029c')
assert.equal(
order.takerAssetData,
'0xf47261b0000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c'
Expand All @@ -423,7 +423,11 @@ describe('NewOrder', function () {
// verify signature
const rfqAddr = updaterStack['markerMakerConfigUpdater'].cacheResult.addressBookV5.RFQ
const orderHash = getOrderSignDigest(toRFQOrder(signedOrderResp.order), 1, rfqAddr)
const recoved = utils.verifyMessage(orderHash, utils.hexlify(sigBytes.slice(0, 65)))

const recoved = utils.verifyMessage(
utils.arrayify(orderHash),
utils.hexlify(sigBytes.slice(0, 65))
)
assert.equal(recoved.toLowerCase(), signer.address.toLowerCase())
// verify random values
assert.isTrue(signedOrderResp.order.salt.length > 0)
Expand Down

0 comments on commit faf4919

Please sign in to comment.