Added new checks and fixed up deploy for Contract.
This commit is contained in:
parent
f7bfa50f15
commit
c411d9744d
@ -2,10 +2,9 @@
|
||||
|
||||
import { Interface } from './interface';
|
||||
|
||||
// @TODO: Move to utils?
|
||||
import { TransactionResponse } from '../providers/provider';
|
||||
import { Network } from '../providers/networks';
|
||||
import { Provider, TransactionResponse } from '../providers/provider';
|
||||
|
||||
import { getContractAddress } from '../utils/address';
|
||||
import { ParamType } from '../utils/abi-coder';
|
||||
import { BigNumber, ConstantZero } from '../utils/bignumber';
|
||||
import { defineReadOnly, resolveProperties } from '../utils/properties';
|
||||
@ -211,6 +210,7 @@ function runMethod(contract: Contract, functionName: string, estimateOnly: boole
|
||||
throw new Error('unsupport type - ' + method.type);
|
||||
}
|
||||
}
|
||||
/*
|
||||
interface Provider {
|
||||
getNetwork(): Promise<Network>;
|
||||
getGasPrice(): Promise<BigNumber>;
|
||||
@ -219,11 +219,12 @@ interface Provider {
|
||||
estimateGas(tx: any): Promise<BigNumber>;
|
||||
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
|
||||
}
|
||||
|
||||
*/
|
||||
interface Signer {
|
||||
defaultGasLimit?: BigNumber;
|
||||
defaultGasPrice?: BigNumber;
|
||||
address?: string;
|
||||
provider?: Provider;
|
||||
|
||||
getAddress(): Promise<string>;
|
||||
getTransactionCount(): Promise<number>;
|
||||
@ -232,6 +233,10 @@ interface Signer {
|
||||
sign(tx: any): string | Promise<string>;
|
||||
}
|
||||
|
||||
function isSigner(value: any): value is Signer {
|
||||
return (value && value.provider != null);
|
||||
}
|
||||
|
||||
export type ContractEstimate = (...params: Array<any>) => Promise<BigNumber>;
|
||||
export type ContractFunction = (...params: Array<any>) => Promise<any>;
|
||||
export type ContractEvent = (...params: Array<any>) => void;
|
||||
@ -253,11 +258,14 @@ export class Contract {
|
||||
|
||||
readonly addressPromise: Promise<string>;
|
||||
|
||||
// This is only set if the contract was created with a call to deploy
|
||||
readonly deployTransaction: TransactionResponse;
|
||||
|
||||
// https://github.com/Microsoft/TypeScript/issues/5453
|
||||
// Once this issue is resolved (there are open PR) we can do this nicer. :)
|
||||
|
||||
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: any) {
|
||||
//if (!(this instanceof Contract)) { throw new Error('missing new'); }
|
||||
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: Signer | Provider) {
|
||||
errors.checkNew(this, Contract);
|
||||
|
||||
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
||||
//address = getAddress(address);
|
||||
@ -269,28 +277,29 @@ export class Contract {
|
||||
|
||||
if (!signerOrProvider) { throw new Error('missing signer or provider'); }
|
||||
|
||||
var signer = signerOrProvider;
|
||||
var provider = null;
|
||||
|
||||
if (signerOrProvider.provider) {
|
||||
provider = signerOrProvider.provider;
|
||||
if (isSigner(signerOrProvider)) {
|
||||
defineReadOnly(this, 'provider', signerOrProvider.provider);
|
||||
defineReadOnly(this, 'signer', signerOrProvider);
|
||||
} else {
|
||||
provider = signerOrProvider;
|
||||
signer = null;
|
||||
defineReadOnly(this, 'provider', signerOrProvider);
|
||||
defineReadOnly(this, 'signer', null);
|
||||
}
|
||||
|
||||
defineReadOnly(this, 'signer', signer);
|
||||
defineReadOnly(this, 'provider', provider);
|
||||
|
||||
if (!addressOrName) { return; }
|
||||
|
||||
defineReadOnly(this, 'address', addressOrName);
|
||||
defineReadOnly(this, 'addressPromise', provider.resolveName(addressOrName));
|
||||
|
||||
defineReadOnly(this, 'estimate', { });
|
||||
defineReadOnly(this, 'events', { });
|
||||
defineReadOnly(this, 'functions', { });
|
||||
|
||||
// Not connected to an on-chain instance, so do not connect functions and events
|
||||
if (!addressOrName) {
|
||||
defineReadOnly(this, 'address', null);
|
||||
defineReadOnly(this, 'addressPromise', Promise.resolve(null));
|
||||
return;
|
||||
}
|
||||
|
||||
defineReadOnly(this, 'address', addressOrName || null);
|
||||
defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName || null));
|
||||
|
||||
Object.keys(this.interface.functions).forEach((name) => {
|
||||
var run = runMethod(this, name, false);
|
||||
|
||||
@ -311,9 +320,9 @@ export class Contract {
|
||||
|
||||
let eventCallback = null;
|
||||
|
||||
let addressPromise = this.addressPromise;
|
||||
let contract = this;
|
||||
function handleEvent(log) {
|
||||
addressPromise.then((address) => {
|
||||
contract.addressPromise.then((address) => {
|
||||
// Not meant for us (the topics just has the same name)
|
||||
if (address != log.address) { return; }
|
||||
|
||||
@ -325,12 +334,12 @@ export class Contract {
|
||||
log.event = eventName;
|
||||
log.parse = eventInfo.parse;
|
||||
log.removeListener = function() {
|
||||
provider.removeListener(eventInfo.topics, handleEvent);
|
||||
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||
}
|
||||
|
||||
log.getBlock = function() { return provider.getBlock(log.blockHash);; }
|
||||
log.getTransaction = function() { return provider.getTransaction(log.transactionHash); }
|
||||
log.getTransactionReceipt = function() { return provider.getTransactionReceipt(log.transactionHash); }
|
||||
log.getBlock = function() { return contract.provider.getBlock(log.blockHash);; }
|
||||
log.getTransaction = function() { return contract.provider.getTransaction(log.transactionHash); }
|
||||
log.getTransactionReceipt = function() { return contract.provider.getTransactionReceipt(log.transactionHash); }
|
||||
log.eventSignature = eventInfo.signature;
|
||||
|
||||
eventCallback.apply(log, Array.prototype.slice.call(result));
|
||||
@ -349,10 +358,10 @@ export class Contract {
|
||||
if (!value) { value = null; }
|
||||
|
||||
if (!value && eventCallback) {
|
||||
provider.removeListener(eventInfo.topics, handleEvent);
|
||||
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||
|
||||
} else if (value && !eventCallback) {
|
||||
provider.on(eventInfo.topics, handleEvent);
|
||||
contract.provider.on(eventInfo.topics, handleEvent);
|
||||
}
|
||||
|
||||
eventCallback = value;
|
||||
@ -369,11 +378,15 @@ export class Contract {
|
||||
}, this);
|
||||
}
|
||||
|
||||
connect(signerOrProvider) {
|
||||
// Reconnect to a different signer or provider
|
||||
connect(signerOrProvider: Signer | Provider): Contract {
|
||||
return new Contract(this.address, this.interface, signerOrProvider);
|
||||
}
|
||||
|
||||
deploy(bytecode: string, ...args): Promise<TransactionResponse> {
|
||||
// Deploy the contract with the bytecode, resolving to the deployed address.
|
||||
// Use contract.deployTransaction.wait() to wait until the contract has
|
||||
// been mined.
|
||||
deploy(bytecode: string, ...args: Array<any>): Promise<Contract> {
|
||||
if (this.signer == null) {
|
||||
throw new Error('missing signer'); // @TODO: errors.throwError
|
||||
}
|
||||
@ -381,6 +394,10 @@ export class Contract {
|
||||
// @TODO: overrides of args.length = this.interface.deployFunction.inputs.length + 1
|
||||
return this.signer.sendTransaction({
|
||||
data: this.interface.deployFunction.encode(bytecode, args)
|
||||
}).then((tx) => {
|
||||
let contract = new Contract(getContractAddress(tx), this.interface, this.provider);
|
||||
defineReadOnly(contract, 'deployTransaction', tx);
|
||||
return contract;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -331,7 +331,7 @@ export class Interface {
|
||||
readonly deployFunction: DeployDescription;
|
||||
|
||||
constructor(abi: Array<string | ParamType> | string) {
|
||||
if (!(this instanceof Interface)) { throw new Error('missing new'); }
|
||||
errors.checkNew(this, Interface);
|
||||
|
||||
if (typeof(abi) === 'string') {
|
||||
try {
|
||||
|
@ -5,6 +5,7 @@ import { Network } from './networks';
|
||||
import { hexlify, hexStripZeros } from '../utils/convert';
|
||||
import { fetchJson } from '../utils/web';
|
||||
|
||||
import * as errors from '../utils/errors';
|
||||
|
||||
// The transaction has already been sanitized by the calls in Provider
|
||||
function getTransactionString(transaction: TransactionRequest): string {
|
||||
@ -70,6 +71,7 @@ export class EtherscanProvider extends Provider{
|
||||
constructor(network?: Network | string, apiKey?: string) {
|
||||
|
||||
super(network || 'homestead');
|
||||
errors.checkNew(this, EtherscanProvider);
|
||||
|
||||
let name = 'invalid';
|
||||
if (this.network) { name = this.network.name; }
|
||||
|
@ -46,15 +46,16 @@ export class FallbackProvider extends Provider {
|
||||
private _providers: Array<Provider>;
|
||||
|
||||
constructor(providers: Array<Provider>) {
|
||||
//if (!(this instanceof FallbackProvider)) { throw new Error('missing new'); }
|
||||
|
||||
if (providers.length === 0) { throw new Error('no providers'); }
|
||||
|
||||
let ready = checkNetworks(providers.map((p) => p.network));
|
||||
if (ready) {
|
||||
super(providers[0].network);
|
||||
errors.checkNew(this, FallbackProvider);
|
||||
} else {
|
||||
super(null);
|
||||
errors.checkNew(this, FallbackProvider);
|
||||
|
||||
// We re-assign the ready function to make sure all networks actually match
|
||||
this.ready = Promise.all(providers.map((p) => p.getNetwork())).then((networks) => {
|
||||
@ -65,6 +66,7 @@ export class FallbackProvider extends Provider {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
this._providers = providers.slice(0);
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,6 @@ export class InfuraProvider extends JsonRpcProvider {
|
||||
readonly apiAccessToken: string;
|
||||
|
||||
constructor(network?: Network | string, apiAccessToken?: string) {
|
||||
//errors.checkNew(this, InfuraProvider);
|
||||
|
||||
network = getNetwork(network || 'homestead');
|
||||
|
||||
@ -32,6 +31,8 @@ export class InfuraProvider extends JsonRpcProvider {
|
||||
}
|
||||
|
||||
super('https://' + host + '/' + (apiAccessToken || ''), network);
|
||||
errors.checkNew(this, InfuraProvider);
|
||||
|
||||
this.apiAccessToken = (apiAccessToken || null);
|
||||
}
|
||||
|
||||
|
@ -9,13 +9,13 @@ import * as errors from '../utils/errors';
|
||||
export class IpcProvider extends JsonRpcProvider {
|
||||
readonly path: string;
|
||||
constructor(path: string, network?: Network | string) {
|
||||
//errors.checkNew(this, IpcProvider);
|
||||
|
||||
if (path == null) {
|
||||
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
||||
}
|
||||
|
||||
super('ipc://' + path, network);
|
||||
errors.checkNew(this, IpcProvider);
|
||||
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ export class JsonRpcSigner {
|
||||
// private _syncAddress: boolean;
|
||||
|
||||
constructor(provider: JsonRpcProvider, address?: string) {
|
||||
//errors.checkNew(this, JsonRpcSigner);
|
||||
errors.checkNew(this, JsonRpcSigner);
|
||||
|
||||
this.provider = provider;
|
||||
|
||||
@ -161,7 +161,6 @@ export class JsonRpcProvider extends Provider {
|
||||
private _pendingFilter: Promise<number>;
|
||||
|
||||
constructor(url?: ConnectionInfo | string, network?: Network | string) {
|
||||
//errors.checkNew(this, JsonRpcProvider);
|
||||
|
||||
// One parameter, but it is a network name, so swap it with the URL
|
||||
if (typeof(url) === 'string') {
|
||||
@ -172,6 +171,7 @@ export class JsonRpcProvider extends Provider {
|
||||
}
|
||||
|
||||
super(network);
|
||||
errors.checkNew(this, JsonRpcProvider);
|
||||
|
||||
// Default URL
|
||||
if (!url) { url = 'http://localhost:8545'; }
|
||||
|
@ -2,11 +2,8 @@
|
||||
|
||||
//import inherits = require('inherits');
|
||||
|
||||
//import networks = require('./networks.json');
|
||||
|
||||
import { getAddress } from '../utils/address';
|
||||
import { getAddress, getContractAddress } from '../utils/address';
|
||||
import { BigNumber, bigNumberify, BigNumberish } from '../utils/bignumber';
|
||||
import { getContractAddress } from '../utils/contract-address';
|
||||
import { Arrayish, hexlify, hexStripZeros, isHexString, stripZeros } from '../utils/convert';
|
||||
import { toUtf8String } from '../utils/utf8';
|
||||
import { decode as rlpDecode, encode as rlpEncode } from '../utils/rlp';
|
||||
@ -604,7 +601,7 @@ export class Provider {
|
||||
*/
|
||||
|
||||
constructor(network: string | Network) {
|
||||
//if (!(this instanceof Provider)) { throw new Error('missing new'); }
|
||||
errors.checkNew(this, Provider);
|
||||
|
||||
network = getNetwork(network);
|
||||
|
||||
@ -803,7 +800,7 @@ export class Provider {
|
||||
}
|
||||
|
||||
|
||||
getBalance(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
|
||||
getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
|
||||
return this.ready.then(() => {
|
||||
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
||||
return this.resolveName(addressOrName).then((address) => {
|
||||
@ -816,7 +813,7 @@ export class Provider {
|
||||
});
|
||||
}
|
||||
|
||||
getTransactionCount(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<number> {
|
||||
getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number> {
|
||||
return this.ready.then(() => {
|
||||
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
||||
return this.resolveName(addressOrName).then((address) => {
|
||||
@ -831,7 +828,7 @@ export class Provider {
|
||||
});
|
||||
}
|
||||
|
||||
getCode(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<string> {
|
||||
getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
||||
return this.ready.then(() => {
|
||||
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
||||
return this.resolveName(addressOrName).then((address) => {
|
||||
@ -844,7 +841,7 @@ export class Provider {
|
||||
});
|
||||
}
|
||||
|
||||
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag: BlockTag | Promise<BlockTag>): Promise<string> {
|
||||
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
||||
return this.ready.then(() => {
|
||||
return resolveProperties({ addressOrName: addressOrName, position: position, blockTag: blockTag }).then(({ addressOrName, position, blockTag }) => {
|
||||
return this.resolveName(addressOrName).then((address) => {
|
||||
|
@ -24,7 +24,6 @@ export class Web3Provider extends JsonRpcProvider {
|
||||
readonly _web3Provider: AsyncProvider;
|
||||
|
||||
constructor(web3Provider: AsyncProvider, network?: Network | string) {
|
||||
//errors.checkNew(this, Web3Provider);
|
||||
|
||||
if (!web3Provider || !web3Provider.sendAsync) {
|
||||
errors.throwError(
|
||||
@ -38,6 +37,8 @@ export class Web3Provider extends JsonRpcProvider {
|
||||
var url = web3Provider.host || web3Provider.path || 'unknown';
|
||||
|
||||
super(url, network);
|
||||
errors.checkNew(this, Web3Provider);
|
||||
|
||||
this._web3Provider = web3Provider;
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
// We use this for base 36 maths
|
||||
import BN = require('bn.js');
|
||||
|
||||
import { arrayify } from './convert';
|
||||
import { BigNumber } from './bignumber';
|
||||
import { arrayify, Arrayish, stripZeros, hexlify } from './convert';
|
||||
import { keccak256 } from './keccak256';
|
||||
import { encode } from './rlp';
|
||||
|
||||
import errors = require('./errors');
|
||||
|
||||
|
||||
function getChecksumAddress(address: string): string {
|
||||
if (typeof(address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
||||
errors.throwError('invalid address', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
||||
@ -118,3 +122,14 @@ export function getAddress(address: string, icapFormat?: boolean): string {
|
||||
return result;
|
||||
}
|
||||
|
||||
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
||||
export function getContractAddress(transaction: { from: string, nonce: Arrayish | BigNumber | number }) {
|
||||
if (!transaction.from) { throw new Error('missing from address'); }
|
||||
var nonce = transaction.nonce;
|
||||
|
||||
return getAddress('0x' + keccak256(encode([
|
||||
getAddress(transaction.from),
|
||||
stripZeros(hexlify(nonce))
|
||||
])).substring(26));
|
||||
}
|
||||
|
||||
|
@ -1,18 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
import { getAddress } from './address';
|
||||
import { BigNumber } from './bignumber';
|
||||
import { Arrayish, stripZeros, hexlify } from './convert';
|
||||
import { keccak256 } from './keccak256';
|
||||
import { encode } from './rlp';
|
||||
|
||||
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
||||
export function getContractAddress(transaction: { from: string, nonce: Arrayish | BigNumber | number }) {
|
||||
if (!transaction.from) { throw new Error('missing from address'); }
|
||||
var nonce = transaction.nonce;
|
||||
|
||||
return getAddress('0x' + keccak256(encode([
|
||||
getAddress(transaction.from),
|
||||
stripZeros(hexlify(nonce))
|
||||
])).substring(26));
|
||||
}
|
@ -3,11 +3,10 @@
|
||||
// This is SUPER useful, but adds 140kb (even zipped, adds 40kb)
|
||||
//var unorm = require('unorm');
|
||||
|
||||
import { getAddress } from './address';
|
||||
import { getAddress, getContractAddress } from './address';
|
||||
import { AbiCoder, defaultAbiCoder, parseSignature } from './abi-coder';
|
||||
import * as base64 from './base64';
|
||||
import * as bigNumber from './bignumber';
|
||||
import { getContractAddress } from './contract-address';
|
||||
import * as convert from './convert';
|
||||
import { id } from './id';
|
||||
import { keccak256 } from './keccak256';
|
||||
|
@ -15,6 +15,8 @@ import { pbkdf2 } from '../utils/pbkdf2';
|
||||
import { createSha512Hmac } from '../utils/hmac';
|
||||
import { sha256 } from '../utils/sha2';
|
||||
|
||||
import * as errors from '../utils/errors';
|
||||
|
||||
// "Bitcoin seed"
|
||||
var MasterSecret = toUtf8Bytes('Bitcoin seed');
|
||||
|
||||
@ -46,7 +48,7 @@ export class HDNode {
|
||||
|
||||
// @TODO: Private constructor?
|
||||
constructor(keyPair: secp256k1.KeyPair, chainCode: Uint8Array, index: number, depth: number, mnemonic: string, path: string) {
|
||||
//if (!(this instanceof HDNode)) { throw new Error('missing new'); }
|
||||
errors.checkNew(this, HDNode);
|
||||
|
||||
this.keyPair = keyPair;
|
||||
|
||||
|
@ -27,7 +27,7 @@ export class SigningKey {
|
||||
private readonly keyPair: secp256k1.KeyPair;
|
||||
|
||||
constructor(privateKey: any) {
|
||||
//errors.checkNew(this, SigningKey);
|
||||
errors.checkNew(this, SigningKey);
|
||||
|
||||
if (privateKey.privateKey) {
|
||||
this.mnemonic = privateKey.mnemonic;
|
||||
|
19
src/contracts/contract.d.ts
vendored
19
src/contracts/contract.d.ts
vendored
@ -1,20 +1,12 @@
|
||||
import { Interface } from './interface';
|
||||
import { TransactionResponse } from '../providers/provider';
|
||||
import { Network } from '../providers/networks';
|
||||
import { Provider, TransactionResponse } from '../providers/provider';
|
||||
import { ParamType } from '../utils/abi-coder';
|
||||
import { BigNumber } from '../utils/bignumber';
|
||||
interface Provider {
|
||||
getNetwork(): Promise<Network>;
|
||||
getGasPrice(): Promise<BigNumber>;
|
||||
getTransactionCount(address: string | Promise<string>): Promise<number>;
|
||||
call(data: string): Promise<string>;
|
||||
estimateGas(tx: any): Promise<BigNumber>;
|
||||
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
|
||||
}
|
||||
interface Signer {
|
||||
defaultGasLimit?: BigNumber;
|
||||
defaultGasPrice?: BigNumber;
|
||||
address?: string;
|
||||
provider?: Provider;
|
||||
getAddress(): Promise<string>;
|
||||
getTransactionCount(): Promise<number>;
|
||||
estimateGas(tx: any): Promise<BigNumber>;
|
||||
@ -37,8 +29,9 @@ export declare class Contract {
|
||||
readonly functions: Bucket<ContractFunction>;
|
||||
readonly events: Bucket<ContractEvent>;
|
||||
readonly addressPromise: Promise<string>;
|
||||
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: any);
|
||||
connect(signerOrProvider: any): Contract;
|
||||
deploy(bytecode: string, ...args: any[]): Promise<TransactionResponse>;
|
||||
readonly deployTransaction: TransactionResponse;
|
||||
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: Signer | Provider);
|
||||
connect(signerOrProvider: Signer | Provider): Contract;
|
||||
deploy(bytecode: string, ...args: Array<any>): Promise<Contract>;
|
||||
}
|
||||
export {};
|
||||
|
@ -8,6 +8,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var interface_1 = require("./interface");
|
||||
var address_1 = require("../utils/address");
|
||||
var bignumber_1 = require("../utils/bignumber");
|
||||
var properties_1 = require("../utils/properties");
|
||||
var errors = __importStar(require("../utils/errors"));
|
||||
@ -193,12 +194,15 @@ function runMethod(contract, functionName, estimateOnly) {
|
||||
throw new Error('unsupport type - ' + method.type);
|
||||
};
|
||||
}
|
||||
function isSigner(value) {
|
||||
return (value && value.provider != null);
|
||||
}
|
||||
var Contract = /** @class */ (function () {
|
||||
// https://github.com/Microsoft/TypeScript/issues/5453
|
||||
// Once this issue is resolved (there are open PR) we can do this nicer. :)
|
||||
function Contract(addressOrName, contractInterface, signerOrProvider) {
|
||||
//if (!(this instanceof Contract)) { throw new Error('missing new'); }
|
||||
var _this = this;
|
||||
errors.checkNew(this, Contract);
|
||||
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
||||
//address = getAddress(address);
|
||||
if (contractInterface instanceof interface_1.Interface) {
|
||||
@ -210,25 +214,25 @@ var Contract = /** @class */ (function () {
|
||||
if (!signerOrProvider) {
|
||||
throw new Error('missing signer or provider');
|
||||
}
|
||||
var signer = signerOrProvider;
|
||||
var provider = null;
|
||||
if (signerOrProvider.provider) {
|
||||
provider = signerOrProvider.provider;
|
||||
if (isSigner(signerOrProvider)) {
|
||||
properties_1.defineReadOnly(this, 'provider', signerOrProvider.provider);
|
||||
properties_1.defineReadOnly(this, 'signer', signerOrProvider);
|
||||
}
|
||||
else {
|
||||
provider = signerOrProvider;
|
||||
signer = null;
|
||||
properties_1.defineReadOnly(this, 'provider', signerOrProvider);
|
||||
properties_1.defineReadOnly(this, 'signer', null);
|
||||
}
|
||||
properties_1.defineReadOnly(this, 'signer', signer);
|
||||
properties_1.defineReadOnly(this, 'provider', provider);
|
||||
if (!addressOrName) {
|
||||
return;
|
||||
}
|
||||
properties_1.defineReadOnly(this, 'address', addressOrName);
|
||||
properties_1.defineReadOnly(this, 'addressPromise', provider.resolveName(addressOrName));
|
||||
properties_1.defineReadOnly(this, 'estimate', {});
|
||||
properties_1.defineReadOnly(this, 'events', {});
|
||||
properties_1.defineReadOnly(this, 'functions', {});
|
||||
// Not connected to an on-chain instance, so do not connect functions and events
|
||||
if (!addressOrName) {
|
||||
properties_1.defineReadOnly(this, 'address', null);
|
||||
properties_1.defineReadOnly(this, 'addressPromise', Promise.resolve(null));
|
||||
return;
|
||||
}
|
||||
properties_1.defineReadOnly(this, 'address', addressOrName || null);
|
||||
properties_1.defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName || null));
|
||||
Object.keys(this.interface.functions).forEach(function (name) {
|
||||
var run = runMethod(_this, name, false);
|
||||
if (_this[name] == null) {
|
||||
@ -245,9 +249,9 @@ var Contract = /** @class */ (function () {
|
||||
Object.keys(this.interface.events).forEach(function (eventName) {
|
||||
var eventInfo = _this.interface.events[eventName];
|
||||
var eventCallback = null;
|
||||
var addressPromise = _this.addressPromise;
|
||||
var contract = _this;
|
||||
function handleEvent(log) {
|
||||
addressPromise.then(function (address) {
|
||||
contract.addressPromise.then(function (address) {
|
||||
// Not meant for us (the topics just has the same name)
|
||||
if (address != log.address) {
|
||||
return;
|
||||
@ -259,11 +263,11 @@ var Contract = /** @class */ (function () {
|
||||
log.event = eventName;
|
||||
log.parse = eventInfo.parse;
|
||||
log.removeListener = function () {
|
||||
provider.removeListener(eventInfo.topics, handleEvent);
|
||||
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||
};
|
||||
log.getBlock = function () { return provider.getBlock(log.blockHash); ; };
|
||||
log.getTransaction = function () { return provider.getTransaction(log.transactionHash); };
|
||||
log.getTransactionReceipt = function () { return provider.getTransactionReceipt(log.transactionHash); };
|
||||
log.getBlock = function () { return contract.provider.getBlock(log.blockHash); ; };
|
||||
log.getTransaction = function () { return contract.provider.getTransaction(log.transactionHash); };
|
||||
log.getTransactionReceipt = function () { return contract.provider.getTransactionReceipt(log.transactionHash); };
|
||||
log.eventSignature = eventInfo.signature;
|
||||
eventCallback.apply(log, Array.prototype.slice.call(result));
|
||||
}
|
||||
@ -282,10 +286,10 @@ var Contract = /** @class */ (function () {
|
||||
value = null;
|
||||
}
|
||||
if (!value && eventCallback) {
|
||||
provider.removeListener(eventInfo.topics, handleEvent);
|
||||
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||
}
|
||||
else if (value && !eventCallback) {
|
||||
provider.on(eventInfo.topics, handleEvent);
|
||||
contract.provider.on(eventInfo.topics, handleEvent);
|
||||
}
|
||||
eventCallback = value;
|
||||
}
|
||||
@ -297,10 +301,15 @@ var Contract = /** @class */ (function () {
|
||||
Object.defineProperty(_this.events, eventName, property);
|
||||
}, this);
|
||||
}
|
||||
// Reconnect to a different signer or provider
|
||||
Contract.prototype.connect = function (signerOrProvider) {
|
||||
return new Contract(this.address, this.interface, signerOrProvider);
|
||||
};
|
||||
// Deploy the contract with the bytecode, resolving to the deployed address.
|
||||
// Use contract.deployTransaction.wait() to wait until the contract has
|
||||
// been mined.
|
||||
Contract.prototype.deploy = function (bytecode) {
|
||||
var _this = this;
|
||||
var args = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
args[_i - 1] = arguments[_i];
|
||||
@ -311,6 +320,10 @@ var Contract = /** @class */ (function () {
|
||||
// @TODO: overrides of args.length = this.interface.deployFunction.inputs.length + 1
|
||||
return this.signer.sendTransaction({
|
||||
data: this.interface.deployFunction.encode(bytecode, args)
|
||||
}).then(function (tx) {
|
||||
var contract = new Contract(address_1.getContractAddress(tx), _this.interface, _this.provider);
|
||||
properties_1.defineReadOnly(contract, 'deployTransaction', tx);
|
||||
return contract;
|
||||
});
|
||||
};
|
||||
return Contract;
|
||||
|
@ -303,9 +303,7 @@ function addMethod(method) {
|
||||
var Interface = /** @class */ (function () {
|
||||
function Interface(abi) {
|
||||
var _this = this;
|
||||
if (!(this instanceof Interface)) {
|
||||
throw new Error('missing new');
|
||||
}
|
||||
errors.checkNew(this, Interface);
|
||||
if (typeof (abi) === 'string') {
|
||||
try {
|
||||
abi = JSON.parse(abi);
|
||||
|
@ -9,10 +9,18 @@ var __extends = (this && this.__extends) || (function () {
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var provider_1 = require("./provider");
|
||||
var convert_1 = require("../utils/convert");
|
||||
var web_1 = require("../utils/web");
|
||||
var errors = __importStar(require("../utils/errors"));
|
||||
// The transaction has already been sanitized by the calls in Provider
|
||||
function getTransactionString(transaction) {
|
||||
var result = [];
|
||||
@ -75,6 +83,7 @@ var EtherscanProvider = /** @class */ (function (_super) {
|
||||
__extends(EtherscanProvider, _super);
|
||||
function EtherscanProvider(network, apiKey) {
|
||||
var _this = _super.call(this, network || 'homestead') || this;
|
||||
errors.checkNew(_this, EtherscanProvider);
|
||||
var name = 'invalid';
|
||||
if (_this.network) {
|
||||
name = _this.network.name;
|
||||
|
@ -50,7 +50,6 @@ function checkNetworks(networks) {
|
||||
var FallbackProvider = /** @class */ (function (_super) {
|
||||
__extends(FallbackProvider, _super);
|
||||
function FallbackProvider(providers) {
|
||||
//if (!(this instanceof FallbackProvider)) { throw new Error('missing new'); }
|
||||
var _this = this;
|
||||
if (providers.length === 0) {
|
||||
throw new Error('no providers');
|
||||
@ -58,9 +57,11 @@ var FallbackProvider = /** @class */ (function (_super) {
|
||||
var ready = checkNetworks(providers.map(function (p) { return p.network; }));
|
||||
if (ready) {
|
||||
_this = _super.call(this, providers[0].network) || this;
|
||||
errors.checkNew(_this, FallbackProvider);
|
||||
}
|
||||
else {
|
||||
_this = _super.call(this, null) || this;
|
||||
errors.checkNew(_this, FallbackProvider);
|
||||
// We re-assign the ready function to make sure all networks actually match
|
||||
_this.ready = Promise.all(providers.map(function (p) { return p.getNetwork(); })).then(function (networks) {
|
||||
if (!checkNetworks(networks)) {
|
||||
|
@ -23,7 +23,6 @@ var errors = __importStar(require("../utils/errors"));
|
||||
var InfuraProvider = /** @class */ (function (_super) {
|
||||
__extends(InfuraProvider, _super);
|
||||
function InfuraProvider(network, apiAccessToken) {
|
||||
//errors.checkNew(this, InfuraProvider);
|
||||
var _this = this;
|
||||
network = networks_1.getNetwork(network || 'homestead');
|
||||
var host = null;
|
||||
@ -44,6 +43,7 @@ var InfuraProvider = /** @class */ (function (_super) {
|
||||
throw new Error('unsupported network');
|
||||
}
|
||||
_this = _super.call(this, 'https://' + host + '/' + (apiAccessToken || ''), network) || this;
|
||||
errors.checkNew(_this, InfuraProvider);
|
||||
_this.apiAccessToken = (apiAccessToken || null);
|
||||
return _this;
|
||||
}
|
||||
|
@ -26,12 +26,12 @@ var errors = __importStar(require("../utils/errors"));
|
||||
var IpcProvider = /** @class */ (function (_super) {
|
||||
__extends(IpcProvider, _super);
|
||||
function IpcProvider(path, network) {
|
||||
//errors.checkNew(this, IpcProvider);
|
||||
var _this = this;
|
||||
if (path == null) {
|
||||
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
||||
}
|
||||
_this = _super.call(this, 'ipc://' + path, network) || this;
|
||||
errors.checkNew(_this, IpcProvider);
|
||||
_this.path = path;
|
||||
return _this;
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ function getLowerCase(value) {
|
||||
var JsonRpcSigner = /** @class */ (function () {
|
||||
// private _syncAddress: boolean;
|
||||
function JsonRpcSigner(provider, address) {
|
||||
//errors.checkNew(this, JsonRpcSigner);
|
||||
errors.checkNew(this, JsonRpcSigner);
|
||||
this.provider = provider;
|
||||
// Statically attach to a given address
|
||||
if (address) {
|
||||
@ -165,7 +165,6 @@ exports.JsonRpcSigner = JsonRpcSigner;
|
||||
var JsonRpcProvider = /** @class */ (function (_super) {
|
||||
__extends(JsonRpcProvider, _super);
|
||||
function JsonRpcProvider(url, network) {
|
||||
//errors.checkNew(this, JsonRpcProvider);
|
||||
var _this = this;
|
||||
// One parameter, but it is a network name, so swap it with the URL
|
||||
if (typeof (url) === 'string') {
|
||||
@ -175,6 +174,7 @@ var JsonRpcProvider = /** @class */ (function (_super) {
|
||||
}
|
||||
}
|
||||
_this = _super.call(this, network) || this;
|
||||
errors.checkNew(_this, JsonRpcProvider);
|
||||
// Default URL
|
||||
if (!url) {
|
||||
url = 'http://localhost:8545';
|
||||
|
8
src/providers/provider.d.ts
vendored
8
src/providers/provider.d.ts
vendored
@ -98,10 +98,10 @@ export declare class Provider {
|
||||
waitForTransaction(transactionHash: string, timeout?: number): Promise<TransactionResponse>;
|
||||
getBlockNumber(): Promise<number>;
|
||||
getGasPrice(): Promise<BigNumber>;
|
||||
getBalance(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<BigNumber>;
|
||||
getTransactionCount(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<number>;
|
||||
getCode(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<string>;
|
||||
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag: BlockTag | Promise<BlockTag>): Promise<string>;
|
||||
getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber>;
|
||||
getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number>;
|
||||
getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
|
||||
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
|
||||
sendTransaction(signedTransaction: string | Promise<string>): Promise<string>;
|
||||
call(transaction: TransactionRequest): Promise<string>;
|
||||
estimateGas(transaction: TransactionRequest): Promise<BigNumber>;
|
||||
|
@ -8,10 +8,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
//import inherits = require('inherits');
|
||||
//import networks = require('./networks.json');
|
||||
var address_1 = require("../utils/address");
|
||||
var bignumber_1 = require("../utils/bignumber");
|
||||
var contract_address_1 = require("../utils/contract-address");
|
||||
var convert_1 = require("../utils/convert");
|
||||
var utf8_1 = require("../utils/utf8");
|
||||
var rlp_1 = require("../utils/rlp");
|
||||
@ -194,7 +192,7 @@ function checkTransactionResponse(transaction) {
|
||||
}
|
||||
// If to and creates are empty, populate the creates from the transaction
|
||||
if (transaction.to == null && transaction.creates == null) {
|
||||
transaction.creates = contract_address_1.getContractAddress(transaction);
|
||||
transaction.creates = address_1.getContractAddress(transaction);
|
||||
}
|
||||
if (!transaction.raw) {
|
||||
// Very loose providers (e.g. TestRPC) don't provide a signature or raw
|
||||
@ -438,7 +436,7 @@ var Provider = /** @class */ (function () {
|
||||
* - Otherwise, the sub-class must assign a Promise to ready
|
||||
*/
|
||||
function Provider(network) {
|
||||
//if (!(this instanceof Provider)) { throw new Error('missing new'); }
|
||||
errors.checkNew(this, Provider);
|
||||
network = networks_1.getNetwork(network);
|
||||
if (network) {
|
||||
this._network = network;
|
||||
|
@ -22,7 +22,6 @@ var errors = __importStar(require("../utils/errors"));
|
||||
var Web3Provider = /** @class */ (function (_super) {
|
||||
__extends(Web3Provider, _super);
|
||||
function Web3Provider(web3Provider, network) {
|
||||
//errors.checkNew(this, Web3Provider);
|
||||
var _this = this;
|
||||
if (!web3Provider || !web3Provider.sendAsync) {
|
||||
errors.throwError('invalid web3Provider', errors.INVALID_ARGUMENT, { arg: 'web3Provider', value: web3Provider });
|
||||
@ -30,6 +29,7 @@ var Web3Provider = /** @class */ (function (_super) {
|
||||
// HTTP has a host; IPC has a path.
|
||||
var url = web3Provider.host || web3Provider.path || 'unknown';
|
||||
_this = _super.call(this, url, network) || this;
|
||||
errors.checkNew(_this, Web3Provider);
|
||||
_this._web3Provider = web3Provider;
|
||||
return _this;
|
||||
}
|
||||
|
6
src/utils/address.d.ts
vendored
6
src/utils/address.d.ts
vendored
@ -1 +1,7 @@
|
||||
import { BigNumber } from './bignumber';
|
||||
import { Arrayish } from './convert';
|
||||
export declare function getAddress(address: string, icapFormat?: boolean): string;
|
||||
export declare function getContractAddress(transaction: {
|
||||
from: string;
|
||||
nonce: Arrayish | BigNumber | number;
|
||||
}): string;
|
||||
|
@ -1,8 +1,10 @@
|
||||
'use strict';
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
// We use this for base 36 maths
|
||||
var BN = require("bn.js");
|
||||
var convert_1 = require("./convert");
|
||||
var keccak256_1 = require("./keccak256");
|
||||
var rlp_1 = require("./rlp");
|
||||
var errors = require("./errors");
|
||||
function getChecksumAddress(address) {
|
||||
if (typeof (address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
||||
@ -104,3 +106,15 @@ function getAddress(address, icapFormat) {
|
||||
return result;
|
||||
}
|
||||
exports.getAddress = getAddress;
|
||||
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
||||
function getContractAddress(transaction) {
|
||||
if (!transaction.from) {
|
||||
throw new Error('missing from address');
|
||||
}
|
||||
var nonce = transaction.nonce;
|
||||
return getAddress('0x' + keccak256_1.keccak256(rlp_1.encode([
|
||||
getAddress(transaction.from),
|
||||
convert_1.stripZeros(convert_1.hexlify(nonce))
|
||||
])).substring(26));
|
||||
}
|
||||
exports.getContractAddress = getContractAddress;
|
||||
|
3
src/utils/index.d.ts
vendored
3
src/utils/index.d.ts
vendored
@ -1,8 +1,7 @@
|
||||
import { getAddress } from './address';
|
||||
import { getAddress, getContractAddress } from './address';
|
||||
import { AbiCoder, parseSignature } from './abi-coder';
|
||||
import * as base64 from './base64';
|
||||
import * as bigNumber from './bignumber';
|
||||
import { getContractAddress } from './contract-address';
|
||||
import * as convert from './convert';
|
||||
import { id } from './id';
|
||||
import { keccak256 } from './keccak256';
|
||||
|
@ -13,7 +13,6 @@ var address_1 = require("./address");
|
||||
var abi_coder_1 = require("./abi-coder");
|
||||
var base64 = __importStar(require("./base64"));
|
||||
var bigNumber = __importStar(require("./bignumber"));
|
||||
var contract_address_1 = require("./contract-address");
|
||||
var convert = __importStar(require("./convert"));
|
||||
var id_1 = require("./id");
|
||||
var keccak256_1 = require("./keccak256");
|
||||
@ -50,7 +49,7 @@ exports.default = {
|
||||
namehash: namehash_1.namehash,
|
||||
id: id_1.id,
|
||||
getAddress: address_1.getAddress,
|
||||
getContractAddress: contract_address_1.getContractAddress,
|
||||
getContractAddress: address_1.getContractAddress,
|
||||
formatEther: units.formatEther,
|
||||
parseEther: units.parseEther,
|
||||
formatUnits: units.formatUnits,
|
||||
|
@ -21,6 +21,7 @@ var utf8_1 = require("../utils/utf8");
|
||||
var pbkdf2_1 = require("../utils/pbkdf2");
|
||||
var hmac_1 = require("../utils/hmac");
|
||||
var sha2_1 = require("../utils/sha2");
|
||||
var errors = __importStar(require("../utils/errors"));
|
||||
// "Bitcoin seed"
|
||||
var MasterSecret = utf8_1.toUtf8Bytes('Bitcoin seed');
|
||||
var HardenedBit = 0x80000000;
|
||||
@ -35,7 +36,7 @@ function getLowerMask(bits) {
|
||||
var HDNode = /** @class */ (function () {
|
||||
// @TODO: Private constructor?
|
||||
function HDNode(keyPair, chainCode, index, depth, mnemonic, path) {
|
||||
//if (!(this instanceof HDNode)) { throw new Error('missing new'); }
|
||||
errors.checkNew(this, HDNode);
|
||||
this.keyPair = keyPair;
|
||||
this.privateKey = convert_1.hexlify(keyPair.priv.toArray('be', 32));
|
||||
this.publicKey = '0x' + keyPair.getPublic(true, 'hex');
|
||||
|
@ -19,7 +19,7 @@ var keccak256_1 = require("../utils/keccak256");
|
||||
var errors = require("../utils/errors");
|
||||
var SigningKey = /** @class */ (function () {
|
||||
function SigningKey(privateKey) {
|
||||
//errors.checkNew(this, SigningKey);
|
||||
errors.checkNew(this, SigningKey);
|
||||
if (privateKey.privateKey) {
|
||||
this.mnemonic = privateKey.mnemonic;
|
||||
this.path = privateKey.path;
|
||||
|
Loading…
Reference in New Issue
Block a user