@cryptorubic/web3 0.10.0 → 0.10.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Abi, MulticallResponse, MulticallParameters } from 'viem';
|
|
2
2
|
import { AbstractAdapter } from './abstract-adapter';
|
|
3
|
-
import { AddressLookupTableAccount, Connection, Keypair, PublicKey, TransactionInstruction, VersionedTransaction } from '@solana/web3.js';
|
|
3
|
+
import { AddressLookupTableAccount, Connection, Keypair, PublicKey, Transaction, TransactionInstruction, VersionedTransaction } from '@solana/web3.js';
|
|
4
4
|
import { HttpClient, ICustomLogger, PriceTokenAmount, SolanaBlockchainName, Token, TokenAmount } from '@cryptorubic/core';
|
|
5
5
|
import { EvmTransactionConfig } from '../../utils/models/evm-transaction-config';
|
|
6
6
|
import BigNumber from 'bignumber.js';
|
|
@@ -32,4 +32,8 @@ export declare class SolanaAdapter extends AbstractAdapter<Connection, Connectio
|
|
|
32
32
|
getTxWithPayer(tx: VersionedTransaction, feePayer: string): Promise<VersionedTransaction>;
|
|
33
33
|
deserializeTransaction(data: string): VersionedTransaction;
|
|
34
34
|
serializeTransactionToBase58(tx: VersionedTransaction): string;
|
|
35
|
+
createTransferData(fromToken: PriceTokenAmount, walletAddress: string, receiver: string, convertToVersionedTx: boolean): Promise<Transaction | VersionedTransaction>;
|
|
36
|
+
createNativeTransfer(fromAddress: PublicKey, toAddress: PublicKey, fromWeiAmount: string): Transaction;
|
|
37
|
+
createTokenTransfer(fromAddress: PublicKey, toAddress: PublicKey, fromTokenAddress: string, fromWeiAmount: string): Promise<Transaction>;
|
|
38
|
+
getLatestBlockhash(): Promise<string>;
|
|
35
39
|
}
|
|
@@ -115,7 +115,7 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
115
115
|
return accounts.filter((v) => !!v);
|
|
116
116
|
}
|
|
117
117
|
async createVersionedTransaction(instructions, walletAddress, addressLookupTableAccounts = []) {
|
|
118
|
-
const
|
|
118
|
+
const blockhash = await this.getLatestBlockhash();
|
|
119
119
|
const message = new web3_js_1.TransactionMessage({
|
|
120
120
|
instructions,
|
|
121
121
|
payerKey: new web3_js_1.PublicKey(walletAddress),
|
|
@@ -163,5 +163,47 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
163
163
|
serializeTransactionToBase58(tx) {
|
|
164
164
|
return bs58_1.default.encode(tx.serialize());
|
|
165
165
|
}
|
|
166
|
+
async createTransferData(fromToken, walletAddress, receiver, convertToVersionedTx) {
|
|
167
|
+
const fromAddress = new web3_js_1.PublicKey(walletAddress);
|
|
168
|
+
const toAddress = new web3_js_1.PublicKey(receiver);
|
|
169
|
+
const transaction = fromToken.isNative
|
|
170
|
+
? this.createNativeTransfer(fromAddress, toAddress, fromToken.stringWeiAmount)
|
|
171
|
+
: await this.createTokenTransfer(fromAddress, toAddress, fromToken.address, fromToken.stringWeiAmount);
|
|
172
|
+
if (convertToVersionedTx) {
|
|
173
|
+
const blockhash = await this.getLatestBlockhash();
|
|
174
|
+
const message = new web3_js_1.TransactionMessage({
|
|
175
|
+
payerKey: fromAddress,
|
|
176
|
+
recentBlockhash: blockhash,
|
|
177
|
+
instructions: transaction.instructions
|
|
178
|
+
}).compileToV0Message();
|
|
179
|
+
return new web3_js_1.VersionedTransaction(message);
|
|
180
|
+
}
|
|
181
|
+
return transaction;
|
|
182
|
+
}
|
|
183
|
+
createNativeTransfer(fromAddress, toAddress, fromWeiAmount) {
|
|
184
|
+
return new web3_js_1.Transaction().add(web3_js_1.SystemProgram.transfer({
|
|
185
|
+
fromPubkey: fromAddress,
|
|
186
|
+
toPubkey: toAddress,
|
|
187
|
+
lamports: BigInt(fromWeiAmount)
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
async createTokenTransfer(fromAddress, toAddress, fromTokenAddress, fromWeiAmount) {
|
|
191
|
+
const mintTokenAccount = new web3_js_1.PublicKey(fromTokenAddress);
|
|
192
|
+
const [fromATA, toATA] = await Promise.all([
|
|
193
|
+
(0, spl_token_1.getAssociatedTokenAddress)(mintTokenAccount, fromAddress),
|
|
194
|
+
(0, spl_token_1.getAssociatedTokenAddress)(mintTokenAccount, toAddress)
|
|
195
|
+
]);
|
|
196
|
+
const transaction = new web3_js_1.Transaction();
|
|
197
|
+
const receiverAccountInfo = await this.public.getAccountInfo(toATA);
|
|
198
|
+
if (!receiverAccountInfo) {
|
|
199
|
+
transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(fromAddress, toATA, toAddress, mintTokenAccount));
|
|
200
|
+
}
|
|
201
|
+
transaction.add((0, spl_token_1.createTransferInstruction)(fromATA, toATA, fromAddress, BigInt(fromWeiAmount)));
|
|
202
|
+
return transaction;
|
|
203
|
+
}
|
|
204
|
+
async getLatestBlockhash() {
|
|
205
|
+
const { blockhash } = await this.public.getLatestBlockhash();
|
|
206
|
+
return blockhash;
|
|
207
|
+
}
|
|
166
208
|
}
|
|
167
209
|
exports.SolanaAdapter = SolanaAdapter;
|