@@ -16,13 +16,12 @@ import {
16
16
NATIVE_TOKEN_UID ,
17
17
NANO_CONTRACTS_INITIALIZE_METHOD ,
18
18
TOKEN_MINT_MASK ,
19
- TOKEN_MELT_MASK
19
+ TOKEN_MELT_MASK ,
20
20
} from '../constants' ;
21
21
import Serializer from './serializer' ;
22
22
import HathorWallet from '../new/wallet' ;
23
23
import { NanoContractTransactionError , UtxoError } from '../errors' ;
24
24
import {
25
- ActionTypeToActionHeaderType ,
26
25
NanoContractActionHeader ,
27
26
NanoContractActionType ,
28
27
NanoContractAction ,
@@ -32,9 +31,9 @@ import {
32
31
NanoContractBuilderCreateTokenOptions ,
33
32
NanoContractVertexType ,
34
33
} from './types' ;
35
- import { IDataInput , IDataOutput , ITokenData } from '../types' ;
34
+ import { IDataInput , IDataOutput } from '../types' ;
36
35
import ncApi from '../api/nano' ;
37
- import { validateAndUpdateBlueprintMethodArgs } from './utils' ;
36
+ import { mapActionToActionHeader , validateAndUpdateBlueprintMethodArgs } from './utils' ;
38
37
import NanoContractHeader from './header' ;
39
38
40
39
class NanoContractTransactionBuilder {
@@ -350,7 +349,9 @@ class NanoContractTransactionBuilder {
350
349
}
351
350
352
351
if ( ! action . authority || ! action . token ) {
353
- throw new NanoContractTransactionError ( 'Authority and token are required for grant authority action.' ) ;
352
+ throw new NanoContractTransactionError (
353
+ 'Authority and token are required for grant authority action.'
354
+ ) ;
354
355
}
355
356
356
357
const authorityAddressParam = action . authorityAddress ;
@@ -359,10 +360,16 @@ class NanoContractTransactionBuilder {
359
360
}
360
361
361
362
// Get the utxos with the authority of the action and create the input
362
- const utxos = await this . wallet . getAuthorityUtxo ( action . token , action . authority , { many : false , only_available_utxos : true , filter_address : action . address } ) ;
363
+ const utxos = await this . wallet . getAuthorityUtxo ( action . token , action . authority , {
364
+ many : false ,
365
+ only_available_utxos : true ,
366
+ filter_address : action . address ,
367
+ } ) ;
363
368
364
369
if ( ! utxos || utxos . length === 0 ) {
365
- throw new NanoContractTransactionError ( 'Not enough authority utxos to execute the grant authority.' ) ;
370
+ throw new NanoContractTransactionError (
371
+ 'Not enough authority utxos to execute the grant authority.'
372
+ ) ;
366
373
}
367
374
368
375
const inputs : IDataInput [ ] = [ ] ;
@@ -533,17 +540,35 @@ class NanoContractTransactionBuilder {
533
540
tokens = Array . from ( tokenSet ) ;
534
541
for ( const action of this . actions ) {
535
542
// Call action
536
- if ( action . type === NanoContractActionType . DEPOSIT ) {
537
- const ret = await this . executeDeposit ( action , tokens ) ;
538
- inputs = concat ( inputs , ret [ 0 ] ) ;
539
- outputs = concat ( outputs , ret [ 1 ] ) ;
540
- } else if ( action . type === NanoContractActionType . WITHDRAWAL ) {
541
- const output = this . executeWithdrawal ( action , tokens ) ;
542
- if ( output ) {
543
- outputs = concat ( outputs , output ) ;
543
+ switch ( action . type ) {
544
+ case NanoContractActionType . DEPOSIT : {
545
+ const retDeposit = await this . executeDeposit ( action ) ;
546
+ inputs = concat ( inputs , retDeposit [ 0 ] ) ;
547
+ outputs = concat ( outputs , retDeposit [ 1 ] ) ;
548
+ break ;
549
+ }
550
+ case NanoContractActionType . WITHDRAWAL : {
551
+ const outputWithdrawal = this . executeWithdrawal ( action ) ;
552
+ if ( outputWithdrawal ) {
553
+ outputs = concat ( outputs , outputWithdrawal ) ;
554
+ }
555
+ break ;
556
+ }
557
+ case NanoContractActionType . GRANT_AUTHORITY : {
558
+ const retGrant = await this . executeGrantAuthority ( action ) ;
559
+ inputs = concat ( inputs , retGrant [ 0 ] ) ;
560
+ outputs = concat ( outputs , retGrant [ 1 ] ) ;
561
+ break ;
562
+ }
563
+ case NanoContractActionType . INVOKE_AUTHORITY : {
564
+ const outputInvoke = this . executeInvokeAuthority ( action ) ;
565
+ if ( outputInvoke ) {
566
+ outputs = concat ( outputs , outputInvoke ) ;
567
+ }
568
+ break ;
544
569
}
545
- } else {
546
- throw new Error ( 'Invalid type for nano contract action.' ) ;
570
+ default :
571
+ throw new Error ( 'Invalid type for nano contract action.' ) ;
547
572
}
548
573
}
549
574
}
@@ -646,21 +671,7 @@ class NanoContractTransactionBuilder {
646
671
647
672
if ( this . actions ) {
648
673
nanoHeaderActions = this . actions . map ( action => {
649
- const headerActionType = ActionTypeToActionHeaderType [ action . type ] ;
650
-
651
- const mappedTokens : ITokenData [ ] = tokens . map ( token => {
652
- return {
653
- uid : token ,
654
- name : '' ,
655
- symbol : '' ,
656
- } ;
657
- } ) ;
658
-
659
- return {
660
- type : headerActionType ,
661
- amount : action . amount ,
662
- tokenIndex : tokensUtils . getTokenIndex ( mappedTokens , action . token ) ,
663
- } ;
674
+ return mapActionToActionHeader ( action , tokens ) ;
664
675
} ) ;
665
676
}
666
677
0 commit comments