@@ -103,11 +103,19 @@ type RosettaRevokeDelegateContractArgs = {
103
103
result : string ;
104
104
} ;
105
105
106
- export function parseTransactionMemo ( tx : BaseTx ) : string | null {
107
- if ( tx . token_transfer_memo && tx . token_transfer_memo != '' ) {
106
+ export function parseTransactionMemo ( memoHex : string | undefined ) : string | null {
107
+ if ( memoHex ) {
108
108
// Memos are a fixed-length 34 byte array. Any memo representing a string that is
109
109
// less than 34 bytes long will have right-side padded null-bytes.
110
- return tx . token_transfer_memo . replace ( / \0 .* $ / g, '' ) ;
110
+ let memoBuffer = hexToBuffer ( memoHex ) ;
111
+ while ( memoBuffer . length > 0 && memoBuffer [ memoBuffer . length - 1 ] === 0 ) {
112
+ memoBuffer = memoBuffer . slice ( 0 , memoBuffer . length - 1 ) ;
113
+ }
114
+ if ( memoBuffer . length === 0 ) {
115
+ return null ;
116
+ }
117
+ const memoDecoded = memoBuffer . toString ( 'utf8' ) ;
118
+ return memoDecoded ;
111
119
}
112
120
return null ;
113
121
}
@@ -120,22 +128,22 @@ export async function getOperations(
120
128
stxUnlockEvents ?: StxUnlockEvent [ ]
121
129
) : Promise < RosettaOperation [ ] > {
122
130
const operations : RosettaOperation [ ] = [ ] ;
123
- const txType = getTxTypeString ( tx . type_id ) ;
131
+ const txType = getTxTypeString ( tx . type_id ) as RosettaOperationType ;
124
132
switch ( txType ) {
125
- case 'token_transfer' :
133
+ case RosettaOperationType . TokenTransfer :
126
134
operations . push ( makeFeeOperation ( tx ) ) ;
127
- operations . push ( makeSenderOperation ( tx , operations . length ) ) ;
128
- operations . push ( makeReceiverOperation ( tx , operations . length ) ) ;
135
+ operations . push ( makeSenderOperation ( tx , operations . length , tx . token_transfer_memo ) ) ;
136
+ operations . push ( makeReceiverOperation ( tx , operations . length , tx . token_transfer_memo ) ) ;
129
137
break ;
130
- case 'contract_call' :
138
+ case RosettaOperationType . ContractCall :
131
139
operations . push ( makeFeeOperation ( tx ) ) ;
132
140
operations . push ( await makeCallContractOperation ( tx , db , operations . length ) ) ;
133
141
break ;
134
- case 'smart_contract' :
142
+ case RosettaOperationType . SmartContract :
135
143
operations . push ( makeFeeOperation ( tx ) ) ;
136
144
operations . push ( makeDeployContractOperation ( tx , operations . length ) ) ;
137
145
break ;
138
- case 'coinbase' :
146
+ case RosettaOperationType . Coinbase :
139
147
operations . push ( makeCoinbaseOperation ( tx , 0 ) ) ;
140
148
if ( minerRewards !== undefined ) {
141
149
getMinerOperations ( minerRewards , operations ) ;
@@ -144,7 +152,7 @@ export async function getOperations(
144
152
processUnlockingEvents ( stxUnlockEvents , operations ) ;
145
153
}
146
154
break ;
147
- case 'poison_microblock' :
155
+ case RosettaOperationType . PoisonMicroblock :
148
156
operations . push ( makePoisonMicroblockOperation ( tx , 0 ) ) ;
149
157
break ;
150
158
default :
@@ -196,8 +204,8 @@ async function processEvents(
196
204
stxAssetEvent . amount ,
197
205
( ) => 'Unexpected nullish amount'
198
206
) ;
199
- operations . push ( makeSenderOperation ( tx , operations . length ) ) ;
200
- operations . push ( makeReceiverOperation ( tx , operations . length ) ) ;
207
+ operations . push ( makeSenderOperation ( tx , operations . length , stxAssetEvent . memo ) ) ;
208
+ operations . push ( makeReceiverOperation ( tx , operations . length , stxAssetEvent . memo ) ) ;
201
209
break ;
202
210
case DbAssetEventTypeId . Burn :
203
211
operations . push ( makeBurnOperation ( stxAssetEvent , baseTx , operations . length ) ) ;
@@ -254,7 +262,7 @@ function makeStakeLockOperation(
254
262
stake_metadata . unlock_height = tx . unlock_height . toString ( ) ;
255
263
const lock : RosettaOperation = {
256
264
operation_identifier : { index : index } ,
257
- type : getEventTypeString ( tx . event_type ) ,
265
+ type : RosettaOperationType . StxLock ,
258
266
status : getTxStatus ( baseTx . status ) ,
259
267
account : {
260
268
address : unwrapOptional ( tx . locked_address , ( ) => 'Unexpected nullish locked_address' ) ,
@@ -337,7 +345,7 @@ function makeFeeOperation(tx: BaseTx): RosettaOperation {
337
345
function makeBurnOperation ( tx : DbStxEvent , baseTx : BaseTx , index : number ) : RosettaOperation {
338
346
const burn : RosettaOperation = {
339
347
operation_identifier : { index : index } ,
340
- type : getAssetEventTypeString ( tx . asset_event_type_id ) ,
348
+ type : RosettaOperationType . Burn ,
341
349
status : getTxStatus ( baseTx . status ) ,
342
350
account : {
343
351
address : unwrapOptional ( baseTx . sender_address , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -359,7 +367,7 @@ function makeFtBurnOperation(
359
367
) : RosettaOperation {
360
368
const burn : RosettaOperation = {
361
369
operation_identifier : { index : index } ,
362
- type : getAssetEventTypeString ( ftEvent . asset_event_type_id ) ,
370
+ type : RosettaOperationType . Burn ,
363
371
status : getTxStatus ( baseTx . status ) ,
364
372
account : {
365
373
address : unwrapOptional ( ftEvent . sender , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -379,7 +387,7 @@ function makeFtBurnOperation(
379
387
function makeMintOperation ( tx : DbStxEvent , baseTx : BaseTx , index : number ) : RosettaOperation {
380
388
const mint : RosettaOperation = {
381
389
operation_identifier : { index : index } ,
382
- type : getAssetEventTypeString ( tx . asset_event_type_id ) ,
390
+ type : RosettaOperationType . Mint ,
383
391
status : getTxStatus ( baseTx . status ) ,
384
392
account : {
385
393
address : unwrapOptional ( tx . recipient , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -403,7 +411,7 @@ function makeFtMintOperation(
403
411
) : RosettaOperation {
404
412
const mint : RosettaOperation = {
405
413
operation_identifier : { index : index } ,
406
- type : getAssetEventTypeString ( ftEvent . asset_event_type_id ) ,
414
+ type : RosettaOperationType . Mint ,
407
415
status : getTxStatus ( baseTx . status ) ,
408
416
account : {
409
417
address : unwrapOptional ( ftEvent . recipient , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -423,10 +431,14 @@ function makeFtMintOperation(
423
431
return mint ;
424
432
}
425
433
426
- function makeSenderOperation ( tx : BaseTx , index : number ) : RosettaOperation {
434
+ function makeSenderOperation (
435
+ tx : BaseTx ,
436
+ index : number ,
437
+ memo : string | undefined
438
+ ) : RosettaOperation {
427
439
const sender : RosettaOperation = {
428
440
operation_identifier : { index : index } ,
429
- type : 'token_transfer' , //Sender operation should always be token_transfer,
441
+ type : RosettaOperationType . TokenTransfer , //Sender operation should always be token_transfer,
430
442
status : getTxStatus ( tx . status ) ,
431
443
account : {
432
444
address : unwrapOptional ( tx . sender_address , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -444,6 +456,13 @@ function makeSenderOperation(tx: BaseTx, index: number): RosettaOperation {
444
456
} ,
445
457
} ;
446
458
459
+ if ( memo ) {
460
+ sender . metadata = {
461
+ ...sender . metadata ,
462
+ memo : parseTransactionMemo ( memo ) ,
463
+ } ;
464
+ }
465
+
447
466
return sender ;
448
467
}
449
468
@@ -455,7 +474,7 @@ function makeFtSenderOperation(
455
474
) : RosettaOperation {
456
475
const sender : RosettaOperation = {
457
476
operation_identifier : { index : index } ,
458
- type : 'token_transfer' ,
477
+ type : RosettaOperationType . TokenTransfer ,
459
478
status : getTxStatus ( tx . status ) ,
460
479
account : {
461
480
address : unwrapOptional ( ftEvent . sender , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -478,11 +497,15 @@ function makeFtSenderOperation(
478
497
return sender ;
479
498
}
480
499
481
- function makeReceiverOperation ( tx : BaseTx , index : number ) : RosettaOperation {
500
+ function makeReceiverOperation (
501
+ tx : BaseTx ,
502
+ index : number ,
503
+ memo : string | undefined
504
+ ) : RosettaOperation {
482
505
const receiver : RosettaOperation = {
483
506
operation_identifier : { index : index } ,
484
507
related_operations : [ { index : index - 1 } ] ,
485
- type : 'token_transfer' , //Receiver operation should always be token_transfer
508
+ type : RosettaOperationType . TokenTransfer , //Receiver operation should always be token_transfer
486
509
status : getTxStatus ( tx . status ) ,
487
510
account : {
488
511
address : unwrapOptional (
@@ -503,6 +526,13 @@ function makeReceiverOperation(tx: BaseTx, index: number): RosettaOperation {
503
526
} ,
504
527
} ;
505
528
529
+ if ( memo ) {
530
+ receiver . metadata = {
531
+ ...receiver . metadata ,
532
+ memo : parseTransactionMemo ( memo ) ,
533
+ } ;
534
+ }
535
+
506
536
return receiver ;
507
537
}
508
538
@@ -515,7 +545,7 @@ function makeFtReceiverOperation(
515
545
const receiver : RosettaOperation = {
516
546
operation_identifier : { index : index } ,
517
547
related_operations : [ { index : index - 1 } ] ,
518
- type : 'token_transfer' ,
548
+ type : RosettaOperationType . TokenTransfer ,
519
549
status : getTxStatus ( tx . status ) ,
520
550
account : {
521
551
address : unwrapOptional (
@@ -545,7 +575,7 @@ function makeFtReceiverOperation(
545
575
function makeDeployContractOperation ( tx : BaseTx , index : number ) : RosettaOperation {
546
576
const deployer : RosettaOperation = {
547
577
operation_identifier : { index : index } ,
548
- type : getTxTypeString ( tx . type_id ) ,
578
+ type : RosettaOperationType . SmartContract ,
549
579
status : getTxStatus ( tx . status ) ,
550
580
account : {
551
581
address : unwrapOptional ( tx . sender_address , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -562,7 +592,7 @@ async function makeCallContractOperation(
562
592
) : Promise < RosettaOperation > {
563
593
const contractCallOp : RosettaOperation = {
564
594
operation_identifier : { index : index } ,
565
- type : getTxTypeString ( tx . type_id ) ,
595
+ type : RosettaOperationType . ContractCall ,
566
596
status : getTxStatus ( tx . status ) ,
567
597
account : {
568
598
address : unwrapOptional ( tx . sender_address , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -598,7 +628,7 @@ function makeCoinbaseOperation(tx: BaseTx, index: number): RosettaOperation {
598
628
// TODO : Add more mappings in operations for coinbase
599
629
const sender : RosettaOperation = {
600
630
operation_identifier : { index : index } ,
601
- type : getTxTypeString ( tx . type_id ) ,
631
+ type : RosettaOperationType . Coinbase ,
602
632
status : getTxStatus ( tx . status ) ,
603
633
account : {
604
634
address : unwrapOptional ( tx . sender_address , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -612,7 +642,7 @@ function makePoisonMicroblockOperation(tx: BaseTx, index: number): RosettaOperat
612
642
// TODO : add more mappings in operations for poison-microblock
613
643
const sender : RosettaOperation = {
614
644
operation_identifier : { index : index } ,
615
- type : getTxTypeString ( tx . type_id ) ,
645
+ type : RosettaOperationType . PoisonMicroblock ,
616
646
status : getTxStatus ( tx . status ) ,
617
647
account : {
618
648
address : unwrapOptional ( tx . sender_address , ( ) => 'Unexpected nullish sender_address' ) ,
@@ -645,7 +675,7 @@ export function getOptionsFromOperations(operations: RosettaOperation[]): Rosett
645
675
const options : RosettaOptions = { } ;
646
676
647
677
for ( const operation of operations ) {
648
- switch ( operation . type ) {
678
+ switch ( operation . type as RosettaOperationType ) {
649
679
case RosettaOperationType . Fee :
650
680
options . fee = operation . amount ?. value ;
651
681
break ;
0 commit comments