diff --git a/package.json b/package.json index 3c5666e..8766eee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "protocol2-js", - "version": "0.2.21", + "version": "0.2.22", "description": "loopring protocol simulator core lib", "main": "index.ts", "repository": { diff --git a/src/bitstream.ts b/src/bitstream.ts index 1170726..227a2fa 100644 --- a/src/bitstream.ts +++ b/src/bitstream.ts @@ -1,4 +1,5 @@ import { BigNumber } from "bignumber.js"; +import BN = require("bn.js"); import abi = require("ethereumjs-abi"); export class Bitstream { @@ -29,7 +30,12 @@ export class Bitstream { } public addBigNumber(x: BigNumber, numBytes = 32, forceAppend = true) { - const formattedData = this.padString(web3.toHex(x).slice(2), numBytes * 2); + const formattedData = this.padString(x.toString(16), numBytes * 2); + return this.insert(formattedData, forceAppend); + } + + public addBN(x: BN, numBytes = 32, forceAppend = true) { + const formattedData = this.padString(x.toString(16), numBytes * 2); return this.insert(formattedData, forceAppend); } @@ -58,9 +64,7 @@ export class Bitstream { } public addRawBytes(bs: string, forceAppend = true) { - const bsHex = web3.toHex(bs); - // logDebug("bsHex:", bsHex); - return this.insert(bsHex.slice(2), forceAppend); + return this.insert(bs.slice(2), forceAppend); } public extractUint8(offset: number) { diff --git a/src/context.ts b/src/context.ts index 07b8707..b85990f 100644 --- a/src/context.ts +++ b/src/context.ts @@ -58,27 +58,63 @@ export class Context { const orderBookAbi = fs.readFileSync(ABIPath + "IOrderBook.abi", "ascii"); const burnRateTableAbi = fs.readFileSync(ABIPath + "IBurnRateTable.abi", "ascii"); - if (!web3) { + /*if (!web3) { web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); - } + }*/ - this.ERC20Contract = web3.eth.contract(JSON.parse(erc20Abi)); - this.TradeDelegateContract = web3.eth.contract(JSON.parse(tradeDelegateAbi)); - this.TradeHistoryContract = web3.eth.contract(JSON.parse(tradeHistoryAbi)); - this.BrokerRegistryContract = web3.eth.contract(JSON.parse(brokerRegistryAbi)); - this.OrderRegistryContract = web3.eth.contract(JSON.parse(orderRegistryAbi)); - this.FeeHolderContract = web3.eth.contract(JSON.parse(feeHolderAbi)); - this.OrderBookContract = web3.eth.contract(JSON.parse(orderBookAbi)); - this.BrokerInterceptorContract = web3.eth.contract(JSON.parse(brokerInterceptorAbi)); - this.BurnRateTableContract = web3.eth.contract(JSON.parse(burnRateTableAbi)); + console.log("tradeDelegateAddress: " + tradeDelegateAddress); - this.tradeDelegate = this.TradeHistoryContract.at(tradeDelegateAddress); + this.ERC20Contract = new web3.eth.Contract(JSON.parse(erc20Abi)); + this.TradeDelegateContract = new web3.eth.Contract(JSON.parse(tradeDelegateAbi), tradeDelegateAddress); + this.TradeHistoryContract = new web3.eth.Contract(JSON.parse(tradeHistoryAbi), tradeHistoryAddress); + this.BrokerRegistryContract = new web3.eth.Contract(JSON.parse(brokerRegistryAbi), orderBrokerRegistryAddress); + this.OrderRegistryContract = new web3.eth.Contract(JSON.parse(orderRegistryAbi), orderRegistryAddress); + this.FeeHolderContract = new web3.eth.Contract(JSON.parse(feeHolderAbi), feeHolderAddress); + this.OrderBookContract = new web3.eth.Contract(JSON.parse(orderBookAbi), orderBookAddress); + this.BrokerInterceptorContract = new web3.eth.Contract(JSON.parse(brokerInterceptorAbi)); + this.BurnRateTableContract = new web3.eth.Contract(JSON.parse(burnRateTableAbi), burnRateTableAddress); + + /*this.tradeDelegate = new web3.eth.Contract(JSON.parse(tradeDelegateAbi), tradeDelegateAddress); + this.tradeHistory = this.TradeHistoryContract; + this.orderBrokerRegistry = this.BrokerRegistryContract; + this.orderRegistry = this.OrderRegistryContract; + this.feeHolder = this.FeeHolderContract; + this.orderBook = this.OrderBookContract; + this.burnRateTable = this.BurnRateTableContract;*/ + + /*this.ERC20Contract = new web3.eth.Contract(JSON.parse(erc20Abi)); + this.TradeDelegateContract = new web3.eth.Contract(JSON.parse(tradeDelegateAbi)); + this.TradeHistoryContract = new web3.eth.Contract(JSON.parse(tradeHistoryAbi)); + this.BrokerRegistryContract = new web3.eth.Contract(JSON.parse(brokerRegistryAbi)); + this.OrderRegistryContract = new web3.eth.Contract(JSON.parse(orderRegistryAbi)); + this.FeeHolderContract = new web3.eth.Contract(JSON.parse(feeHolderAbi)); + this.OrderBookContract = new web3.eth.Contract(JSON.parse(orderBookAbi)); + this.BrokerInterceptorContract = new web3.eth.Contract(JSON.parse(brokerInterceptorAbi)); + this.BurnRateTableContract = new web3.eth.Contract(JSON.parse(burnRateTableAbi));*/ + + /*this.tradeDelegate = this.TradeHistoryContract.at(tradeDelegateAddress); this.tradeHistory = this.TradeHistoryContract.at(tradeHistoryAddress); this.orderBrokerRegistry = this.BrokerRegistryContract.at(orderBrokerRegistryAddress); this.orderRegistry = this.OrderRegistryContract.at(orderRegistryAddress); this.feeHolder = this.FeeHolderContract.at(feeHolderAddress); this.orderBook = this.OrderBookContract.at(orderBookAddress); - this.burnRateTable = this.BurnRateTableContract.at(burnRateTableAddress); + this.burnRateTable = this.BurnRateTableContract.at(burnRateTableAddress);*/ + + /*this.tradeDelegate = this.TradeHistoryContract.clone(); + this.tradeHistory = this.TradeHistoryContract.clone(); + this.orderBrokerRegistry = this.BrokerRegistryContract.clone(); + this.orderRegistry = this.OrderRegistryContract.clone(); + this.feeHolder = this.FeeHolderContract.clone(); + this.orderBook = this.OrderBookContract.clone(); + this.burnRateTable = this.BurnRateTableContract.clone(); + + this.tradeDelegate.options.address = tradeDelegateAddress; + this.tradeHistory.options.address = tradeHistoryAddress; + this.orderBrokerRegistry.options.address = orderBrokerRegistryAddress; + this.orderRegistry.options.address = orderRegistryAddress; + this.feeHolder.options.address = feeHolderAddress; + this.orderBook.options.address = orderBookAddress; + this.burnRateTable.options.address = burnRateTableAddress;*/ } } diff --git a/src/exchange_deserializer.ts b/src/exchange_deserializer.ts index 7ac5556..5978d7e 100644 --- a/src/exchange_deserializer.ts +++ b/src/exchange_deserializer.ts @@ -163,7 +163,7 @@ export class ExchangeDeserializer { private nextAddress() { const offset = this.getNextOffset() * 4; if (offset !== 0) { - return this.data.extractAddress(this.dataOffset + offset); + return web3.utils.toChecksumAddress(this.data.extractAddress(this.dataOffset + offset)); } else { return undefined; } diff --git a/src/expectThrow.ts b/src/expectThrow.ts index a2b3c39..2e5d9ea 100644 --- a/src/expectThrow.ts +++ b/src/expectThrow.ts @@ -2,7 +2,6 @@ export async function expectThrow(promise: Promise, expectedRevertMessage?: try { await promise; } catch (error) { - if (expectedRevertMessage) { const message = error.message.search(expectedRevertMessage) >= 0; assert(message, "Expected throw with message " + expectedRevertMessage + ", got '" + error + "' instead"); diff --git a/src/multihash.ts b/src/multihash.ts index bf40712..5d994a0 100644 --- a/src/multihash.ts +++ b/src/multihash.ts @@ -68,7 +68,7 @@ export class MultiHashUtil { const msgHash = ethUtil.hashPersonalMessage(hash); const pub = ethUtil.ecrecover(msgHash, v, r, s); const recoveredAddress = "0x" + ethUtil.pubToAddress(pub).toString("hex"); - return signer === recoveredAddress; + return signer.toLowerCase() === recoveredAddress.toLowerCase(); } catch { return false; } @@ -83,7 +83,7 @@ export class MultiHashUtil { try { const pub = ethUtil.ecrecover(hash, v, r, s); const recoveredAddress = "0x" + ethUtil.pubToAddress(pub).toString("hex"); - return signer === recoveredAddress; + return signer.toLowerCase() === recoveredAddress.toLowerCase(); } catch { return false; } @@ -93,7 +93,7 @@ export class MultiHashUtil { } private async signEthereumAsync(sig: Bitstream, hash: Buffer, address: string) { - const signature = await promisify(this.web3Instance.eth.sign)(address, ethUtil.bufferToHex(hash)); + const signature = await promisify(this.web3Instance.eth.sign)(ethUtil.bufferToHex(hash), address); const { v, r, s } = ethUtil.fromRpcSig(signature); sig.addNumber(1 + 32 + 32, 1); diff --git a/src/order.ts b/src/order.ts index db2ee25..be0cb13 100644 --- a/src/order.ts +++ b/src/order.ts @@ -22,13 +22,14 @@ export class OrderUtil { if (!order.broker) { order.broker = order.owner; } else { - const [registered, brokerInterceptor] = await this.context.orderBrokerRegistry.getBroker( + const returnValue = await this.context.orderBrokerRegistry.getBroker( order.owner, order.broker, ); - order.valid = order.valid && ensure(registered, "order broker is not registered"); + order.valid = order.valid && ensure(returnValue.registered, "order broker is not registered"); // order.brokerInterceptor = - // (brokerInterceptor === "0x0000000000000000000000000000000000000000") ? undefined : brokerInterceptor; + // (brokerInterceptor === "0x0000000000000000000000000000000000000000") ? + // undefined : returnValue.brokerInterceptor; } } @@ -290,11 +291,11 @@ export class OrderUtil { public async getERC20Spendable(spender: string, tokenAddress: string, owner: string) { - const token = this.context.ERC20Contract.at(tokenAddress); + const token = await this.context.ERC20Contract.at(tokenAddress); const balance = await token.balanceOf(owner); const allowance = await token.allowance(owner, spender); const spendable = BigNumber.min(balance, allowance); - return spendable; + return new BigNumber(spendable.toString()); } public async getBrokerAllowance(tokenAddr: string, diff --git a/src/protocol_simulator.ts b/src/protocol_simulator.ts index cb11003..102a35f 100644 --- a/src/protocol_simulator.ts +++ b/src/protocol_simulator.ts @@ -203,10 +203,10 @@ export class ProtocolSimulator { const fills = await this.context.tradeHistory.batchGetFilledAndCheckCancelled(bitstream.getBytes32Array()); - const cancelledValue = new BigNumber("F".repeat(64), 16); + const cancelledValue = new BN("F".repeat(64), 16); for (const [i, order] of orders.entries()) { - order.filledAmountS = fills[i]; - order.valid = order.valid && ensure(!fills[i].equals(cancelledValue), "order is cancelled"); + order.filledAmountS = new BigNumber(fills[i].toString()); + order.valid = order.valid && ensure(!fills[i].eq(cancelledValue), "order is cancelled"); } } @@ -296,10 +296,11 @@ export class ProtocolSimulator { for (const order of orders) { const tokens = [order.tokenS, order.tokenB, order.feeToken]; for (const token of tokens) { - const Token = this.context.ERC20Contract.at(order.tokenS); + const Token = await this.context.ERC20Contract.at(order.tokenS); // feeRecipient if (!balancesBefore[token][mining.feeRecipient]) { - balancesBefore[token][mining.feeRecipient] = await Token.balanceOf(mining.feeRecipient); + balancesBefore[token][mining.feeRecipient] = + new BigNumber((await Token.balanceOf(mining.feeRecipient)).toString()); } } } @@ -346,23 +347,23 @@ export class ProtocolSimulator { // Owner if (!feeBalancesBefore[token][order.owner]) { feeBalancesBefore[token][order.owner] = - await this.context.feeHolder.feeBalances(token, order.owner); + new BigNumber((await this.context.feeHolder.feeBalances(token, order.owner)).toString()); } // Wallet if (order.walletAddr && !feeBalancesBefore[token][order.walletAddr]) { feeBalancesBefore[token][order.walletAddr] = - await this.context.feeHolder.feeBalances(token, order.walletAddr); + new BigNumber((await this.context.feeHolder.feeBalances(token, order.walletAddr)).toString()); } // FeeRecipient if (!feeBalancesBefore[token][mining.feeRecipient]) { feeBalancesBefore[token][mining.feeRecipient] = - await this.context.feeHolder.feeBalances(token, mining.feeRecipient); + new BigNumber((await this.context.feeHolder.feeBalances(token, mining.feeRecipient)).toString()); } // Burned const feeHolder = this.context.feeHolder.address; if (!feeBalancesBefore[token][feeHolder]) { feeBalancesBefore[token][feeHolder] = - await this.context.feeHolder.feeBalances(token, feeHolder); + new BigNumber((await this.context.feeHolder.feeBalances(token, feeHolder)).toString()); } } } @@ -384,7 +385,8 @@ export class ProtocolSimulator { const filledAmountsBefore: { [hash: string]: BigNumber; } = {}; for (const order of orders) { const orderHash = order.hash.toString("hex"); - filledAmountsBefore[orderHash] = await this.context.tradeHistory.filled("0x" + orderHash); + filledAmountsBefore[orderHash] = + new BigNumber((await this.context.tradeHistory.filled("0x" + orderHash)).toString()); } // Filled amounts after