p-sdk-wallet
Version:
A comprehensive wallet SDK for React Native (pwc), supporting multi-chain and multi-account features.
137 lines (136 loc) • 5.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChainService = void 0;
const ethers_1 = require("ethers");
/**
* Standard ERC-20 token interface ABI for common token operations.
*/
const ERC20_ABI = [
"function name() view returns (string)",
"function symbol() view returns (string)",
"function decimals() view returns (uint8)",
"function totalSupply() view returns (uint256)",
"function balanceOf(address) view returns (uint)",
"function transfer(address to, uint amount) returns (bool)",
"function approve(address spender, uint amount) returns (bool)"
];
/**
* Service for interacting with EVM-compatible blockchains.
* Provides methods for balance checking, token operations, and transaction sending.
*/
class ChainService {
/**
* Creates a new ChainService instance for the specified chain.
* @param privateKey - The private key to use for signing transactions (hex string without '0x' prefix)
* @param chainConfig - The chain configuration to use
*/
constructor(privateKey, chainConfig) {
if (chainConfig.type !== 'evm') {
throw new Error('ChainService only supports EVM chains');
}
const provider = new ethers_1.ethers.JsonRpcProvider(chainConfig.rpcUrl);
this.wallet = new ethers_1.Wallet(privateKey, provider);
}
/**
* Derives the public address from a private key.
* @param privateKey - The private key to derive the address from (hex string without '0x' prefix)
* @returns The derived Ethereum address
*/
static getAddress(privateKey) {
return new ethers_1.Wallet(privateKey).address;
}
/**
* Gets the native token balance (e.g., ETH, MATIC) for the wallet.
* @returns Promise resolving to the balance as a bigint in wei
* @throws Error if the provider is not set
*/
async getNativeBalance() {
if (!this.wallet.provider) {
throw new Error('Provider not set for wallet.');
}
return this.wallet.provider.getBalance(this.wallet.address);
}
/**
* Gets the balance of a specific ERC-20 token for the wallet.
* @param tokenAddress - The contract address of the ERC-20 token
* @returns Promise resolving to the token balance as a bigint
* @throws Error if the provider is not set or token contract is invalid
*/
async getTokenBalance(tokenAddress) {
if (!this.wallet.provider) {
throw new Error('Provider not set for wallet.');
}
const contract = new ethers_1.ethers.Contract(tokenAddress, ERC20_ABI, this.wallet.provider);
return contract.balanceOf(this.wallet.address);
}
/**
* Gets metadata information about an ERC-20 token.
* @param tokenAddress - The contract address of the ERC-20 token
* @returns Promise resolving to token metadata including name, symbol, and decimals
* @throws Error if the provider is not set or token contract is invalid
*/
async getTokenInfo(tokenAddress) {
if (!this.wallet.provider) {
throw new Error('Provider not set for wallet.');
}
const contract = new ethers_1.ethers.Contract(tokenAddress, ERC20_ABI, this.wallet.provider);
const [name, symbol, decimals] = await Promise.all([
contract.name(),
contract.symbol(),
contract.decimals(),
]);
return { address: tokenAddress, name, symbol, decimals: Number(decimals) };
}
/**
* Estimates the gas cost for a transaction.
* @param transaction - The transaction request to estimate gas for
* @returns Promise resolving to the estimated gas cost as a bigint
* @throws Error if the provider is not set or estimation fails
*/
async estimateGas(transaction) {
if (!this.wallet.provider) {
throw new Error('Provider not set for wallet.');
}
return this.wallet.provider.estimateGas(transaction);
}
/**
* Sends a transaction to the blockchain.
* @param transaction - The transaction request to send
* @returns Promise resolving to the transaction response with hash and details
* @throws Error if the transaction fails or wallet is not properly configured
*/
async sendTransaction(transaction) {
return this.wallet.sendTransaction(transaction);
}
/**
* Sends ERC-20 tokens to another address.
* @param tokenAddress - The contract address of the ERC-20 token to send
* @param to - The recipient's address
* @param amount - The amount of tokens to send as a string (e.g., "100.5")
* @returns Promise resolving to the transaction response with hash and details
* @throws Error if insufficient balance, invalid addresses, or transaction fails
*/
async sendToken(tokenAddress, to, amount) {
if (!this.wallet.provider) {
throw new Error('Provider not set for wallet.');
}
const contract = new ethers_1.Contract(tokenAddress, ERC20_ABI, this.wallet);
const tokenInfo = await this.getTokenInfo(tokenAddress);
const parsedAmount = ethers_1.ethers.parseUnits(amount, tokenInfo.decimals);
// The contract instance is connected to the wallet, which will sign and send the transaction.
return contract.transfer(to, parsedAmount);
}
/**
* Gets the transaction receipt for a given transaction hash.
* @param hash - The transaction hash to get the receipt for
* @returns Promise resolving to the transaction receipt or null if not found
* @throws Error if the provider is not set
*/
async getTransactionReceipt(hash) {
if (!this.wallet.provider) {
throw new Error('Provider not set for wallet.');
}
return this.wallet.provider.getTransactionReceipt(hash);
}
}
exports.ChainService = ChainService;