Created
December 13, 2017 14:34
-
-
Save anonymous/d865de223f954d5ab6311cd0f93ff9c8 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'; | |
import { W3, SoltsiceContract } from 'soltsice'; | |
/** | |
* Example API | |
*/ | |
export class Example extends SoltsiceContract { | |
static get Artifacts() { return require('artifacts/example.json'); } | |
static get BytecodeHash() { | |
// we need this before ctor, but artifacts are static and we cannot pass it to the base class, so need to generate | |
let artifacts = Example.Artifacts; | |
if (!artifacts || !artifacts.bytecode) { | |
return undefined; | |
} | |
let hash = W3.sha3(JSON.stringify(artifacts.bytecode)); | |
return hash; | |
} | |
// tslint:disable-next-line:max-line-length | |
static async New(deploymentParams: W3.TC.TxParams, ctorParams?: {}, w3?: W3, link?: SoltsiceContract[]): Promise<Example> { | |
let contract = new Example(deploymentParams, ctorParams, w3, link); | |
await contract._instancePromise; | |
return contract; | |
} | |
static async At(address: string | object, w3?: W3): Promise<Example> { | |
let contract = new Example(address, undefined, w3, undefined); | |
await contract._instancePromise; | |
return contract; | |
} | |
protected constructor( | |
deploymentParams: string | W3.TC.TxParams | object, | |
ctorParams?: {}, | |
w3?: W3, | |
link?: SoltsiceContract[] | |
) { | |
// tslint:disable-next-line:max-line-length | |
super( | |
w3, | |
Example.Artifacts, | |
ctorParams ? [] : [], | |
deploymentParams, | |
link | |
); | |
} | |
/* | |
Contract methods | |
*/ | |
// tslint:disable-next-line:max-line-length | |
// tslint:disable-next-line:variable-name | |
public balanceOf(_owner: string, txParams?: W3.TC.TxParams): Promise<BigNumber> { | |
return new Promise((resolve, reject) => { | |
this._instance.balanceOf | |
.call(_owner, txParams || this._sendParams) | |
.then((res) => resolve(res)) | |
.catch((err) => reject(err)); | |
}); | |
} | |
// tslint:disable-next-line:member-ordering | |
public transfer = Object.assign( | |
// tslint:disable-next-line:max-line-length | |
// tslint:disable-next-line:variable-name | |
(_to: string, _value: BigNumber | number, txParams?: W3.TC.TxParams): Promise<W3.TC.TransactionResult> => { | |
return new Promise((resolve, reject) => { | |
this._instance.transfer(_to, _value, txParams || this._sendParams) | |
.then((res) => resolve(res)) | |
.catch((err) => reject(err)); | |
}); | |
}, | |
{ | |
// tslint:disable-next-line:max-line-length | |
// tslint:disable-next-line:variable-name | |
sendTransaction: (_to: string, _value: BigNumber | number, txParams?: W3.TC.TxParams): Promise<string> => { | |
return new Promise((resolve, reject) => { | |
this._instance.transfer.sendTransaction(_to, _value, txParams || this._sendParams) | |
.then((res) => resolve(res)) | |
.catch((err) => reject(err)); | |
}); | |
} | |
}, | |
{ | |
// tslint:disable-next-line:max-line-length | |
// tslint:disable-next-line:variable-name | |
data: (_to: string, _value: BigNumber | number): Promise<string> => { | |
return new Promise((resolve, reject) => { | |
resolve(this._instance.transfer.request(_to, _value).params[0].data); | |
}); | |
} | |
}, | |
{ | |
// tslint:disable-next-line:max-line-length | |
// tslint:disable-next-line:variable-name | |
estimateGas: (_to: string, _value: BigNumber | number): Promise<number> => { | |
return new Promise((resolve, reject) => { | |
this._instance.transfer.estimateGas(_to, _value).then((g) => resolve(g)); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment