@@ -17,6 +17,34 @@ import {
17
17
import { startEventServer } from '../event-stream/event-server' ;
18
18
import { Server } from 'net' ;
19
19
import { RosettaConstants , RosettaErrors } from './../api/rosetta-constants' ;
20
+ import {
21
+ makeSTXTokenTransfer ,
22
+ makeContractDeploy ,
23
+ PostConditionMode ,
24
+ makeContractCall ,
25
+ ClarityValue ,
26
+ StacksTestnet ,
27
+ getAddressFromPrivateKey ,
28
+ sponsorTransaction ,
29
+ makeUnsignedSTXTokenTransfer ,
30
+ TransactionSigner ,
31
+ createStacksPrivateKey ,
32
+ pubKeyfromPrivKey ,
33
+ publicKeyToString ,
34
+ addressFromPublicKeys ,
35
+ AddressHashMode ,
36
+ createStacksPublicKey ,
37
+ TransactionVersion ,
38
+ AddressVersion ,
39
+ addressToString ,
40
+ UnsignedTokenTransferOptions ,
41
+ getPublicKey ,
42
+ getAddressFromPublicKey ,
43
+ SignedTokenTransferOptions ,
44
+ } from '@blockstack/stacks-transactions' ;
45
+ import * as BN from 'bn.js' ;
46
+ import { GetStacksTestnetNetwork , testnetKeys } from '../api/routes/debug' ;
47
+ import { bufferToHexPrefixString } from '../helpers' ;
20
48
21
49
describe ( 'Rosetta API' , ( ) => {
22
50
let db : PgDataStore ;
@@ -555,187 +583,85 @@ describe('Rosetta API', () => {
555
583
} ) ;
556
584
557
585
test ( 'parse api signed' , async ( ) => {
586
+ const publicKey = publicKeyToString (
587
+ getPublicKey ( createStacksPrivateKey ( testnetKeys [ 0 ] . secretKey ) )
588
+ ) ;
589
+ const senderAddr = testnetKeys [ 0 ] . stacksAddress ;
590
+ const recipientAddr = 'STDE7Y8HV3RX8VBM2TZVWJTS7ZA1XB0SSC3NEVH0' ;
591
+ const amount = new BN ( 1000 ) ;
592
+ const fee = new BN ( 180 ) ;
593
+ const options : SignedTokenTransferOptions = {
594
+ recipient : recipientAddr ,
595
+ amount : amount ,
596
+ fee : fee ,
597
+ senderKey : testnetKeys [ 0 ] . secretKey ,
598
+ network : GetStacksTestnetNetwork ( ) ,
599
+ } ;
600
+ const testTransaction = await makeSTXTokenTransfer ( options ) ;
558
601
const request : RosettaConstructionParseRequest = {
559
602
network_identifier : {
560
603
blockchain : 'stacks' ,
561
604
network : 'testnet' ,
562
605
} ,
563
606
signed : true ,
564
- transaction :
565
- '0x80800000000400164247d6f2b425ac5771423ae6c80c754f7172b0000000000000000000000000000000b400011ae06c14c967f999184ea8a7913125f09ab64004446fca89940f092509124b9e773aef483e925476c78ec58166dcecab3875b8fab8e9aa4213179d164463962803020000000000051a1ae3f911d8f1d46d7416bfbe4b593fd41eac19cb00000000000003e800000000000000000000000000000000000000000000000000000000000000000000' ,
607
+ transaction : bufferToHexPrefixString ( testTransaction . serialize ( ) ) ,
566
608
} ;
567
609
568
610
const result = await supertest ( api . server ) . post ( `/rosetta/v1/construction/parse` ) . send ( request ) ;
569
611
expect ( result . status ) . toBe ( 200 ) ;
570
612
expect ( result . type ) . toBe ( 'application/json' ) ;
571
- const expectedResponseParseSigned : RosettaConstructionParseResponse = {
572
- operations : [
573
- {
574
- operation_identifier : {
575
- index : 0 ,
576
- } ,
577
- type : 'fee' ,
578
- status : 'pending' ,
579
- account : {
580
- address : 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6' ,
581
- } ,
582
- amount : {
583
- value : '-180' ,
584
- currency : {
585
- symbol : 'STX' ,
586
- decimals : 6 ,
587
- } ,
588
- } ,
589
- } ,
590
- {
591
- operation_identifier : {
592
- index : 1 ,
593
- } ,
594
- type : 'token_transfer' ,
595
- status : 'pending' ,
596
- account : {
597
- address : 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6' ,
598
- } ,
599
- amount : {
600
- value : '-1000' ,
601
- currency : {
602
- symbol : 'STX' ,
603
- decimals : 6 ,
604
- } ,
605
- } ,
606
- coin_change : {
607
- coin_action : 'coin_spent' ,
608
- coin_identifier : {
609
- identifier : '0xaa16520ec7b15f2eb44b91957dfb7aa2484e76f430233971cdcfa452560e182f:1' ,
610
- } ,
611
- } ,
612
- } ,
613
- {
614
- operation_identifier : {
615
- index : 2 ,
616
- } ,
617
- related_operations : [
618
- {
619
- index : 0 ,
620
- operation_identifier : {
621
- index : 1 ,
622
- } ,
623
- } ,
624
- ] ,
625
- type : 'token_transfer' ,
626
- status : 'pending' ,
627
- account : {
628
- address : 'STDE7Y8HV3RX8VBM2TZVWJTS7ZA1XB0SSC3NEVH0' ,
629
- } ,
630
- amount : {
631
- value : '1000' ,
632
- currency : {
633
- symbol : 'STX' ,
634
- decimals : 6 ,
635
- } ,
636
- } ,
637
- coin_change : {
638
- coin_action : 'coin_created' ,
639
- coin_identifier : {
640
- identifier : '0xaa16520ec7b15f2eb44b91957dfb7aa2484e76f430233971cdcfa452560e182f:2' ,
641
- } ,
642
- } ,
643
- } ,
644
- ] ,
645
- } ;
646
- expect ( JSON . parse ( result . text ) ) . toEqual ( expectedResponseParseSigned ) ;
613
+
614
+ const actual : RosettaConstructionParseResponse = JSON . parse ( result . text ) ;
615
+ // test fee operation
616
+ expect ( actual . operations [ 0 ] . account ?. address ) . toEqual ( senderAddr ) ;
617
+ expect ( actual . operations [ 0 ] . amount ?. value ) . toEqual ( '-' + fee . toString ( ) ) ;
618
+ // test sender
619
+ expect ( actual . operations [ 1 ] . account ?. address ) . toEqual ( senderAddr ) ;
620
+ expect ( actual . operations [ 1 ] . amount ?. value ) . toEqual ( '-' + amount . toString ( ) ) ;
621
+ // test recipient
622
+ expect ( actual . operations [ 2 ] . account ?. address ) . toEqual ( recipientAddr ) ;
623
+ expect ( actual . operations [ 2 ] . amount ?. value ) . toEqual ( amount . toString ( ) ) ;
647
624
} ) ;
648
625
649
626
test ( 'parse api unsigned' , async ( ) => {
627
+ const publicKey = publicKeyToString (
628
+ getPublicKey ( createStacksPrivateKey ( testnetKeys [ 0 ] . secretKey ) )
629
+ ) ;
630
+ const senderAddr = testnetKeys [ 0 ] . stacksAddress ;
631
+ const recipientAddr = 'STDE7Y8HV3RX8VBM2TZVWJTS7ZA1XB0SSC3NEVH0' ;
632
+ const amount = new BN ( 1000 ) ;
633
+ const fee = new BN ( 180 ) ;
634
+ const tokenTransferOptions : UnsignedTokenTransferOptions = {
635
+ recipient : recipientAddr ,
636
+ amount : amount ,
637
+ fee : fee ,
638
+ publicKey : publicKey ,
639
+ network : GetStacksTestnetNetwork ( ) ,
640
+ } ;
641
+ const testTransaction = await makeUnsignedSTXTokenTransfer ( tokenTransferOptions ) ;
642
+
650
643
const request : RosettaConstructionParseRequest = {
651
644
network_identifier : {
652
645
blockchain : 'stacks' ,
653
646
network : 'testnet' ,
654
647
} ,
655
648
signed : false ,
656
- transaction :
657
- '0x80800000000400164247d6f2b425ac5771423ae6c80c754f7172b0000000000000000000000000000000b400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003020000000000051a1ae3f911d8f1d46d7416bfbe4b593fd41eac19cb00000000000003e800000000000000000000000000000000000000000000000000000000000000000000' ,
649
+ transaction : bufferToHexPrefixString ( testTransaction . serialize ( ) ) ,
658
650
} ;
659
651
660
652
const result = await supertest ( api . server ) . post ( `/rosetta/v1/construction/parse` ) . send ( request ) ;
661
653
expect ( result . status ) . toBe ( 200 ) ;
662
654
expect ( result . type ) . toBe ( 'application/json' ) ;
663
- const expectedResponseParseUnsigned : RosettaConstructionParseResponse = {
664
- operations : [
665
- {
666
- operation_identifier : {
667
- index : 0 ,
668
- } ,
669
- type : 'fee' ,
670
- status : 'pending' ,
671
- account : {
672
- address : 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6' ,
673
- } ,
674
- amount : {
675
- value : '-180' ,
676
- currency : {
677
- symbol : 'STX' ,
678
- decimals : 6 ,
679
- } ,
680
- } ,
681
- } ,
682
- {
683
- operation_identifier : {
684
- index : 1 ,
685
- } ,
686
- type : 'token_transfer' ,
687
- status : 'pending' ,
688
- account : {
689
- address : 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6' ,
690
- } ,
691
- amount : {
692
- value : '-1000' ,
693
- currency : {
694
- symbol : 'STX' ,
695
- decimals : 6 ,
696
- } ,
697
- } ,
698
- coin_change : {
699
- coin_action : 'coin_spent' ,
700
- coin_identifier : {
701
- identifier : '0x8687d54aab157110decd8f9fe223d4bfb5d9e7d0d6afe7672bfe5510521c7b27:1' ,
702
- } ,
703
- } ,
704
- } ,
705
- {
706
- operation_identifier : {
707
- index : 2 ,
708
- } ,
709
- related_operations : [
710
- {
711
- index : 0 ,
712
- operation_identifier : {
713
- index : 1 ,
714
- } ,
715
- } ,
716
- ] ,
717
- type : 'token_transfer' ,
718
- status : 'pending' ,
719
- account : {
720
- address : 'STDE7Y8HV3RX8VBM2TZVWJTS7ZA1XB0SSC3NEVH0' ,
721
- } ,
722
- amount : {
723
- value : '1000' ,
724
- currency : {
725
- symbol : 'STX' ,
726
- decimals : 6 ,
727
- } ,
728
- } ,
729
- coin_change : {
730
- coin_action : 'coin_created' ,
731
- coin_identifier : {
732
- identifier : '0x8687d54aab157110decd8f9fe223d4bfb5d9e7d0d6afe7672bfe5510521c7b27:2' ,
733
- } ,
734
- } ,
735
- } ,
736
- ] ,
737
- } ;
738
- expect ( JSON . parse ( result . text ) ) . toEqual ( expectedResponseParseUnsigned ) ;
655
+ const actual : RosettaConstructionParseResponse = JSON . parse ( result . text ) ;
656
+ // test fee operation
657
+ expect ( actual . operations [ 0 ] . account ?. address ) . toEqual ( senderAddr ) ;
658
+ expect ( actual . operations [ 0 ] . amount ?. value ) . toEqual ( '-' + fee . toString ( ) ) ;
659
+ // test sender
660
+ expect ( actual . operations [ 1 ] . account ?. address ) . toEqual ( senderAddr ) ;
661
+ expect ( actual . operations [ 1 ] . amount ?. value ) . toEqual ( '-' + amount . toString ( ) ) ;
662
+ // test recipient
663
+ expect ( actual . operations [ 2 ] . account ?. address ) . toEqual ( recipientAddr ) ;
664
+ expect ( actual . operations [ 2 ] . amount ?. value ) . toEqual ( amount . toString ( ) ) ;
739
665
} ) ;
740
666
741
667
/** end */
0 commit comments