diff --git a/src/context.ts b/src/context.ts index b85990f..e6d5e59 100644 --- a/src/context.ts +++ b/src/context.ts @@ -58,11 +58,9 @@ 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")); - }*/ - - console.log("tradeDelegateAddress: " + tradeDelegateAddress); + } this.ERC20Contract = new web3.eth.Contract(JSON.parse(erc20Abi)); this.TradeDelegateContract = new web3.eth.Contract(JSON.parse(tradeDelegateAbi), tradeDelegateAddress); @@ -74,47 +72,13 @@ export class Context { 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.tradeDelegate = this.TradeHistoryContract.clone(); + this.tradeDelegate = this.TradeDelegateContract.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/order.ts b/src/order.ts index be0cb13..48e15d0 100644 --- a/src/order.ts +++ b/src/order.ts @@ -22,10 +22,10 @@ export class OrderUtil { if (!order.broker) { order.broker = order.owner; } else { - const returnValue = await this.context.orderBrokerRegistry.getBroker( + const returnValue = await this.context.orderBrokerRegistry.methods.getBroker( order.owner, order.broker, - ); + ).call(); order.valid = order.valid && ensure(returnValue.registered, "order broker is not registered"); // order.brokerInterceptor = // (brokerInterceptor === "0x0000000000000000000000000000000000000000") ? @@ -90,9 +90,9 @@ export class OrderUtil { signatureValid = true; } else if (!order.sig) { const orderHashHex = "0x" + order.hash.toString("hex"); - const isRegistered = await this.context.orderRegistry.isOrderHashRegistered(order.broker, - orderHashHex); - const isOnchainOrder = await this.context.orderBook.orderSubmitted(orderHashHex); + const isRegistered = await this.context.orderRegistry.methods.isOrderHashRegistered(order.broker, + orderHashHex).call(); + const isOnchainOrder = await this.context.orderBook.methods.orderSubmitted(orderHashHex).call(); signatureValid = isRegistered || isOnchainOrder; } else { const multiHashUtil = new MultiHashUtil(); @@ -291,9 +291,10 @@ export class OrderUtil { public async getERC20Spendable(spender: string, tokenAddress: string, owner: string) { - const token = await this.context.ERC20Contract.at(tokenAddress); - const balance = await token.balanceOf(owner); - const allowance = await token.allowance(owner, spender); + const token = this.context.ERC20Contract; + token.options.address = tokenAddress; + const balance = await token.methods.balanceOf(owner).call(); + const allowance = await token.methods.allowance(owner, spender).call(); const spendable = BigNumber.min(balance, allowance); return new BigNumber(spendable.toString()); } @@ -320,7 +321,7 @@ export class OrderUtil { tokenSpendable: Spendable, brokerSpendable: Spendable) { if (!tokenSpendable.initialized) { - tokenSpendable.amount = await this.getERC20Spendable(this.context.tradeDelegate.address, + tokenSpendable.amount = await this.getERC20Spendable(this.context.tradeDelegate.options.address, token, owner); tokenSpendable.initialized = true; diff --git a/src/protocol_simulator.ts b/src/protocol_simulator.ts index 102a35f..b0812f5 100644 --- a/src/protocol_simulator.ts +++ b/src/protocol_simulator.ts @@ -201,12 +201,18 @@ export class ProtocolSimulator { bitstream.addNumber(0, 12); } - const fills = await this.context.tradeHistory.batchGetFilledAndCheckCancelled(bitstream.getBytes32Array()); + const fills = await this.context.tradeHistory.methods.batchGetFilledAndCheckCancelled( + bitstream.getBytes32Array(), + ).call(); const cancelledValue = new BN("F".repeat(64), 16); for (const [i, order] of orders.entries()) { - order.filledAmountS = new BigNumber(fills[i].toString()); - order.valid = order.valid && ensure(!fills[i].eq(cancelledValue), "order is cancelled"); + const fillBN = new BN(new BigNumber(fills[i].toString(16)).toString(16), 16); + order.filledAmountS = new BigNumber(fillBN.toString()); + order.valid = order.valid && ensure( + !fillBN.eq(cancelledValue), + "order is cancelled", + ); } } @@ -276,19 +282,19 @@ export class ProtocolSimulator { } if (!balancesBefore[order.tokenS][order.owner]) { balancesBefore[order.tokenS][order.owner] = - await this.orderUtil.getERC20Spendable(this.context.tradeDelegate.address, + await this.orderUtil.getERC20Spendable(this.context.tradeDelegate.options.address, order.tokenS, order.owner); } if (!balancesBefore[order.tokenB][order.tokenRecipient]) { balancesBefore[order.tokenB][order.tokenRecipient] = - await this.orderUtil.getERC20Spendable(this.context.tradeDelegate.address, + await this.orderUtil.getERC20Spendable(this.context.tradeDelegate.options.address, order.tokenB, order.tokenRecipient); } if (!balancesBefore[order.feeToken][order.owner]) { balancesBefore[order.feeToken][order.owner] = - await this.orderUtil.getERC20Spendable(this.context.tradeDelegate.address, + await this.orderUtil.getERC20Spendable(this.context.tradeDelegate.options.address, order.feeToken, order.owner); } @@ -296,11 +302,12 @@ export class ProtocolSimulator { for (const order of orders) { const tokens = [order.tokenS, order.tokenB, order.feeToken]; for (const token of tokens) { - const Token = await this.context.ERC20Contract.at(order.tokenS); + const Token = this.context.ERC20Contract; + Token.options.address = order.tokenS; // feeRecipient if (!balancesBefore[token][mining.feeRecipient]) { balancesBefore[token][mining.feeRecipient] = - new BigNumber((await Token.balanceOf(mining.feeRecipient)).toString()); + new BigNumber((await Token.methods.balanceOf(mining.feeRecipient).call()).toString()); } } } @@ -344,26 +351,27 @@ export class ProtocolSimulator { if (!feeBalancesBefore[token]) { feeBalancesBefore[token] = {}; } + const feeHolderMethods = this.context.feeHolder.methods; // Owner if (!feeBalancesBefore[token][order.owner]) { feeBalancesBefore[token][order.owner] = - new BigNumber((await this.context.feeHolder.feeBalances(token, order.owner)).toString()); + new BigNumber((await feeHolderMethods.feeBalances(token, order.owner).call()).toString()); } // Wallet if (order.walletAddr && !feeBalancesBefore[token][order.walletAddr]) { feeBalancesBefore[token][order.walletAddr] = - new BigNumber((await this.context.feeHolder.feeBalances(token, order.walletAddr)).toString()); + new BigNumber((await feeHolderMethods.feeBalances(token, order.walletAddr).call()).toString()); } // FeeRecipient if (!feeBalancesBefore[token][mining.feeRecipient]) { feeBalancesBefore[token][mining.feeRecipient] = - new BigNumber((await this.context.feeHolder.feeBalances(token, mining.feeRecipient)).toString()); + new BigNumber((await feeHolderMethods.feeBalances(token, mining.feeRecipient).call()).toString()); } // Burned - const feeHolder = this.context.feeHolder.address; + const feeHolder = this.context.feeHolder.options.address; if (!feeBalancesBefore[token][feeHolder]) { feeBalancesBefore[token][feeHolder] = - new BigNumber((await this.context.feeHolder.feeBalances(token, feeHolder)).toString()); + new BigNumber((await feeHolderMethods.feeBalances(token, feeHolder).call()).toString()); } } } @@ -386,7 +394,7 @@ export class ProtocolSimulator { for (const order of orders) { const orderHash = order.hash.toString("hex"); filledAmountsBefore[orderHash] = - new BigNumber((await this.context.tradeHistory.filled("0x" + orderHash)).toString()); + new BigNumber((await this.context.tradeHistory.methods.filled("0x" + orderHash).call()).toString()); } // Filled amounts after diff --git a/src/protocol_validator.ts b/src/protocol_validator.ts index c4e8a6a..e209aaa 100644 --- a/src/protocol_validator.ts +++ b/src/protocol_validator.ts @@ -393,7 +393,7 @@ export class ProtocolValidator { } // Pay the burn rate with the feeHolder as owner - const burnAddress = this.context.feeHolder.address; + const burnAddress = this.context.feeHolder.options.address; const walletFee = amount.times(walletSplitPercentage).dividedToIntegerBy(100); let minerFee = amount.minus(walletFee); @@ -409,7 +409,7 @@ export class ProtocolValidator { } // Calculate burn rates and rebates - const burnRateToken = (await this.context.burnRateTable.getBurnRate(token)).toNumber(); + const burnRateToken = await this.context.burnRateTable.methods.getBurnRate(token).call(); const burnRate = orderExpectation.P2P ? (burnRateToken >> 16) : (burnRateToken & 0xFFFF); const rebateRate = 0; // Miner fee diff --git a/src/ring.ts b/src/ring.ts index c368629..5007849 100644 --- a/src/ring.ts +++ b/src/ring.ts @@ -344,7 +344,7 @@ export class Ring { const prevIndex = (i + ringSize - 1) % ringSize; const p = this.participations[i]; const prevP = this.participations[prevIndex]; - const feeHolder = this.context.feeHolder.address; + const feeHolder = this.context.feeHolder.options.address; const buyerFeeAmountAfterRebateB = prevP.feeAmountB.minus(prevP.rebateB); assert(buyerFeeAmountAfterRebateB.gte(0), "buyerFeeAmountAfterRebateB >= 0"); @@ -439,7 +439,7 @@ export class Ring { } // Pay the burn rate with the feeHolder as owner - const burnAddress = this.context.feeHolder.address; + const burnAddress = this.context.feeHolder.options.address; // BEGIN diagnostics let feeDesc = "Fee"; @@ -482,7 +482,7 @@ export class Ring { } // Calculate burn rates and rebates - const burnRateToken = (await this.context.burnRateTable.getBurnRate(token)).toNumber(); + const burnRateToken = await this.context.burnRateTable.methods.getBurnRate(token).call(); const burnRate = p.order.P2P ? (burnRateToken >> 16) : (burnRateToken & 0xFFFF); const rebateRate = 0; // Miner fee @@ -708,7 +708,7 @@ export class Ring { minerFee = new BigNumber(0); } // Calculate burn rates and rebates - const burnRateToken = (await this.context.burnRateTable.getBurnRate(token)).toNumber(); + const burnRateToken = await this.context.burnRateTable.methods.getBurnRate(token).call(); const burnRate = p.order.P2P ? (burnRateToken >> 16) : (burnRateToken & 0xFFFF); const rebateRate = 0; // Miner fee @@ -850,7 +850,7 @@ export class Ring { } // Check fee holder balances of all possible tokens used to pay fees for (const token of [...Object.keys(expectedFeeHolderBalances), ...Object.keys(balances)]) { - const feeAddress = this.context.feeHolder.address; + const feeAddress = this.context.feeHolder.options.address; const expectedBalance = expectedFeeHolderBalances[token] ? expectedFeeHolderBalances[token] : new BigNumber(0); const balance = @@ -862,7 +862,7 @@ export class Ring { // Ensure total fee payments match transferred amounts to feeHolder contract { for (const token of [...Object.keys(this.feeBalances), ...Object.keys(balances)]) { - const feeAddress = this.context.feeHolder.address; + const feeAddress = this.context.feeHolder.options.address; let totalFee = new BigNumber(0); if (this.feeBalances[token]) { for (const owner of Object.keys(this.feeBalances[token])) { @@ -880,7 +880,7 @@ export class Ring { // Ensure total burn payments match expected total burned for (const token of [...Object.keys(expectedTotalBurned), ...Object.keys(this.feeBalances)]) { - const feeAddress = this.context.feeHolder.address; + const feeAddress = this.context.feeHolder.options.address; let burned = new BigNumber(0); let expected = new BigNumber(0); if (this.feeBalances[token] && this.feeBalances[token][feeAddress]) {