-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8aeeec3
commit 2bbd89c
Showing
5 changed files
with
997 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
clients/js/src/generated/instructions/withdrawExcessLamports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.