jcc-ethereum-utils
Version:
Toolkit of crossing chain from Ethereum to SWTC chain
318 lines (317 loc) • 8.43 kB
TypeScript
import { IWalletModel } from "jcc_wallet/lib/types";
import { Contract } from "web3-eth-contract";
import { ContractAbi } from "web3-types";
/**
* Toolkit of Ethereum
*
* @export
* @class Ethereum
*/
export default class Ethereum {
/**
* instance of web3
*
* @protected
* @type {*}
* @memberof Ethereum
*/
protected _web3: any;
/**
* http node
*
* @private
* @type {string}
* @memberof Ethereum
*/
private _node;
/**
* gas limit
*
* @private
* @type {number}
* @memberof Ethereum
*/
private _gasLimit;
/**
* min gas price
*
* @private
* @type {number}
* @memberof Ethereum
*/
private _minGasPrice;
/**
* default gas price
*
* @private
* @type {number}
* @memberof Ethereum
*/
private _defaultGasPrice;
/**
* min fee per gas
*
* @private
* @type {number}
* @memberof Ethereum
*/
private _minFeePerGas;
/**
* min priority fee per gas
*
* @private
* @type {number}
* @memberof Ethereum
*/
private _minPriorityFeePerGas;
/**
* Creates an instance of Ethereum
* @param {string} node http node
* @memberof Ethereum
*/
constructor(node: string);
/**
* set & get _gasLimit
*
* @type {number}
* @memberof Ethereum
*/
get gasLimit(): number;
set gasLimit(gas: number);
/**
* set & get _minGasPrice
*
* @type {number}
* @memberof Ethereum
*/
get minGasPrice(): number;
set minGasPrice(value: number);
/**
* set & get _defaultGasPrice
*
* @memberof Ethereum
*/
set defaultGasPrice(v: number);
get defaultGasPrice(): number;
/**
* set & get _minFeePerGas
*
* @type {number}
* @memberof Ethereum
*/
set minFeePerGas(v: number);
get minFeePerGas(): number;
/**
* set & get _minPriorityFeePerGas
*
* @type {number}
* @memberof Ethereum
*/
set minPriorityFeePerGas(v: number);
get minPriorityFeePerGas(): number;
/**
* validate ethereum address
*
* @static
* @param {string} address ethereum address
* @returns {boolean} return true if the address is valid
* @memberof Ethereum
*/
static isValidAddress(address: string): boolean;
/**
* validate ethereum secret
*
* @static
* @param {string} secret ethereum secret
* @returns {boolean} return true if the secret is valid
* @memberof Ethereum
*/
static isValidSecret(secret: string): boolean;
/**
* retrieve ethereum address via secret
*
* @static
* @param {string} secret ethereum secret
* @returns {string} return address if the secret is valid, otherwise return null
* @memberof Ethereum
*/
static getAddress(secret: string): string;
/**
* create ethereum wallet
*
* @static
* @returns {IWalletModel}
* @memberof Ethereum
*/
static createWallet(): IWalletModel;
/**
* prefix `0x` if the given string not start with `0x`
*
* @static
* @param {string} str
* @returns {string}
* @memberof Ethereum
*/
static prefix0x(str: string): string;
/**
* filter `0x` if the given string starts with `0x`
*
* @static
* @param {string} str
* @returns {string}
* @memberof Ethereum
*/
static filter0x(str: string): string;
/**
* init instance of web3
*
* @memberof Ethereum
*/
initWeb3(): void;
/**
* destroy instance of web3
*
* @memberof Ethereum
*/
destroyWeb3(): void;
/**
* get instance of web3
*
* @memberof Ethereum
*/
getWeb3(): any;
/**
* request info of block
*
* @param {number|string} block number or string latest
* @returns {Promise<any>} resolve null if request failed, return block info
* @memberof Ethereum
*/
getBlock(block: number | string): Promise<any>;
/**
* request balance of ether
*
* @param {string} address ethereum address
* @returns {Promise<string>} resolve "0" if request failed
* @memberof Ethereum
*/
getBalance(address: string): Promise<string>;
/**
* request current gas price
*
* @returns {Promise<number>} resolve gas price if success
* @memberof Ethereum
*/
getGasPrice(): Promise<number>;
/**
* request current fee data
*
* @returns {Promise<IFeeData>} resolve gas price if success
* @memberof Ethereum
*/
getFeeData(): Promise<IFeeData>;
/**
* request nonce
*
* @param {string} address ethereum address
* @returns {Promise<number>} resolve nonce if success
* @memberof Ethereum
*/
getNonce(address: string): Promise<number>;
/**
* check if has pending transaction
*
* @param {string} address
* @returns {Promise<boolean>} resolve true if has pending transaction
* @memberof Ethereum
*/
hasPendingTransactions(address: string): Promise<boolean>;
/**
* check if has pending block transaction
*
* @param {string} address
* @returns {Promise<boolean>} resolve true if has pending block transaction
* @memberof Ethereum
*/
hasPendingBlockTransactions(address: string): Promise<boolean>;
/**
* format transaction info
*
* @param {string} from sender address
* @param {string} to destination address
* @param {number} nonce nonce
* @param {number} gasLimit gas limit
* @param {number} gasPrice gas price
* @param {string} value value
* @param {string} calldata call data
* @returns {EthereumTransaction}
* @memberof Ethereum
*/
getTx(from: string, to: string, nonce: number, gasLimit: number, gasPrice: number, value: string, calldata: string): EthereumTransaction;
/**
* format EIP1559 transaction info
*
* @param {string} from sender address
* @param {string} to destination address
* @param {number} nonce nonce
* @param {number} gasLimit gas limit
* @param {number} maxFeePerGas max fee per gas
* @param {number} maxPriorityFeePerGas max priority fee per gas
* @param {string} value value
* @param {string} calldata call data
* @returns {EthereumTransaction}
* @memberof Ethereum
*/
get1559Tx(from: string, to: string, nonce: number, gasLimit: number, maxFeePerGas: number, maxPriorityFeePerGas: number, value: string, calldata: string): EthereumTransaction;
/**
* sign transaction with ethereum secret
*
* @param {EthereumTransaction} tx transaction
* @param {string} secret ethereum secret
* @returns {Promise<string>} return signed info
* @memberof Ethereum
*/
signTransaction(tx: EthereumTransaction, secret: string): Promise<string>;
/**
* send signed transaction
*
* @param {string} sign
* @returns {Promise<string>} resolve hash if success
* @memberof Ethereum
*/
sendSignedTransaction(sign: string): Promise<string>;
/**
* get transaction
*
* @param {string} hash transaction hash
* @returns {any} null or transaction object
* @memberof Ethereum
*/
getTransaction(hash: string): Promise<any>;
/**
* get transaction receipt
*
* @param {string} hash transaction hash
* @returns {any} null or transaction receipt object
* @memberof Ethereum
*/
getTransactionReceipt(hash: string): Promise<any>;
/**
* init instance of ethereum or erc20 contract
*
* @param {abitItem} abi definition of ethereum abi or erc20 abi
* @param {string} address
* @returns {Contract} return instance of ethereum or erc20 contract
* @memberof Ethereum
*/
contract(abi: ContractAbi, address: string): Contract<ContractAbi>;
/**
* check instance of contract if initialied
*
* @param {Contract} contract current contract instance
* @param {string} address current contract address
* @returns {boolean} return true if initialied
* @memberof Ethereum
*/
contractInitialied(contract: Contract<ContractAbi> | null, address: string): boolean;
}