|
| 1 | +import { struct, u8, f64 } from '@solana/buffer-layout'; |
| 2 | +import { publicKey, u64 } from '@solana/buffer-layout-utils'; |
| 3 | +import { TokenInstruction } from '../../instructions/types.js'; |
| 4 | +import type { Signer } from '@solana/web3.js'; |
| 5 | +import { TransactionInstruction, PublicKey } from '@solana/web3.js'; |
| 6 | +import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js'; |
| 7 | +import { TokenUnsupportedInstructionError } from '../../errors.js'; |
| 8 | +import { addSigners } from '../../instructions/internal.js'; |
| 9 | + |
| 10 | +export enum ScaledUiAmountInstruction { |
| 11 | + Initialize = 0, |
| 12 | + UpdateMultiplier = 1, |
| 13 | +} |
| 14 | + |
| 15 | +export interface InitializeScaledUiAmountConfigData { |
| 16 | + instruction: TokenInstruction.ScaledUiAmountExtension; |
| 17 | + scaledUiAmountInstruction: ScaledUiAmountInstruction.Initialize; |
| 18 | + authority: PublicKey | null; |
| 19 | + multiplier: number; |
| 20 | +} |
| 21 | + |
| 22 | +export const initializeScaledUiAmountConfigInstructionData = struct<InitializeScaledUiAmountConfigData>([ |
| 23 | + u8('instruction'), |
| 24 | + u8('scaledUiAmountInstruction'), |
| 25 | + publicKey('authority'), |
| 26 | + f64('multiplier'), |
| 27 | +]); |
| 28 | + |
| 29 | +/** |
| 30 | + * Construct an InitializeScaledUiAmountConfig instruction |
| 31 | + * |
| 32 | + * @param mint Token mint account |
| 33 | + * @param authority Optional authority that can update the multipliers |
| 34 | + * @param signers The signer account(s) |
| 35 | + * @param programId SPL Token program account |
| 36 | + * |
| 37 | + * @return Instruction to add to a transaction |
| 38 | + */ |
| 39 | +export function createInitializeScaledUiAmountConfigInstruction( |
| 40 | + mint: PublicKey, |
| 41 | + authority: PublicKey | null, |
| 42 | + multiplier: number, |
| 43 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID, |
| 44 | +): TransactionInstruction { |
| 45 | + if (!programSupportsExtensions(programId)) { |
| 46 | + throw new TokenUnsupportedInstructionError(); |
| 47 | + } |
| 48 | + const keys = [{ pubkey: mint, isSigner: false, isWritable: true }]; |
| 49 | + |
| 50 | + const data = Buffer.alloc(initializeScaledUiAmountConfigInstructionData.span); |
| 51 | + initializeScaledUiAmountConfigInstructionData.encode( |
| 52 | + { |
| 53 | + instruction: TokenInstruction.ScaledUiAmountExtension, |
| 54 | + scaledUiAmountInstruction: ScaledUiAmountInstruction.Initialize, |
| 55 | + authority: authority ?? PublicKey.default, |
| 56 | + multiplier: multiplier, |
| 57 | + }, |
| 58 | + data, |
| 59 | + ); |
| 60 | + |
| 61 | + return new TransactionInstruction({ keys, programId, data }); |
| 62 | +} |
| 63 | + |
| 64 | +export interface UpdateMultiplierData { |
| 65 | + instruction: TokenInstruction.ScaledUiAmountExtension; |
| 66 | + scaledUiAmountInstruction: ScaledUiAmountInstruction.UpdateMultiplier; |
| 67 | + multiplier: number; |
| 68 | + effectiveTimestamp: bigint; |
| 69 | +} |
| 70 | + |
| 71 | +export const updateMultiplierData = struct<UpdateMultiplierData>([ |
| 72 | + u8('instruction'), |
| 73 | + u8('scaledUiAmountInstruction'), |
| 74 | + f64('multiplier'), |
| 75 | + u64('effectiveTimestamp'), |
| 76 | +]); |
| 77 | + |
| 78 | +/** |
| 79 | + * Construct an UpdateMultiplierData instruction |
| 80 | + * |
| 81 | + * @param mint Token mint account |
| 82 | + * @param authority Optional authority that can update the multipliers |
| 83 | + * @param multiplier New multiplier |
| 84 | + * @param effectiveTimestamp Effective time stamp for the new multiplier |
| 85 | + * @param multiSigners Signing accounts if `owner` is a multisig |
| 86 | + * @param programId SPL Token program account |
| 87 | + * |
| 88 | + * @return Instruction to add to a transaction |
| 89 | + */ |
| 90 | +export function createUpdateMultiplierDataInstruction( |
| 91 | + mint: PublicKey, |
| 92 | + authority: PublicKey, |
| 93 | + multiplier: number, |
| 94 | + effectiveTimestamp: bigint, |
| 95 | + multiSigners: (Signer | PublicKey)[] = [], |
| 96 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID, |
| 97 | +): TransactionInstruction { |
| 98 | + if (!programSupportsExtensions(programId)) { |
| 99 | + throw new TokenUnsupportedInstructionError(); |
| 100 | + } |
| 101 | + const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], authority, multiSigners); |
| 102 | + |
| 103 | + const data = Buffer.alloc(updateMultiplierData.span); |
| 104 | + updateMultiplierData.encode( |
| 105 | + { |
| 106 | + instruction: TokenInstruction.ScaledUiAmountExtension, |
| 107 | + scaledUiAmountInstruction: ScaledUiAmountInstruction.UpdateMultiplier, |
| 108 | + multiplier, |
| 109 | + effectiveTimestamp, |
| 110 | + }, |
| 111 | + data, |
| 112 | + ); |
| 113 | + |
| 114 | + return new TransactionInstruction({ keys, programId, data }); |
| 115 | +} |
0 commit comments