@robertprp/intents-sdk
Version:
Shogun Network Intent-based cross-chain swaps SDK
219 lines (204 loc) • 7.71 kB
text/typescript
/**
* 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 {
assertAccountExists,
assertAccountsExist,
decodeAccount,
fetchEncodedAccount,
fetchEncodedAccounts,
type Account,
type EncodedAccount,
type FetchAccountConfig,
type FetchAccountsConfig,
type MaybeAccount,
type MaybeEncodedAccount,
} from '@solana/kit';
import { getAddressDecoder, getAddressEncoder, type Address } from '@solana/kit';
import {
combineCodec,
fixDecoderSize,
fixEncoderSize,
getBooleanDecoder,
getBooleanEncoder,
getBytesDecoder,
getBytesEncoder,
getStructDecoder,
getStructEncoder,
getU32Decoder,
getU32Encoder,
getU64Decoder,
getU64Encoder,
transformEncoder,
type Codec,
type Decoder,
type Encoder,
type ReadonlyUint8Array,
} from '@solana/kit';
export const ORDER_DISCRIMINATOR = new Uint8Array([134, 173, 223, 185, 77, 86, 28, 51]);
export function getOrderDiscriminatorBytes() {
return fixEncoderSize(getBytesEncoder(), 8).encode(ORDER_DISCRIMINATOR);
}
export type Order = {
discriminator: ReadonlyUint8Array;
/** User address, that created this order */
user: Address;
/** Guard address, that will protect this order execution */
guard: Address;
/** Token IN that user wants to spend */
tokenInMint: Address;
/** Amount of tokens In locked */
amountIn: bigint;
/** Min amount of stablecoins to receive if token IN will be swapped */
minStablecoinsAmount: bigint;
/** Deadline for the operation, in Unix timestamp format, in SECONDS */
deadline: number;
/** 32 bytes SHA-256 hash of execution details JSON string. Everything that's not used in smart contract */
executionDetailsHash: ReadonlyUint8Array;
/** `true` if tokens IN were taken for swap during `pre_swap` instruction */
tokensInWereSwapped: boolean;
/** Amount of stablecoins, locked after swap */
lockedStablecoins: bigint;
/** Collateral amount, locked during order execution */
lockedCollateral: bigint;
/**
* Collateral amount, taken after solver failed to fulfill the order in time.
* Transferred to the user on order fulfillment or cancellation
*/
takenCollateral: bigint;
/** Active solver which is currently executing this order */
activeSolver: Address;
/**
* Timestamp, until which `active_solver` MUST execute the order and claim tokens
* Order is locked for everyone but `active_solver` until this deadline
*/
executionDeadline: number;
};
export type OrderArgs = {
/** User address, that created this order */
user: Address;
/** Guard address, that will protect this order execution */
guard: Address;
/** Token IN that user wants to spend */
tokenInMint: Address;
/** Amount of tokens In locked */
amountIn: number | bigint;
/** Min amount of stablecoins to receive if token IN will be swapped */
minStablecoinsAmount: number | bigint;
/** Deadline for the operation, in Unix timestamp format, in SECONDS */
deadline: number;
/** 32 bytes SHA-256 hash of execution details JSON string. Everything that's not used in smart contract */
executionDetailsHash: ReadonlyUint8Array;
/** `true` if tokens IN were taken for swap during `pre_swap` instruction */
tokensInWereSwapped: boolean;
/** Amount of stablecoins, locked after swap */
lockedStablecoins: number | bigint;
/** Collateral amount, locked during order execution */
lockedCollateral: number | bigint;
/**
* Collateral amount, taken after solver failed to fulfill the order in time.
* Transferred to the user on order fulfillment or cancellation
*/
takenCollateral: number | bigint;
/** Active solver which is currently executing this order */
activeSolver: Address;
/**
* Timestamp, until which `active_solver` MUST execute the order and claim tokens
* Order is locked for everyone but `active_solver` until this deadline
*/
executionDeadline: number;
};
export function getOrderEncoder(): Encoder<OrderArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', fixEncoderSize(getBytesEncoder(), 8)],
['user', getAddressEncoder()],
['guard', getAddressEncoder()],
['tokenInMint', getAddressEncoder()],
['amountIn', getU64Encoder()],
['minStablecoinsAmount', getU64Encoder()],
['deadline', getU32Encoder()],
['executionDetailsHash', fixEncoderSize(getBytesEncoder(), 32)],
['tokensInWereSwapped', getBooleanEncoder()],
['lockedStablecoins', getU64Encoder()],
['lockedCollateral', getU64Encoder()],
['takenCollateral', getU64Encoder()],
['activeSolver', getAddressEncoder()],
['executionDeadline', getU32Encoder()],
]),
(value) => ({ ...value, discriminator: ORDER_DISCRIMINATOR }),
);
}
export function getOrderDecoder(): Decoder<Order> {
return getStructDecoder([
['discriminator', fixDecoderSize(getBytesDecoder(), 8)],
['user', getAddressDecoder()],
['guard', getAddressDecoder()],
['tokenInMint', getAddressDecoder()],
['amountIn', getU64Decoder()],
['minStablecoinsAmount', getU64Decoder()],
['deadline', getU32Decoder()],
['executionDetailsHash', fixDecoderSize(getBytesDecoder(), 32)],
['tokensInWereSwapped', getBooleanDecoder()],
['lockedStablecoins', getU64Decoder()],
['lockedCollateral', getU64Decoder()],
['takenCollateral', getU64Decoder()],
['activeSolver', getAddressDecoder()],
['executionDeadline', getU32Decoder()],
]);
}
export function getOrderCodec(): Codec<OrderArgs, Order> {
return combineCodec(getOrderEncoder(), getOrderDecoder());
}
export function decodeOrder<TAddress extends string = string>(
encodedAccount: EncodedAccount<TAddress>,
): Account<Order, TAddress>;
export function decodeOrder<TAddress extends string = string>(
encodedAccount: MaybeEncodedAccount<TAddress>,
): MaybeAccount<Order, TAddress>;
export function decodeOrder<TAddress extends string = string>(
encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>,
): Account<Order, TAddress> | MaybeAccount<Order, TAddress> {
return decodeAccount(encodedAccount as MaybeEncodedAccount<TAddress>, getOrderDecoder());
}
export async function fetchOrder<TAddress extends string = string>(
rpc: Parameters<typeof fetchEncodedAccount>[0],
address: Address<TAddress>,
config?: FetchAccountConfig,
): Promise<Account<Order, TAddress>> {
const maybeAccount = await fetchMaybeOrder(rpc, address, config);
assertAccountExists(maybeAccount);
return maybeAccount;
}
export async function fetchMaybeOrder<TAddress extends string = string>(
rpc: Parameters<typeof fetchEncodedAccount>[0],
address: Address<TAddress>,
config?: FetchAccountConfig,
): Promise<MaybeAccount<Order, TAddress>> {
const maybeAccount = await fetchEncodedAccount(rpc, address, config);
return decodeOrder(maybeAccount);
}
export async function fetchAllOrder(
rpc: Parameters<typeof fetchEncodedAccounts>[0],
addresses: Array<Address>,
config?: FetchAccountsConfig,
): Promise<Account<Order>[]> {
const maybeAccounts = await fetchAllMaybeOrder(rpc, addresses, config);
assertAccountsExist(maybeAccounts);
return maybeAccounts;
}
export async function fetchAllMaybeOrder(
rpc: Parameters<typeof fetchEncodedAccounts>[0],
addresses: Array<Address>,
config?: FetchAccountsConfig,
): Promise<MaybeAccount<Order>[]> {
const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
return maybeAccounts.map((maybeAccount) => decodeOrder(maybeAccount));
}
export function getOrderSize(): number {
return 217;
}