Skip to content

Commit

Permalink
Merge pull request #7 from FinBook/nonceFix
Browse files Browse the repository at this point in the history
Nonce fix
  • Loading branch information
yizhoucao committed Jan 23, 2019
2 parents 220bfcb + 5b4a9db commit da0e7af
Show file tree
Hide file tree
Showing 11 changed files with 573 additions and 137 deletions.
100 changes: 100 additions & 0 deletions src/DualClassWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '@babel/polyfill';

import * as CST from './constants';
import DualClassWrapper from './DualClassWrapper';
import { ITransactionOption } from './types';

test('convertCustodianState', () => {
expect(DualClassWrapper.convertCustodianState(CST.STATE_INCEPTION)).toBe(CST.CTD_INCEPTION);
Expand Down Expand Up @@ -516,6 +517,15 @@ const web3Wrapper = {
wrongEnvReject: jest.fn(() => Promise.reject('wrong env')),
getGasPrice: jest.fn(() => Promise.resolve(1000000000)),
getTransactionCount: jest.fn(() => Promise.resolve(2)),
getTransactionOption: jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
),
createContract: jest.fn(() => ({
methods: {
getStates: jest.fn(() => ({
Expand Down Expand Up @@ -619,6 +629,15 @@ test('getUserAddress', async () => {
});

test('collectFee', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
try {
await dualClassWrapper.collectFee('account', 10);
} catch (err) {
Expand All @@ -636,9 +655,19 @@ test('collectFee', async () => {
(dualClassWrapper.contract.methods.collectFee as jest.Mock).mock.calls
).toMatchSnapshot();
expect(collectFeeSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('setValue', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isReadOnly = jest.fn(() => true);
try {
await dualClassWrapper.setValue('account', 1, 10);
Expand All @@ -654,9 +683,19 @@ test('setValue', async () => {
});
expect((dualClassWrapper.contract.methods.setValue as jest.Mock).mock.calls).toMatchSnapshot();
expect(setValueSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('create', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isReadOnly = jest.fn(() => true);
try {
await dualClassWrapper.create('account', 1, '');
Expand Down Expand Up @@ -686,9 +725,19 @@ test('create', async () => {
).toMatchSnapshot();
expect(createSend.mock.calls).toMatchSnapshot();
expect(createWithWETHSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('redeem', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isReadOnly = jest.fn(() => true);
try {
await dualClassWrapper.redeem('account', 1, 1);
Expand All @@ -707,9 +756,19 @@ test('redeem', async () => {

expect((dualClassWrapper.contract.methods.redeem as jest.Mock).mock.calls).toMatchSnapshot();
expect(redeemSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('redeemAll', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isReadOnly = jest.fn(() => true);
try {
await dualClassWrapper.redeemAll('account');
Expand All @@ -727,9 +786,19 @@ test('redeemAll', async () => {
).toMatchSnapshot();
expect(dualClassWrapper.contract.methods.redeemAll as jest.Mock).toBeCalledTimes(2);
expect(redeemAllSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('startCustodian', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
try {
await dualClassWrapper.startCustodian('account', 'aAddr', 'bAddr', 'oracleAddr');
} catch (err) {
Expand All @@ -746,9 +815,19 @@ test('startCustodian', async () => {
(dualClassWrapper.contract.methods.startCustodian as jest.Mock).mock.calls
).toMatchSnapshot();
expect(startCustodianSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('fetchPrice', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isLocal = jest.fn(() => false);
try {
await dualClassWrapper.fetchPrice('account');
Expand All @@ -764,9 +843,19 @@ test('fetchPrice', async () => {
});
expect(dualClassWrapper.contract.methods.fetchPrice as jest.Mock).toBeCalledTimes(2);
expect(fetchPriceSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('triggerPreReset', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isLocal = jest.fn(() => false);
try {
await dualClassWrapper.triggerPreReset('account');
Expand All @@ -782,9 +871,19 @@ test('triggerPreReset', async () => {
});
expect(dualClassWrapper.contract.methods.startPreReset as jest.Mock).toBeCalledTimes(2);
expect(startPreResetSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

test('triggerReset', async () => {
web3Wrapper.getTransactionOption = jest.fn(
(account: string, gasLimit: number, option: ITransactionOption = {}) =>
Promise.resolve({
from: account,
gasPrice: option.gasPrice || 1000000000,
gas: option.gasLimit || gasLimit,
nonce: option.nonce || 10
})
);
web3Wrapper.isLocal = jest.fn(() => false);
try {
await dualClassWrapper.triggerReset('account');
Expand All @@ -800,4 +899,5 @@ test('triggerReset', async () => {
});
expect(dualClassWrapper.contract.methods.startReset as jest.Mock).toBeCalledTimes(2);
expect(startResetSend.mock.calls).toMatchSnapshot();
expect((web3Wrapper.getTransactionOption as jest.Mock).mock.calls).toMatchSnapshot();
});

0 comments on commit da0e7af

Please sign in to comment.