Skip to content

Commit

Permalink
Add withdraw excess with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DogukanGun committed Nov 12, 2024
1 parent 8aeeec3 commit 2bbd89c
Show file tree
Hide file tree
Showing 5 changed files with 997 additions and 233 deletions.
1 change: 1 addition & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ export * from './updateTokenGroupMaxSize';
export * from './updateTokenGroupUpdateAuthority';
export * from './updateTokenMetadataField';
export * from './updateTokenMetadataUpdateAuthority';
export * from './withdrawExcessLamports';
export * from './withdrawWithheldTokensFromAccounts';
export * from './withdrawWithheldTokensFromMint';
204 changes: 204 additions & 0 deletions clients/js/src/generated/instructions/withdrawExcessLamports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/

import {
combineCodec,
getStructDecoder,
getStructEncoder,
getU64Decoder,
getU64Encoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type WritableAccount,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const WITHDRAW_EXCESS_LAMPORTS_DISCRIMINATOR = 27;

export function getWithdrawExcessLamportsDiscriminatorBytes() {
return getU8Encoder().encode(WITHDRAW_EXCESS_LAMPORTS_DISCRIMINATOR);
}

export type WithdrawExcessLamportsInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountSourceAccount extends string | IAccountMeta<string> = string,
TAccountDestinationAccount extends string | IAccountMeta<string> = string,
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountSourceAccount extends string
? WritableAccount<TAccountSourceAccount>
: TAccountSourceAccount,
TAccountDestinationAccount extends string
? WritableAccount<TAccountDestinationAccount>
: TAccountDestinationAccount,
...TRemainingAccounts,
]
>;

export type WithdrawExcessLamportsInstructionData = {
discriminator: number;
amount: bigint;
};

export type WithdrawExcessLamportsInstructionDataArgs = {
discriminator?: number;
amount: number | bigint;
};

export function getWithdrawExcessLamportsInstructionDataEncoder(): Encoder<WithdrawExcessLamportsInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['amount', getU64Encoder()],
]),
(value) => ({
...value,
discriminator:
value.discriminator ?? WITHDRAW_EXCESS_LAMPORTS_DISCRIMINATOR,
})
);
}

export function getWithdrawExcessLamportsInstructionDataDecoder(): Decoder<WithdrawExcessLamportsInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['amount', getU64Decoder()],
]);
}

export function getWithdrawExcessLamportsInstructionDataCodec(): Codec<
WithdrawExcessLamportsInstructionDataArgs,
WithdrawExcessLamportsInstructionData
> {
return combineCodec(
getWithdrawExcessLamportsInstructionDataEncoder(),
getWithdrawExcessLamportsInstructionDataDecoder()
);
}

export type WithdrawExcessLamportsInput<
TAccountSourceAccount extends string = string,
TAccountDestinationAccount extends string = string,
> = {
/** Account holding excess lamports. */
sourceAccount: Address<TAccountSourceAccount>;
/** Destination account for withdrawn lamports. */
destinationAccount: Address<TAccountDestinationAccount>;
discriminator?: WithdrawExcessLamportsInstructionDataArgs['discriminator'];
amount: WithdrawExcessLamportsInstructionDataArgs['amount'];
};

export function getWithdrawExcessLamportsInstruction<
TAccountSourceAccount extends string,
TAccountDestinationAccount extends string,
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS,
>(
input: WithdrawExcessLamportsInput<
TAccountSourceAccount,
TAccountDestinationAccount
>,
config?: { programAddress?: TProgramAddress }
): WithdrawExcessLamportsInstruction<
TProgramAddress,
TAccountSourceAccount,
TAccountDestinationAccount
> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_2022_PROGRAM_ADDRESS;

// Original accounts.
const originalAccounts = {
sourceAccount: { value: input.sourceAccount ?? null, isWritable: true },
destinationAccount: {
value: input.destinationAccount ?? null,
isWritable: true,
},
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;

// Original args.
const args = { ...input };

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [
getAccountMeta(accounts.sourceAccount),
getAccountMeta(accounts.destinationAccount),
],
programAddress,
data: getWithdrawExcessLamportsInstructionDataEncoder().encode(
args as WithdrawExcessLamportsInstructionDataArgs
),
} as WithdrawExcessLamportsInstruction<
TProgramAddress,
TAccountSourceAccount,
TAccountDestinationAccount
>;

