MyCrypto/common/libs/nodes/rpc/requests.ts

73 lines
1.6 KiB
TypeScript

import { Token } from 'config';
import ERC20 from 'libs/erc20';
import {
CallRequest,
EstimateGasRequest,
GetBalanceRequest,
GetTokenBalanceRequest,
GetTransactionCountRequest,
SendRawTxRequest,
GetCurrentBlockRequest
} from './types';
import { hexEncodeData } from './utils';
import { TxObj } from '../INode';
export default class RPCRequests {
public getNetVersion() {
return { method: 'net_version' };
}
public sendRawTx(signedTx: string): SendRawTxRequest | any {
return {
method: 'eth_sendRawTransaction',
params: [signedTx]
};
}
public estimateGas(transaction): EstimateGasRequest | any {
return {
method: 'eth_estimateGas',
params: [transaction]
};
}
public getBalance(address: string): GetBalanceRequest | any {
return {
method: 'eth_getBalance',
params: [hexEncodeData(address), 'pending']
};
}
public ethCall(txObj: TxObj): CallRequest | any {
return {
method: 'eth_call',
params: [txObj, 'pending']
};
}
public getTransactionCount(address: string): GetTransactionCountRequest | any {
return {
method: 'eth_getTransactionCount',
params: [address, 'pending']
};
}
public getTokenBalance(address: string, token: Token): GetTokenBalanceRequest | any {
return {
method: 'eth_call',
params: [
{
to: token.address,
data: ERC20.balanceOf.encodeInput({ _owner: address })
},
'pending'
]
};
}
public getCurrentBlock(): GetCurrentBlockRequest | any {
return {
method: 'eth_blockNumber'
};
}
}