Created
December 13, 2017 14:28
-
-
Save anonymous/400ef04ea366765455e117775dae8b36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BigNumber } from "bignumber.js"; | |
export interface ITxParams { | |
from?: string; | |
gas?: number | string | BigNumber; | |
gasPrice?: number | string | BigNumber; | |
} | |
export interface IPayableTxParams { | |
value: string | number | BigNumber; | |
from?: string; | |
gas?: number | string | BigNumber; | |
gasPrice?: number | string | BigNumber; | |
} | |
export class TypeChainContract { | |
public readonly rawWeb3Contract: any; | |
public readonly address: string; | |
constructor(web3: any, address: string | BigNumber, public readonly contractAbi: object) { | |
this.address = address.toString(); | |
this.rawWeb3Contract = web3.eth.contract(contractAbi).at(address); | |
} | |
} | |
export class DeferredTransactionWrapper<T extends ITxParams> { | |
constructor( | |
private readonly parentContract: TypeChainContract, | |
private readonly methodName: string, | |
private readonly methodArgs: any[], | |
) {} | |
send(params: T, customWeb3?: any): Promise<string> { | |
let method: any; | |
if (customWeb3) { | |
const tmpContract = customWeb3.eth | |
.contract(this.parentContract.contractAbi) | |
.at(this.parentContract.address); | |
method = tmpContract[this.methodName].sendTransaction; | |
} else { | |
method = this.parentContract.rawWeb3Contract[this.methodName].sendTransaction; | |
} | |
return promisify(method, [...this.methodArgs, params]); | |
} | |
getData(): string { | |
return this.parentContract.rawWeb3Contract[this.methodName].getData( | |
...this.methodArgs, | |
) as string; | |
} | |
} | |
export function promisify(func: any, args: any): Promise<any> { | |
return new Promise((res, rej) => { | |
func(...args, (err: any, data: any) => { | |
if (err) return rej(err); | |
return res(data); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment