Revert "Use transfer checked instruction (#191)"

This reverts commit d368648476.
This commit is contained in:
armaniferrante 2021-04-19 21:11:44 -07:00
parent cc9d882ffe
commit bae31ce0d2
No known key found for this signature in database
GPG Key ID: 58BEF301E91F7828
5 changed files with 46 additions and 33 deletions

View File

@ -115,7 +115,6 @@ export default function MergeAccountsDialog({ open, onClose }) {
assocTokAddr,
mintGroup,
mint,
tokenInfo.decimals,
wallet,
connection,
enqueueSnackbar,
@ -243,11 +242,11 @@ async function mergeMint(
assocTokAddr,
mintAccountSet,
mint,
decimals,
wallet,
connection,
enqueueSnackbar,
) {
console.log('mint', mint, mint.toString());
if (mintAccountSet.length === 0) {
return;
}
@ -293,7 +292,6 @@ async function mergeMint(
associatedTokenAccount,
tokenAccount.account.amount,
mint,
decimals,
);
}
}

View File

@ -261,7 +261,6 @@ function SendSplDialog({ onClose, publicKey, balanceInfo, onSubmitRef }) {
new PublicKey(destinationAddress),
amount,
balanceInfo.mint,
decimals,
null,
overrideDestinationCheck,
);

View File

@ -14,7 +14,7 @@ import {
memoInstruction,
mintTo,
TOKEN_PROGRAM_ID,
transferChecked,
transfer,
} from './instructions';
import {
ACCOUNT_LAYOUT,
@ -300,7 +300,6 @@ export async function transferTokens({
amount,
memo,
mint,
decimals,
overrideDestinationCheck,
}) {
const destinationAccountInfo = await connection.getAccountInfo(
@ -313,8 +312,6 @@ export async function transferTokens({
return await transferBetweenSplTokenAccounts({
connection,
owner,
mint,
decimals,
sourcePublicKey,
destinationPublicKey,
amount,
@ -342,8 +339,6 @@ export async function transferTokens({
return await transferBetweenSplTokenAccounts({
connection,
owner,
mint,
decimals,
sourcePublicKey,
destinationPublicKey: destinationSplTokenAccount.publicKey,
amount,
@ -358,24 +353,44 @@ export async function transferTokens({
amount,
memo,
mint,
decimals,
});
}
// SPL tokens only.
export async function transferAndClose({
connection,
owner,
sourcePublicKey,
destinationPublicKey,
amount,
}) {
const tx = createTransferBetweenSplTokenAccountsInstruction({
ownerPublicKey: owner.publicKey,
sourcePublicKey,
destinationPublicKey,
amount,
});
tx.add(
closeAccount({
source: sourcePublicKey,
destination: owner.publicKey,
owner: owner.publicKey,
}),
);
let signers = [];
return await signAndSendTransaction(connection, tx, owner, signers);
}
function createTransferBetweenSplTokenAccountsInstruction({
ownerPublicKey,
mint,
decimals,
sourcePublicKey,
destinationPublicKey,
amount,
memo,
}) {
let transaction = new Transaction().add(
transferChecked({
transfer({
source: sourcePublicKey,
mint,
decimals,
destination: destinationPublicKey,
owner: ownerPublicKey,
amount,
@ -390,8 +405,6 @@ function createTransferBetweenSplTokenAccountsInstruction({
async function transferBetweenSplTokenAccounts({
connection,
owner,
mint,
decimals,
sourcePublicKey,
destinationPublicKey,
amount,
@ -399,8 +412,6 @@ async function transferBetweenSplTokenAccounts({
}) {
const transaction = createTransferBetweenSplTokenAccountsInstruction({
ownerPublicKey: owner.publicKey,
mint,
decimals,
sourcePublicKey,
destinationPublicKey,
amount,
@ -418,7 +429,6 @@ async function createAndTransferToAccount({
amount,
memo,
mint,
decimals,
}) {
const [
createAccountInstruction,
@ -439,8 +449,6 @@ async function createAndTransferToAccount({
const transferBetweenAccountsTxn = createTransferBetweenSplTokenAccountsInstruction(
{
ownerPublicKey: owner.publicKey,
mint,
decimals,
sourcePublicKey,
destinationPublicKey: newAddress,
amount,

View File

@ -29,6 +29,11 @@ LAYOUT.addVariant(
'initializeMint',
);
LAYOUT.addVariant(1, BufferLayout.struct([]), 'initializeAccount');
LAYOUT.addVariant(
3,
BufferLayout.struct([BufferLayout.nu64('amount')]),
'transfer',
);
LAYOUT.addVariant(
7,
BufferLayout.struct([BufferLayout.nu64('amount')]),
@ -40,11 +45,6 @@ LAYOUT.addVariant(
'burn',
);
LAYOUT.addVariant(9, BufferLayout.struct([]), 'closeAccount');
LAYOUT.addVariant(
12,
BufferLayout.struct([BufferLayout.nu64('amount'), BufferLayout.u8('decimals')]),
'transferChecked',
);
const instructionMaxSpan = Math.max(
...Object.values(LAYOUT.registry).map((r) => r.span),
@ -96,17 +96,16 @@ export function initializeAccount({ account, mint, owner }) {
});
}
export function transferChecked({ source, mint, destination, amount, decimals, owner }) {
export function transfer({ source, destination, amount, owner }) {
let keys = [
{ pubkey: source, isSigner: false, isWritable: true },
{ pubkey: mint, isSigner: false, isWritable: false },
{ pubkey: destination, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: true, isWritable: false },
];
return new TransactionInstruction({
keys,
data: encodeTokenInstructionData({
transferChecked: { amount, decimals },
transfer: { amount },
}),
programId: TOKEN_PROGRAM_ID,
});

View File

@ -14,6 +14,7 @@ import {
getOwnedTokenAccounts,
nativeTransfer,
transferTokens,
transferAndClose,
} from './tokens';
import { TOKEN_PROGRAM_ID } from './tokens/instructions';
import {
@ -98,7 +99,6 @@ export class Wallet {
destination,
amount,
mint,
decimals,
memo = null,
overrideDestinationCheck = false,
) => {
@ -116,7 +116,6 @@ export class Wallet {
amount,
memo,
mint,
decimals,
overrideDestinationCheck,
});
};
@ -134,6 +133,16 @@ export class Wallet {
});
};
transferAndClose = async (source, destination, amount) => {
return await transferAndClose({
connection: this.connection,
owner: this,
sourcePublicKey: source,
destinationPublicKey: destination,
amount,
});
};
signTransaction = async (transaction) => {
return this.provider.signTransaction(transaction);
};