@@ -46,6 +46,7 @@ import {
4646 calculateCosmosStdFee ,
4747} from "./utils/cosmos" ;
4848import { fillUnsignedEVMTx } from "./utils/evm" ;
49+ import { Subscriber , TxExecutableEvent } from "./internal" ;
4950export class BackgroundTxExecutorService {
5051 @observable
5152 protected recentTxExecutionSeq : number = 0 ;
@@ -61,7 +62,8 @@ export class BackgroundTxExecutorService {
6162 protected readonly backgroundTxService : BackgroundTxService ,
6263 protected readonly backgroundTxEthereumService : BackgroundTxEthereumService ,
6364 protected readonly analyticsService : AnalyticsService ,
64- protected readonly recentSendHistoryService : RecentSendHistoryService
65+ protected readonly recentSendHistoryService : RecentSendHistoryService ,
66+ protected readonly subscriber : Subscriber < TxExecutableEvent >
6567 ) {
6668 makeObservable ( this ) ;
6769 }
@@ -103,16 +105,31 @@ export class BackgroundTxExecutorService {
103105 ) ;
104106 } ) ;
105107
106- // TODO: 간단한 메시지 큐를 구현해서 recent send history service에서 multi tx를 처리할 조건이 만족되었을 때
107- // 이 서비스로 메시지를 보내 트랜잭션을 자동으로 실행할 수 있도록 한다. 굳
108+ this . subscriber . subscribe ( async ( { executionId, executableChainIds } ) => {
109+ const execution = this . getTxExecution ( executionId ) ;
110+ if ( ! execution ) {
111+ return ;
112+ }
113+
114+ const newExecutableChainIds = executableChainIds . filter ( ( chainId ) =>
115+ execution . executableChainIds . includes ( chainId )
116+ ) ;
108117
109- // CHECK: 현재 활성화되어 있는 vault에서만 실행할 수 있으면 좋을 듯, how? vaultId 변경 감지? how?
110- // CHECK: 굳이 이걸 백그라운드에서 자동으로 실행할 필요가 있을까?
111- // 불러왔는데 pending 상태거나 오래된 실행이면 사실상 이 작업을 이어가는 것이 의미가 있는지 의문이 든다.
112- // for (const execution of this.getRecentDirectTxsExecutions()) {
118+ if ( newExecutableChainIds . length === 0 ) {
119+ return ;
120+ }
121+
122+ // update the executable chain ids
123+ for ( const chainId of newExecutableChainIds ) {
124+ execution . executableChainIds . push ( chainId ) ;
125+ }
113126
114- // this.executeDirectTxs(execution.id);
115- // }
127+ // cause new executable chain ids are available, resume the execution
128+
129+ // CHECK: 현재 활성화되어 있는 vault에서만 실행할 수 있으면 좋을 듯, how? vaultId 변경 감지? how?
130+ // 불러왔는데 pending 상태거나 오래된 실행이면 사실상 이 작업을 이어가는 것이 의미가 있는지 의문이 든다.
131+ await this . executeTxs ( executionId ) ;
132+ } ) ;
116133 }
117134
118135 /**
@@ -225,10 +242,17 @@ export class BackgroundTxExecutorService {
225242 throw new KeplrError ( "direct-tx-executor" , 105 , "Execution not found" ) ;
226243 }
227244
245+ if ( execution . status === TxExecutionStatus . PROCESSING ) {
246+ throw new KeplrError (
247+ "direct-tx-executor" ,
248+ 108 ,
249+ "Execution is already processing"
250+ ) ;
251+ }
252+
228253 // Only pending/processing/blocked executions can be executed
229254 const needResume =
230255 execution . status === TxExecutionStatus . PENDING ||
231- execution . status === TxExecutionStatus . PROCESSING ||
232256 execution . status === TxExecutionStatus . BLOCKED ;
233257 if ( ! needResume ) {
234258 return execution . status ;
0 commit comments