@cryptorubic/web3 0.8.17-alpha.solana.11 → 0.8.17-alpha.solana.12
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 BigNumber from 'bignumber.js';
|
|
6
6
|
import { SolanaRawInstruction } from './models/solana-web3-types';
|
|
@@ -36,4 +36,8 @@ export declare class SolanaAdapter extends AbstractAdapter<Connection, Connectio
|
|
|
36
36
|
getTxWithPayer(tx: VersionedTransaction, feePayer: string): Promise<VersionedTransaction>;
|
|
37
37
|
deserializeTransaction(data: string): VersionedTransaction;
|
|
38
38
|
serializeTransactionToBase58(tx: VersionedTransaction): string;
|
|
39
|
+
createTransferData(fromToken: PriceTokenAmount, walletAddress: string, receiver: string, convertToVersionedTx: boolean): Promise<Transaction | VersionedTransaction>;
|
|
40
|
+
createNativeTransfer(fromAddress: PublicKey, toAddress: PublicKey, fromWeiAmount: string): Transaction;
|
|
41
|
+
createTokenTransfer(fromAddress: PublicKey, toAddress: PublicKey, fromTokenAddress: string, fromWeiAmount: string): Promise<Transaction>;
|
|
42
|
+
getLatestBlockhash(): Promise<string>;
|
|
39
43
|
}
|
|
@@ -141,7 +141,7 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
141
141
|
return accounts.filter((v) => !!v);
|
|
142
142
|
}
|
|
143
143
|
async createVersionedTransaction(instructions, walletAddress, addressLookupTableAccounts = []) {
|
|
144
|
-
const
|
|
144
|
+
const blockhash = await this.getLatestBlockhash();
|
|
145
145
|
const message = new web3_js_1.TransactionMessage({
|
|
146
146
|
instructions,
|
|
147
147
|
payerKey: new web3_js_1.PublicKey(walletAddress),
|
|
@@ -189,5 +189,47 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
|
|
|
189
189
|
serializeTransactionToBase58(tx) {
|
|
190
190
|
return bs58_1.default.encode(tx.serialize());
|
|
191
191
|
}
|
|
192
|
+
async createTransferData(fromToken, walletAddress, receiver, convertToVersionedTx) {
|
|
193
|
+
const fromAddress = new web3_js_1.PublicKey(walletAddress);
|
|
194
|
+
const toAddress = new web3_js_1.PublicKey(receiver);
|
|
195
|
+
const transaction = fromToken.isNative
|
|
196
|
+
? this.createNativeTransfer(fromAddress, toAddress, fromToken.stringWeiAmount)
|
|
197
|
+
: await this.createTokenTransfer(fromAddress, toAddress, fromToken.address, fromToken.stringWeiAmount);
|
|
198
|
+
if (convertToVersionedTx) {
|
|
199
|
+
const blockhash = await this.getLatestBlockhash();
|
|
200
|
+
const message = new web3_js_1.TransactionMessage({
|
|
201
|
+
payerKey: fromAddress,
|
|
202
|
+
recentBlockhash: blockhash,
|
|
203
|
+
instructions: transaction.instructions
|
|
204
|
+
}).compileToV0Message();
|
|
205
|
+
return new web3_js_1.VersionedTransaction(message);
|
|
206
|
+
}
|
|
207
|
+
return transaction;
|
|
208
|
+
}
|
|
209
|
+
createNativeTransfer(fromAddress, toAddress, fromWeiAmount) {
|
|
210
|
+
return new web3_js_1.Transaction().add(web3_js_1.SystemProgram.transfer({
|
|
211
|
+
fromPubkey: fromAddress,
|
|
212
|
+
toPubkey: toAddress,
|
|
213
|
+
lamports: BigInt(fromWeiAmount)
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
async createTokenTransfer(fromAddress, toAddress, fromTokenAddress, fromWeiAmount) {
|
|
217
|
+
const mintTokenAccount = new web3_js_1.PublicKey(fromTokenAddress);
|
|
218
|
+
const [fromATA, toATA] = await Promise.all([
|
|
219
|
+
(0, spl_token_1.getAssociatedTokenAddress)(mintTokenAccount, fromAddress),
|
|
220
|
+
(0, spl_token_1.getAssociatedTokenAddress)(mintTokenAccount, toAddress)
|
|
221
|
+
]);
|
|
222
|
+
const transaction = new web3_js_1.Transaction();
|
|
223
|
+
const receiverAccountInfo = await this.public.getAccountInfo(toATA);
|
|
224
|
+
if (!receiverAccountInfo) {
|
|
225
|
+
transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(fromAddress, toATA, toAddress, mintTokenAccount));
|
|
226
|
+
}
|
|
227
|
+
transaction.add((0, spl_token_1.createTransferInstruction)(fromATA, toATA, fromAddress, BigInt(fromWeiAmount)));
|
|
228
|
+
return transaction;
|
|
229
|
+
}
|
|
230
|
+
async getLatestBlockhash() {
|
|
231
|
+
const { blockhash } = await this.public.getLatestBlockhash();
|
|
232
|
+
return blockhash;
|
|
233
|
+
}
|
|
192
234
|
}
|
|
193
235
|
exports.SolanaAdapter = SolanaAdapter;
|