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