@atomiqlabs/chain-evm
Version:
EVM specific base implementation
91 lines (90 loc) • 3.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EVMChainInterface = void 0;
const Utils_1 = require("../../utils/Utils");
const ethers_1 = require("ethers");
const EVMBlocks_1 = require("./modules/EVMBlocks");
const EVMEvents_1 = require("./modules/EVMEvents");
const EVMFees_1 = require("./modules/EVMFees");
const EVMTokens_1 = require("./modules/EVMTokens");
const EVMTransactions_1 = require("./modules/EVMTransactions");
const EVMSignatures_1 = require("./modules/EVMSignatures");
const EVMAddresses_1 = require("./modules/EVMAddresses");
const EVMSigner_1 = require("../wallet/EVMSigner");
class EVMChainInterface {
constructor(chainId, evmChainId, provider, config, retryPolicy, evmFeeEstimator = new EVMFees_1.EVMFees(provider)) {
var _a;
this.chainId = chainId;
this.evmChainId = evmChainId;
this.provider = provider;
this.retryPolicy = retryPolicy;
this.config = config;
(_a = this.config).safeBlockTag ?? (_a.safeBlockTag = "safe");
this.logger = (0, Utils_1.getLogger)("EVMChainInterface(" + this.evmChainId + "): ");
this.Fees = evmFeeEstimator;
this.Tokens = new EVMTokens_1.EVMTokens(this);
this.Transactions = new EVMTransactions_1.EVMTransactions(this);
this.Signatures = new EVMSignatures_1.EVMSignatures(this);
this.Events = new EVMEvents_1.EVMEvents(this);
this.Blocks = new EVMBlocks_1.EVMBlocks(this);
}
async getBalance(signer, tokenAddress) {
//TODO: For native token we should discount the cost of transactions
return await this.Tokens.getTokenBalance(signer, tokenAddress);
}
getNativeCurrencyAddress() {
return this.Tokens.getNativeCurrencyAddress();
}
isValidToken(tokenIdentifier) {
return this.Tokens.isValidToken(tokenIdentifier);
}
isValidAddress(address) {
return EVMAddresses_1.EVMAddresses.isValidAddress(address);
}
///////////////////////////////////
//// Callbacks & handlers
offBeforeTxReplace(callback) {
return true;
}
onBeforeTxReplace(callback) { }
onBeforeTxSigned(callback) {
this.Transactions.onBeforeTxSigned(callback);
}
offBeforeTxSigned(callback) {
return this.Transactions.offBeforeTxSigned(callback);
}
randomAddress() {
const wallet = ethers_1.Wallet.createRandom();
return wallet.address;
}
randomSigner() {
const wallet = ethers_1.Wallet.createRandom();
return new EVMSigner_1.EVMSigner(wallet, wallet.address);
}
////////////////////////////////////////////
//// Transactions
sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
return this.Transactions.sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish);
}
serializeTx(tx) {
return this.Transactions.serializeTx(tx);
}
deserializeTx(txData) {
return this.Transactions.deserializeTx(txData);
}
getTxIdStatus(txId) {
return this.Transactions.getTxIdStatus(txId);
}
getTxStatus(tx) {
return this.Transactions.getTxStatus(tx);
}
async txsTransfer(signer, token, amount, dstAddress, feeRate) {
return [await this.Tokens.Transfer(signer, token, amount, dstAddress, feeRate)];
}
async transfer(signer, token, amount, dstAddress, txOptions) {
const tx = await this.Tokens.Transfer(signer.getAddress(), token, amount, dstAddress, txOptions?.feeRate);
const [txId] = await this.Transactions.sendAndConfirm(signer, [tx], txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
return txId;
}
}
exports.EVMChainInterface = EVMChainInterface;