-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef6c352
commit a227a44
Showing
11 changed files
with
213 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { isHexString } from 'ethereumjs-util'; | ||
import { ITransactionOption } from "../interfaces"; | ||
import { AbiItem } from "../types"; | ||
import { BaseToken, Web3SideChainClient } from "../utils"; | ||
import { IBaseClientConfig } from "../interfaces"; | ||
|
||
export class Contract extends BaseToken<IBaseClientConfig> { | ||
|
||
constructor( | ||
abi: AbiItem[], | ||
tokenAddress: string, | ||
networkId: number, | ||
client: Web3SideChainClient<IBaseClientConfig>, | ||
) { | ||
super({ | ||
networkId, | ||
address: tokenAddress, | ||
abi, | ||
name: '', | ||
}, client); | ||
} | ||
|
||
/** | ||
* Read from contract | ||
* | ||
* @param {string} method - Method to call | ||
* @param {any[]} args - method arguments | ||
* @returns | ||
* @memberof Contract | ||
*/ | ||
read(method: string, ...args: any[]) { | ||
return this.getContract().then(contract => { | ||
const methodInstance = contract.method( | ||
method, | ||
...args | ||
); | ||
return this.processRead<any>(methodInstance); | ||
}); | ||
} | ||
|
||
/** | ||
* write to contract | ||
* | ||
* @param {ITransactionOption} option - options | ||
* @param {string} method - Method to call | ||
* @param {any[]} args - method arguments | ||
* @returns | ||
* @memberof Contract | ||
*/ | ||
write(option: ITransactionOption, method: string, ...args: any[]) { | ||
return this.getContract().then(contract => { | ||
const methodInstance = contract.method( | ||
method, | ||
...args | ||
); | ||
return this.processWrite(methodInstance, option); | ||
}); | ||
} | ||
|
||
/** | ||
* encode abi call | ||
* @param {string} method - Method to call | ||
* @param {any[]} args - method arguments | ||
* @returns | ||
* @memberof Contract | ||
*/ | ||
encodeAbi(method: string, ...args: any[]) { | ||
return this.getContract().then(contract => { | ||
const methodInstance = contract.method( | ||
method, | ||
...args | ||
); | ||
return methodInstance.encodeABI(); | ||
}); | ||
} | ||
|
||
/** | ||
* get {r, s, v} from signature | ||
* @param {string} signature | ||
* | ||
* @returns | ||
* @memberof ERC20 | ||
*/ | ||
getSignatureParameters(signature: string) { | ||
const client = this.client.providers[this.contractParam.networkId].provider; | ||
if (!isHexString(signature)) { | ||
throw new Error( | ||
'Given value "'.concat(signature, '" is not a valid hex string.'), | ||
); | ||
} | ||
|
||
if (signature.slice(0, 2) !== '0x') { | ||
signature = '0x'.concat(signature); | ||
} | ||
|
||
const r = signature.slice(0, 66); | ||
const s = '0x'.concat(signature.slice(66, 130)); | ||
let v = client.hexToNumber('0x'.concat(signature.slice(130, 132))); | ||
if (![27, 28].includes(v as any)) { | ||
v += 27; | ||
} | ||
return { | ||
r: r, | ||
s: s, | ||
v: v, | ||
}; | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
import { BaseBigNumber } from "../abstracts"; | ||
|
||
export type TYPE_AMOUNT = BaseBigNumber | string | number; | ||
export type TYPE_AMOUNT = BaseBigNumber | string | number; | ||
|
||
export type AbiInput = { | ||
name: string; | ||
type: string; | ||
internalType?: string; | ||
indexed?: boolean; | ||
}; | ||
|
||
export type AbiOutput = { | ||
name: string; | ||
type: string; | ||
internalType?: string; | ||
}; | ||
|
||
export type AbiItem = { | ||
name?: string; | ||
type: string; | ||
constant?: boolean; | ||
payable?: boolean; | ||
stateMutability?: string; | ||
gas?: number; | ||
target?: string; | ||
signature?: string; | ||
inputs?: AbiInput[]; | ||
outputs?: AbiOutput[]; | ||
anonymous?: boolean; | ||
}; |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.