@@ -26,7 +26,7 @@ import { DID_SOL_PROGRAM } from "@identity.com/sol-did-client";
2626import { MiddlewareRegistry } from "./middlewareRegistry" ;
2727import {
2828 ControllerPubkeys ,
29- ExecuteResult ,
29+ TransactionResult ,
3030 ProposalResult ,
3131} from "../types/cryptid" ;
3232import { MiddlewareResult } from "../types/middleware" ;
@@ -181,7 +181,7 @@ export class CryptidService {
181181 private async executeMiddlewareInstructions (
182182 account : CryptidAccountDetails ,
183183 transactionAccount : PublicKey ,
184- stage : "Propose" | "Execute"
184+ stage : "Propose" | "Execute" | "Close"
185185 ) : Promise < MiddlewareResult > {
186186 const middlewareContexts =
187187 MiddlewareRegistry . get ( ) . getMiddlewareContexts ( account ) ;
@@ -198,8 +198,13 @@ export class CryptidService {
198198 cryptidAccountDetails : account ,
199199 } ;
200200
201- // Middlewares may have functions taht are executed on propose stage, on execute stage, or both
202- const middlewareFn = stage === "Propose" ? "onPropose" : "onExecute" ;
201+ // Middlewares may have functions interface functions for propose, execute and close.
202+ const middlewareFn =
203+ stage === "Propose"
204+ ? "onPropose"
205+ : stage === "Close"
206+ ? "onClose"
207+ : "onExecute" ;
203208
204209 // For each middleware, execute their onPropose or onExecute function
205210 const middlewareExecutions = middlewareContexts . map (
@@ -346,7 +351,7 @@ export class CryptidService {
346351 public async proposeAndExecuteTransaction (
347352 account : CryptidAccountDetails ,
348353 transaction : Transaction
349- ) : Promise < ExecuteResult > {
354+ ) : Promise < TransactionResult > {
350355 const transactionAccountKeypair = Keypair . generate ( ) ;
351356 const cryptidTransaction = CryptidTransaction . fromSolanaInstructions (
352357 account ,
@@ -390,8 +395,8 @@ export class CryptidService {
390395 ) ;
391396
392397 return {
393- executeTransaction,
394- executeSigners : [
398+ transaction : executeTransaction ,
399+ signers : [
395400 transactionAccountKeypair ,
396401 ...middlewareProposeResults . signers ,
397402 ...middlewareExecuteResults . signers ,
@@ -403,7 +408,7 @@ export class CryptidService {
403408 account : CryptidAccountDetails ,
404409 transactionAccountAddress : PublicKey ,
405410 cryptidTransaction ?: CryptidTransaction
406- ) : Promise < ExecuteResult > {
411+ ) : Promise < TransactionResult > {
407412 const middlewareResult = await this . executeMiddlewareInstructions (
408413 account ,
409414 transactionAccountAddress ,
@@ -421,8 +426,35 @@ export class CryptidService {
421426 . transaction ( ) ;
422427
423428 return {
424- executeTransaction,
425- executeSigners : [ ...middlewareResult . signers ] ,
429+ transaction : executeTransaction ,
430+ signers : [ ...middlewareResult . signers ] ,
431+ } ;
432+ }
433+
434+ public async close (
435+ account : CryptidAccountDetails ,
436+ transactionAccountAddress : PublicKey ,
437+ cryptidTransaction ?: CryptidTransaction
438+ ) : Promise < TransactionResult > {
439+ const middlewareResult = await this . executeMiddlewareInstructions (
440+ account ,
441+ transactionAccountAddress ,
442+ "Close"
443+ ) ;
444+
445+ const resolvedCryptidTransaction =
446+ cryptidTransaction ||
447+ ( await this . getCryptidTransaction ( account , transactionAccountAddress ) ) ;
448+
449+ const closeTransaction = await resolvedCryptidTransaction
450+ . close ( this . program , transactionAccountAddress )
451+ . preInstructions ( middlewareResult . instructions )
452+ . signers ( middlewareResult . signers )
453+ . transaction ( ) ;
454+
455+ return {
456+ transaction : closeTransaction ,
457+ signers : [ ...middlewareResult . signers ] ,
426458 } ;
427459 }
428460
0 commit comments