pwc-sdk-wallet 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2062 -0
- package/dist/Vault.d.ts +493 -0
- package/dist/Vault.js +1090 -0
- package/dist/chain/ChainService.d.ts +84 -0
- package/dist/chain/ChainService.js +136 -0
- package/dist/chain/SolanaChainService.d.ts +94 -0
- package/dist/chain/SolanaChainService.js +167 -0
- package/dist/config/chains.d.ts +233 -0
- package/dist/config/chains.js +285 -0
- package/dist/config/constants.d.ts +102 -0
- package/dist/config/constants.js +109 -0
- package/dist/config/environment.d.ts +46 -0
- package/dist/config/environment.js +114 -0
- package/dist/config/gas.d.ts +107 -0
- package/dist/config/gas.js +123 -0
- package/dist/crypto/EncryptionService.d.ts +74 -0
- package/dist/crypto/EncryptionService.js +178 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +96 -0
- package/dist/keyrings/HDKeyring.d.ts +72 -0
- package/dist/keyrings/HDKeyring.js +156 -0
- package/dist/keyrings/SimpleKeyring.d.ts +31 -0
- package/dist/keyrings/SimpleKeyring.js +49 -0
- package/dist/keyrings/SolanaKeyring.d.ts +71 -0
- package/dist/keyrings/SolanaKeyring.js +159 -0
- package/dist/services/BatchProcessor.d.ts +42 -0
- package/dist/services/BatchProcessor.js +188 -0
- package/dist/services/MultiTransferService.d.ts +78 -0
- package/dist/services/MultiTransferService.js +252 -0
- package/dist/services/QRCodeService.d.ts +193 -0
- package/dist/services/QRCodeService.js +299 -0
- package/dist/services/TokenUtils.d.ts +307 -0
- package/dist/services/TokenUtils.js +429 -0
- package/dist/services/nft/MetadataResolver.d.ts +57 -0
- package/dist/services/nft/MetadataResolver.js +162 -0
- package/dist/services/nft/NFTAPIService.d.ts +53 -0
- package/dist/services/nft/NFTAPIService.js +122 -0
- package/dist/services/nft/NFTService.d.ts +241 -0
- package/dist/services/nft/NFTService.js +910 -0
- package/dist/types/multiTransfer.d.ts +68 -0
- package/dist/types/multiTransfer.js +2 -0
- package/dist/types/nft/index.d.ts +68 -0
- package/dist/types/nft/index.js +2 -0
- package/dist/types/nft.d.ts +265 -0
- package/dist/types/nft.js +6 -0
- package/package.json +70 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { TransactionRequest, TransactionResponse, TransactionReceipt } from 'ethers';
|
|
2
|
+
import { type ChainConfig } from '../config/chains';
|
|
3
|
+
/**
|
|
4
|
+
* Interface representing token metadata for ERC-20 tokens.
|
|
5
|
+
*/
|
|
6
|
+
export interface Token {
|
|
7
|
+
/** The contract address of the token */
|
|
8
|
+
address: string;
|
|
9
|
+
/** The human-readable name of the token */
|
|
10
|
+
name: string;
|
|
11
|
+
/** The token's symbol/ticker */
|
|
12
|
+
symbol: string;
|
|
13
|
+
/** The number of decimal places for the token */
|
|
14
|
+
decimals: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Service for interacting with EVM-compatible blockchains.
|
|
18
|
+
* Provides methods for balance checking, token operations, and transaction sending.
|
|
19
|
+
*/
|
|
20
|
+
export declare class ChainService {
|
|
21
|
+
private wallet;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new ChainService instance for the specified chain.
|
|
24
|
+
* @param privateKey - The private key to use for signing transactions (hex string without '0x' prefix)
|
|
25
|
+
* @param chainConfig - The chain configuration to use
|
|
26
|
+
*/
|
|
27
|
+
constructor(privateKey: string, chainConfig: ChainConfig);
|
|
28
|
+
/**
|
|
29
|
+
* Derives the public address from a private key.
|
|
30
|
+
* @param privateKey - The private key to derive the address from (hex string without '0x' prefix)
|
|
31
|
+
* @returns The derived Ethereum address
|
|
32
|
+
*/
|
|
33
|
+
static getAddress(privateKey: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Gets the native token balance (e.g., ETH, MATIC) for the wallet.
|
|
36
|
+
* @returns Promise resolving to the balance as a bigint in wei
|
|
37
|
+
* @throws Error if the provider is not set
|
|
38
|
+
*/
|
|
39
|
+
getNativeBalance(): Promise<bigint>;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the balance of a specific ERC-20 token for the wallet.
|
|
42
|
+
* @param tokenAddress - The contract address of the ERC-20 token
|
|
43
|
+
* @returns Promise resolving to the token balance as a bigint
|
|
44
|
+
* @throws Error if the provider is not set or token contract is invalid
|
|
45
|
+
*/
|
|
46
|
+
getTokenBalance(tokenAddress: string): Promise<bigint>;
|
|
47
|
+
/**
|
|
48
|
+
* Gets metadata information about an ERC-20 token.
|
|
49
|
+
* @param tokenAddress - The contract address of the ERC-20 token
|
|
50
|
+
* @returns Promise resolving to token metadata including name, symbol, and decimals
|
|
51
|
+
* @throws Error if the provider is not set or token contract is invalid
|
|
52
|
+
*/
|
|
53
|
+
getTokenInfo(tokenAddress: string): Promise<Token>;
|
|
54
|
+
/**
|
|
55
|
+
* Estimates the gas cost for a transaction.
|
|
56
|
+
* @param transaction - The transaction request to estimate gas for
|
|
57
|
+
* @returns Promise resolving to the estimated gas cost as a bigint
|
|
58
|
+
* @throws Error if the provider is not set or estimation fails
|
|
59
|
+
*/
|
|
60
|
+
estimateGas(transaction: TransactionRequest): Promise<bigint>;
|
|
61
|
+
/**
|
|
62
|
+
* Sends a transaction to the blockchain.
|
|
63
|
+
* @param transaction - The transaction request to send
|
|
64
|
+
* @returns Promise resolving to the transaction response with hash and details
|
|
65
|
+
* @throws Error if the transaction fails or wallet is not properly configured
|
|
66
|
+
*/
|
|
67
|
+
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>;
|
|
68
|
+
/**
|
|
69
|
+
* Sends ERC-20 tokens to another address.
|
|
70
|
+
* @param tokenAddress - The contract address of the ERC-20 token to send
|
|
71
|
+
* @param to - The recipient's address
|
|
72
|
+
* @param amount - The amount of tokens to send as a string (e.g., "100.5")
|
|
73
|
+
* @returns Promise resolving to the transaction response with hash and details
|
|
74
|
+
* @throws Error if insufficient balance, invalid addresses, or transaction fails
|
|
75
|
+
*/
|
|
76
|
+
sendToken(tokenAddress: string, to: string, amount: string): Promise<TransactionResponse>;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the transaction receipt for a given transaction hash.
|
|
79
|
+
* @param hash - The transaction hash to get the receipt for
|
|
80
|
+
* @returns Promise resolving to the transaction receipt or null if not found
|
|
81
|
+
* @throws Error if the provider is not set
|
|
82
|
+
*/
|
|
83
|
+
getTransactionReceipt(hash: string): Promise<TransactionReceipt | null>;
|
|
84
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChainService = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
/**
|
|
6
|
+
* Standard ERC-20 token interface ABI for common token operations.
|
|
7
|
+
*/
|
|
8
|
+
const ERC20_ABI = [
|
|
9
|
+
"function name() view returns (string)",
|
|
10
|
+
"function symbol() view returns (string)",
|
|
11
|
+
"function decimals() view returns (uint8)",
|
|
12
|
+
"function totalSupply() view returns (uint256)",
|
|
13
|
+
"function balanceOf(address) view returns (uint)",
|
|
14
|
+
"function transfer(address to, uint amount) returns (bool)",
|
|
15
|
+
"function approve(address spender, uint amount) returns (bool)"
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* Service for interacting with EVM-compatible blockchains.
|
|
19
|
+
* Provides methods for balance checking, token operations, and transaction sending.
|
|
20
|
+
*/
|
|
21
|
+
class ChainService {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new ChainService instance for the specified chain.
|
|
24
|
+
* @param privateKey - The private key to use for signing transactions (hex string without '0x' prefix)
|
|
25
|
+
* @param chainConfig - The chain configuration to use
|
|
26
|
+
*/
|
|
27
|
+
constructor(privateKey, chainConfig) {
|
|
28
|
+
if (chainConfig.type !== 'evm') {
|
|
29
|
+
throw new Error('ChainService only supports EVM chains');
|
|
30
|
+
}
|
|
31
|
+
const provider = new ethers_1.ethers.JsonRpcProvider(chainConfig.rpcUrl);
|
|
32
|
+
this.wallet = new ethers_1.Wallet(privateKey, provider);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Derives the public address from a private key.
|
|
36
|
+
* @param privateKey - The private key to derive the address from (hex string without '0x' prefix)
|
|
37
|
+
* @returns The derived Ethereum address
|
|
38
|
+
*/
|
|
39
|
+
static getAddress(privateKey) {
|
|
40
|
+
return new ethers_1.Wallet(privateKey).address;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Gets the native token balance (e.g., ETH, MATIC) for the wallet.
|
|
44
|
+
* @returns Promise resolving to the balance as a bigint in wei
|
|
45
|
+
* @throws Error if the provider is not set
|
|
46
|
+
*/
|
|
47
|
+
async getNativeBalance() {
|
|
48
|
+
if (!this.wallet.provider) {
|
|
49
|
+
throw new Error('Provider not set for wallet.');
|
|
50
|
+
}
|
|
51
|
+
return this.wallet.provider.getBalance(this.wallet.address);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the balance of a specific ERC-20 token for the wallet.
|
|
55
|
+
* @param tokenAddress - The contract address of the ERC-20 token
|
|
56
|
+
* @returns Promise resolving to the token balance as a bigint
|
|
57
|
+
* @throws Error if the provider is not set or token contract is invalid
|
|
58
|
+
*/
|
|
59
|
+
async getTokenBalance(tokenAddress) {
|
|
60
|
+
if (!this.wallet.provider) {
|
|
61
|
+
throw new Error('Provider not set for wallet.');
|
|
62
|
+
}
|
|
63
|
+
const contract = new ethers_1.ethers.Contract(tokenAddress, ERC20_ABI, this.wallet.provider);
|
|
64
|
+
return contract.balanceOf(this.wallet.address);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Gets metadata information about an ERC-20 token.
|
|
68
|
+
* @param tokenAddress - The contract address of the ERC-20 token
|
|
69
|
+
* @returns Promise resolving to token metadata including name, symbol, and decimals
|
|
70
|
+
* @throws Error if the provider is not set or token contract is invalid
|
|
71
|
+
*/
|
|
72
|
+
async getTokenInfo(tokenAddress) {
|
|
73
|
+
if (!this.wallet.provider) {
|
|
74
|
+
throw new Error('Provider not set for wallet.');
|
|
75
|
+
}
|
|
76
|
+
const contract = new ethers_1.ethers.Contract(tokenAddress, ERC20_ABI, this.wallet.provider);
|
|
77
|
+
const [name, symbol, decimals] = await Promise.all([
|
|
78
|
+
contract.name(),
|
|
79
|
+
contract.symbol(),
|
|
80
|
+
contract.decimals(),
|
|
81
|
+
]);
|
|
82
|
+
return { address: tokenAddress, name, symbol, decimals: Number(decimals) };
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Estimates the gas cost for a transaction.
|
|
86
|
+
* @param transaction - The transaction request to estimate gas for
|
|
87
|
+
* @returns Promise resolving to the estimated gas cost as a bigint
|
|
88
|
+
* @throws Error if the provider is not set or estimation fails
|
|
89
|
+
*/
|
|
90
|
+
async estimateGas(transaction) {
|
|
91
|
+
if (!this.wallet.provider) {
|
|
92
|
+
throw new Error('Provider not set for wallet.');
|
|
93
|
+
}
|
|
94
|
+
return this.wallet.provider.estimateGas(transaction);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Sends a transaction to the blockchain.
|
|
98
|
+
* @param transaction - The transaction request to send
|
|
99
|
+
* @returns Promise resolving to the transaction response with hash and details
|
|
100
|
+
* @throws Error if the transaction fails or wallet is not properly configured
|
|
101
|
+
*/
|
|
102
|
+
async sendTransaction(transaction) {
|
|
103
|
+
return this.wallet.sendTransaction(transaction);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Sends ERC-20 tokens to another address.
|
|
107
|
+
* @param tokenAddress - The contract address of the ERC-20 token to send
|
|
108
|
+
* @param to - The recipient's address
|
|
109
|
+
* @param amount - The amount of tokens to send as a string (e.g., "100.5")
|
|
110
|
+
* @returns Promise resolving to the transaction response with hash and details
|
|
111
|
+
* @throws Error if insufficient balance, invalid addresses, or transaction fails
|
|
112
|
+
*/
|
|
113
|
+
async sendToken(tokenAddress, to, amount) {
|
|
114
|
+
if (!this.wallet.provider) {
|
|
115
|
+
throw new Error('Provider not set for wallet.');
|
|
116
|
+
}
|
|
117
|
+
const contract = new ethers_1.Contract(tokenAddress, ERC20_ABI, this.wallet);
|
|
118
|
+
const tokenInfo = await this.getTokenInfo(tokenAddress);
|
|
119
|
+
const parsedAmount = ethers_1.ethers.parseUnits(amount, tokenInfo.decimals);
|
|
120
|
+
// The contract instance is connected to the wallet, which will sign and send the transaction.
|
|
121
|
+
return contract.transfer(to, parsedAmount);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Gets the transaction receipt for a given transaction hash.
|
|
125
|
+
* @param hash - The transaction hash to get the receipt for
|
|
126
|
+
* @returns Promise resolving to the transaction receipt or null if not found
|
|
127
|
+
* @throws Error if the provider is not set
|
|
128
|
+
*/
|
|
129
|
+
async getTransactionReceipt(hash) {
|
|
130
|
+
if (!this.wallet.provider) {
|
|
131
|
+
throw new Error('Provider not set for wallet.');
|
|
132
|
+
}
|
|
133
|
+
return this.wallet.provider.getTransactionReceipt(hash);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.ChainService = ChainService;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type ChainConfig } from '../config/chains';
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing Solana transaction response.
|
|
4
|
+
* Similar to EVM transaction response but adapted for Solana's transaction structure.
|
|
5
|
+
*/
|
|
6
|
+
export interface SolanaTransactionResponse {
|
|
7
|
+
/** The transaction signature/hash */
|
|
8
|
+
hash: string;
|
|
9
|
+
/** The sender's public key address */
|
|
10
|
+
from: string;
|
|
11
|
+
/** The recipient's public key address */
|
|
12
|
+
to: string;
|
|
13
|
+
/** The block number (optional, not always available on Solana) */
|
|
14
|
+
blockNumber?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Interface representing Solana SPL token metadata.
|
|
18
|
+
* Extends the basic token interface with Solana-specific fields.
|
|
19
|
+
*/
|
|
20
|
+
export interface SolanaToken {
|
|
21
|
+
/** The token's mint address */
|
|
22
|
+
address: string;
|
|
23
|
+
/** The human-readable name of the token */
|
|
24
|
+
name: string;
|
|
25
|
+
/** The token's symbol/ticker */
|
|
26
|
+
symbol: string;
|
|
27
|
+
/** The number of decimal places for the token */
|
|
28
|
+
decimals: number;
|
|
29
|
+
/** The total supply of the token (optional) */
|
|
30
|
+
totalSupply?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Service for interacting with Solana blockchain.
|
|
34
|
+
* Provides methods for SOL balance checking, SPL token operations, and transaction sending.
|
|
35
|
+
*/
|
|
36
|
+
export declare class SolanaChainService {
|
|
37
|
+
private connection;
|
|
38
|
+
private keypair;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new SolanaChainService instance for the specified Solana network.
|
|
41
|
+
* @param privateKey - The private key to use for signing transactions (hex string)
|
|
42
|
+
* @param chainConfig - The chain configuration to use
|
|
43
|
+
* @throws Error if the chain is not a Solana chain
|
|
44
|
+
*/
|
|
45
|
+
constructor(privateKey: string, chainConfig: ChainConfig);
|
|
46
|
+
/**
|
|
47
|
+
* Gets the native SOL balance for the wallet.
|
|
48
|
+
* @returns Promise resolving to the balance as a bigint in lamports
|
|
49
|
+
* @throws Error if the balance query fails
|
|
50
|
+
*/
|
|
51
|
+
getNativeBalance(): Promise<bigint>;
|
|
52
|
+
/**
|
|
53
|
+
* Sends native SOL to another address.
|
|
54
|
+
* @param to - The recipient's public key address
|
|
55
|
+
* @param amount - The amount of SOL to send as a string (e.g., "1.5")
|
|
56
|
+
* @returns Promise resolving to the transaction response with signature and details
|
|
57
|
+
* @throws Error if the transaction fails or recipient address is invalid
|
|
58
|
+
*/
|
|
59
|
+
sendTransaction(to: string, amount: string): Promise<SolanaTransactionResponse>;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the balance of a specific SPL token for the wallet.
|
|
62
|
+
* @param tokenAddress - The token's mint address
|
|
63
|
+
* @returns Promise resolving to the token balance as a bigint
|
|
64
|
+
* @throws Error if the balance query fails (returns 0 if token account doesn't exist)
|
|
65
|
+
*/
|
|
66
|
+
getTokenBalance(tokenAddress: string): Promise<bigint>;
|
|
67
|
+
/**
|
|
68
|
+
* Gets metadata information about an SPL token.
|
|
69
|
+
* @param tokenAddress - The token's mint address
|
|
70
|
+
* @returns Promise resolving to token metadata including supply information
|
|
71
|
+
* @throws Error if the token info query fails
|
|
72
|
+
*/
|
|
73
|
+
getTokenInfo(tokenAddress: string): Promise<SolanaToken>;
|
|
74
|
+
/**
|
|
75
|
+
* Sends SPL tokens to another address.
|
|
76
|
+
* @param tokenAddress - The token's mint address to send
|
|
77
|
+
* @param to - The recipient's public key address
|
|
78
|
+
* @param amount - The amount of tokens to send as a string
|
|
79
|
+
* @returns Promise resolving to the transaction response with signature and details
|
|
80
|
+
* @throws Error if the transaction fails, insufficient balance, or addresses are invalid
|
|
81
|
+
*/
|
|
82
|
+
sendToken(tokenAddress: string, to: string, amount: string): Promise<SolanaTransactionResponse>;
|
|
83
|
+
/**
|
|
84
|
+
* Gets the public key address of the wallet.
|
|
85
|
+
* @returns The public key address as a string
|
|
86
|
+
*/
|
|
87
|
+
getAddress(): string;
|
|
88
|
+
/**
|
|
89
|
+
* Estimates the transaction fee for a typical transaction.
|
|
90
|
+
* @returns Promise resolving to the estimated fee as a bigint in lamports
|
|
91
|
+
* @throws Error if the fee estimation fails
|
|
92
|
+
*/
|
|
93
|
+
estimateTransactionFee(): Promise<bigint>;
|
|
94
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SolanaChainService = void 0;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
6
|
+
const buffer_1 = require("buffer");
|
|
7
|
+
/**
|
|
8
|
+
* Service for interacting with Solana blockchain.
|
|
9
|
+
* Provides methods for SOL balance checking, SPL token operations, and transaction sending.
|
|
10
|
+
*/
|
|
11
|
+
class SolanaChainService {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new SolanaChainService instance for the specified Solana network.
|
|
14
|
+
* @param privateKey - The private key to use for signing transactions (hex string)
|
|
15
|
+
* @param chainConfig - The chain configuration to use
|
|
16
|
+
* @throws Error if the chain is not a Solana chain
|
|
17
|
+
*/
|
|
18
|
+
constructor(privateKey, chainConfig) {
|
|
19
|
+
if (chainConfig.type !== 'solana') {
|
|
20
|
+
throw new Error('SolanaChainService only supports Solana chains');
|
|
21
|
+
}
|
|
22
|
+
this.connection = new web3_js_1.Connection(chainConfig.rpcUrl, 'confirmed');
|
|
23
|
+
this.keypair = web3_js_1.Keypair.fromSecretKey(buffer_1.Buffer.from(privateKey, 'hex'));
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Gets the native SOL balance for the wallet.
|
|
27
|
+
* @returns Promise resolving to the balance as a bigint in lamports
|
|
28
|
+
* @throws Error if the balance query fails
|
|
29
|
+
*/
|
|
30
|
+
async getNativeBalance() {
|
|
31
|
+
try {
|
|
32
|
+
const balance = await this.connection.getBalance(this.keypair.publicKey);
|
|
33
|
+
return BigInt(balance);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
throw new Error(`Failed to get SOL balance: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Sends native SOL to another address.
|
|
41
|
+
* @param to - The recipient's public key address
|
|
42
|
+
* @param amount - The amount of SOL to send as a string (e.g., "1.5")
|
|
43
|
+
* @returns Promise resolving to the transaction response with signature and details
|
|
44
|
+
* @throws Error if the transaction fails or recipient address is invalid
|
|
45
|
+
*/
|
|
46
|
+
async sendTransaction(to, amount) {
|
|
47
|
+
try {
|
|
48
|
+
const toPubkey = new web3_js_1.PublicKey(to);
|
|
49
|
+
const lamports = parseFloat(amount) * web3_js_1.LAMPORTS_PER_SOL;
|
|
50
|
+
const transaction = new web3_js_1.Transaction().add(web3_js_1.SystemProgram.transfer({
|
|
51
|
+
fromPubkey: this.keypair.publicKey,
|
|
52
|
+
toPubkey: toPubkey,
|
|
53
|
+
lamports: lamports
|
|
54
|
+
}));
|
|
55
|
+
const signature = await (0, web3_js_1.sendAndConfirmTransaction)(this.connection, transaction, [this.keypair]);
|
|
56
|
+
return {
|
|
57
|
+
hash: signature,
|
|
58
|
+
from: this.keypair.publicKey.toString(),
|
|
59
|
+
to: to
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new Error(`Failed to send SOL transaction: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Gets the balance of a specific SPL token for the wallet.
|
|
68
|
+
* @param tokenAddress - The token's mint address
|
|
69
|
+
* @returns Promise resolving to the token balance as a bigint
|
|
70
|
+
* @throws Error if the balance query fails (returns 0 if token account doesn't exist)
|
|
71
|
+
*/
|
|
72
|
+
async getTokenBalance(tokenAddress) {
|
|
73
|
+
try {
|
|
74
|
+
const tokenPubkey = new web3_js_1.PublicKey(tokenAddress);
|
|
75
|
+
const associatedTokenAddress = await (0, spl_token_1.getAssociatedTokenAddress)(tokenPubkey, this.keypair.publicKey);
|
|
76
|
+
const balance = await this.connection.getTokenAccountBalance(associatedTokenAddress);
|
|
77
|
+
if (balance.value === null) {
|
|
78
|
+
return BigInt(0);
|
|
79
|
+
}
|
|
80
|
+
return BigInt(balance.value.amount);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
// If token account doesn't exist, return 0
|
|
84
|
+
if (error instanceof Error && error.message.includes('TokenAccountNotFoundError')) {
|
|
85
|
+
return BigInt(0);
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`Failed to get token balance: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Gets metadata information about an SPL token.
|
|
92
|
+
* @param tokenAddress - The token's mint address
|
|
93
|
+
* @returns Promise resolving to token metadata including supply information
|
|
94
|
+
* @throws Error if the token info query fails
|
|
95
|
+
*/
|
|
96
|
+
async getTokenInfo(tokenAddress) {
|
|
97
|
+
try {
|
|
98
|
+
const tokenPubkey = new web3_js_1.PublicKey(tokenAddress);
|
|
99
|
+
// Get token supply
|
|
100
|
+
const supply = await this.connection.getTokenSupply(tokenPubkey);
|
|
101
|
+
// For now, return basic info. In a real implementation,
|
|
102
|
+
// you'd fetch metadata from the token's metadata account
|
|
103
|
+
return {
|
|
104
|
+
address: tokenAddress,
|
|
105
|
+
name: 'Unknown Token',
|
|
106
|
+
symbol: 'UNKNOWN',
|
|
107
|
+
decimals: 0,
|
|
108
|
+
totalSupply: supply.value.amount
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
throw new Error(`Failed to get token info: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Sends SPL tokens to another address.
|
|
117
|
+
* @param tokenAddress - The token's mint address to send
|
|
118
|
+
* @param to - The recipient's public key address
|
|
119
|
+
* @param amount - The amount of tokens to send as a string
|
|
120
|
+
* @returns Promise resolving to the transaction response with signature and details
|
|
121
|
+
* @throws Error if the transaction fails, insufficient balance, or addresses are invalid
|
|
122
|
+
*/
|
|
123
|
+
async sendToken(tokenAddress, to, amount) {
|
|
124
|
+
try {
|
|
125
|
+
const tokenPubkey = new web3_js_1.PublicKey(tokenAddress);
|
|
126
|
+
const toPubkey = new web3_js_1.PublicKey(to);
|
|
127
|
+
// Get or create associated token account for recipient
|
|
128
|
+
const recipientTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(tokenPubkey, toPubkey);
|
|
129
|
+
// Get sender's associated token account
|
|
130
|
+
const senderTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(tokenPubkey, this.keypair.publicKey);
|
|
131
|
+
// Create transfer instruction
|
|
132
|
+
const transferInstruction = (0, spl_token_1.createTransferInstruction)(senderTokenAccount, recipientTokenAccount, this.keypair.publicKey, parseFloat(amount));
|
|
133
|
+
const transaction = new web3_js_1.Transaction().add(transferInstruction);
|
|
134
|
+
const signature = await (0, web3_js_1.sendAndConfirmTransaction)(this.connection, transaction, [this.keypair]);
|
|
135
|
+
return {
|
|
136
|
+
hash: signature,
|
|
137
|
+
from: this.keypair.publicKey.toString(),
|
|
138
|
+
to: to
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
throw new Error(`Failed to send token: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Gets the public key address of the wallet.
|
|
147
|
+
* @returns The public key address as a string
|
|
148
|
+
*/
|
|
149
|
+
getAddress() {
|
|
150
|
+
return this.keypair.publicKey.toString();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Estimates the transaction fee for a typical transaction.
|
|
154
|
+
* @returns Promise resolving to the estimated fee as a bigint in lamports
|
|
155
|
+
* @throws Error if the fee estimation fails
|
|
156
|
+
*/
|
|
157
|
+
async estimateTransactionFee() {
|
|
158
|
+
try {
|
|
159
|
+
const { feeCalculator } = await this.connection.getRecentBlockhash();
|
|
160
|
+
return BigInt(feeCalculator.lamportsPerSignature);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
throw new Error(`Failed to estimate transaction fee: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.SolanaChainService = SolanaChainService;
|