import { providers as ethersProviders } from 'ethers' ; import { buildApprovePreTransactions } from '../utils/transactions' ; import { ForwardingPathData , StepDescribed , TokenData , TransactionData , } from '../types' ; import ForwardingPathDescription , { describePath , } from '../utils/descriptor/index' ; import App from './App' ; import Transaction from './Transaction' ; const normalizePreTransactions = ( preTransactions ) : Transaction [ ] => { return preTransactions . map ( ( preTransaction ) => ( preTransaction instanceof Transaction ) ? preTransaction : new Transaction ( preTransaction ) ) ; } ; export default class ForwardingPath { #installedApps : App [ ] ; #provider : ethersProviders . Provider ; readonly destination : App ; readonly path : Transaction [ ] ; readonly transactions : Transaction [ ] ; constructor ( data , installedApps , provider ) { this . #installedApps = installedApps ; this . #provider = provider ; this . destination = data . destination ; this . path = data . path ; this . transactions = data . transactions ; } async sign < Receipt > ( callback ) { return Promise . all ( this . transactions . map ( async ( tx ) => await callback ( tx ) ) ) ; } async describe ( ) { let description = [ ] ; if ( this . path . length > 0 ) { try { description = await describePath ( this . path , this . #installedApps , this . #provider ) ; } catch ( _ ) { } } return new ForwardingPathDescription ( description , this . #installedApps ) ; } async buildApprovePreTransactions ( tokenData ) { return buildApprovePreTransactions ( this . transactions [ 0 ] , tokenData , this . #provider ) ; } applyPreTransactions ( preTransactions ) { normalizePreTransactions ( preTransactions ) . reverse ( ) . filter ( preTransaction => ! ! preTransaction ) . forEach ( preTransaction => this . transactions . unshift ( preTransaction ) ) ; } }