import "./type_extensions"; import { PublicKey } from "@solana/web3.js"; import * as types from "."; import { PIDS, SolanaContext, SolanaPIDS } from "./context"; import { info, RetryOptions, retry, dbg, _undef } from "./utilities"; import { TransferPayloadWithData } from "./wh_helpers"; import BN from "bn.js"; import { EscrowState } from "./generated_client/accounts"; export async function tryFetchEscrowState( ctx: SolanaContext, foreignProxyContract: types.ForeignAddr, header: types.Header, retryOptions?: RetryOptions, ): Promise { try { const escrowStateKey = getEscrowState( ctx, header.sender, foreignProxyContract, header.id, )[0]; return await retry(retryOptions, () => types.accounts.EscrowState.fetch(ctx.conn, escrowStateKey), ).then(_undef); } catch (e) { if (!retryOptions?.silent) { info(e, "Could not fetch escrow state"); } return undefined; } } export async function fetchEscrowState( ctx: SolanaContext, transfer: TransferPayloadWithData, header: types.Header, retryOptions?: RetryOptions, ): Promise { const escrowStateKey = ( await getEscrowState( ctx, header.sender, transfer.fromAddress, header.id, ) )[0]; return await retry(retryOptions, () => types.accounts.EscrowState.fetch(ctx.conn, escrowStateKey), ).then(_undef); } export function getEscrowState( pids: SolanaPIDS, foreignSender: types.ForeignAddr, foreignProxyContract: types.ForeignAddr, id: BN, ): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from("escrow"), foreignSender.toBytes(), foreignProxyContract.toBytes(), id.toArrayLike(Buffer, "le", 8), ], pids.solanaProxy, ); }