Skip to content

Commit

Permalink
Merge branch 'master' into feature/w3util-export
Browse files Browse the repository at this point in the history
  • Loading branch information
josipbagaric committed Sep 3, 2018
2 parents 6175aaf + 4a3ba33 commit 8c54f27
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/Actions/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import BigNumber from 'bignumber.js';
import Config from '../Config';
import { isExecuted, isTransactionStatusSuccessful } from './Helpers';
import {
isExecuted,
isTransactionStatusSuccessful,
isAborted,
getAbortedExecuteStatus
} from './Helpers';
import hasPending from './Pending';
import { IWalletReceipt } from '../Wallet';
import { ExecuteStatus, ClaimStatus } from '../Enum';
Expand Down Expand Up @@ -134,7 +139,9 @@ export default class Actions implements IActions {
const gasUsed = new BigNumber(receipt.gasUsed);
const gasPrice = new BigNumber(opts.gasPrice);
cost = gasUsed.mul(gasPrice);
status = ExecuteStatus.FAILED;

const aborted = isAborted(receipt);
status = aborted ? getAbortedExecuteStatus(receipt) : ExecuteStatus.FAILED;
}

this.config.statsDb.executed(from, txRequest.address, cost, bounty, success);
Expand Down
37 changes: 36 additions & 1 deletion src/Actions/Helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { AbortReason, ExecuteStatus } from '../Enum';

const EXECUTED_EVENT = '0x3e504bb8b225ad41f613b0c3c4205cdd752d1615b4d77cd1773417282fcfb5d9';
const ABORTED_EVENT = '0xc008bc849b42227c61d5063a1313ce509a6e99211bfd59e827e417be6c65c81b';
const abortReasonToExecuteStatus = new Map<AbortReason, ExecuteStatus>([
[AbortReason.WasCancelled, ExecuteStatus.ABORTED_WAS_CANCELLED],
[AbortReason.AlreadyCalled, ExecuteStatus.ABORTED_ALREADY_CALLED],
[AbortReason.BeforeCallWindow, ExecuteStatus.ABORTED_BEFORE_CALL_WINDOW],
[AbortReason.AfterCallWindow, ExecuteStatus.ABORTED_AFTER_CALL_WINDOW],
[AbortReason.ReservedForClaimer, ExecuteStatus.ABORTED_RESERVED_FOR_CLAIMER],
[AbortReason.InsufficientGas, ExecuteStatus.ABORTED_INSUFFICIENT_GAS],
[AbortReason.TooLowGasPrice, ExecuteStatus.ABORTED_TOO_LOW_GAS_PRICE],
[AbortReason.Unknown, ExecuteStatus.ABORTED_UNKNOWN]
]);

