Skip to content

Commit

Permalink
Use ITxRequest instead of any (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
kosecki123 committed Aug 16, 2018
1 parent 765562a commit c8e2d78
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 179 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"ethereumjs-tx": "1.3.7",
"ethereumjs-wallet": "0.6.2",
"lokijs": "1.5.5",
"typescript": "2.9.1",
"typescript": "3.0.1",
"web3-providers-ws": "1.0.0-beta.34"
},
"devDependencies": {
Expand Down
18 changes: 9 additions & 9 deletions src/Actions/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IWalletReceipt } from '../Wallet';
import { ExecuteStatus, ClaimStatus } from '../Enum';
import { getExecutionGasPrice } from '../EconomicStrategy';
import { TxSendErrors } from '../Enum/TxSendErrors';
import { Address } from '../Types';
import { ITxRequest, Address } from '../Types';

export function shortenAddress(address: string) {
return `${address.slice(0, 6)}...${address.slice(address.length - 5, address.length)}`;
Expand All @@ -19,7 +19,7 @@ export default class Actions {
this.config = config;
}

public async claim(txRequest: any, nextAccount: Address): Promise<ClaimStatus> {
public async claim(txRequest: ITxRequest, nextAccount: Address): Promise<ClaimStatus> {
if (!this.config.claiming) {
return ClaimStatus.NOT_ENABLED;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class Actions {
return ClaimStatus.FAILED;
}

public async execute(txRequest: any): Promise<ExecuteStatus> {
public async execute(txRequest: ITxRequest): Promise<ExecuteStatus> {
if (this.config.wallet.hasPendingTransaction(txRequest.address)) {
return ExecuteStatus.IN_PROGRESS;
}
Expand Down Expand Up @@ -105,12 +105,12 @@ export default class Actions {
return ExecuteStatus.FAILED;
}

public async cleanup(txRequest: any): Promise<boolean> {
public async cleanup(): Promise<boolean> {
throw Error('Not implemented according to latest EAC changes.');
}

private async handleSuccessfulExecution(
txRequest: any,
txRequest: ITxRequest,
receipt: any,
opts: any,
from: string
Expand All @@ -136,14 +136,14 @@ export default class Actions {
this.config.statsDb.updateExecuted(from, bounty, cost);
}

private async hasPendingExecuteTransaction(txRequest: any): Promise<boolean> {
private async hasPendingExecuteTransaction(txRequest: ITxRequest): Promise<boolean> {
return hasPending(this.config, txRequest, {
type: 'execute',
minPrice: txRequest.gasPrice
});
}

private async getClaimingOpts(txRequest: any): Promise<any> {
private async getClaimingOpts(txRequest: ITxRequest): Promise<any> {
return {
to: txRequest.address,
value: txRequest.requiredDeposit,
Expand All @@ -153,7 +153,7 @@ export default class Actions {
};
}

private async getExecutionOpts(txRequest: any): Promise<any> {
private async getExecutionOpts(txRequest: ITxRequest): Promise<any> {
const gas = this.config.util.calculateGasAmount(txRequest);
const gasPrice = await getExecutionGasPrice(txRequest, this.config);

Expand All @@ -166,7 +166,7 @@ export default class Actions {
};
}

private async accountClaimingCost(receipt: any, txRequest: any, opts: any, from: string) {
private async accountClaimingCost(receipt: any, txRequest: ITxRequest, opts: any, from: string) {
if (receipt) {
await txRequest.refreshData();
const gasUsed = new BigNumber(receipt.gasUsed);
Expand Down
23 changes: 19 additions & 4 deletions src/Actions/Pending.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Cache from '../Cache';
import { FnSignatures } from '../Enum';
import { ITxRequest } from '../Types';
import BigNumber from 'bignumber.js';
import { ITxRequestPending } from '../Types/ITxRequest';

interface PendingOpts {
type?: string;
checkGasPrice?: boolean;
minPrice?: number;
minPrice?: BigNumber;
}

/**
Expand All @@ -16,7 +19,11 @@ interface PendingOpts {
* @param {number} minPrice (optional) Expected gasPrice.
* @returns {Promise<boolean>} True if a pending transaction to this address exists.
*/
const hasPendingParity = (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
const hasPendingParity = (
conf: any,
txRequest: ITxRequestPending,
opts: PendingOpts
): Promise<boolean> => {
opts.checkGasPrice = opts.checkGasPrice === undefined ? true : opts.checkGasPrice;
const provider = conf.web3.currentProvider;

Expand Down Expand Up @@ -71,7 +78,11 @@ const hasPendingParity = (conf: any, txRequest: any, opts: PendingOpts): Promise
* @param {number} minPrice (optional) Expected gasPrice.
* @returns {Promise<object>} Transaction, if a pending transaction to this address exists.
*/
const hasPendingGeth = (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
const hasPendingGeth = (
conf: any,
txRequest: ITxRequestPending,
opts: PendingOpts
): Promise<boolean> => {
opts.checkGasPrice = opts.checkGasPrice === undefined ? true : opts.checkGasPrice;
const provider = conf.web3.currentProvider;

Expand Down Expand Up @@ -173,7 +184,11 @@ const isOfType = (transaction: any, type?: string) => {
* @param {boolean} checkGasPrice (optional, default: true) Check if transaction's gasPrice is sufficient for Network.
* @param {number} minPrice (optional) Expected gasPrice to compare.
*/
const hasPending = async (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
const hasPending = async (
conf: any,
txRequest: ITxRequestPending,
opts: PendingOpts
): Promise<boolean> => {
let result = false;

if (conf.client === 'parity') {
Expand Down
10 changes: 5 additions & 5 deletions src/EconomicStrategy/EconomicStrategyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ITxRequest, Address } from '../Types';
* @param {TransactionRequest} txRequest Transaction Request object to check.
* @param {IEconomicStrategy} economicStrategy Economic strategy configuration object.
*/
const exceedsMaxDeposit = (txRequest: any, economicStrategy: IEconomicStrategy): boolean => {
const exceedsMaxDeposit = (txRequest: ITxRequest, economicStrategy: IEconomicStrategy): boolean => {
const requiredDeposit = txRequest.requiredDeposit;
const maxDeposit = economicStrategy.maxDeposit;

Expand Down Expand Up @@ -77,7 +77,7 @@ const isAboveMinBalanceLimit = async (config: Config, nextAccount: Address): Pro
* @param {IEconomicStrategy} economicStrategy Economic strategy configuration object.
*/
const isProfitable = async (
txRequest: any,
txRequest: ITxRequest,
economicStrategy: IEconomicStrategy
): Promise<boolean> => {
const paymentModifier = await txRequest.claimPaymentModifier();
Expand All @@ -98,7 +98,7 @@ const isProfitable = async (
* @param {Config} config Configuration object.
*/
const shouldClaimTx = async (
txRequest: any,
txRequest: ITxRequest,
config: Config,
nextAccount: Address
): Promise<boolean> => {
Expand Down Expand Up @@ -127,7 +127,7 @@ const shouldClaimTx = async (
* @param {TransactionRequest} txRequest Transaction Request object to check.
* @param {Config} config Configuration object.
*/
const getExecutionGasPrice = async (txRequest: any, config: Config): Promise<BigNumber> => {
const getExecutionGasPrice = async (txRequest: ITxRequest, config: Config): Promise<BigNumber> => {
const currentNetworkPrice = await config.util.networkGasPrice();

if (!config.economicStrategy) {
Expand Down Expand Up @@ -161,7 +161,7 @@ const getExecutionGasPrice = async (txRequest: any, config: Config): Promise<Big
* @param {TransactionRequest} txRequest Transaction Request object to check.
* @param {Config} config Configuration object.
*/
const shouldExecuteTx = async (txRequest: any, config: Config): Promise<boolean> => {
const shouldExecuteTx = async (txRequest: ITxRequest, config: Config): Promise<boolean> => {
const isClaimedByMe = config.wallet.getAddresses().indexOf(txRequest.claimedBy) !== -1;

const gasPrice = await this.getExecutionGasPrice(txRequest, config);
Expand Down
32 changes: 18 additions & 14 deletions src/Router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export default class Router {
this.transitions[TxStatus.ExecutionWindow] = this.executionWindow.bind(this);
this.transitions[TxStatus.Executed] = this.executed.bind(this);
this.transitions[TxStatus.Missed] = this.missed.bind(this);
this.transitions[TxStatus.Done] = (txRequest: any) => {
this.transitions[TxStatus.Done] = (txRequest: ITxRequest) => {
this.config.logger.info('Finished. Deleting from cache...', txRequest.address);
this.config.cache.del(txRequest.address);
return TxStatus.Done;
};
}

public async beforeClaimWindow(txRequest: any): Promise<TxStatus> {
public async beforeClaimWindow(txRequest: ITxRequest): Promise<TxStatus> {
if (txRequest.isCancelled) {
// TODO Status.CleanUp?
return TxStatus.Executed;
Expand All @@ -45,7 +45,7 @@ export default class Router {
return TxStatus.ClaimWindow;
}

public async claimWindow(txRequest: any): Promise<TxStatus> {
public async claimWindow(txRequest: ITxRequest): Promise<TxStatus> {
if (!(await txRequest.inClaimWindow()) || txRequest.isClaimed) {
return TxStatus.FreezePeriod;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ export default class Router {
return TxStatus.ClaimWindow;
}

public async freezePeriod(txRequest: any): Promise<TxStatus> {
public async freezePeriod(txRequest: ITxRequest): Promise<TxStatus> {
if (await txRequest.inFreezePeriod()) {
return TxStatus.FreezePeriod;
}
Expand All @@ -90,12 +90,12 @@ export default class Router {
return TxStatus.FreezePeriod;
}

public async inReservedWindowAndNotClaimedLocally(txRequest: any): Promise<boolean> {
public async inReservedWindowAndNotClaimedLocally(txRequest: ITxRequest): Promise<boolean> {
const inReserved = await txRequest.inReservedWindow();
return inReserved && txRequest.isClaimed && !this.isLocalClaim(txRequest);
}

public async executionWindow(txRequest: any): Promise<TxStatus> {
public async executionWindow(txRequest: ITxRequest): Promise<TxStatus> {
if (txRequest.wasCalled) {
return TxStatus.Executed;
}
Expand Down Expand Up @@ -129,7 +129,7 @@ export default class Router {
return TxStatus.ExecutionWindow;
}

public async executed(txRequest: any): Promise<TxStatus> {
public async executed(): Promise<TxStatus> {
/**
* We don't cleanup because cleanup needs refactor according to latest logic in EAC
* https://github.com/ethereum-alarm-clock/ethereum-alarm-clock/blob/master/contracts/Library/RequestLib.sol#L433
Expand All @@ -141,18 +141,19 @@ export default class Router {
return TxStatus.Done;
}

public async missed(txRequest: any): Promise<TxStatus> {
public async missed(): Promise<TxStatus> {
// TODO cleanup
return TxStatus.Done;
}

public async isTransactionMissed(txRequest: any): Promise<boolean> {
const afterExecutionWindow =
parseInt(txRequest.executionWindowEnd, 10) <= parseInt(await txRequest.now(), 10);
return Boolean(afterExecutionWindow && !txRequest.wasCalled);
public async isTransactionMissed(txRequest: ITxRequest): Promise<boolean> {
const now = await txRequest.now();
const afterExecutionWindow = txRequest.executionWindowEnd.lessThanOrEqualTo(now);

return afterExecutionWindow && !txRequest.wasCalled;
}

public isLocalClaim(txRequest: any): boolean {
public isLocalClaim(txRequest: ITxRequest): boolean {
const localClaim = this.config.wallet.isKnownAddress(txRequest.claimedBy);

if (!localClaim) {
Expand Down Expand Up @@ -184,7 +185,10 @@ export default class Router {
return nextStatus;
}

private handleWalletTransactionResult(status: ClaimStatus | ExecuteStatus, txRequest: any): void {
private handleWalletTransactionResult(
status: ClaimStatus | ExecuteStatus,
txRequest: ITxRequest
): void {
switch (status) {
case ClaimStatus.SUCCESS:
this.config.logger.info('CLAIMED.', txRequest.address); //TODO: replace with SUCCESS string
Expand Down
12 changes: 7 additions & 5 deletions src/Scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IBlock, IntervalId, ITxRequest } from '../Types';
import { Bucket, IBucketPair, IBuckets, BucketSize } from '../Buckets';
import W3Util from '../Util';
import { CacheStates } from '../Enum';
import { BigNumber } from 'bignumber.js';
import { ITxRequestRaw } from '../Types/ITxRequest';

export default class {
public config: Config;
Expand Down Expand Up @@ -102,7 +104,7 @@ export default class {
* and should be stored in a TimeNodes cache.
* @param txRequest Transaction Request Object
*/
public async isUpcoming(txRequest: any): Promise<boolean> {
public async isUpcoming(txRequest: ITxRequest): Promise<boolean> {
return (
(await txRequest.beforeClaimWindow()) ||
(await txRequest.inClaimWindow()) ||
Expand Down Expand Up @@ -140,7 +142,7 @@ export default class {
};
}

public handleRequest(request: any): void {
public handleRequest(request: ITxRequestRaw): void {
if (!this.isValid(request.address)) {
throw new Error(`[${request.address}] NOT VALID`);
}
Expand Down Expand Up @@ -257,11 +259,11 @@ export default class {
return CacheStates.REFRESHED; //0 = cache loaded successfully
}

public store(request: any) {
this.config.cache.set(request.address, {
public store(txRequest: ITxRequestRaw) {
this.config.cache.set(txRequest.address, {
claimedBy: null,
wasCalled: false,
windowStart: request.params[7]
windowStart: txRequest.params[7]
});
}
}
40 changes: 37 additions & 3 deletions src/Types/ITxRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
import { BigNumber } from 'bignumber.js';

// TODO this is only temporary
export interface ITxRequest {
export interface ITxRequest extends ITxRequestPending {
claimedBy: string;
requiredDeposit: BigNumber;
claimData: string;
executeData: string;
bounty: BigNumber;
callGas: BigNumber;
isCancelled: boolean;
isClaimed: boolean;
wasCalled: boolean;
executionWindowEnd: BigNumber;
temporalUnit: number;
claimWindowStart: BigNumber;
windowStart: BigNumber;
windowSize: BigNumber;
freezePeriod: BigNumber;
reservedWindowSize: BigNumber;
claimWindowEnd: BigNumber;
freezePeriodEnd: BigNumber;
reservedWindowEnd: BigNumber;

refreshData(): Promise<any>;
claimPaymentModifier(): Promise<BigNumber>; //TODO: refactor as BigNumber not required here
inReservedWindow(): Promise<boolean>;
beforeClaimWindow(): Promise<boolean>;
inClaimWindow(): Promise<boolean>;
inFreezePeriod(): Promise<boolean>;
inExecutionWindow(): Promise<boolean>;
now(): Promise<BigNumber>;
isClaimedBy(address: string): boolean;
}

export interface ITxRequestPending {
address: string;
gasPrice: BigNumber;
}

refreshData(): Promise<any>;
export interface ITxRequestRaw {
address: string;
params: BigNumber[];
}
Loading

0 comments on commit c8e2d78

Please sign in to comment.