@@ -586,53 +586,47 @@ function OnRecvPacket(packet: Packet): Uint8Array | undefined {
586
586
const data: IBCSwapPacketData = packet .data ;
587
587
switch (data .type ) {
588
588
case " MAKE_POOL" :
589
- const makePoolMsg: MsgMakePoolRequest = protobuf . decode ( MsgMakePoolRequest , data .Data );
589
+ const makePoolMsg: MsgMakePoolRequest = unmarshalJSON ( data .Data );
590
590
abortTransactionUnless (data .stateChange .poolId !== " " );
591
591
abortTransactionUnless (store .has (data .stateChange .poolId )); // existed already.
592
592
const poolId = store .OnMakePoolReceived (makePoolMsg , data .stateChange .poolId , data .stateChange .sourceChainId );
593
593
const makePoolRes = protobuf .encode ({ poolId });
594
594
return makePoolRes ;
595
595
596
596
case " TAKE_POOL" :
597
- const takePoolMsg: MsgTakePoolRequest = protobuf . decode ( MsgTakePoolRequest , data .Data );
597
+ const takePoolMsg: MsgTakePoolRequest = unmarshalJSON ( data .Data );
598
598
const takePoolRes = store .OnTakePoolReceived (takePoolMsg );
599
599
const takePoolResEncoded = protobuf .encode ({ poolId: takePoolRes });
600
600
return takePoolResEncoded ;
601
601
602
602
case " SINGLE_DEPOSIT" :
603
- const singleDepositMsg: MsgSingleAssetDepositRequest = protobuf . decode ( MsgSingleAssetDepositRequest , data .Data );
603
+ const singleDepositMsg: MsgSingleAssetDepositRequest = unmarshalJSON ( data .Data );
604
604
abortTransactionUnless (data .stateChange .poolId === " " );
605
605
const singleDepositRes = store .OnSingleAssetDepositReceived (singleDepositMsg , data .stateChange );
606
606
const singleDepositResEncoded = protobuf .encode (singleDepositRes );
607
607
return singleDepositResEncoded ;
608
608
609
609
case " MAKE_MULTI_DEPOSIT" :
610
- const makeMultiDepositMsg: MsgMakeMultiAssetDepositRequest = protobuf .decode (
611
- MsgMakeMultiAssetDepositRequest ,
612
- data .Data
613
- );
610
+ const makeMultiDepositMsg: MsgMakeMultiAssetDepositRequest = unmarshalJSON (data .Data );
614
611
const makeMultiDepositRes = k .OnMakeMultiAssetDepositReceived (makeMultiDepositMsg , data .stateChange );
615
612
const makeMultiDepositResEncoded = protobuf .encode (makeMultiDepositRes );
616
613
return makeMultiDepositResEncoded ;
617
614
618
615
case " TAKE_MULTI_DEPOSIT" :
619
- const takeMultiDepositMsg: MsgTakeMultiAssetDepositRequest = protobuf .decode (
620
- MsgTakeMultiAssetDepositRequest ,
621
- data .Data
622
- );
616
+ const takeMultiDepositMsg: MsgTakeMultiAssetDepositRequest = unmarshalJSON (data .Data );
623
617
const takeMultiDepositRes = k .OnTakeMultiAssetDepositReceived (takeMultiDepositMsg , data .stateChange );
624
618
const takeMultiDepositResEncoded = protobuf .encode (takeMultiDepositRes );
625
619
return takeMultiDepositResEncoded ;
626
620
627
621
case " MULTI_WITHDRAW" :
628
- const multiWithdrawMsg: MsgMultiAssetWithdrawRequest = protobuf . decode ( MsgMultiAssetWithdrawRequest , data .Data );
622
+ const multiWithdrawMsg: MsgMultiAssetWithdrawRequest = unmarshalJSON ( data .Data );
629
623
const multiWithdrawRes = k .OnMultiAssetWithdrawReceived (multiWithdrawMsg , data .stateChange );
630
624
const multiWithdrawResEncoded = protobuf .encode (multiWithdrawRes );
631
625
return multiWithdrawResEncoded ;
632
626
633
627
case " LEFT_SWAP" :
634
628
case " RIGHT_SWAP" :
635
- const swapMsg: MsgSwapRequest = protobuf . decode ( MsgSwapRequest , data .Data );
629
+ const swapMsg: MsgSwapRequest = unmarshalJSON ( data .Data );
636
630
const swapRes = k .OnSwapReceived (swapMsg , data .stateChange );
637
631
const swapResEncoded = protobuf .encode (swapRes );
638
632
return swapResEncoded ;
@@ -656,26 +650,26 @@ function OnAcknowledgementPacket(packet: Packet, data: IBCSwapPacketData, ack: A
656
650
default :
657
651
switch (data .type ) {
658
652
case " MAKE_POOL" :
659
- const msgMakePool: MsgMakePoolRequest = protobuf . decode (data .Data );
653
+ const msgMakePool: MsgMakePoolRequest = unmarshalJSON (data .Data );
660
654
const errMakePool = store .OnMakePoolAcknowledged (msgMakePool , data .StateChange .PoolId );
661
655
abortTransactionUnless (errMakePool === undefined );
662
656
break ;
663
657
664
658
case " TAKE_POOL" :
665
- const msgTakePool: MsgTakePoolRequest = protobuf . decode (data .Data );
659
+ const msgTakePool: MsgTakePoolRequest = unmarshalJSON (data .Data );
666
660
const errTakePool = store .OnTakePoolAcknowledged (msgTakePool );
667
661
abortTransactionUnless (errTakePool === undefined );
668
662
break ;
669
663
670
664
case " SINGLE_DEPOSIT" :
671
- const msgSingleDeposit: MsgSingleAssetDepositRequest = protobuf . decode (data .Data );
665
+ const msgSingleDeposit: MsgSingleAssetDepositRequest = punmarshalJSON (data .Data );
672
666
const resSingleDeposit: MsgSingleAssetDepositResponse = protobuf .decode (ack .GetResult ());
673
667
const errSingleDeposit = store .OnSingleAssetDepositAcknowledged (msgSingleDeposit , resSingleDeposit );
674
668
abortTransactionUnless (errSingleDeposit === undefined );
675
669
break ;
676
670
677
671
case " MAKE_MULTI_DEPOSIT" :
678
- const msgMakeMultiDeposit: MsgMakeMultiAssetDepositRequest = protobuf . decode (data .Data );
672
+ const msgMakeMultiDeposit: MsgMakeMultiAssetDepositRequest = unmarshalJSON (data .Data );
679
673
const resMakeMultiDeposit: MsgMultiAssetDepositResponse = protobuf .decode (ack .GetResult ());
680
674
const errMakeMultiDeposit = store .OnMakeMultiAssetDepositAcknowledged (
681
675
msgMakeMultiDeposit ,
@@ -685,22 +679,22 @@ function OnAcknowledgementPacket(packet: Packet, data: IBCSwapPacketData, ack: A
685
679
break ;
686
680
687
681
case " TAKE_MULTI_DEPOSIT" :
688
- const msgTakeMultiDeposit: MsgTakeMultiAssetDepositRequest = protobuf . decode (data .Data );
682
+ const msgTakeMultiDeposit: MsgTakeMultiAssetDepositRequest = unmarshalJSON (data .Data );
689
683
const resTakeMultiDeposit: MsgTakePoolResponse = protobuf .decode (ack .GetResult ());
690
684
const errTakeMultiDeposit = store .OnTakeMultiAssetDepositAcknowledged (msgTakeMultiDeposit , data .StateChange );
691
685
abortTransactionUnless (errTakeMultiDeposit === undefined );
692
686
break ;
693
687
694
688
case " MULTI_WITHDRAW" :
695
- const msgMultiWithdraw: MsgMultiAssetWithdrawRequest = protobuf . decode (data .Data );
689
+ const msgMultiWithdraw: MsgMultiAssetWithdrawRequest = unmarshalJSON (data .Data );
696
690
const resMultiWithdraw: MsgMultiAssetWithdrawResponse = protobuf .decode (ack .GetResult ());
697
691
const errMultiWithdraw = store .OnMultiAssetWithdrawAcknowledged (msgMultiWithdraw , resMultiWithdraw );
698
692
abortTransactionUnless (errMultiWithdraw === undefined );
699
693
break ;
700
694
701
695
case " LEFT_SWAP" :
702
696
case " RIGHT_SWAP" :
703
- const msgSwap: MsgSwapRequest = protobuf . decode (data .Data );
697
+ const msgSwap: MsgSwapRequest = unmarshalJSON (data .Data );
704
698
const resSwap: MsgSwapResponse = protobuf .decode (ack .GetResult ());
705
699
const errSwap = store .OnSwapAcknowledged (msgSwap , resSwap );
706
700
abortTransactionUnless (errSwap === undefined );
@@ -729,45 +723,39 @@ function refundPacketToken(packet: Packet, data: IBCSwapPacketData): Error | und
729
723
730
724
switch (data .type ) {
731
725
case " MAKE_POOL" :
732
- const makePoolMsg: MsgMakePoolRequest = protobuf . decode ( MsgMakePoolRequest , data .Data );
726
+ const makePoolMsg: MsgMakePoolRequest = unmarshalJSON ( data .Data );
733
727
// Refund initial liquidity
734
728
sender = makePoolMsg .creator ;
735
729
token = makePoolMsg .liquidity [0 ].balance ;
736
730
break ;
737
731
738
732
case " SINGLE_DEPOSIT" :
739
- const singleDepositMsg: MsgSingleAssetDepositRequest = protobuf . decode ( MsgSingleAssetDepositRequest , data .Data );
733
+ const singleDepositMsg: MsgSingleAssetDepositRequest = unmarshalJSON ( data .Data );
740
734
token = singleDepositMsg .token ;
741
735
sender = singleDepositMsg .sender ;
742
736
break ;
743
737
744
738
case " MAKE_MULTI_DEPOSIT" :
745
- const makeMultiDepositMsg: MsgMakeMultiAssetDepositRequest = protobuf .decode (
746
- MsgMakeMultiAssetDepositRequest ,
747
- data .Data
748
- );
739
+ const makeMultiDepositMsg: MsgMakeMultiAssetDepositRequest = unmarshalJSON (data .Data );
749
740
token = makeMultiDepositMsg .deposits [0 ].balance ;
750
741
sender = makeMultiDepositMsg .deposits [0 ].sender ;
751
742
break ;
752
743
753
744
case " TAKE_MULTI_DEPOSIT" :
754
- const takeMultiDepositMsg: MsgTakeMultiAssetDepositRequest = protobuf .decode (
755
- MsgTakeMultiAssetDepositRequest ,
756
- data .Data
757
- );
745
+ const takeMultiDepositMsg: MsgTakeMultiAssetDepositRequest = unmarshalJSON (data .Data );
758
746
const { order, found } = store .getMultiDepositOrder (takeMultiDepositMsg .poolId , takeMultiDepositMsg .orderId );
759
747
abortTransactionUnless (found );
760
748
token = order .Deposits [1 ];
761
749
sender = msg .Sender ;
762
750
break ;
763
751
case " MULTI_WITHDRAW" :
764
- const multiWithdrawMsg: MsgMultiAssetWithdrawRequest = protobuf . decode ( MsgMultiAssetWithdrawRequest , data .Data );
752
+ const multiWithdrawMsg: MsgMultiAssetWithdrawRequest = unmarshalJSON ( data .Data );
765
753
token = multiWithdrawMsg .poolToken ;
766
754
sender = multiWithdrawMsg .receiver ;
767
755
break ;
768
756
769
757
case " RIGHT_SWAP" :
770
- const swapMsg: MsgSwapRequest = protobuf . decode ( MsgSwapRequest , data .Data );
758
+ const swapMsg: MsgSwapRequest = unmarshalJSON ( data .Data );
771
759
token = swapMsg .tokenIn ;
772
760
sender = swapMsg .sender ;
773
761
break ;
@@ -921,6 +909,17 @@ interface MsgSwapResponse {
921
909
These are methods that output a state change on the source chain, which will be subsequently synced to the destination chain.
922
910
923
911
``` ts
912
+ // Custom helper to support compatability with cosmwasm: Same function MarshalJSON/unmarshalJSON in Golang
913
+ function marshalJSON(data : any ): Uint8Array {
914
+ const jsonData = JSON .stringify (any );
915
+ return protobuf .encode (json );
916
+ }
917
+
918
+ function unmarshalJSON<T >(data : any ): T {
919
+ const jsonData = protobuf .decode (data );
920
+ return json .parse (jsonData ) as T
921
+ }
922
+
924
923
function makePool(msg : MsgMakePoolRequest ): MsgMakePoolResponse {
925
924
926
925
const { counterPartyChainId, connected } = store .GetCounterPartyChainID (msg .sourcePort , msg .sourceChannel );
@@ -949,7 +948,7 @@ These are methods that output a state change on the source chain, which will be
949
948
950
949
const packet: IBCSwapPacketData = {
951
950
type: " MAKE_POOL" ,
952
- data: protobuf . encode (msg ),
951
+ data: marshalJSON (msg ),
953
952
stateChange: {
954
953
poolId: poolId ,
955
954
sourceChainId: store .ChainID (),
@@ -1045,7 +1044,7 @@ These are methods that output a state change on the source chain, which will be
1045
1044
1046
1045
const packet: IBCSwapPacketData = {
1047
1046
type: " MAKE_MULTI_DEPOSIT" ,
1048
- data: protobuf . encode (msg ),
1047
+ data: marshalJSON (msg ),
1049
1048
stateChange: { poolTokens: poolTokens },
1050
1049
};
1051
1050
@@ -1087,7 +1086,7 @@ These are methods that output a state change on the source chain, which will be
1087
1086
1088
1087
const packet: IBCSwapPacketData = {
1089
1088
type: " TAKE_MULTI_DEPOSIT" ,
1090
- data: protobuf . encode (msg ),
1089
+ data: marshalJSON (msg ),
1091
1090
stateChange: { poolTokens },
1092
1091
};
1093
1092
@@ -1125,7 +1124,7 @@ function singleAssetDeposit(msg: MsgSingleAssetDepositRequest): MsgSingleAssetDe
1125
1124
1126
1125
const packet: IBCSwapPacketData = {
1127
1126
type: " SINGLE_DEPOSIT" ,
1128
- data: protobuf . encode (msg );,
1127
+ data: marshalJSON (msg );,
1129
1128
stateChange: { poolTokens: [poolToken ] },
1130
1129
};
1131
1130
@@ -1156,7 +1155,7 @@ function multiAssetWithdraw(msg: MsgMultiAssetWithdrawRequest): MsgMultiAssetWit
1156
1155
1157
1156
const packet: IBCSwapPacketData = {
1158
1157
type: " MULTI_WITHDRAW" ,
1159
- data: protobuf . encode (msg ),
1158
+ data: marshalJSON (msg ),
1160
1159
stateChange: {
1161
1160
out: outs ,
1162
1161
poolTokens: [msg .poolToken ],
@@ -1206,7 +1205,7 @@ function swap(msg: MsgSwapRequest): MsgSwapResponse {
1206
1205
1207
1206
const packet : IBCSwapPacketData = {
1208
1207
type: msgType ,
1209
- data: protobuf . encode (msg ),
1208
+ data: marshalJSON (msg ),
1210
1209
stateChange: { out: [tokenOut ] },
1211
1210
};
1212
1211
0 commit comments