const isExecuted = (receipt: any) => {
if (receipt) {
Expand All @@ -8,11 +21,33 @@ const isExecuted = (receipt: any) => {
return false;
};

const isAborted = (receipt: any) => {
if (receipt) {
return receipt.logs[0].topics.indexOf(ABORTED_EVENT) > -1;
}

return false;
};

const getAbortedExecuteStatus = (receipt: any) => {
const reason = parseInt(receipt.logs[0].data, 16);
const abortReason = receipt && !isNaN(reason) ? (reason as AbortReason) : AbortReason.Unknown;

return abortReasonToExecuteStatus.get(abortReason) || ExecuteStatus.ABORTED_UNKNOWN;
};

const isTransactionStatusSuccessful = (status: string | number) => {
if (status) {
return [1, '0x1', '0x01'].indexOf(status) !== -1;
}
return false;
};

export { isExecuted, isTransactionStatusSuccessful, EXECUTED_EVENT };
export {
isExecuted,
isAborted,
getAbortedExecuteStatus,
isTransactionStatusSuccessful,
EXECUTED_EVENT,
ABORTED_EVENT
};
10 changes: 10 additions & 0 deletions src/Enum/AbortReason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum AbortReason {
WasCancelled, //0
AlreadyCalled, //1
BeforeCallWindow, //2
AfterCallWindow, //3
ReservedForClaimer, //4
InsufficientGas, //5
TooLowGasPrice,
Unknown //6
}
10 changes: 9 additions & 1 deletion src/Enum/ExecuteStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ export enum ExecuteStatus {
WALLET_BUSY = 'Execution: Skipped - Wallet is busy',
IN_PROGRESS = 'Execution: Skipped - In progress',
FAILED = 'Execution: Unable to send the execute action',
SUCCESS = 'Execution: Success'
SUCCESS = 'Execution: Success',
ABORTED_WAS_CANCELLED = 'Execution: Aborted with reason WasCancelled',
ABORTED_ALREADY_CALLED = 'Execution: Aborted with reason AlreadyCalled',
ABORTED_BEFORE_CALL_WINDOW = 'Execution: Aborted with reason BeforeCallWindow',
ABORTED_AFTER_CALL_WINDOW = 'Execution: Aborted with reason AfterCallWindow',
ABORTED_RESERVED_FOR_CLAIMER = 'Execution: Aborted with reason ReservedForClaimer',
ABORTED_INSUFFICIENT_GAS = 'Execution: Aborted with reason InsufficientGas',
ABORTED_TOO_LOW_GAS_PRICE = 'Execution: Aborted with reason TooLowGasPrice',
ABORTED_UNKNOWN = 'Execution: Aborted with reason UNKNOWN'
}
1 change: 1 addition & 0 deletions src/Enum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { CacheStates } from './CacheStates';
export { ExecuteStatus } from './ExecuteStatus';
export { ClaimStatus } from './ClaimStatus';
export { EconomicStrategyStatus } from './EconomicStrategyStatus';
export { AbortReason } from './AbortReason';
11 changes: 9 additions & 2 deletions src/Router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export default class Router implements IRouter {
};
}

public get util (): W3Util {
public get util(): W3Util {
return this.config.util;
}
}

public async beforeClaimWindow(txRequest: ITxRequest): Promise<TxStatus> {
if (txRequest.isCancelled) {
Expand Down Expand Up @@ -215,6 +215,13 @@ export default class Router implements IRouter {
break;
case ClaimStatus.FAILED:
case ExecuteStatus.FAILED:
case ExecuteStatus.ABORTED_AFTER_CALL_WINDOW:
case ExecuteStatus.ABORTED_BEFORE_CALL_WINDOW:
case ExecuteStatus.ABORTED_ALREADY_CALLED:
case ExecuteStatus.ABORTED_INSUFFICIENT_GAS:
case ExecuteStatus.ABORTED_RESERVED_FOR_CLAIMER:
case ExecuteStatus.ABORTED_TOO_LOW_GAS_PRICE:
case ExecuteStatus.ABORTED_WAS_CANCELLED:
this.config.logger.error(status, txRequest.address);
break;
case ClaimStatus.IN_PROGRESS:
Expand Down
157 changes: 155 additions & 2 deletions test/unit/UnitTestActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { shortenAddress } from '../../src/Actions/Actions';
import {
isExecuted,
EXECUTED_EVENT,
isTransactionStatusSuccessful
isTransactionStatusSuccessful,
ABORTED_EVENT,
isAborted,
getAbortedExecuteStatus
} from '../../src/Actions/Helpers';
import { ClaimStatus } from '../../src/Enum';
import { ClaimStatus, ExecuteStatus } from '../../src/Enum';

describe('shortenAddress()', () => {
const address = '0x487a54e1d033db51c8ee8c03edac2a0f8a6892c6';
Expand Down Expand Up @@ -95,4 +98,154 @@ describe('Actions Helpers Unit Tests', () => {
assert.isFalse(isTransactionStatusSuccessful('0x02'));
});
});

describe('isAborted()', () => {
it('returns true when executed event was aborted', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT]
}
]
};
assert.isTrue(isAborted(receipt));
});
});

describe('getAbortedExecuteStatus()', () => {
it('returns ExecuteStatus.ABORTED_WAS_CANCELLED when AbortReason.WasCancelled', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000000'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_WAS_CANCELLED, executeStatus);
});

it('returns ExecuteStatus.ABORTED_ALREADY_CALLED when AbortReason.AlreadyCalled', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000001'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_ALREADY_CALLED, executeStatus);
});

it('returns ExecuteStatus.ABORTED_BEFORE_CALL_WINDOW when AbortReason.BeforeCallWindow', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000002'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_BEFORE_CALL_WINDOW, executeStatus);
});

it('returns ExecuteStatus.ABORTED_AFTER_CALL_WINDOW when AbortReason.AfterCallWindow', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000003'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_AFTER_CALL_WINDOW, executeStatus);
});

it('returns ExecuteStatus.ABORTED_RESERVED_FOR_CLAIMER when AbortReason.ReservedForClaimer', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000004'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_RESERVED_FOR_CLAIMER, executeStatus);
});

it('returns ExecuteStatus.ABORTED_INSUFFICIENT_GAS when AbortReason.InsufficientGas', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000005'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_INSUFFICIENT_GAS, executeStatus);
});

it('returns ExecuteStatus.ABORTED_TOO_LOW_GAS_PRICE when AbortReason.TooLowGasPrice', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000006'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_TOO_LOW_GAS_PRICE, executeStatus);
});

it('returns ExecuteStatus.ABORTED_UNKNOWN when unknown reason appeared', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: '0x0000000000000000000000000000000000000000000000000000000000000008'
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_UNKNOWN, executeStatus);
});

it('returns ExecuteStatus.ABORTED_UNKNOWN when no data found', () => {
const receipt = {
logs: [
{
topics: [ABORTED_EVENT],
data: ''
}
]
};

const executeStatus = getAbortedExecuteStatus(receipt);

assert.equal(ExecuteStatus.ABORTED_UNKNOWN, executeStatus);
});
});
});

0 comments on commit 8c54f27

Please sign in to comment.