Skip to content

Commit

Permalink
feat: add refund msg (#251)
Browse files Browse the repository at this point in the history
Co-authored-by: fx0x55 <[email protected]>
  • Loading branch information
zakir-code and fx0x55 authored Mar 8, 2024
1 parent 530de76 commit c500f34
Show file tree
Hide file tree
Showing 9 changed files with 832 additions and 131 deletions.
10 changes: 10 additions & 0 deletions proto/fx/crosschain/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ message MsgBridgeTokenClaim {

message MsgBridgeTokenClaimResponse {}

message MsgConfirmRefund {
uint64 nonce = 1;
string bridger_address = 3;
string external_address = 4;
string signature = 5;
string chain_name = 6;
}

message MsgConfirmRefundResponse {}

// Deprecated: after block 5713000
message MsgSetOrchestratorAddress {
string oracle_address = 1;
Expand Down
27 changes: 25 additions & 2 deletions x/crosschain/types/abi_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
// truncate the first several bytes where the call name is encoded to finally get the equal of the

var (
outgoingBatchTxCheckpointABI abi.ABI
oracleSetCheckpointABI abi.ABI
outgoingBatchTxCheckpointABI abi.ABI
oracleSetCheckpointABI abi.ABI
bridgeCallRefundTxCheckpointABI abi.ABI
)

func init() {
Expand Down Expand Up @@ -63,6 +64,24 @@ func init() {
{ "internalType": "bytes32", "name": "", "type": "bytes32" }
]
}]`

bridgeCallRefundTxCheckpointABIJSON = `[{
"name":"refundToken",
"stateMutability":"nonpayable",
"type":"function",
"inputs":[
{ "internalType": "bytes32", "name": "_fxbridgeId", "type": "bytes32" },
{ "internalType": "bytes32", "name": "_methodName", "type": "bytes32" },
{ "internalType": "address", "name": "_receiver", "type": "address" },
{ "internalType": "address[]", "name": "_tokenContract", "type": "address[]" },
{ "internalType": "uint256[]", "name": "_amounts", "type": "uint256[]" },
{ "internalType": "uint256", "name": "_eventNonce", "type": "uint256" },
{ "internalType": "uint256", "name": "_timeout", "type": "uint256" }
],
"outputs": [
{ "internalType": "bytes32", "name": "", "type": "bytes32" }
]
}]`
)

var err error
Expand All @@ -74,4 +93,8 @@ func init() {
if err != nil {
panic(err.Error())
}
bridgeCallRefundTxCheckpointABI, err = abi.JSON(strings.NewReader(bridgeCallRefundTxCheckpointABIJSON))
if err != nil {
panic(err.Error())
}
}
16 changes: 16 additions & 0 deletions x/crosschain/types/msg_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ func (b MsgValidate) MsgConfirmBatchValidate(m *MsgConfirmBatch) (err error) {
return nil
}

func (b MsgValidate) MsgConfirmRefundValidate(m *MsgConfirmRefund) (err error) {
if _, err = sdk.AccAddressFromBech32(m.BridgerAddress); err != nil {
return errortypes.ErrInvalidAddress.Wrapf("invalid bridger address: %s", err)
}
if err = fxtypes.ValidateEthereumAddress(m.ExternalAddress); err != nil {
return errortypes.ErrInvalidAddress.Wrapf("invalid external address: %s", err)
}
if len(m.Signature) == 0 {
return errortypes.ErrInvalidRequest.Wrap("empty signature")
}
if _, err = hex.DecodeString(m.Signature); err != nil {
return errortypes.ErrInvalidRequest.Wrap("could not hex decode signature")
}
return nil
}

func (b MsgValidate) ValidateExternalAddress(addr string) error {
return fxtypes.ValidateEthereumAddress(addr)
}
Expand Down
49 changes: 49 additions & 0 deletions x/crosschain/types/msg_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,55 @@ func TestUpdateChainOraclesProposal_ValidateBasic(t *testing.T) {
}
}

func TestMsgConfirmRefund_ValidateBasic(t *testing.T) {
moduleName := getRandModule()
normalBridgeAddress := sdk.AccAddress(tmrand.Bytes(20)).String()
normalExternalAddress := helpers.GenerateAddressByModule(moduleName)
testCases := []struct {
testName string
msg *types.MsgConfirmRefund
expectPass bool
err error
errReason string
}{
{
testName: "err - empty chain name",
msg: &types.MsgConfirmRefund{
ChainName: "",
},
expectPass: false,
err: errortypes.ErrInvalidRequest,
errReason: "unrecognized cross chain name: invalid request",
},
{
testName: "success",
msg: &types.MsgConfirmRefund{
Nonce: uint64(tmrand.Int63n(100000)),
BridgerAddress: normalBridgeAddress,
ExternalAddress: normalExternalAddress,
Signature: hex.EncodeToString(tmrand.Bytes(100)),
ChainName: moduleName,
},
expectPass: true,
err: nil,
errReason: "",
},
}

for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
err := testCase.msg.ValidateBasic()
if testCase.expectPass {
require.NoError(t, err)
} else {
require.NotNil(t, err)
require.ErrorIs(t, err, testCase.err, "%+v", testCase.msg)
require.EqualValuesf(t, testCase.errReason, err.Error(), "%+v", testCase.msg)
}
})
}
}

// externalAddressToUpper for test case address to upper
func externalAddressToUpper(address string) string {
if strings.HasPrefix(address, "0x") {
Expand Down
31 changes: 29 additions & 2 deletions x/crosschain/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ const (
TypeMsgIncreaseBridgeFee = "increase_bridge_fee"
TypeMsgSendToExternalClaim = "send_to_external_claim"

TypeMsgRequestBatch = "request_batch"
TypeMsgConfirmBatch = "confirm_batch"
TypeMsgRequestBatch = "request_batch"
TypeMsgConfirmBatch = "confirm_batch"
TypeMsgConfirmRefund = "confirm_refund"

TypeMsgUpdateParams = "update_params"

Expand Down Expand Up @@ -94,6 +95,9 @@ var (
_ sdk.Msg = &MsgConfirmBatch{}
_ CrossChainMsg = &MsgConfirmBatch{}

_ sdk.Msg = &MsgConfirmRefund{}
_ CrossChainMsg = &MsgConfirmRefund{}

_ sdk.Msg = &MsgUpdateParams{}
_ CrossChainMsg = &MsgUpdateParams{}

Expand Down Expand Up @@ -122,6 +126,7 @@ type MsgValidateBasic interface {
MsgIncreaseBridgeFeeValidate(m *MsgIncreaseBridgeFee) (err error)
MsgRequestBatchValidate(m *MsgRequestBatch) (err error)
MsgConfirmBatchValidate(m *MsgConfirmBatch) (err error)
MsgConfirmRefundValidate(m *MsgConfirmRefund) (err error)

ValidateExternalAddress(addr string) error
ExternalAddressToAccAddress(addr string) (sdk.AccAddress, error)
Expand Down Expand Up @@ -418,6 +423,28 @@ func (m *MsgConfirmBatch) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.BridgerAddress)}
}

// MsgConfirmRefund

func (m *MsgConfirmRefund) Route() string { return RouterKey }

func (m *MsgConfirmRefund) Type() string { return TypeMsgConfirmRefund }

func (m *MsgConfirmRefund) ValidateBasic() error {
if router, ok := msgValidateBasicRouter[m.ChainName]; !ok {
return errortypes.ErrInvalidRequest.Wrap("unrecognized cross chain name")
} else {
return router.MsgConfirmRefundValidate(m)
}
}

func (m *MsgConfirmRefund) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.BridgerAddress)}
}

func (m *MsgConfirmRefund) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m))
}

// MsgCancelSendToExternal

// Route should return the name of the module
Expand Down
Loading

0 comments on commit c500f34

Please sign in to comment.