-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgas-less-transfer.ts
76 lines (71 loc) · 2.2 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
import { Result, Try } from '~/suite-utils';
import { Account } from '~/account';
import { Node } from '~/node';
import { Pubkey, Secret } from '~/types/account';
import { TransactionBuilder } from '~/transaction-builder';
import { Transaction } from '@solana/web3.js';
import { CompressedNft as Transfer } from './transfer';
import { CompressedNft as Delegate } from './gas-less-delegate';
import {
GasLessTransferOptions,
PartialSignStructure,
} from '~/types/transaction-builder';
export namespace CompressedNft {
/**
* Transfer with gas-less
* @param {Pubkey} mint
* @param {Secret} owner
* @param {Pubkey} dest
* @param {Pubkey} feePayer
* @param {Partial<GassLessTransferOptions> } options
* @returns {Promise<Result<PartialSignTransaction[], Error>>}
*/
export const gasLessTransfer = async (
mint: Pubkey,
owner: Secret,
dest: Pubkey,
feePayer: Pubkey,
options: Partial<GasLessTransferOptions> = {},
): Promise<Result<PartialSignStructure, Error>> => {
return Try(async () => {
const delegate = await Delegate.gasLessDelegate(mint, owner, feePayer);
await delegate.submit();
const blockhashObj = await Node.getConnection().getLatestBlockhash();
const tx = new Transaction({
lastValidBlockHeight: blockhashObj.lastValidBlockHeight,
blockhash: blockhashObj.blockhash,
feePayer: feePayer.toPublicKey(),
});
tx.add(
await Transfer.createTransfer(
mint,
new Account.Keypair({ secret: owner }).pubkey,
dest,
feePayer,
),
);
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;
return new TransactionBuilder.PartialSign(
tx
.serialize({
requireAllSignatures: false,
})
.toString('hex'),
);
});
};
}