diff --git a/packages/token-sdk/src/types/WithContractId.ts b/packages/token-sdk/src/types/WithContractId.ts new file mode 100644 index 0000000..f78676a --- /dev/null +++ b/packages/token-sdk/src/types/WithContractId.ts @@ -0,0 +1,6 @@ +import { ContractId } from "./daml.js"; + +export type WithContractId> = + ContractParams & { + contractId: ContractId; + }; diff --git a/packages/token-sdk/src/types/index.ts b/packages/token-sdk/src/types/index.ts index a57471e..a55e343 100644 --- a/packages/token-sdk/src/types/index.ts +++ b/packages/token-sdk/src/types/index.ts @@ -4,3 +4,4 @@ export * from "./CreatedEvent.js"; export * from "./daml.js"; export * from "./InstrumentId.js"; export * from "./UserKeyPair.js"; +export * from "./WithContractId.js"; diff --git a/packages/token-sdk/src/wrappedSdk/bonds/lifecycleEffect.ts b/packages/token-sdk/src/wrappedSdk/bonds/lifecycleEffect.ts index e175c53..5de2f00 100644 --- a/packages/token-sdk/src/wrappedSdk/bonds/lifecycleEffect.ts +++ b/packages/token-sdk/src/wrappedSdk/bonds/lifecycleEffect.ts @@ -2,24 +2,22 @@ import { LedgerController } from "@canton-network/wallet-sdk"; import { ActiveContractResponse } from "../../types/ActiveContractResponse.js"; import { ContractId, Party } from "../../types/daml.js"; import { bondLifecycleEffectTemplateId } from "../../constants/templateIds.js"; +import { InstrumentId } from "../../types/InstrumentId.js"; +import { WithContractId } from "../../types/WithContractId.js"; -export interface BondLifecycleEffectParams { - producedVersion: string | null; - eventType: "CouponPayment" | "Redemption"; - targetInstrumentId: string; - targetVersion: string; - eventDate: string; - amount: number; -} +export type LifecycleEventType = "CouponPayment" | "Redemption"; -export interface BondLifecycleEffect { - contractId: ContractId; - producedVersion: string | null; - eventType: "CouponPayment" | "Redemption"; +export interface BondLifecycleEffectParams { + issuer: Party; + depository: Party; + eventType: LifecycleEventType; targetInstrumentId: string; targetVersion: string; - eventDate: string; + producedVersion?: string; + eventDate: number; + settlementTime?: number; amount: number; + currencyInstrumentId: InstrumentId; } export async function getLatestBondLifecycleEffect( @@ -58,34 +56,33 @@ export async function getLatestBondLifecycleEffect( export async function getAllBondLifecycleEffects( ledger: LedgerController, party: Party -): Promise { +): Promise[]> { const end = await ledger.ledgerEnd(); - const effects = (await ledger.activeContracts({ + const activeContracts = (await ledger.activeContracts({ offset: end.offset, templateIds: [bondLifecycleEffectTemplateId], filterByParty: true, parties: [party], })) as ActiveContractResponse[]; - return effects - .map((contract) => { - const jsActive = contract.contractEntry.JsActiveContract; - if (!jsActive) return null; + const filteredEntries = activeContracts.filter(({ contractEntry }) => { + const jsActive = contractEntry.JsActiveContract; + if (!jsActive) return false; + return true; + // TODO: consider filtering by issuer or other criteria + // const { createArgument } = jsActive.createdEvent; + }); - const createArg = jsActive.createdEvent.createArgument; - const contractId = jsActive.createdEvent.contractId; + return filteredEntries.map((contract) => { + const bondLifecycleEffect = + contract.contractEntry.JsActiveContract!.createdEvent + .createArgument; - return { - contractId, - producedVersion: createArg.producedVersion, - eventType: createArg.eventType, - targetInstrumentId: createArg.targetInstrumentId, - targetVersion: createArg.targetVersion, - eventDate: createArg.eventDate, - amount: createArg.amount, - }; - }) - .filter( - (effect): effect is NonNullable => effect !== null - ); + return { + ...bondLifecycleEffect, + contractId: + contract.contractEntry.JsActiveContract!.createdEvent + .contractId, + }; + }); } diff --git a/packages/token-sdk/src/wrappedSdk/bonds/lifecycleInstruction.ts b/packages/token-sdk/src/wrappedSdk/bonds/lifecycleInstruction.ts index d5865a5..49ab109 100644 --- a/packages/token-sdk/src/wrappedSdk/bonds/lifecycleInstruction.ts +++ b/packages/token-sdk/src/wrappedSdk/bonds/lifecycleInstruction.ts @@ -12,21 +12,7 @@ import { ActiveContractResponse } from "../../types/ActiveContractResponse.js"; import { getContractDisclosure } from "../contractDisclosure.js"; import { InstrumentId } from "../../types/InstrumentId.js"; import { CreatedEvent } from "../../types/CreatedEvent.js"; - -export type LifecycleEventType = "CouponPayment" | "Redemption"; - -export interface BondLifecycleEffect { - issuer: Party; - depository: Party; - eventType: LifecycleEventType; - targetInstrumentId: string; - targetVersion: string; - producedVersion?: string; - eventDate: number; - settlementTime?: number; - amount: number; - currencyInstrumentId: InstrumentId; -} +import { LifecycleEventType } from "./lifecycleEffect.js"; export interface BondLifecycleInstructionParams { eventType: LifecycleEventType;