2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2019-11-20 18:57:38 +09:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Signer } from "@ethersproject/abstract-signer";
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { hexlify, hexValue } from "@ethersproject/bytes";
|
2019-11-20 18:57:38 +09:00
|
|
|
import { checkProperties, deepCopy, defineReadOnly, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { toUtf8Bytes } from "@ethersproject/strings";
|
|
|
|
import { fetchJson, poll } from "@ethersproject/web";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
import { BaseProvider } from "./base-provider";
|
2019-05-14 18:48:48 -04:00
|
|
|
function timer(timeout) {
|
|
|
|
return new Promise(function (resolve) {
|
2020-05-03 17:53:58 -04:00
|
|
|
setTimeout(resolve, timeout);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function getResult(payload) {
|
|
|
|
if (payload.error) {
|
|
|
|
// @TODO: not any
|
2020-06-03 03:47:17 -04:00
|
|
|
const error = new Error(payload.error.message);
|
2019-05-14 18:48:48 -04:00
|
|
|
error.code = payload.error.code;
|
|
|
|
error.data = payload.error.data;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return payload.result;
|
|
|
|
}
|
|
|
|
function getLowerCase(value) {
|
|
|
|
if (value) {
|
|
|
|
return value.toLowerCase();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
const _constructorGuard = {};
|
|
|
|
export class JsonRpcSigner extends Signer {
|
|
|
|
constructor(constructorGuard, provider, addressOrIndex) {
|
|
|
|
logger.checkNew(new.target, JsonRpcSigner);
|
|
|
|
super();
|
2019-05-14 18:48:48 -04:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
throw new Error("do not call the JsonRpcSigner constructor directly; use provider.getSigner");
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "provider", provider);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (addressOrIndex == null) {
|
|
|
|
addressOrIndex = 0;
|
|
|
|
}
|
2019-06-01 14:19:43 -04:00
|
|
|
if (typeof (addressOrIndex) === "string") {
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "_address", this.provider.formatter.address(addressOrIndex));
|
|
|
|
defineReadOnly(this, "_index", null);
|
2019-06-01 14:19:43 -04:00
|
|
|
}
|
|
|
|
else if (typeof (addressOrIndex) === "number") {
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "_index", addressOrIndex);
|
|
|
|
defineReadOnly(this, "_address", null);
|
2019-06-01 14:19:43 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid address or index", "addressOrIndex", addressOrIndex);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
connect(provider) {
|
|
|
|
return logger.throwError("cannot alter JSON-RPC Signer connection", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-14 18:48:48 -04:00
|
|
|
operation: "connect"
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
connectUnchecked() {
|
2019-05-14 18:48:48 -04:00
|
|
|
return new UncheckedJsonRpcSigner(_constructorGuard, this.provider, this._address || this._index);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
getAddress() {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (this._address) {
|
|
|
|
return Promise.resolve(this._address);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.provider.send("eth_accounts", []).then((accounts) => {
|
|
|
|
if (accounts.length <= this._index) {
|
|
|
|
logger.throwError("unknown account #" + this._index, Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-08-02 02:10:58 -04:00
|
|
|
operation: "getAddress"
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.provider.formatter.address(accounts[this._index]);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
sendUncheckedTransaction(transaction) {
|
|
|
|
transaction = shallowCopy(transaction);
|
2020-06-03 03:47:17 -04:00
|
|
|
const fromAddress = this.getAddress().then((address) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (address) {
|
|
|
|
address = address.toLowerCase();
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
});
|
|
|
|
// The JSON-RPC for eth_sendTransaction uses 90000 gas; if the user
|
|
|
|
// wishes to use this, it is easy to specify explicitly, otherwise
|
|
|
|
// we look it up for them.
|
|
|
|
if (transaction.gasLimit == null) {
|
2020-06-03 03:47:17 -04:00
|
|
|
const estimate = shallowCopy(transaction);
|
2019-05-14 18:48:48 -04:00
|
|
|
estimate.from = fromAddress;
|
|
|
|
transaction.gasLimit = this.provider.estimateGas(estimate);
|
|
|
|
}
|
2020-06-03 03:47:17 -04:00
|
|
|
return resolveProperties({
|
|
|
|
tx: resolveProperties(transaction),
|
|
|
|
sender: fromAddress
|
|
|
|
}).then(({ tx, sender }) => {
|
|
|
|
if (tx.from != null) {
|
|
|
|
if (tx.from.toLowerCase() !== sender) {
|
|
|
|
logger.throwArgumentError("from address mismatch", "transaction", transaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tx.from = sender;
|
|
|
|
}
|
|
|
|
const hexTx = this.provider.constructor.hexlifyTransaction(tx, { from: true });
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.provider.send("eth_sendTransaction", [hexTx]).then((hash) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
return hash;
|
2019-08-25 02:39:20 -04:00
|
|
|
}, (error) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (error.responseText) {
|
|
|
|
// See: JsonRpcProvider.sendTransaction (@TODO: Expose a ._throwError??)
|
|
|
|
if (error.responseText.indexOf("insufficient funds") >= 0) {
|
2019-08-25 02:39:20 -04:00
|
|
|
logger.throwError("insufficient funds", Logger.errors.INSUFFICIENT_FUNDS, {
|
2019-05-14 18:48:48 -04:00
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (error.responseText.indexOf("nonce too low") >= 0) {
|
2019-08-25 02:39:20 -04:00
|
|
|
logger.throwError("nonce has already been used", Logger.errors.NONCE_EXPIRED, {
|
2019-05-14 18:48:48 -04:00
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (error.responseText.indexOf("replacement transaction underpriced") >= 0) {
|
2019-08-25 02:39:20 -04:00
|
|
|
logger.throwError("replacement fee too low", Logger.errors.REPLACEMENT_UNDERPRICED, {
|
2019-05-14 18:48:48 -04:00
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
signTransaction(transaction) {
|
|
|
|
return logger.throwError("signing transactions is unsupported", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-14 18:48:48 -04:00
|
|
|
operation: "signTransaction"
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
sendTransaction(transaction) {
|
|
|
|
return this.sendUncheckedTransaction(transaction).then((hash) => {
|
|
|
|
return poll(() => {
|
|
|
|
return this.provider.getTransaction(hash).then((tx) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (tx === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.provider._wrapTransaction(tx, hash);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}, { onceBlock: this.provider }).catch((error) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
error.transactionHash = hash;
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
signMessage(message) {
|
2020-06-03 03:47:17 -04:00
|
|
|
const data = ((typeof (message) === "string") ? toUtf8Bytes(message) : message);
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.getAddress().then((address) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.provider.send("eth_sign", [address.toLowerCase(), hexlify(data)]);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
unlock(password) {
|
2020-06-03 03:47:17 -04:00
|
|
|
const provider = this.provider;
|
2019-05-14 18:48:48 -04:00
|
|
|
return this.getAddress().then(function (address) {
|
|
|
|
return provider.send("personal_unlockAccount", [address.toLowerCase(), password, null]);
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
class UncheckedJsonRpcSigner extends JsonRpcSigner {
|
|
|
|
sendTransaction(transaction) {
|
|
|
|
return this.sendUncheckedTransaction(transaction).then((hash) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
|
|
|
hash: hash,
|
|
|
|
nonce: null,
|
|
|
|
gasLimit: null,
|
|
|
|
gasPrice: null,
|
|
|
|
data: null,
|
|
|
|
value: null,
|
|
|
|
chainId: null,
|
|
|
|
confirmations: 0,
|
|
|
|
from: null,
|
2019-08-25 02:39:20 -04:00
|
|
|
wait: (confirmations) => { return this.provider.waitForTransaction(hash, confirmations); }
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const allowedTransactionKeys = {
|
2019-05-14 18:48:48 -04:00
|
|
|
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
export class JsonRpcProvider extends BaseProvider {
|
|
|
|
constructor(url, network) {
|
|
|
|
logger.checkNew(new.target, JsonRpcProvider);
|
2020-05-08 15:28:55 -04:00
|
|
|
let networkOrReady = network;
|
|
|
|
// The network is unknown, query the JSON-RPC for it
|
|
|
|
if (networkOrReady == null) {
|
|
|
|
networkOrReady = new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.detectNetwork().then((network) => {
|
|
|
|
resolve(network);
|
|
|
|
}, (error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}, 0);
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-08 15:28:55 -04:00
|
|
|
super(networkOrReady);
|
2019-05-14 18:48:48 -04:00
|
|
|
// Default URL
|
|
|
|
if (!url) {
|
2020-03-12 19:14:50 +01:00
|
|
|
url = getStatic(this.constructor, "defaultUrl")();
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (typeof (url) === "string") {
|
2020-05-08 15:28:55 -04:00
|
|
|
defineReadOnly(this, "connection", Object.freeze({
|
2019-05-14 18:48:48 -04:00
|
|
|
url: url
|
2020-05-08 15:28:55 -04:00
|
|
|
}));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-05-08 15:28:55 -04:00
|
|
|
defineReadOnly(this, "connection", Object.freeze(shallowCopy(url)));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
this._nextId = 42;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-03-12 19:14:50 +01:00
|
|
|
static defaultUrl() {
|
2020-04-27 06:31:43 -04:00
|
|
|
return "http:/\/localhost:8545";
|
2020-03-12 19:14:50 +01:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
detectNetwork() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
yield timer(0);
|
|
|
|
let chainId = null;
|
|
|
|
try {
|
|
|
|
chainId = yield this.send("eth_chainId", []);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
try {
|
|
|
|
chainId = yield this.send("net_version", []);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
if (chainId != null) {
|
|
|
|
const getNetwork = getStatic(this.constructor, "getNetwork");
|
|
|
|
try {
|
|
|
|
return getNetwork(BigNumber.from(chainId).toNumber());
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return logger.throwError("could not detect network", Logger.errors.NETWORK_ERROR, {
|
|
|
|
chainId: chainId,
|
2020-06-13 21:39:36 -04:00
|
|
|
event: "invalidNetwork",
|
2020-05-03 17:53:58 -04:00
|
|
|
serverError: error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 21:39:36 -04:00
|
|
|
return logger.throwError("could not detect network", Logger.errors.NETWORK_ERROR, {
|
|
|
|
event: "noNetwork"
|
|
|
|
});
|
2020-05-03 17:53:58 -04:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
getSigner(addressOrIndex) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
getUncheckedSigner(addressOrIndex) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return this.getSigner(addressOrIndex).connectUnchecked();
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
listAccounts() {
|
|
|
|
return this.send("eth_accounts", []).then((accounts) => {
|
|
|
|
return accounts.map((a) => this.formatter.address(a));
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
send(method, params) {
|
2020-06-03 03:47:17 -04:00
|
|
|
const request = {
|
2019-05-14 18:48:48 -04:00
|
|
|
method: method,
|
|
|
|
params: params,
|
2019-05-24 18:15:42 -04:00
|
|
|
id: (this._nextId++),
|
2019-05-14 18:48:48 -04:00
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
2019-08-02 02:10:58 -04:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
2019-08-25 02:39:20 -04:00
|
|
|
request: deepCopy(request),
|
2019-08-02 02:10:58 -04:00
|
|
|
provider: this
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
return fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
|
|
|
|
this.emit("debug", {
|
2019-08-02 02:10:58 -04:00
|
|
|
action: "response",
|
2019-05-14 18:48:48 -04:00
|
|
|
request: request,
|
|
|
|
response: result,
|
2019-08-25 02:39:20 -04:00
|
|
|
provider: this
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
return result;
|
2020-02-26 10:06:48 +00:00
|
|
|
}, (error) => {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
error: error,
|
|
|
|
request: request,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
throw error;
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-03-21 12:48:22 -04:00
|
|
|
prepareRequest(method, params) {
|
2019-05-14 18:48:48 -04:00
|
|
|
switch (method) {
|
|
|
|
case "getBlockNumber":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_blockNumber", []];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getGasPrice":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_gasPrice", []];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getBalance":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getBalance", [getLowerCase(params.address), params.blockTag]];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getTransactionCount":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getTransactionCount", [getLowerCase(params.address), params.blockTag]];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getCode":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getCode", [getLowerCase(params.address), params.blockTag]];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getStorageAt":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getStorageAt", [getLowerCase(params.address), params.position, params.blockTag]];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "sendTransaction":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_sendRawTransaction", [params.signedTransaction]];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getBlock":
|
|
|
|
if (params.blockTag) {
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getBlockByNumber", [params.blockTag, !!params.includeTransactions]];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else if (params.blockHash) {
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getBlockByHash", [params.blockHash, !!params.includeTransactions]];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-03-21 12:48:22 -04:00
|
|
|
return null;
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getTransaction":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getTransactionByHash", [params.transactionHash]];
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getTransactionReceipt":
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getTransactionReceipt", [params.transactionHash]];
|
2019-11-20 18:57:38 +09:00
|
|
|
case "call": {
|
|
|
|
const hexlifyTransaction = getStatic(this.constructor, "hexlifyTransaction");
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_call", [hexlifyTransaction(params.transaction, { from: true }), params.blockTag]];
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
|
|
|
case "estimateGas": {
|
|
|
|
const hexlifyTransaction = getStatic(this.constructor, "hexlifyTransaction");
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_estimateGas", [hexlifyTransaction(params.transaction, { from: true })]];
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
case "getLogs":
|
|
|
|
if (params.filter && params.filter.address != null) {
|
|
|
|
params.filter.address = getLowerCase(params.filter.address);
|
|
|
|
}
|
2020-03-21 12:48:22 -04:00
|
|
|
return ["eth_getLogs", [params.filter]];
|
2019-05-14 18:48:48 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-03-21 12:48:22 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
perform(method, params) {
|
|
|
|
const args = this.prepareRequest(method, params);
|
|
|
|
if (args == null) {
|
|
|
|
logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
|
|
|
|
}
|
|
|
|
// We need a little extra logic to process errors from sendTransaction
|
|
|
|
if (method === "sendTransaction") {
|
|
|
|
return this.send(args[0], args[1]).catch((error) => {
|
|
|
|
if (error.responseText) {
|
|
|
|
// "insufficient funds for gas * price + value"
|
|
|
|
if (error.responseText.indexOf("insufficient funds") > 0) {
|
|
|
|
logger.throwError("insufficient funds", Logger.errors.INSUFFICIENT_FUNDS, {});
|
|
|
|
}
|
|
|
|
// "nonce too low"
|
|
|
|
if (error.responseText.indexOf("nonce too low") > 0) {
|
|
|
|
logger.throwError("nonce has already been used", Logger.errors.NONCE_EXPIRED, {});
|
|
|
|
}
|
|
|
|
// "replacement transaction underpriced"
|
|
|
|
if (error.responseText.indexOf("replacement transaction underpriced") > 0) {
|
|
|
|
logger.throwError("replacement fee too low", Logger.errors.REPLACEMENT_UNDERPRICED, {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.send(args[0], args[1]);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-03-12 19:14:50 +01:00
|
|
|
_startEvent(event) {
|
|
|
|
if (event.tag === "pending") {
|
|
|
|
this._startPending();
|
|
|
|
}
|
|
|
|
super._startEvent(event);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
_startPending() {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (this._pendingFilter != null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-03 03:47:17 -04:00
|
|
|
const self = this;
|
|
|
|
const pendingFilter = this.send("eth_newPendingTransactionFilter", []);
|
2019-05-14 18:48:48 -04:00
|
|
|
this._pendingFilter = pendingFilter;
|
|
|
|
pendingFilter.then(function (filterId) {
|
|
|
|
function poll() {
|
|
|
|
self.send("eth_getFilterChanges", [filterId]).then(function (hashes) {
|
|
|
|
if (self._pendingFilter != pendingFilter) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let seq = Promise.resolve();
|
2019-05-14 18:48:48 -04:00
|
|
|
hashes.forEach(function (hash) {
|
|
|
|
// @TODO: This should be garbage collected at some point... How? When?
|
|
|
|
self._emitted["t:" + hash.toLowerCase()] = "pending";
|
|
|
|
seq = seq.then(function () {
|
|
|
|
return self.getTransaction(hash).then(function (tx) {
|
|
|
|
self.emit("pending", tx);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return seq.then(function () {
|
|
|
|
return timer(1000);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
if (self._pendingFilter != pendingFilter) {
|
|
|
|
self.send("eth_uninstallFilter", [filterId]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(function () { poll(); }, 0);
|
|
|
|
return null;
|
2019-08-25 02:39:20 -04:00
|
|
|
}).catch((error) => { });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
poll();
|
|
|
|
return filterId;
|
2019-08-25 02:39:20 -04:00
|
|
|
}).catch((error) => { });
|
|
|
|
}
|
2020-03-12 19:14:50 +01:00
|
|
|
_stopEvent(event) {
|
|
|
|
if (event.tag === "pending" && this.listenerCount("pending") === 0) {
|
|
|
|
this._pendingFilter = null;
|
|
|
|
}
|
|
|
|
super._stopEvent(event);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// Convert an ethers.js transaction into a JSON-RPC transaction
|
|
|
|
// - gasLimit => gas
|
|
|
|
// - All values hexlified
|
|
|
|
// - All numeric values zero-striped
|
2020-03-21 12:48:22 -04:00
|
|
|
// - All addresses are lowercased
|
2019-05-14 18:48:48 -04:00
|
|
|
// NOTE: This allows a TransactionRequest, but all values should be resolved
|
|
|
|
// before this is called
|
2020-03-21 12:48:22 -04:00
|
|
|
// @TODO: This will likely be removed in future versions and prepareRequest
|
|
|
|
// will be the preferred method for this.
|
2019-08-25 02:39:20 -04:00
|
|
|
static hexlifyTransaction(transaction, allowExtra) {
|
2019-05-14 18:48:48 -04:00
|
|
|
// Check only allowed properties are given
|
2019-11-20 18:57:38 +09:00
|
|
|
const allowed = shallowCopy(allowedTransactionKeys);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (allowExtra) {
|
2020-06-03 03:47:17 -04:00
|
|
|
for (const key in allowExtra) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (allowExtra[key]) {
|
|
|
|
allowed[key] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
checkProperties(transaction, allowed);
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = {};
|
2019-05-14 18:48:48 -04:00
|
|
|
// Some nodes (INFURA ropsten; INFURA mainnet is fine) do not like leading zeros.
|
|
|
|
["gasLimit", "gasPrice", "nonce", "value"].forEach(function (key) {
|
|
|
|
if (transaction[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
const value = hexValue(transaction[key]);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (key === "gasLimit") {
|
|
|
|
key = "gas";
|
|
|
|
}
|
|
|
|
result[key] = value;
|
|
|
|
});
|
|
|
|
["from", "to", "data"].forEach(function (key) {
|
|
|
|
if (transaction[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
result[key] = hexlify(transaction[key]);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
return result;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=json-rpc-provider.js.map
|