Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
change variable names for ZeroEx V2
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanChou committed Sep 13, 2018
1 parent 312283c commit d06a4c6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 73 deletions.
14 changes: 7 additions & 7 deletions test/helpers/BytesHelper.js
Expand Up @@ -44,15 +44,15 @@ function zeroExV1OrderToBytes(order) {

function zeroExV2OrderToBytes(order) {
const v = []
.concat(toBytes32(order.maker))
.concat(toBytes32(order.taker))
.concat(toBytes32(order.feeRecipient))
.concat(toBytes32(order.sender))
.concat(toBytes32(order.makerTokenAmount))
.concat(toBytes32(order.takerTokenAmount))
.concat(toBytes32(order.makerAddress))
.concat(toBytes32(order.takerAddress))
.concat(toBytes32(order.feeRecipientAddress))
.concat(toBytes32(order.senderAddress))
.concat(toBytes32(order.makerAssetAmount))
.concat(toBytes32(order.takerAssetAmount))
.concat(toBytes32(order.makerFee))
.concat(toBytes32(order.takerFee))
.concat(toBytes32(order.expirationUnixTimestampSec))
.concat(toBytes32(order.expirationTimeSeconds))
.concat(toBytes32(order.salt))
.concat(toBytes32(order.signature));
return web3Instance.utils.bytesToHex(v);
Expand Down
79 changes: 39 additions & 40 deletions test/helpers/ZeroExV2Helper.js
@@ -1,4 +1,3 @@

const Web3 = require('web3');
const BigNumber = require('bignumber.js');

Expand All @@ -18,30 +17,30 @@ async function createSignedV2SellOrder(
accounts,
{
salt = DEFAULT_SALT,
feeRecipient,
feeRecipientAddress,
} = {}
) {
let order = {
type: ORDER_TYPE.ZERO_EX_V2,
exchangeContractAddress: ZeroExExchangeV2.address,
exchangeAddress: ZeroExExchangeV2.address,

maker: accounts[5],
taker: ADDRESSES.ZERO,
feeRecipient: feeRecipient || accounts[6],
sender: ADDRESSES.ZERO,
makerAddress: accounts[5],
takerAddress: ADDRESSES.ZERO,
feeRecipientAddress: feeRecipientAddress || accounts[6],
senderAddress: ADDRESSES.ZERO,

makerFee: BASE_AMOUNT.times(0.010928345).floor(),
takerFee: BASE_AMOUNT.times(0.109128341).floor(),
expirationUnixTimestampSec: new BigNumber(100000000000000),
expirationTimeSeconds: new BigNumber(100000000000000),
salt: new BigNumber(salt),

// owedToken
makerTokenAddress: OwedToken.address,
makerTokenAmount: BASE_AMOUNT.times(6.382472).floor(),
makerAssetAmount: BASE_AMOUNT.times(6.382472).floor(),

// heldToken
takerTokenAddress: HeldToken.address,
takerTokenAmount: BASE_AMOUNT.times(19.123475).floor()
takerAssetAmount: BASE_AMOUNT.times(19.123475).floor()
};

order.signature = await signV2Order(order);
Expand All @@ -53,30 +52,30 @@ async function createSignedV2BuyOrder(
accounts,
{
salt = DEFAULT_SALT,
feeRecipient,
feeRecipientAddress,
} = {}
) {
let order = {
type: ORDER_TYPE.ZERO_EX_V2,
exchangeContractAddress: ZeroExExchangeV2.address,
exchangeAddress: ZeroExExchangeV2.address,

maker: accounts[2],
taker: ADDRESSES.ZERO,
feeRecipient: feeRecipient || accounts[4],
sender: ADDRESSES.ZERO,
makerAddress: accounts[2],
takerAddress: ADDRESSES.ZERO,
feeRecipientAddress: feeRecipientAddress || accounts[4],
senderAddress: ADDRESSES.ZERO,

makerFee: BASE_AMOUNT.times(.02012398).floor(),
takerFee: BASE_AMOUNT.times(.1019238).floor(),
expirationUnixTimestampSec: new BigNumber(100000000000000),
expirationTimeSeconds: new BigNumber(100000000000000),
salt: new BigNumber(salt),

// heldToken
makerTokenAddress: HeldToken.address,
makerTokenAmount: BASE_AMOUNT.times(30.091234687).floor(),
makerAssetAmount: BASE_AMOUNT.times(30.091234687).floor(),

// owedToken
takerTokenAddress: OwedToken.address,
takerTokenAmount: BASE_AMOUNT.times(10.092138781).floor(),
takerAssetAmount: BASE_AMOUNT.times(10.092138781).floor(),
};

order.signature = await signV2Order(order);
Expand All @@ -88,32 +87,32 @@ async function createSignedV2Order(
accounts,
{
salt = DEFAULT_SALT,
feeRecipient,
maker,
feeRecipientAddress,
makerAddress,
makerToken,
makerTokenAmount,
makerAssetAmount,
takerToken,
takerTokenAmount,
takerAssetAmount,
}
) {
const order = {
type: ORDER_TYPE.ZERO_EX_V2,
exchangeContractAddress: ZeroExExchangeV2.address,
exchangeAddress: ZeroExExchangeV2.address,

maker: maker || accounts[5],
taker: ADDRESSES.ZERO,
feeRecipient: feeRecipient || accounts[6],
sender: ADDRESSES.ZERO,
makerAddress: makerAddress || accounts[5],
takerAddress: ADDRESSES.ZERO,
feeRecipientAddress: feeRecipientAddress || accounts[6],
senderAddress: ADDRESSES.ZERO,

makerFee: BASE_AMOUNT.times(0.010928345).floor(),
takerFee: BASE_AMOUNT.times(0.109128341).floor(),
expirationUnixTimestampSec: new BigNumber(100000000000000),
expirationTimeSeconds: new BigNumber(100000000000000),
salt: new BigNumber(salt),

makerTokenAddress: makerToken,
makerTokenAmount: makerTokenAmount || BASE_AMOUNT.times(6.382472).floor(),
makerAssetAmount: makerAssetAmount || BASE_AMOUNT.times(6.382472).floor(),
takerTokenAddress: takerToken,
takerTokenAmount: takerTokenAmount || BASE_AMOUNT.times(19.123475).floor()
takerAssetAmount: takerAssetAmount || BASE_AMOUNT.times(19.123475).floor()
};

order.signature = await signV2Order(order);
Expand All @@ -123,7 +122,7 @@ async function createSignedV2Order(

async function signV2Order(order) {
const signature = await promisify(web3Instance.eth.sign)(
getV2OrderHash(order), order.maker
getV2OrderHash(order), order.makerAddress
);

const { v, r, s } = ethUtil.fromRpcSig(signature);
Expand Down Expand Up @@ -157,15 +156,15 @@ function getV2OrderHash(order) {

const basicHash = web3Instance.utils.soliditySha3(
{ t: 'bytes32', v: eip712Hash },
{ t: 'bytes32', v: addressToBytes32(order.maker) },
{ t: 'bytes32', v: addressToBytes32(order.taker) },
{ t: 'bytes32', v: addressToBytes32(order.feeRecipient) },
{ t: 'bytes32', v: addressToBytes32(order.sender) },
{ t: 'uint256', v: order.makerTokenAmount },
{ t: 'uint256', v: order.takerTokenAmount },
{ t: 'bytes32', v: addressToBytes32(order.makerAddress) },
{ t: 'bytes32', v: addressToBytes32(order.takerAddress) },
{ t: 'bytes32', v: addressToBytes32(order.feeRecipientAddress) },
{ t: 'bytes32', v: addressToBytes32(order.senderAddress) },
{ t: 'uint256', v: order.makerAssetAmount },
{ t: 'uint256', v: order.takerAssetAmount },
{ t: 'uint256', v: order.makerFee },
{ t: 'uint256', v: order.takerFee },
{ t: 'uint256', v: order.expirationUnixTimestampSec },
{ t: 'uint256', v: order.expirationTimeSeconds },
{ t: 'uint256', v: order.salt },
{ t: 'bytes32', v: web3Instance.utils.soliditySha3({ t: 'bytes', v: makerAssetData })},
{ t: 'bytes32', v: web3Instance.utils.soliditySha3({ t: 'bytes', v: takerAssetData })}
Expand All @@ -177,7 +176,7 @@ function getV2OrderHash(order) {
{ t: 'bytes32', v: eip712DomSepHash },
{ t: 'bytes32', v: web3Instance.utils.soliditySha3({ t: 'string', v: '0x Protocol' })},
{ t: 'bytes32', v: web3Instance.utils.soliditySha3({ t: 'string', v: '2' })},
{ t: 'bytes32', v: addressToBytes32(order.exchangeContractAddress) }
{ t: 'bytes32', v: addressToBytes32(order.exchangeAddress) }
);

const retVal = web3Instance.utils.soliditySha3(
Expand Down
Expand Up @@ -68,21 +68,21 @@ describe('ZeroExV2ExchangeWrapper', () => {
const order = await createSignedV2SellOrder(accounts);
const amount = new BigNumber(baseAmount.times(2));

const requiredTakerTokenAmount = await exchangeWrapper.getExchangeCost.call(
const requiredtakerAssetAmount = await exchangeWrapper.getExchangeCost.call(
order.makerTokenAddress,
order.takerTokenAddress,
amount,
zeroExV2OrderToBytes(order)
);

const expected = getPartialAmount(
order.takerTokenAmount,
order.makerTokenAmount,
order.takerAssetAmount,
order.makerAssetAmount,
amount,
true
);

expect(requiredTakerTokenAmount).to.be.bignumber.eq(expected);
expect(requiredtakerAssetAmount).to.be.bignumber.eq(expected);
});
});
});
Expand All @@ -98,7 +98,7 @@ describe('ZeroExV2ExchangeWrapper', () => {
} = await setup(accounts);

const order = await createSignedV2SellOrder(accounts);
order.feeRecipient = ADDRESSES.ZERO;
order.feeRecipientAddress = ADDRESSES.ZERO;
order.signature = await signV2Order(order);

// test for un-taken order
Expand All @@ -107,10 +107,10 @@ describe('ZeroExV2ExchangeWrapper', () => {
order.takerTokenAddress,
zeroExV2OrderToBytes(order)
);
expect(responseFull).to.be.bignumber.eq(order.makerTokenAmount);
expect(responseFull).to.be.bignumber.eq(order.makerAssetAmount);

// fill half of order
const amount = order.takerTokenAmount.div(2).floor();
const amount = order.takerAssetAmount.div(2).floor();
await grantTokens(order, exchangeWrapper, tradeOriginator, amount);
await exchangeWrapper.exchange(
tradeOriginator,
Expand All @@ -126,7 +126,7 @@ describe('ZeroExV2ExchangeWrapper', () => {
expect(filled).to.be.bignumber.eq(amount);

// test for partially-taken order
const expectedAmount = order.makerTokenAmount.div(2).floor()
const expectedAmount = order.makerAssetAmount.div(2).floor()
const responsePart = await exchangeWrapper.getMaxMakerAmount.call(
order.makerTokenAddress,
order.takerTokenAddress,
Expand All @@ -143,7 +143,7 @@ describe('ZeroExV2ExchangeWrapper', () => {
} = await setup(accounts);

const order = await createSignedV2SellOrder(accounts);
order.expirationUnixTimestampSec = new BigNumber(1);
order.expirationTimeSeconds = new BigNumber(1);
order.signature = await signV2Order(order);

// test for partially-taken order
Expand Down Expand Up @@ -363,7 +363,7 @@ describe('ZeroExV2ExchangeWrapper', () => {
} = await setup(accounts);

const order = await createSignedV2SellOrder(accounts);
order.makerTokenAmount = new BigNumber(0);
order.makerAssetAmount = new BigNumber(0);
order.signature = await signV2Order(order);

const amount = new BigNumber(baseAmount.times(2));
Expand All @@ -383,7 +383,7 @@ describe('ZeroExV2ExchangeWrapper', () => {
});

contract('ZeroExV1ExchangeWrapper', accounts => {
it('succeeds if no-fee is provided by anyone', async () => {
it('succeeds if zero taker fee for any msg.sender', async () => {
const {
exchangeWrapper,
tradeOriginator,
Expand Down Expand Up @@ -436,7 +436,7 @@ describe('ZeroExV2ExchangeWrapper', () => {

const order = await createSignedV2SellOrder(accounts);

const amount = new BigNumber(order.takerTokenAmount.plus(1));
const amount = new BigNumber(order.takerAssetAmount.plus(1));

await grantTokens(order, exchangeWrapper, tradeOriginator, amount);

Expand All @@ -463,7 +463,7 @@ describe('ZeroExV2ExchangeWrapper', () => {

const order = await createSignedV2SellOrder(accounts);

const amount = new BigNumber(order.takerTokenAmount.times(2).div(3).floor());
const amount = new BigNumber(order.takerAssetAmount.times(2).div(3).floor());

await grantTokens(order, exchangeWrapper, tradeOriginator, amount);

Expand Down Expand Up @@ -527,8 +527,8 @@ async function grantTokens(order, exchangeWrapper, tradeOriginator, amount) {
// Maker Token
issueAndSetAllowance(
makerToken,
order.maker,
order.makerTokenAmount,
order.makerAddress,
order.makerAssetAmount,
ZeroExProxyV2.address
),

Expand All @@ -538,7 +538,7 @@ async function grantTokens(order, exchangeWrapper, tradeOriginator, amount) {
// Maker Fee Token
issueAndSetAllowance(
feeToken,
order.maker,
order.makerAddress,
order.makerFee,
ZeroExProxyV2.address
),
Expand Down Expand Up @@ -575,15 +575,15 @@ async function getBalances(order, exchangeWrapper, tradeOriginator, dydxProxy) {

exchangeWrapperProxyAllowance
] = await Promise.all([
makerToken.balanceOf.call(order.maker),
takerToken.balanceOf.call(order.maker),
feeToken.balanceOf.call(order.maker),
makerToken.balanceOf.call(order.makerAddress),
takerToken.balanceOf.call(order.makerAddress),
feeToken.balanceOf.call(order.makerAddress),

makerToken.balanceOf.call(exchangeWrapper.address),
takerToken.balanceOf.call(exchangeWrapper.address),
feeToken.balanceOf.call(exchangeWrapper.address),

feeToken.balanceOf.call(order.feeRecipient),
feeToken.balanceOf.call(order.feeRecipientAddress),

feeToken.balanceOf.call(tradeOriginator),

Expand Down Expand Up @@ -633,17 +633,17 @@ async function validateBalances(

const tradedMakerToken = getPartialAmount(
amount,
order.takerTokenAmount,
order.makerTokenAmount
order.takerAssetAmount,
order.makerAssetAmount
);
const makerFee = order.feeRecipient === ADDRESSES.ZERO ? BIGNUMBERS.ZERO : getPartialAmount(
const makerFee = getPartialAmount(
amount,
order.takerTokenAmount,
order.takerAssetAmount,
order.makerFee
);
const takerFee = order.feeRecipient === ADDRESSES.ZERO ? BIGNUMBERS.ZERO : getPartialAmount(
const takerFee = getPartialAmount(
amount,
order.takerTokenAmount,
order.takerAssetAmount,
order.takerFee
);

Expand Down

0 comments on commit d06a4c6

Please sign in to comment.