-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgas-less-transfer.ts
100 lines (90 loc) · 3.03 KB
/
gas-less-transfer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { createTransferCheckedInstruction } from '@solana/spl-token';
import { Transaction } from '@solana/web3.js';
import { Node } from '~/node';
import { Result, Try } from '~/suite-utils';
import { TransactionBuilder } from '~/transaction-builder';
import { Pubkey, Secret } from '~/types/account';
import { SplToken as Calculator } from './calculate-amount';
import { Account } from '~/account';
import { PartialSignStructure } from '~/types/transaction-builder';
import { GasLessTransferOptions } from '~/types/transaction-builder';
export namespace SplToken {
/**
* Transfer without solana sol, delegate feepayer for commission
*
* @param {Pubkey} mint
* @param {Secret} owner
* @param {Pubkey} dest
* @param {number} amount
* @param {number} mintDecimal
* @param {Pubkey} feePayer
* @param {Partial<GasLessTransferOptions>} options
* @return Promise<Result<PartialSignStructure, Error>>
*/
export const gasLessTransfer = async (
mint: Pubkey,
owner: Secret,
dest: Pubkey,
amount: number,
mintDecimal: number,
feePayer: Pubkey,
options: Partial<GasLessTransferOptions> = {},
): Promise<Result<PartialSignStructure, Error>> => {
return Try(async () => {
const ownerPublicKey = owner.toKeypair().publicKey;
const sourceToken = await Account.Associated.makeOrCreateInstruction(
mint,
ownerPublicKey.toString(),
feePayer,
);
const destToken = await Account.Associated.makeOrCreateInstruction(
mint,
dest,
feePayer,
);
const blockhashObj = await Node.getConnection().getLatestBlockhash();
const tx = new Transaction({
lastValidBlockHeight: blockhashObj.lastValidBlockHeight,
blockhash: blockhashObj.blockhash,
feePayer: feePayer.toPublicKey(),
});
const inst2 = createTransferCheckedInstruction(
sourceToken.tokenAccount.toPublicKey(),
mint.toPublicKey(),
destToken.tokenAccount.toPublicKey(),
ownerPublicKey,
Calculator.calculateAmount(amount, mintDecimal),
mintDecimal,
[owner.toKeypair()],
);
// return associated token account
if (!destToken.inst) {
tx.add(inst2);
} else {
// return instruction and undecided associated token account
tx.add(destToken.inst).add(inst2);
}
if (options.isPriorityFee) {
tx.instructions.unshift(
await TransactionBuilder.PriorityFee.createInstruction(
tx.instructions,
options.addSolPriorityFee,
),
);
}
tx.instructions.unshift(
await TransactionBuilder.ComputeUnit.createInstruction(
tx.instructions,
owner.toKeypair(),
),
);
tx.recentBlockhash = blockhashObj.blockhash;
tx.partialSign(owner.toKeypair());
const serializedTx = tx.serialize({
requireAllSignatures: false,
});
const hex = serializedTx.toString('hex');
return new TransactionBuilder.PartialSign(hex);
});
};
}