Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Implement Limit Orders #276

Merged
merged 5 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address some comments
  • Loading branch information
Brendan Chou committed Jul 13, 2019
commit b82f5368ba3056c3ca3a878030c97a052e6b6fca
1 change: 1 addition & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
copyPackages: ['openzeppelin-solidity', '@dydxprotocol/exchange-wrappers'],
skipFiles: [
'testing/',
'external/proxies/PayableProxyForSoloMargin.sol',
'external/multisig/MultiSig.sol',
'external/multisig/DelayedMultiSig.sol',
'Migrations.sol'
Expand Down
83 changes: 83 additions & 0 deletions __tests__/Library.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import BigNumber from 'bignumber.js';
import { soliditySha3, hexToBytes } from 'web3-utils';
import { getSolo } from './helpers/Solo';
import { Solo } from '../src/Solo';
import { resetEVM, mineAvgBlock } from './helpers/EVM';
import { expectThrow } from '../src/lib/Expect';
import { ADDRESSES, INTEGERS } from '../src/lib/Constants';
import { stripHexPrefix } from '../src/lib/BytesHelper';
import {
SIGNATURE_TYPES,
PREPEND_DEC,
PREPEND_HEX,
createTypedSignature,
} from '../src/lib/SignatureHelper';
import { address } from '../src/types';

let solo: Solo;
Expand All @@ -21,6 +29,81 @@ describe('Library', () => {
await resetEVM();
});

describe('TypedSignature', () => {
const hash = '0x1234567812345678123456781234567812345678123456781234567812345678';
const r = '0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f946';
const s = '0x6d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be4';
const v = '0x1b';
const signature = `${r}${stripHexPrefix(s)}${stripHexPrefix(v)}`;

async function recover(hash: string, typedSignature: string) {
return solo.contracts.callConstantContractFunction(
solo.contracts.testLib.methods.TypedSignatureRecover(
hexToBytes(hash),
hexToBytes(typedSignature),
),
);
}

describe('recover', () => {
it('fails for invalid signature length', async () => {
await expectThrow(
recover(hash, hash.slice(0, -2)),
'TypedSignature: Invalid signature length',
);
});

it('fails for invalid signature type', async () => {
// type too small
await expectThrow(
recover(hash, `0x${'00'.repeat(65)}00`),
'TypedSignature: Invalid signature type',
);

// type to large
await expectThrow(
recover(hash, `0x${'00'.repeat(65)}04`),
'TypedSignature: Invalid signature type',
);
});

it('succeeds for no prepend', async () => {
const signer = solo.web3.eth.accounts.recover({ r, s, v, messageHash: hash });
const recoveredAddress = await recover(
hash,
createTypedSignature(signature, SIGNATURE_TYPES.NO_PREPEND),
);
expect(recoveredAddress).toEqual(signer);
});

it('succeeds for decimal prepend', async () => {
const decHash = soliditySha3(
{ t: 'string', v: PREPEND_DEC },
{ t: 'bytes32', v: hash },
);
const signer = solo.web3.eth.accounts.recover({ r, s, v, messageHash: decHash });
const recoveredAddress = await recover(
hash,
createTypedSignature(signature, SIGNATURE_TYPES.DECIMAL),
);
expect(recoveredAddress).toEqual(signer);
});

it('succeeds for hexadecimal prepend', async () => {
const hexHash = soliditySha3(
{ t: 'string', v: PREPEND_HEX },
{ t: 'bytes32', v: hash },
);
const signer = solo.web3.eth.accounts.recover({ r, s, v, messageHash: hexHash });
const recoveredAddress = await recover(
hash,
createTypedSignature(signature, SIGNATURE_TYPES.HEXADECIMAL),
);
expect(recoveredAddress).toEqual(signer);
});
});
});

describe('Math', () => {
const BN_DOWN = BigNumber.clone({ ROUNDING_MODE: 1 });
const BN_UP = BigNumber.clone({ ROUNDING_MODE: 0 });
Expand Down
2 changes: 1 addition & 1 deletion __tests__/actions/Buy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ async function expectBuyOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().buy(combinedGlob).commit(options);
return solo.operation.initiate().buy(combinedGlob).commit(options);
}

async function expectBuyRevert(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/actions/Call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async function expectCallOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().call(combinedGlob).commit(options);
return solo.operation.initiate().call(combinedGlob).commit(options);
}

async function expectCallRevert(
Expand Down
4 changes: 2 additions & 2 deletions __tests__/actions/Deposit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ async function setAccountBalance(amount: BigNumber) {
}

async function issueTokensToUser(amount: BigNumber) {
return await solo.testing.tokenA.issueTo(amount, who);
return solo.testing.tokenA.issueTo(amount, who);
}

async function expectBalances(
Expand Down Expand Up @@ -590,7 +590,7 @@ async function expectDepositOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().deposit(combinedGlob).commit(options);
return solo.operation.initiate().deposit(combinedGlob).commit(options);
}

async function expectDepositRevert(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/actions/Sell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ async function expectSellOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().sell(combinedGlob).commit(options);
return solo.operation.initiate().sell(combinedGlob).commit(options);
}

async function expectSellRevert(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/actions/Trade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ async function expectTradeOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().trade(combinedGlob).commit(options);
return solo.operation.initiate().trade(combinedGlob).commit(options);
}

async function expectTradeRevert(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/actions/Transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ async function expectTransferOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().transfer(combinedGlob).commit(options);
return solo.operation.initiate().transfer(combinedGlob).commit(options);
}

async function expectTransferRevert(
Expand Down
6 changes: 3 additions & 3 deletions __tests__/actions/Vaporize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,19 +491,19 @@ describe('Vaporize', () => {
// ============ Helper Functions ============

async function issueHeldTokensToSolo(amount: BigNumber) {
return await solo.testing.tokenB.issueTo(amount, solo.contracts.soloMargin.options.address);
return solo.testing.tokenB.issueTo(amount, solo.contracts.soloMargin.options.address);
}

async function issueOwedTokensToSolo(amount: BigNumber) {
return await solo.testing.tokenA.issueTo(amount, solo.contracts.soloMargin.options.address);
return solo.testing.tokenA.issueTo(amount, solo.contracts.soloMargin.options.address);
}

async function expectVaporizeOkay(
glob: Object,
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().vaporize(combinedGlob).commit(options);
return solo.operation.initiate().vaporize(combinedGlob).commit(options);
}

async function expectVaporizeRevert(
Expand Down
4 changes: 2 additions & 2 deletions __tests__/actions/Withdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ async function setAccountBalance(amount: BigNumber) {
}

async function issueTokensToSolo(amount: BigNumber) {
return await solo.testing.tokenA.issueTo(amount, solo.contracts.soloMargin.options.address);
return solo.testing.tokenA.issueTo(amount, solo.contracts.soloMargin.options.address);
}

async function expectBalances(
Expand Down Expand Up @@ -607,7 +607,7 @@ async function expectWithdrawOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().withdraw(combinedGlob).commit(options);
return solo.operation.initiate().withdraw(combinedGlob).commit(options);
}

async function expectWithdrawRevert(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/traders/Expiry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ async function expectExpireOkay(
options?: Object,
) {
const combinedGlob = { ...defaultGlob, ...glob };
return await solo.operation.initiate().trade(combinedGlob).commit(options);
return solo.operation.initiate().trade(combinedGlob).commit(options);
}

async function expectExpireRevert(
Expand Down
Loading