This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
token-instructions.js
138 lines (128 loc) · 3.6 KB
/
token-instructions.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as BufferLayout from 'buffer-layout';
import {
PublicKey,
SYSVAR_RENT_PUBKEY,
TransactionInstruction,
} from '@solana/web3.js';
import { publicKeyLayout } from './layout';
export const TOKEN_PROGRAM_ID = new PublicKey(
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
);
export const WRAPPED_SOL_MINT = new PublicKey(
'So11111111111111111111111111111111111111111',
);
const LAYOUT = BufferLayout.union(BufferLayout.u8('instruction'));
LAYOUT.addVariant(
0,
BufferLayout.struct([
BufferLayout.u8('decimals'),
publicKeyLayout('mintAuthority'),
BufferLayout.u8('freezeAuthorityOption'),
publicKeyLayout('freezeAuthority'),
]),
'initializeMint',
);
LAYOUT.addVariant(1, BufferLayout.struct([]), 'initializeAccount');
LAYOUT.addVariant(
3,
BufferLayout.struct([BufferLayout.nu64('amount')]),
'transfer',
);
LAYOUT.addVariant(
7,
BufferLayout.struct([BufferLayout.nu64('amount')]),
'mintTo',
);
LAYOUT.addVariant(
8,
BufferLayout.struct([BufferLayout.nu64('amount')]),
'burn',
);
const instructionMaxSpan = Math.max(
...Object.values(LAYOUT.registry).map((r) => r.span),
);
function encodeTokenInstructionData(instruction) {
const b = Buffer.alloc(instructionMaxSpan);
const span = LAYOUT.encode(instruction, b);
return b.slice(0, span);
}
export function initializeMint({
mint,
decimals,
mintAuthority,
freezeAuthority = null,
}) {
const keys = [
{ pubkey: mint, isSigner: false, isWritable: true },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
];
return new TransactionInstruction({
keys,
data: encodeTokenInstructionData({
initializeMint: {
decimals,
mintAuthority,
freezeAuthorityOption: !!freezeAuthority,
freezeAuthority: freezeAuthority || new PublicKey(0),
},
}),
programId: TOKEN_PROGRAM_ID,
});
}
export function initializeAccount({ account, mint, owner }) {
const keys = [
{ pubkey: account, isSigner: false, isWritable: true },
{ pubkey: mint, isSigner: false, isWritable: false },
{ pubkey: owner, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
];
return new TransactionInstruction({
keys,
data: encodeTokenInstructionData({
initializeAccount: {},
}),
programId: TOKEN_PROGRAM_ID,
});
}
export function transfer({ source, destination, amount, owner }) {
const keys = [
{ pubkey: source, isSigner: false, isWritable: true },
{ pubkey: destination, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: true, isWritable: false },
];
return new TransactionInstruction({
keys,
data: encodeTokenInstructionData({
transfer: { amount },
}),
programId: TOKEN_PROGRAM_ID,
});
}
export function approve({ source, delegate, amount, owner }) {
const keys = [
{ pubkey: source, isSigner: false, isWritable: true },
{ pubkey: delegate, isSigner: false, isWritable: false },
{ pubkey: owner, isSigner: true, isWritable: false },
];
return new TransactionInstruction({
keys,
data: encodeTokenInstructionData({
approve: { amount },
}),
programId: TOKEN_PROGRAM_ID,
});
}
export function mintTo({ mint, destination, amount, mintAuthority }) {
const keys = [
{ pubkey: mint, isSigner: false, isWritable: true },
{ pubkey: destination, isSigner: false, isWritable: true },
{ pubkey: mintAuthority, isSigner: true, isWritable: false },
];
return new TransactionInstruction({
keys,
data: encodeTokenInstructionData({
mintTo: { amount },
}),
programId: TOKEN_PROGRAM_ID,
});
}