chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
141 lines • 5.21 kB
JavaScript
import { CurrencyUtils } from '../CurrencyUtils';
import { evmAddressBalance, evmAddressTransactionCount, evmBlockByHash, evmBlockByHeight, evmBroadcastTransaction, evmCallSmartContractFunction, evmEstimateGas, evmFeeRate, evmLatestBlock, evmNetworkStatus, evmTransactionDetails, } from '../../../Client';
import { bytesToHex } from '../../../InternalUtils/Utils';
import { toDecimal } from '../../../InternalUtils/NumberLike';
import { ethers, SigningKey, verifyMessage } from 'ethers';
export class EvmCurrencyUtils extends CurrencyUtils {
network;
constructor(client, currencyInfo, markets, network) {
super(client, currencyInfo, markets);
this.network = network;
}
async addressBalance(address) {
const result = await evmAddressBalance({
client: this.client,
path: { network: this.network },
query: { address },
});
return {
confirmed: this.buildAmount(result.data.confirmed),
unconfirmed: this.buildAmount(result.data.unconfirmed),
};
}
async addressTransactionCount(address) {
const result = await evmAddressTransactionCount({
client: this.client,
path: { network: this.network },
query: { address },
});
return Number(result.data);
}
async callSmartContractRaw(smartContractAddress, data) {
const result = await evmCallSmartContractFunction({
client: this.client,
path: { network: this.network },
body: { contract: smartContractAddress, data },
});
return result.data.result;
}
async estimateGas(addressFrom, addressTo, amount, nonce, data) {
const result = await evmEstimateGas({
client: this.client,
path: { network: this.network },
query: {
addressFrom,
addressTo,
amount: amount.baseAmount.toString(),
nonce: toDecimal(nonce).toString(),
data: data.toString(),
},
});
return Number(result.data);
}
async broadcastTransaction(transactionRaw) {
const result = await evmBroadcastTransaction({
client: this.client,
path: { network: this.network },
body: {
transactionRaw: transactionRaw instanceof Uint8Array
? bytesToHex(transactionRaw, false)
: transactionRaw,
},
});
return { transactionId: result.data.txId };
}
async transactionDetails(transactionId) {
const result = await evmTransactionDetails({
client: this.client,
path: { network: this.network },
query: { transactionId },
});
return {
amount: this.buildAmount(result.data.amount),
blockHeight: result.data.blockHeight ?? null,
addressFrom: result.data.from,
addressTo: result.data.to,
gas: toDecimal(result.data.gas),
gasPrice: this.buildAmount(result.data.gasPrice),
maxFeePerGas: this.buildAmount(result.data.maxFeePerGas),
maxPriorityFeePerGas: this.buildAmount(result.data.maxPriorityFeePerGas),
transfers: result.data.transfers.map((transfer) => ({
amount: this.buildAmount(transfer.amount),
addressFrom: transfer.from,
addressTo: transfer.to,
})),
};
}
async getFeeRate() {
const result = await evmFeeRate({
client: this.client,
path: { network: this.network },
});
return result.data;
}
async networkStatus() {
const result = await evmNetworkStatus({
client: this.client,
path: { network: this.network },
});
return result.data;
}
publicKeyToAddress(publicKey) {
return ethers.computeAddress(bytesToHex(publicKey.raw, true));
}
async signMessage(message, privateKey) {
const signer = new ethers.Wallet(new SigningKey(privateKey.raw));
return await signer.signMessage(message);
}
async verifySignedMessage(message, signature, address) {
try {
const recoveredAddress = verifyMessage(message, signature);
return recoveredAddress == address;
}
catch (_ex) {
return false;
}
}
async latestBlock() {
const result = await evmLatestBlock({
client: this.client,
path: { network: this.network },
});
return result.data;
}
async blockByHeight(blockHeight) {
const result = await evmBlockByHeight({
client: this.client,
path: { network: this.network },
query: { blockHeight },
});
return result.data;
}
async blockByHash(blockHash) {
const result = await evmBlockByHash({
client: this.client,
path: { network: this.network },
query: { blockHash },
});
return result.data;
}
}
//# sourceMappingURL=EvmCurrencyUtils.js.map