2020-08-12 11:45:42 -07:00
|
|
|
import * as BufferLayout from 'buffer-layout';
|
2020-08-29 05:11:15 -07:00
|
|
|
import {
|
|
|
|
PublicKey,
|
|
|
|
SYSVAR_RENT_PUBKEY,
|
|
|
|
TransactionInstruction,
|
|
|
|
} from '@solana/web3.js';
|
|
|
|
import { publicKeyLayout } from './layout';
|
2020-08-12 11:45:42 -07:00
|
|
|
|
2020-09-14 08:45:46 -07:00
|
|
|
// NOTE: Update these if the position of arguments for the initializeAccount instruction changes
|
|
|
|
export const INITIALIZE_ACCOUNT_ACCOUNT_INDEX = 0;
|
|
|
|
export const INITIALIZE_ACCOUNT_MINT_INDEX = 1;
|
|
|
|
export const INITIALIZE_ACCOUNT_OWNER_INDEX = 2;
|
|
|
|
|
|
|
|
// NOTE: Update these if the position of arguments for the transfer instruction changes
|
|
|
|
export const TRANSFER_SOURCE_INDEX = 0;
|
|
|
|
export const TRANSFER_DESTINATION_INDEX = 1;
|
|
|
|
export const TRANSFER_OWNER_INDEX = 2;
|
|
|
|
|
|
|
|
// NOTE: Update these if the position of arguments for the closeAccount instruction changes
|
|
|
|
export const CLOSE_ACCOUNT_SOURCE_INDEX = 0;
|
|
|
|
export const CLOSE_ACCOUNT_DESTINATION_INDEX = 1;
|
|
|
|
export const CLOSE_ACCOUNT_OWNER_INDEX = 2;
|
|
|
|
|
2020-08-12 11:45:42 -07:00
|
|
|
export const TOKEN_PROGRAM_ID = new PublicKey(
|
2020-08-29 04:00:10 -07:00
|
|
|
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
|
2020-08-12 11:45:42 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
export const WRAPPED_SOL_MINT = new PublicKey(
|
2020-09-01 05:45:45 -07:00
|
|
|
'So11111111111111111111111111111111111111112',
|
2020-08-12 11:45:42 -07:00
|
|
|
);
|
|
|
|
|
2020-09-06 18:50:56 -07:00
|
|
|
export const SRM_MINT = new PublicKey(
|
|
|
|
'SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt',
|
|
|
|
);
|
|
|
|
|
|
|
|
export const MSRM_MINT = new PublicKey(
|
|
|
|
'MSRMcoVyrFxnSgo5uXwone5SKcGhT1KEJMFEkMEWf9L',
|
|
|
|
);
|
|
|
|
|
2020-08-12 11:45:42 -07:00
|
|
|
const LAYOUT = BufferLayout.union(BufferLayout.u8('instruction'));
|
|
|
|
LAYOUT.addVariant(
|
|
|
|
0,
|
|
|
|
BufferLayout.struct([
|
|
|
|
BufferLayout.u8('decimals'),
|
2020-08-29 05:11:15 -07:00
|
|
|
publicKeyLayout('mintAuthority'),
|
|
|
|
BufferLayout.u8('freezeAuthorityOption'),
|
|
|
|
publicKeyLayout('freezeAuthority'),
|
2020-08-12 11:45:42 -07:00
|
|
|
]),
|
|
|
|
'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',
|
|
|
|
);
|
2020-09-01 05:45:45 -07:00
|
|
|
LAYOUT.addVariant(9, BufferLayout.struct([]), 'closeAccount');
|
2020-08-12 11:45:42 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-14 00:30:19 -07:00
|
|
|
export function decodeTokenInstructionData(instruction) {
|
|
|
|
return LAYOUT.decode(instruction);
|
|
|
|
}
|
|
|
|
|
2020-08-12 11:45:42 -07:00
|
|
|
export function initializeMint({
|
|
|
|
mint,
|
|
|
|
decimals,
|
2020-08-29 05:11:15 -07:00
|
|
|
mintAuthority,
|
|
|
|
freezeAuthority = null,
|
2020-08-12 11:45:42 -07:00
|
|
|
}) {
|
2020-08-29 05:11:15 -07:00
|
|
|
const keys = [
|
|
|
|
{ pubkey: mint, isSigner: false, isWritable: true },
|
|
|
|
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
|
|
|
|
];
|
2020-08-12 11:45:42 -07:00
|
|
|
return new TransactionInstruction({
|
|
|
|
keys,
|
|
|
|
data: encodeTokenInstructionData({
|
|
|
|
initializeMint: {
|
|
|
|
decimals,
|
2020-08-29 05:11:15 -07:00
|
|
|
mintAuthority,
|
|
|
|
freezeAuthorityOption: !!freezeAuthority,
|
|
|
|
freezeAuthority: freezeAuthority || new PublicKey(0),
|
2020-08-12 11:45:42 -07:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
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 },
|
2020-08-29 05:11:15 -07:00
|
|
|
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
|
2020-08-12 11:45:42 -07:00
|
|
|
];
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-29 05:11:15 -07:00
|
|
|
export function mintTo({ mint, destination, amount, mintAuthority }) {
|
2020-08-12 11:45:42 -07:00
|
|
|
const keys = [
|
|
|
|
{ pubkey: mint, isSigner: false, isWritable: true },
|
2020-08-29 05:11:15 -07:00
|
|
|
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
|
|
{ pubkey: mintAuthority, isSigner: true, isWritable: false },
|
2020-08-12 11:45:42 -07:00
|
|
|
];
|
|
|
|
return new TransactionInstruction({
|
|
|
|
keys,
|
|
|
|
data: encodeTokenInstructionData({
|
|
|
|
mintTo: { amount },
|
|
|
|
}),
|
|
|
|
programId: TOKEN_PROGRAM_ID,
|
|
|
|
});
|
|
|
|
}
|
2020-09-01 05:45:45 -07:00
|
|
|
|
|
|
|
export function closeAccount({ source, destination, 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({
|
|
|
|
closeAccount: {},
|
|
|
|
}),
|
|
|
|
programId: TOKEN_PROGRAM_ID,
|
|
|
|
});
|
|
|
|
}
|