Abstracted JSON-RPC parameter generation for others to use.
This commit is contained in:
parent
390497f389
commit
030f65e66c
@ -330,28 +330,76 @@ export class JsonRpcProvider extends BaseProvider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
perform(method: string, params: any): Promise<any> {
|
prepareRequest(method: string, params: any): [ string, Array<any> ] {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "getBlockNumber":
|
case "getBlockNumber":
|
||||||
return this.send("eth_blockNumber", []);
|
return [ "eth_blockNumber", [] ];
|
||||||
|
|
||||||
case "getGasPrice":
|
case "getGasPrice":
|
||||||
return this.send("eth_gasPrice", []);
|
return [ "eth_gasPrice", [] ];
|
||||||
|
|
||||||
case "getBalance":
|
case "getBalance":
|
||||||
return this.send("eth_getBalance", [ getLowerCase(params.address), params.blockTag ]);
|
return [ "eth_getBalance", [ getLowerCase(params.address), params.blockTag ] ];
|
||||||
|
|
||||||
case "getTransactionCount":
|
case "getTransactionCount":
|
||||||
return this.send("eth_getTransactionCount", [ getLowerCase(params.address), params.blockTag ]);
|
return [ "eth_getTransactionCount", [ getLowerCase(params.address), params.blockTag ] ];
|
||||||
|
|
||||||
case "getCode":
|
case "getCode":
|
||||||
return this.send("eth_getCode", [ getLowerCase(params.address), params.blockTag ]);
|
return [ "eth_getCode", [ getLowerCase(params.address), params.blockTag ] ];
|
||||||
|
|
||||||
case "getStorageAt":
|
case "getStorageAt":
|
||||||
return this.send("eth_getStorageAt", [ getLowerCase(params.address), params.position, params.blockTag ]);
|
return [ "eth_getStorageAt", [ getLowerCase(params.address), params.position, params.blockTag ] ];
|
||||||
|
|
||||||
case "sendTransaction":
|
case "sendTransaction":
|
||||||
return this.send("eth_sendRawTransaction", [ params.signedTransaction ]).catch((error) => {
|
return [ "eth_sendRawTransaction", [ params.signedTransaction ] ]
|
||||||
|
|
||||||
|
case "getBlock":
|
||||||
|
if (params.blockTag) {
|
||||||
|
return [ "eth_getBlockByNumber", [ params.blockTag, !!params.includeTransactions ] ];
|
||||||
|
} else if (params.blockHash) {
|
||||||
|
return [ "eth_getBlockByHash", [ params.blockHash, !!params.includeTransactions ] ];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
|
case "getTransaction":
|
||||||
|
return [ "eth_getTransactionByHash", [ params.transactionHash ] ];
|
||||||
|
|
||||||
|
case "getTransactionReceipt":
|
||||||
|
return [ "eth_getTransactionReceipt", [ params.transactionHash ] ];
|
||||||
|
|
||||||
|
case "call": {
|
||||||
|
const hexlifyTransaction = getStatic<(t: TransactionRequest, a?: { [key: string]: boolean }) => { [key: string]: string }>(this.constructor, "hexlifyTransaction");
|
||||||
|
return [ "eth_call", [ hexlifyTransaction(params.transaction, { from: true }), params.blockTag ] ];
|
||||||
|
}
|
||||||
|
|
||||||
|
case "estimateGas": {
|
||||||
|
const hexlifyTransaction = getStatic<(t: TransactionRequest, a?: { [key: string]: boolean }) => { [key: string]: string }>(this.constructor, "hexlifyTransaction");
|
||||||
|
return [ "eth_estimateGas", [ hexlifyTransaction(params.transaction, { from: true }) ] ];
|
||||||
|
}
|
||||||
|
|
||||||
|
case "getLogs":
|
||||||
|
if (params.filter && params.filter.address != null) {
|
||||||
|
params.filter.address = getLowerCase(params.filter.address);
|
||||||
|
}
|
||||||
|
return [ "eth_getLogs", [ params.filter ] ];
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
perform(method: string, params: any): Promise<any> {
|
||||||
|
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) {
|
if (error.responseText) {
|
||||||
// "insufficient funds for gas * price + value"
|
// "insufficient funds for gas * price + value"
|
||||||
if (error.responseText.indexOf("insufficient funds") > 0) {
|
if (error.responseText.indexOf("insufficient funds") > 0) {
|
||||||
@ -368,42 +416,9 @@ export class JsonRpcProvider extends BaseProvider {
|
|||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
|
|
||||||
case "getBlock":
|
|
||||||
if (params.blockTag) {
|
|
||||||
return this.send("eth_getBlockByNumber", [ params.blockTag, !!params.includeTransactions ]);
|
|
||||||
} else if (params.blockHash) {
|
|
||||||
return this.send("eth_getBlockByHash", [ params.blockHash, !!params.includeTransactions ]);
|
|
||||||
}
|
|
||||||
return logger.throwArgumentError("invalid block tag or block hash", "params", params);
|
|
||||||
|
|
||||||
case "getTransaction":
|
|
||||||
return this.send("eth_getTransactionByHash", [ params.transactionHash ]);
|
|
||||||
|
|
||||||
case "getTransactionReceipt":
|
|
||||||
return this.send("eth_getTransactionReceipt", [ params.transactionHash ]);
|
|
||||||
|
|
||||||
case "call": {
|
|
||||||
const hexlifyTransaction = getStatic<(t: TransactionRequest, a?: { [key: string]: boolean }) => { [key: string]: string }>(this.constructor, "hexlifyTransaction");
|
|
||||||
return this.send("eth_call", [ hexlifyTransaction(params.transaction, { from: true }), params.blockTag ]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "estimateGas": {
|
return this.send(args[0], args[1])
|
||||||
const hexlifyTransaction = getStatic<(t: TransactionRequest, a?: { [key: string]: boolean }) => { [key: string]: string }>(this.constructor, "hexlifyTransaction");
|
|
||||||
return this.send("eth_estimateGas", [ hexlifyTransaction(params.transaction, { from: true }) ]);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "getLogs":
|
|
||||||
if (params.filter && params.filter.address != null) {
|
|
||||||
params.filter.address = getLowerCase(params.filter.address);
|
|
||||||
}
|
|
||||||
return this.send("eth_getLogs", [ params.filter ]);
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_startEvent(event: Event): void {
|
_startEvent(event: Event): void {
|
||||||
@ -466,8 +481,11 @@ export class JsonRpcProvider extends BaseProvider {
|
|||||||
// - gasLimit => gas
|
// - gasLimit => gas
|
||||||
// - All values hexlified
|
// - All values hexlified
|
||||||
// - All numeric values zero-striped
|
// - All numeric values zero-striped
|
||||||
|
// - All addresses are lowercased
|
||||||
// NOTE: This allows a TransactionRequest, but all values should be resolved
|
// NOTE: This allows a TransactionRequest, but all values should be resolved
|
||||||
// before this is called
|
// before this is called
|
||||||
|
// @TODO: This will likely be removed in future versions and prepareRequest
|
||||||
|
// will be the preferred method for this.
|
||||||
static hexlifyTransaction(transaction: TransactionRequest, allowExtra?: { [key: string]: boolean }): { [key: string]: string } {
|
static hexlifyTransaction(transaction: TransactionRequest, allowExtra?: { [key: string]: boolean }): { [key: string]: string } {
|
||||||
// Check only allowed properties are given
|
// Check only allowed properties are given
|
||||||
const allowed = shallowCopy(allowedTransactionKeys);
|
const allowed = shallowCopy(allowedTransactionKeys);
|
||||||
|
Loading…
Reference in New Issue
Block a user