@xchainjs/xchain-evm
Version:
Genereic EVM client for XChainJS
294 lines (293 loc) • 14 kB
TypeScript
import { AssetInfo, BaseXChainClient, ExplorerProviders, FeeRates, Fees, Network, PreparedTx, Protocol, TxHash, TxHistoryParams, XChainClientParams } from '@xchainjs/xchain-client';
import { EvmOnlineDataProviders } from '@xchainjs/xchain-evm-providers';
import { Address, Asset, Chain, TokenAsset } from '@xchainjs/xchain-util';
import { Provider } from 'ethers';
import BigNumber from 'bignumber.js';
import { ApproveParams, Balance, CallParams, EstimateApproveParams, EstimateCallParams, EvmDefaults, FeesWithGasPricesAndLimits, GasPrices, ISigner, IsApprovedParams, Tx, TxParams, TxsPage } from '../types';
export interface EVMClient {
approve(params: ApproveParams): Promise<string>;
awaitTxConfirmed(hash: string): Promise<void>;
}
/**
* Parameters for configuring the EVM client.
*/
export type EVMClientParams = XChainClientParams & {
chain: Chain;
gasAsset: Asset;
gasAssetDecimals: number;
defaults: Record<Network, EvmDefaults>;
providers: Record<Network, Provider>;
explorerProviders: ExplorerProviders;
dataProviders: EvmOnlineDataProviders[];
signer?: ISigner;
};
/**
* Custom EVM client class.
*/
export declare class Client extends BaseXChainClient implements EVMClient {
readonly config: Omit<EVMClientParams, 'signer'>;
protected signer?: ISigner;
protected defaults: Record<Network, EvmDefaults>;
private cachedNetworkId;
/**
* Constructor for the EVM client.
* @param {EVMClientParams} params - Parameters for configuring the EVM client.
*/
constructor({ chain, gasAsset, gasAssetDecimals, defaults, network, feeBounds, providers, rootDerivationPaths, explorerProviders, dataProviders, signer, }: EVMClientParams);
/**
* Retrieves the Ethereum Provider interface.
* @returns {Provider} The current Ethereum Provider interface.
*/
getProvider(): Provider;
/**
* Retrieves the explorer URL based on the current network.
* @returns {string} The explorer URL for Ethereum based on the current network.
*/
getExplorerUrl(): string;
/**
* Retrieves asset information.
* @returns {AssetInfo} Asset information containing the asset and its decimal places.
*/
getAssetInfo(): AssetInfo;
/**
* Retrieves the explorer URL for a given address.
* @param {Address} address - The address to retrieve the explorer URL for.
* @returns {string} The explorer URL for the given address.
*/
getExplorerAddressUrl(address: Address): string;
/**
* Retrieves the explorer URL for a given transaction ID.
* @param {string} txID - The transaction ID to retrieve the explorer URL for.
* @returns {string} The explorer URL for the given transaction ID.
*/
getExplorerTxUrl(txID: string): string;
/**
* Sets or updates the current network.
* @param {Network} network - The network to set or update.
* @returns {void}
* @throws {"Network must be provided"} Thrown if the network has not been set before.
*/
setNetwork(network: Network): void;
/**
* @throws {Error} Method not implement
*/
getAddress(): string;
getAddressAsync(walletIndex?: number, verify?: boolean): Promise<string>;
/**
* Validate the given address.
*
* @param {Address} address
* @returns {boolean} `true` or `false`
*/
validateAddress(address: Address): boolean;
/**
* Retrieves the balance of a given address.
* @param {Address} address - The address to retrieve the balance for.
* @param {Asset[]} assets - Assets to retrieve the balance for (optional).
* @returns {Promise<Balance[]>} An array containing the balance of the address.
* @throws {"Invalid asset"} Thrown when the provided asset is invalid.
*/
getBalance(address: Address, assets?: TokenAsset[]): Promise<Balance[]>;
/**
* Retrieves the transaction history of a given address with pagination options.
* @param {TxHistoryParams} params - Options to get transaction history (optional).
* @returns {Promise<TxsPage>} The transaction history.
*/
getTransactions(params?: TxHistoryParams): Promise<TxsPage>;
/**
* Retrieves the transaction details of a given transaction ID.
* @param {string} txId - The transaction ID.
* @param {string} assetAddress - The asset address (optional).
* @returns {Promise<Tx>} The transaction details of the given transaction ID.
* @throws {"Need to provide valid txId"} Thrown if the provided transaction ID is invalid.
*/
getTransactionData(txId: string, assetAddress?: Address): Promise<Tx>;
/**
* Estimates the gas required for calling a contract function.
* @param {Address} contractAddress The contract address.
* @param {ContractInterface} abi The contract ABI json.
* @param {string} funcName The function to be called.
* @param {any[]} funcParams The parameters of the function.
* @param {number} walletIndex (optional) HD wallet index
* @param {EstimateCallParams} params - Parameters for estimating gas.
* @returns {BigNumber} The estimated gas required for the contract function call.
*/
estimateCall({ contractAddress, abi, funcName, funcParams }: EstimateCallParams): Promise<BigNumber>;
/**
* Check allowance.
*
* @param {Address} contractAddress The contract address.
* @param {Address} spenderAddress The spender address.
* @param {BaseAmount} amount The amount to check if it's allowed to spend or not (optional).
* @param {number} walletIndex (optional) HD wallet index
* @param {IsApprovedParams} params - Parameters for checking allowance.
* @returns {boolean} `true` if the allowance is approved, `false` otherwise.
*/
isApproved({ contractAddress, spenderAddress, amount, walletIndex }: IsApprovedParams): Promise<boolean>;
/**
* Estimates the gas required for approving an allowance.
*
* @param {EstimateApproveParams} params - Parameters for estimating gas.
* @param {Address} contractAddress The contract address.
* @param {Address} spenderAddress The spender address.
* @param {Address} fromAddress The address the approve transaction is sent from.
* @param {BaseAmount} amount The amount of token. By default, it will be unlimited token allowance. (optional)
*
* @returns {BigNumber} The estimated gas required for the approval.
*/
estimateApprove({ fromAddress, contractAddress, spenderAddress, amount, }: EstimateApproveParams): Promise<BigNumber>;
/**
* Broadcasts a transaction.
* @param {string} txHex - The transaction in hexadecimal format.
* @returns {Promise<TxHash>} The transaction hash.
*/
broadcastTx(txHex: string): Promise<TxHash>;
/**
* Estimates gas prices (average, fast, fastest) for a transaction.
* @param {Protocol} protocol The protocol to use for estimating gas prices.
* @returns {GasPrices} The gas prices (average, fast, fastest) in `Wei` (`BaseAmount`)
*/
estimateGasPrices(protocol?: Protocol): Promise<GasPrices>;
/**
* Estimates gas limit for a transaction.
*
* @param {TxParams} params The transaction and fees options.
* @returns {BaseAmount} The estimated gas limit.
* @throws Error Thrown if address could not be parsed from the given ERC20 asset.
*/
estimateGasLimit({ asset, recipient, amount, memo, from, isMemoEncoded, }: TxParams & {
from?: Address;
}): Promise<BigNumber>;
/**
* Checks if the given asset matches the gas asset.
*
* @param {Asset} asset - The asset to check.
* @returns {boolean} True if the asset matches the gas asset, false otherwise.
*/
private isGasAsset;
/**
* Estimates gas prices/limits (average, fast, fastest) and fees for a transaction.
*
* @param {TxParams} params The transaction parameters.
* @returns {FeesWithGasPricesAndLimits} The estimated gas prices/limits and fees.
*/
estimateFeesWithGasPricesAndLimits(params: TxParams): Promise<FeesWithGasPricesAndLimits>;
/**
* Wait until tx is confirmed
* @param {string} hash - tx's hash
*/
awaitTxConfirmed(hash: string): Promise<void>;
/**
* Get transaction fees.
*
* @param {TxParams} params - The transaction parameters.
* @returns {Fees} The average, fast, and fastest fees.
* @throws {"Params need to be passed"} Thrown if parameters are not provided.
*/
getFees(): never;
getFees(params: TxParams): Promise<Fees>;
/**
* Retrieves the balance of an address by round-robin querying multiple data providers.
*
* @param {Address} address - The address to query the balance for.
* @param {Asset[]} [assets] - Optional list of assets to query the balance for.
* @returns {Promise<Balance[]>} The balance information for the address.
* @throws Error Thrown if no provider is able to retrieve the balance.
*/
protected roundRobinGetBalance(address: Address, assets?: TokenAsset[]): Promise<Balance[]>;
/**
* Retrieves transaction data by round-robin querying multiple data providers.
*
* @param {string} txId - The transaction ID.
* @param {string} [assetAddress] - Optional asset address.
* @returns {Promise<Tx>} The transaction data.
* @throws Error Thrown if no provider is able to retrieve the transaction data.
*/
protected roundRobinGetTransactionData(txId: string, assetAddress?: string): Promise<Tx>;
/**
* Retrieves transaction history by round-robin querying multiple data providers.
*
* @param {TxHistoryParams} params - The transaction history parameters.
* @returns {Promise<TxsPage>} The transaction history.
* @throws Error Thrown if no provider is able to retrieve the transaction history.
*/
protected roundRobinGetTransactions(params: TxHistoryParams): Promise<TxsPage>;
/**
* Retrieves fee rates by round-robin querying multiple data providers.
*
* @returns {Promise<FeeRates>} The fee rates.
* @throws Error Thrown if no provider is able to retrieve the fee rates.
*/
protected roundRobinGetFeeRates(): Promise<FeeRates>;
/**
* Prepares a transaction for transfer.
*
* @param {TxParams&Address&FeeOption&BaseAmount&BigNumber} params - The transfer options.
* @returns {Promise<PreparedTx>} The raw unsigned transaction.
* @throws Error Thrown if the provided asset chain does not match the client's chain, or if any of the addresses are invalid.
*/
prepareTx({ sender, asset, memo, amount, recipient, isMemoEncoded, }: TxParams & {
sender: Address;
}): Promise<PreparedTx>;
/**
* Prepares an approval transaction.
*
* @param {ApproveParams&Address&FeeOption&BaseAmount&BigNumber} params - The approval options.
* @returns {Promise<PreparedTx>} The raw unsigned transaction.
* @throws Error Thrown if any of the addresses are invalid.
*/
prepareApprove({ contractAddress, spenderAddress, amount, sender, }: ApproveParams & {
sender: string;
}): Promise<PreparedTx>;
/**
* Call a contract function.
* @param {signer} Signer (optional) The address a transaction is send from. If not set, signer will be defined based on `walletIndex`
* @param {Address} contractAddress The contract address.
* @param {number} walletIndex (optional) HD wallet index
* @param {ContractInterface} abi The contract ABI json.
* @param {string} funcName The function to be called.
* @param {unknown[]} funcParams (optional) The parameters of the function.
* @param {CallParams} params - Parameters for calling the contract function.
* @returns {T} The result of the contract function call..
*/
call<T>({ contractAddress, abi, funcName, funcParams, signer }: CallParams): Promise<T>;
/**
* Transfers ETH or ERC20 token
*
* Note: A given `feeOption` wins over `gasPrice` and `gasLimit`
*
* @param {TxParams} params The transfer options.
* @param {feeOption} FeeOption Fee option (optional)
* @param {gasPrice} BaseAmount Gas price (optional)
* @param {maxFeePerGas} BaseAmount Optional. Following EIP-1559, maximum fee per gas. Parameter not compatible with gasPrice
* @param {maxPriorityFeePerGas} BaseAmount Optional. Following EIP-1559, maximum priority fee per gas. Parameter not compatible with gasPrice
* @param {gasLimit} BigNumber Gas limit (optional)
* @throws Error Thrown if address of given `Asset` could not be parsed
* @throws {Error} Error thrown if not compatible fee parameters are provided
* @returns {TxHash} The transaction hash.
*/
transfer({ walletIndex, asset, memo, amount, recipient, feeOption, gasPrice, maxFeePerGas, maxPriorityFeePerGas, gasLimit, isMemoEncoded, }: TxParams): Promise<string>;
/**
* Approves an allowance for spending tokens.
*
* @param {ApproveParams} params - Parameters for approving an allowance.
* @param {Address} contractAddress The contract address.
* @param {Address} spenderAddress The spender address.
* @param {feeOption} FeeOption Fee option (optional)
* @param {BaseAmount} amount The amount of token. By default, it will be unlimited token allowance. (optional)
* @param {number} walletIndex (optional) HD wallet index
* @returns {TransactionResponse} The result of the approval transaction.
* @throws Error If gas estimation fails.
*/
approve({ contractAddress, spenderAddress, feeOption, amount, walletIndex, }: ApproveParams): Promise<string>;
/**
* Purge client
*/
purgeClient(): void;
/**
* Get the account signer the client is using
* @returns {ISigner}
* @throws {Error} if the client is not using an account
*/
protected getSigner(): ISigner;
}