return instruction;
}

export type ParsedWithdrawExcessLamportsInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
/** Account holding excess lamports. */
sourceAccount: TAccountMetas[0];
/** Destination account for withdrawn lamports. */
destinationAccount: TAccountMetas[1];
};
data: WithdrawExcessLamportsInstructionData;
};

export function parseWithdrawExcessLamportsInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedWithdrawExcessLamportsInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 2) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
const accountMeta = instruction.accounts![accountIndex]!;
accountIndex += 1;
return accountMeta;
};
return {
programAddress: instruction.programAddress,
accounts: {
sourceAccount: getNextAccount(),
destinationAccount: getNextAccount(),
},
data: getWithdrawExcessLamportsInstructionDataDecoder().decode(
instruction.data
),
};
}
8 changes: 8 additions & 0 deletions clients/js/src/generated/programs/token2022.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import {
type ParsedUpdateTokenGroupUpdateAuthorityInstruction,
type ParsedUpdateTokenMetadataFieldInstruction,
type ParsedUpdateTokenMetadataUpdateAuthorityInstruction,
type ParsedWithdrawExcessLamportsInstruction,
type ParsedWithdrawWithheldTokensFromAccountsInstruction,
type ParsedWithdrawWithheldTokensFromMintInstruction,
} from '../instructions';
Expand Down Expand Up @@ -121,6 +122,7 @@ export enum Token2022Instruction {
CloseAccount,
FreezeAccount,
ThawAccount,
WithdrawExcessLamports,
TransferChecked,
ApproveChecked,
MintToChecked,
Expand Down Expand Up @@ -217,6 +219,9 @@ export function identifyToken2022Instruction(
if (containsBytes(data, getU8Encoder().encode(11), 0)) {
return Token2022Instruction.ThawAccount;
}
if (containsBytes(data, getU8Encoder().encode(27), 0)) {
return Token2022Instruction.WithdrawExcessLamports;
}
if (containsBytes(data, getU8Encoder().encode(12), 0)) {
return Token2022Instruction.TransferChecked;
}
Expand Down Expand Up @@ -559,6 +564,9 @@ export type ParsedToken2022Instruction<
| ({
instructionType: Token2022Instruction.ThawAccount;
} & ParsedThawAccountInstruction<TProgram>)
| ({
instructionType: Token2022Instruction.WithdrawExcessLamports;
} & ParsedWithdrawExcessLamportsInstruction<TProgram>)
| ({
instructionType: Token2022Instruction.TransferChecked;
} & ParsedTransferCheckedInstruction<TProgram>)
Expand Down
43 changes: 43 additions & 0 deletions clients/js/test/withdrawExcess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Account, generateKeyPairSigner } from '@solana/web3.js';
import test from 'ava';
import {
TOKEN_2022_PROGRAM_ADDRESS,
getWithdrawExcessLamportsInstruction,
} from '../src';
import {
createDefaultSolanaClient,
generateKeyPairSignerWithSol,
sendAndConfirmInstructions,
} from './_setup';
import { getCreateAccountInstruction } from '@solana-program/system';

test('it withdraws excess lamports from a source account to a destination account', async (t) => {
const client = createDefaultSolanaClient();
const [sourceAccount, destinationAccount] = await Promise.all([
generateKeyPairSignerWithSol(client),
generateKeyPairSigner(),
]);

// Create and fund the destination account
await sendAndConfirmInstructions(client, sourceAccount, [
getCreateAccountInstruction({
payer: sourceAccount,
newAccount: destinationAccount,
lamports: BigInt(1_000_000),
space: 0,
programAddress: TOKEN_2022_PROGRAM_ADDRESS,
}),
getWithdrawExcessLamportsInstruction({
sourceAccount: sourceAccount.address,
destinationAccount: destinationAccount.address,
amount: BigInt(500_000),
}),
]);

// Verify withdrawal by checking the balances
const sourceBalance = await client.rpc.getBalance(sourceAccount.address).send();
const destinationBalance = await client.rpc.getBalance(destinationAccount.address).send();

t.true(sourceBalance < 1_000_000);
t.true(destinationBalance >= 1_500_000);
});
Loading

0 comments on commit 2bbd89c

Please sign in to comment.