2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
|
|
|
|
import { BytesLike, isHexString } from "@ethersproject/bytes";
|
|
|
|
import { Network } from "@ethersproject/networks";
|
2020-06-01 04:46:37 -04:00
|
|
|
import { Deferrable, Description, defineReadOnly } from "@ethersproject/properties";
|
2019-05-14 18:25:46 -04:00
|
|
|
import { Transaction } from "@ethersproject/transactions";
|
|
|
|
import { OnceBlockable } from "@ethersproject/web";
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// Exported Types
|
|
|
|
|
|
|
|
|
|
|
|
export type TransactionRequest = {
|
2020-06-01 04:46:37 -04:00
|
|
|
to?: string,
|
|
|
|
from?: string,
|
|
|
|
nonce?: BigNumberish,
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
gasLimit?: BigNumberish,
|
|
|
|
gasPrice?: BigNumberish,
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
data?: BytesLike,
|
|
|
|
value?: BigNumberish,
|
|
|
|
chainId?: number
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TransactionResponse extends Transaction {
|
2019-06-05 21:49:38 -04:00
|
|
|
hash: string;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Only if a transaction has been mined
|
|
|
|
blockNumber?: number,
|
|
|
|
blockHash?: string,
|
|
|
|
timestamp?: number,
|
|
|
|
|
|
|
|
confirmations: number,
|
|
|
|
|
|
|
|
// Not optional (as it is in Transaction)
|
|
|
|
from: string;
|
|
|
|
|
|
|
|
// The raw transaction
|
|
|
|
raw?: string,
|
|
|
|
|
|
|
|
// This function waits until the transaction has been mined
|
|
|
|
wait: (confirmations?: number) => Promise<TransactionReceipt>
|
|
|
|
};
|
|
|
|
|
|
|
|
export type BlockTag = string | number;
|
|
|
|
|
|
|
|
interface _Block {
|
|
|
|
hash: string;
|
|
|
|
parentHash: string;
|
|
|
|
number: number;
|
|
|
|
|
|
|
|
timestamp: number;
|
|
|
|
nonce: string;
|
|
|
|
difficulty: number;
|
|
|
|
|
|
|
|
gasLimit: BigNumber;
|
|
|
|
gasUsed: BigNumber;
|
|
|
|
|
|
|
|
miner: string;
|
|
|
|
extraData: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Block extends _Block {
|
|
|
|
transactions: Array<string>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BlockWithTransactions extends _Block {
|
|
|
|
transactions: Array<TransactionResponse>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface Log {
|
2020-04-03 20:45:23 -04:00
|
|
|
blockNumber: number;
|
|
|
|
blockHash: string;
|
|
|
|
transactionIndex: number;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:11:55 -05:00
|
|
|
removed: boolean;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
address: string;
|
|
|
|
data: string;
|
|
|
|
|
|
|
|
topics: Array<string>;
|
|
|
|
|
2020-04-03 20:45:23 -04:00
|
|
|
transactionHash: string;
|
|
|
|
logIndex: number;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TransactionReceipt {
|
2020-01-18 21:11:55 -05:00
|
|
|
to: string;
|
|
|
|
from: string;
|
|
|
|
contractAddress: string,
|
|
|
|
transactionIndex: number,
|
2019-05-14 18:25:46 -04:00
|
|
|
root?: string,
|
2020-01-18 21:11:55 -05:00
|
|
|
gasUsed: BigNumber,
|
|
|
|
logsBloom: string,
|
|
|
|
blockHash: string,
|
|
|
|
transactionHash: string,
|
|
|
|
logs: Array<Log>,
|
|
|
|
blockNumber: number,
|
|
|
|
confirmations: number,
|
|
|
|
cumulativeGasUsed: BigNumber,
|
2019-05-14 18:25:46 -04:00
|
|
|
byzantium: boolean,
|
|
|
|
status?: number
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface EventFilter {
|
|
|
|
address?: string;
|
|
|
|
topics?: Array<string | Array<string>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Filter extends EventFilter {
|
|
|
|
fromBlock?: BlockTag,
|
|
|
|
toBlock?: BlockTag,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FilterByBlockHash extends EventFilter {
|
2020-05-20 23:50:35 -04:00
|
|
|
blockHash?: string;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//export type CallTransactionable = {
|
|
|
|
// call(transaction: TransactionRequest): Promise<TransactionResponse>;
|
|
|
|
//};
|
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
export abstract class ForkEvent extends Description {
|
2019-05-14 18:25:46 -04:00
|
|
|
readonly expiry: number;
|
|
|
|
|
2020-01-07 19:47:55 -05:00
|
|
|
readonly _isForkEvent?: boolean;
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
static isForkEvent(value: any): value is ForkEvent {
|
|
|
|
return !!(value && value._isForkEvent);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BlockForkEvent extends ForkEvent {
|
2020-05-20 23:50:35 -04:00
|
|
|
readonly blockHash: string;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-07 19:47:55 -05:00
|
|
|
readonly _isBlockForkEvent?: boolean;
|
|
|
|
|
2020-05-20 23:50:35 -04:00
|
|
|
constructor(blockHash: string, expiry?: number) {
|
|
|
|
if (!isHexString(blockHash, 32)) {
|
|
|
|
logger.throwArgumentError("invalid blockHash", "blockHash", blockHash);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
super({
|
|
|
|
_isForkEvent: true,
|
|
|
|
_isBlockForkEvent: true,
|
|
|
|
expiry: (expiry || 0),
|
2020-05-20 23:50:35 -04:00
|
|
|
blockHash: blockHash
|
2019-06-10 22:25:46 -04:00
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TransactionForkEvent extends ForkEvent {
|
|
|
|
readonly hash: string;
|
|
|
|
|
2020-01-07 19:47:55 -05:00
|
|
|
readonly _isTransactionOrderForkEvent?: boolean;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
constructor(hash: string, expiry?: number) {
|
|
|
|
if (!isHexString(hash, 32)) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid transaction hash", "hash", hash);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
super({
|
|
|
|
_isForkEvent: true,
|
|
|
|
_isTransactionForkEvent: true,
|
|
|
|
expiry: (expiry || 0),
|
|
|
|
hash: hash
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TransactionOrderForkEvent extends ForkEvent {
|
|
|
|
readonly beforeHash: string;
|
|
|
|
readonly afterHash: string;
|
|
|
|
|
|
|
|
constructor(beforeHash: string, afterHash: string, expiry?: number) {
|
|
|
|
if (!isHexString(beforeHash, 32)) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid transaction hash", "beforeHash", beforeHash);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
if (!isHexString(afterHash, 32)) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid transaction hash", "afterHash", afterHash);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
super({
|
|
|
|
_isForkEvent: true,
|
|
|
|
_isTransactionOrderForkEvent: true,
|
|
|
|
expiry: (expiry || 0),
|
|
|
|
beforeHash: beforeHash,
|
|
|
|
afterHash: afterHash
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type EventType = string | Array<string | Array<string>> | EventFilter | ForkEvent;
|
|
|
|
|
|
|
|
export type Listener = (...args: Array<any>) => void;
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// Exported Abstracts
|
|
|
|
|
|
|
|
export abstract class Provider implements OnceBlockable {
|
|
|
|
|
|
|
|
// Network
|
|
|
|
abstract getNetwork(): Promise<Network>;
|
|
|
|
|
|
|
|
// Latest State
|
|
|
|
abstract getBlockNumber(): Promise<number>;
|
|
|
|
abstract getGasPrice(): Promise<BigNumber>;
|
|
|
|
|
|
|
|
// Account
|
|
|
|
abstract getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber>;
|
|
|
|
abstract getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number>;
|
|
|
|
abstract getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> ;
|
|
|
|
abstract getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
|
|
|
|
|
|
|
|
// Execution
|
|
|
|
abstract sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
|
2020-06-01 04:46:37 -04:00
|
|
|
abstract call(transaction: Deferrable<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
|
|
|
|
abstract estimateGas(transaction: Deferrable<TransactionRequest>): Promise<BigNumber>;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// Queries
|
|
|
|
abstract getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block>;
|
|
|
|
abstract getBlockWithTransactions(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<BlockWithTransactions>;
|
|
|
|
abstract getTransaction(transactionHash: string): Promise<TransactionResponse>;
|
|
|
|
abstract getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>;
|
|
|
|
|
|
|
|
// Bloom-filter Queries
|
|
|
|
abstract getLogs(filter: Filter): Promise<Array<Log>>;
|
|
|
|
|
|
|
|
// ENS
|
|
|
|
abstract resolveName(name: string | Promise<string>): Promise<string>;
|
|
|
|
abstract lookupAddress(address: string | Promise<string>): Promise<string>;
|
|
|
|
|
|
|
|
// Event Emitter (ish)
|
|
|
|
abstract on(eventName: EventType, listener: Listener): Provider;
|
|
|
|
abstract once(eventName: EventType, listener: Listener): Provider;
|
|
|
|
abstract emit(eventName: EventType, ...args: Array<any>): boolean
|
|
|
|
abstract listenerCount(eventName?: EventType): number;
|
|
|
|
abstract listeners(eventName?: EventType): Array<Listener>;
|
|
|
|
abstract off(eventName: EventType, listener?: Listener): Provider;
|
|
|
|
abstract removeAllListeners(eventName?: EventType): Provider;
|
|
|
|
|
|
|
|
// Alias for "on"
|
|
|
|
addListener(eventName: EventType, listener: Listener): Provider {
|
|
|
|
return this.on(eventName, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alias for "off"
|
|
|
|
removeListener(eventName: EventType, listener: Listener): Provider {
|
|
|
|
return this.off(eventName, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: This *could* be implemented here, but would pull in events...
|
2020-02-03 23:05:04 -05:00
|
|
|
abstract waitForTransaction(transactionHash: string, confirmations?: number, timeout?: number): Promise<TransactionReceipt>;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
readonly _isProvider: boolean;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
constructor() {
|
2019-08-03 01:05:39 -04:00
|
|
|
logger.checkAbstract(new.target, Provider);
|
2019-06-10 22:25:46 -04:00
|
|
|
defineReadOnly(this, "_isProvider", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isProvider(value: any): value is Provider {
|
|
|
|
return !!(value && value._isProvider);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
static getResolver(network: Network, callable: CallTransactionable, namehash: string): string {
|
|
|
|
// No ENS...
|
|
|
|
if (!network.ensAddress) {
|
|
|
|
errors.throwError(
|
|
|
|
"network does support ENS",
|
|
|
|
errors.UNSUPPORTED_OPERATION,
|
|
|
|
{ operation: "ENS", network: network.name }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a namehash
|
|
|
|
if (!isHexString(namehash, 32)) {
|
|
|
|
errors.throwArgumentError("invalid name hash", "namehash", namehash);
|
|
|
|
}
|
|
|
|
|
|
|
|
// keccak256("resolver(bytes32)")
|
|
|
|
let data = "0x0178b8bf" + namehash.substring(2);
|
|
|
|
let transaction = { to: network.ensAddress, data: data };
|
|
|
|
|
|
|
|
return provider.call(transaction).then((data) => {
|
|
|
|
return provider.formatter.callAddress(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static resolveNamehash(network: Network, callable: CallTransactionable, namehash: string): string {
|
|
|
|
return this.getResolver(network, callable, namehash).then((resolverAddress) => {
|
|
|
|
if (!resolverAddress) { return null; }
|
|
|
|
|
|
|
|
// keccak256("addr(bytes32)")
|
|
|
|
let data = "0x3b3b57de" + namehash(name).substring(2);
|
|
|
|
let transaction = { to: resolverAddress, data: data };
|
|
|
|
return callable.call(transaction).then((data) => {
|
|
|
|
return this.formatter.callAddress(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|