|
| 1 | +import type { |
| 2 | + Address, |
| 3 | + Instruction, |
| 4 | + ReadonlyUint8Array, |
| 5 | + TransactionSigner, |
| 6 | +} from '@solana/kit'; |
| 7 | + |
| 8 | +import { concatBytes, u8, u32le, vec } from '../codecs/binary.js'; |
| 9 | +import { |
| 10 | + encodeH256, |
| 11 | + encodeRemoteRouterConfig, |
| 12 | + type H256, |
| 13 | + type RemoteRouterConfig, |
| 14 | +} from '../codecs/shared.js'; |
| 15 | +import { SYSTEM_PROGRAM_ADDRESS } from '../constants.js'; |
| 16 | +import { |
| 17 | + deriveCrossCollateralDispatchAuthorityPda, |
| 18 | + deriveCrossCollateralStatePda, |
| 19 | + deriveHyperlaneTokenPda, |
| 20 | +} from '../pda.js'; |
| 21 | +import { |
| 22 | + getTokenInitInstruction, |
| 23 | + type TokenInitInstructionData, |
| 24 | +} from './token.js'; |
| 25 | +import { |
| 26 | + buildInstruction, |
| 27 | + type InstructionAccountMeta, |
| 28 | + readonlyAccount, |
| 29 | + writableAccount, |
| 30 | + writableSignerAddress, |
| 31 | +} from './utils.js'; |
| 32 | + |
| 33 | +// Cross-collateral plugin discriminator [2; 8], distinct from the base |
| 34 | +// token program discriminator [1; 8] (PROGRAM_INSTRUCTION_DISCRIMINATOR). |
| 35 | +const CC_INSTRUCTION_DISCRIMINATOR = new Uint8Array([2, 2, 2, 2, 2, 2, 2, 2]); |
| 36 | + |
| 37 | +export enum CrossCollateralInstructionKind { |
| 38 | + SetCrossCollateralRouters = 0, |
| 39 | + TransferRemoteTo = 1, |
| 40 | + HandleLocal = 2, |
| 41 | + HandleLocalAccountMetas = 3, |
| 42 | +} |
| 43 | + |
| 44 | +export type CrossCollateralRouterUpdate = |
| 45 | + | { kind: 'add'; config: { domain: number; router: H256 } } |
| 46 | + | { kind: 'remove'; config: RemoteRouterConfig }; |
| 47 | + |
| 48 | +function encodeCrossCollateralRouterUpdate( |
| 49 | + update: CrossCollateralRouterUpdate, |
| 50 | +): ReadonlyUint8Array { |
| 51 | + if (update.kind === 'add') { |
| 52 | + return concatBytes( |
| 53 | + u8(0), |
| 54 | + u32le(update.config.domain), |
| 55 | + encodeH256(update.config.router), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return concatBytes(u8(1), encodeRemoteRouterConfig(update.config)); |
| 60 | +} |
| 61 | + |
| 62 | +export async function getCrossCollateralInitInstruction( |
| 63 | + programAddress: Address, |
| 64 | + payer: TransactionSigner, |
| 65 | + init: TokenInitInstructionData, |
| 66 | + pluginAccounts: InstructionAccountMeta[], |
| 67 | + mailboxOutboxPda: Address, |
| 68 | +): Promise<Instruction> { |
| 69 | + const { address: ccStatePda } = |
| 70 | + await deriveCrossCollateralStatePda(programAddress); |
| 71 | + const { address: ccDispatchAuthority } = |
| 72 | + await deriveCrossCollateralDispatchAuthorityPda(programAddress); |
| 73 | + |
| 74 | + return getTokenInitInstruction(programAddress, payer, init, [ |
| 75 | + ...pluginAccounts, |
| 76 | + writableAccount(ccStatePda), |
| 77 | + writableAccount(ccDispatchAuthority), |
| 78 | + readonlyAccount(mailboxOutboxPda), |
| 79 | + ]); |
| 80 | +} |
| 81 | + |
| 82 | +export async function getSetCrossCollateralRoutersInstruction( |
| 83 | + programAddress: Address, |
| 84 | + owner: Address, |
| 85 | + updates: CrossCollateralRouterUpdate[], |
| 86 | +): Promise<Instruction> { |
| 87 | + const { address: ccStatePda } = |
| 88 | + await deriveCrossCollateralStatePda(programAddress); |
| 89 | + const { address: tokenPda } = await deriveHyperlaneTokenPda(programAddress); |
| 90 | + |
| 91 | + return buildInstruction( |
| 92 | + programAddress, |
| 93 | + [ |
| 94 | + readonlyAccount(SYSTEM_PROGRAM_ADDRESS), |
| 95 | + writableAccount(ccStatePda), |
| 96 | + readonlyAccount(tokenPda), |
| 97 | + writableSignerAddress(owner), |
| 98 | + ], |
| 99 | + concatBytes( |
| 100 | + CC_INSTRUCTION_DISCRIMINATOR, |
| 101 | + u8(CrossCollateralInstructionKind.SetCrossCollateralRouters), |
| 102 | + vec(updates, encodeCrossCollateralRouterUpdate), |
| 103 | + ), |
| 104 | + ); |
| 105 | +} |
0 commit comments