import { PublicKey, Connection } from "@solana/web3.js" import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars import * as borsh from "@project-serum/borsh" // eslint-disable-line @typescript-eslint/no-unused-vars import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars import { PROGRAM_ID } from "../programId" export interface EscrowStateFields { marker: types.EscrowStateMarkerKind inputTokens: Array outputTokens: Array foreignProxyContract: types.ForeignAddrFields header: types.HeaderFields custodySigner: PublicKey custodyBump: Array originChainId: number } export interface EscrowStateJSON { marker: types.EscrowStateMarkerJSON inputTokens: Array outputTokens: Array foreignProxyContract: types.ForeignAddrJSON header: types.HeaderJSON custodySigner: string custodyBump: Array originChainId: number } export class EscrowState { readonly marker: types.EscrowStateMarkerKind readonly inputTokens: Array readonly outputTokens: Array readonly foreignProxyContract: types.ForeignAddr readonly header: types.Header readonly custodySigner: PublicKey readonly custodyBump: Array readonly originChainId: number static readonly discriminator = Buffer.from([ 19, 90, 148, 111, 55, 130, 229, 108, ]) static readonly layout = borsh.struct([ types.EscrowStateMarker.layout("marker"), borsh.vec(types.EscrowAccount.layout(), "inputTokens"), borsh.vec(types.EscrowAccount.layout(), "outputTokens"), types.ForeignAddr.layout("foreignProxyContract"), types.Header.layout("header"), borsh.publicKey("custodySigner"), borsh.array(borsh.u8(), 1, "custodyBump"), borsh.u16("originChainId"), ]) constructor(fields: EscrowStateFields) { this.marker = fields.marker this.inputTokens = fields.inputTokens.map( (item) => new types.EscrowAccount({ ...item }) ) this.outputTokens = fields.outputTokens.map( (item) => new types.EscrowAccount({ ...item }) ) this.foreignProxyContract = new types.ForeignAddr({ ...fields.foreignProxyContract, }) this.header = new types.Header({ ...fields.header }) this.custodySigner = fields.custodySigner this.custodyBump = fields.custodyBump this.originChainId = fields.originChainId } static async fetch( c: Connection, address: PublicKey ): Promise { const info = await c.getAccountInfo(address) if (info === null) { return null } if (!info.owner.equals(PROGRAM_ID)) { throw new Error("account doesn't belong to this program") } return this.decode(info.data) } static async fetchMultiple( c: Connection, addresses: PublicKey[] ): Promise> { const infos = await c.getMultipleAccountsInfo(addresses) return infos.map((info) => { if (info === null) { return null } if (!info.owner.equals(PROGRAM_ID)) { throw new Error("account doesn't belong to this program") } return this.decode(info.data) }) } static decode(data: Buffer): EscrowState { if (!data.slice(0, 8).equals(EscrowState.discriminator)) { throw new Error("invalid account discriminator") } const dec = EscrowState.layout.decode(data.slice(8)) return new EscrowState({ marker: types.EscrowStateMarker.fromDecoded(dec.marker), inputTokens: dec.inputTokens.map((item) => types.EscrowAccount.fromDecoded(item) ), outputTokens: dec.outputTokens.map((item) => types.EscrowAccount.fromDecoded(item) ), foreignProxyContract: types.ForeignAddr.fromDecoded( dec.foreignProxyContract ), header: types.Header.fromDecoded(dec.header), custodySigner: dec.custodySigner, custodyBump: dec.custodyBump, originChainId: dec.originChainId, }) } toJSON(): EscrowStateJSON { return { marker: this.marker.toJSON(), inputTokens: this.inputTokens.map((item) => item.toJSON()), outputTokens: this.outputTokens.map((item) => item.toJSON()), foreignProxyContract: this.foreignProxyContract.toJSON(), header: this.header.toJSON(), custodySigner: this.custodySigner.toString(), custodyBump: this.custodyBump, originChainId: this.originChainId, } } static fromJSON(obj: EscrowStateJSON): EscrowState { return new EscrowState({ marker: types.EscrowStateMarker.fromJSON(obj.marker), inputTokens: obj.inputTokens.map((item) => types.EscrowAccount.fromJSON(item) ), outputTokens: obj.outputTokens.map((item) => types.EscrowAccount.fromJSON(item) ), foreignProxyContract: types.ForeignAddr.fromJSON( obj.foreignProxyContract ), header: types.Header.fromJSON(obj.header), custodySigner: new PublicKey(obj.custodySigner), custodyBump: obj.custodyBump, originChainId: obj.originChainId, }) } }