Skip to content

Commit

Permalink
Restored using contracts by ABI interface with web3 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Brechtpd committed Dec 9, 2018
1 parent e906b06 commit ce46514
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 71 deletions.
42 changes: 3 additions & 39 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;*/
}

}
19 changes: 10 additions & 9 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") ?
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Expand All @@ -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;
Expand Down
36 changes: 22 additions & 14 deletions src/protocol_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}
}

Expand Down Expand Up @@ -276,31 +282,32 @@ 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);
}
}
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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/protocol_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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])) {
Expand All @@ -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]) {
Expand Down

0 comments on commit ce46514

Please sign in to comment.