Skip to content

Commit

Permalink
WIP: Truffle update with solc 0.5.0 and web3 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Brechtpd committed Dec 5, 2018
1 parent bf7c270 commit caee1dc
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 8 additions & 4 deletions src/bitstream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber } from "bignumber.js";
import BN = require("bn.js");
import abi = require("ethereumjs-abi");

export class Bitstream {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
62 changes: 49 additions & 13 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;*/
}

}
2 changes: 1 addition & 1 deletion src/exchange_deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/expectThrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export async function expectThrow(promise: Promise<any>, 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");
Expand Down
6 changes: 3 additions & 3 deletions src/multihash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 12 additions & 10 deletions src/protocol_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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
Expand Down

0 comments on commit caee1dc

Please sign in to comment.