Skip to content

Instantly share code, notes, and snippets.

@pgebheim
Last active October 24, 2018 18:57
Show Gist options
  • Save pgebheim/8c7708e9aa0af7e8e28ec76b4e908ef3 to your computer and use it in GitHub Desktop.
Save pgebheim/8c7708e9aa0af7e8e28ec76b4e908ef3 to your computer and use it in GitHub Desktop.
Ethers.js binding for contract interfaces
// Bindings for interace generated by:
// https://github.com/Zoltu/solidity-typescript-generator
import { Dependencies, AbiFunction, AbiParameter, Transaction } from './generated/liquid-long'
import { keccak256, toUtf8Bytes, BigNumber, AbiCoder } from 'ethers/utils'
import { TransactionResponse, TransactionRequest } from 'ethers/providers';
export interface Provider {
listAccounts(): Promise<Array<string>>
call(transaction: TransactionRequest): Promise<string>
}
export interface Signer {
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>;
}
export class ContractDependenciesEthers implements Dependencies<BigNumber> {
private readonly provider: Provider
private readonly signer: Signer
public constructor(provider: Provider, signer: Signer) {
this.provider = provider
this.signer = signer
}
keccak256 = (utf8String: string) => keccak256(toUtf8Bytes(utf8String))
encodeParams = (abiFunction: AbiFunction, parameters: Array<any>) => new AbiCoder().encode(abiFunction.inputs, parameters).substr(2)
decodeParams = (abiParameters: Array<AbiParameter>, encoded: string) => new AbiCoder().decode(abiParameters, encoded)
getDefaultAddress = async () => (await this.provider.listAccounts())[0]
call = async (transaction: Transaction<BigNumber>) => await this.provider.call(transaction)
submitTransaction = async (transaction: Transaction<BigNumber>) => {
// https://github.com/ethers-io/ethers.js/issues/321
transaction = Object.assign({}, transaction)
delete transaction.from
return { status: (await (await this.signer.sendTransaction(transaction)).wait()).status! }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment