Skip to content

Commit

Permalink
fix: cli command to claim fees
Browse files Browse the repository at this point in the history
  • Loading branch information
izyak committed Jan 15, 2024
1 parent 5ef081b commit abf10d9
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 3 deletions.
46 changes: 46 additions & 0 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Most of these commands take a [path] argument. Make sure:
closeChannelCmd(a),
lineBreakCommand(),
registerCounterpartyCmd(a),
lineBreakCommand(),
claimFeesCmd(a),
)

return cmd
Expand Down Expand Up @@ -1124,3 +1126,47 @@ $ %s reg-cpt channel-1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk juno1g0ny48
}
return cmd
}

func claimFeesCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "claim-fees src_chain dst_chain dst_connection_address src_network_id ",
Aliases: []string{"cf"},
Short: "claim fees of src_chain on dst_chain. dst_chain.claimFees(src_network_id, src_relayer_address)",
Args: withUsage(cobra.MatchAll(cobra.ExactArgs(4))),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s claim-fees archway icon cx6f86ed848f9f0d03ba1220811d95d864c72da88c archway-1
$ %s claim-fees icon archway archway1f68v03g2646z7wk9h9sy5uxhztajcrdgwvdrsftyp4448h067v0shn6l5w 0x1.icon`,
appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {

src, ok := a.config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
dst, ok := a.config.Chains[args[1]]
if !ok {
return errChainNotFound(args[1])
}

Check warning on line 1149 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1141-L1149

Added lines #L1141 - L1149 were not covered by tests

keyName := src.ChainProvider.Key()
relayAddress, err := src.ChainProvider.ShowAddress(keyName)
if err != nil {
return errKeyDoesntExist(keyName)
}

Check warning on line 1155 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1151-L1155

Added lines #L1151 - L1155 were not covered by tests

connectionAdd := args[2]
srcNetworkId := args[3]

msg, err := dst.ChainProvider.MsgClaimFees(srcNetworkId, relayAddress)
if err != nil {
return err
}
_, success, err := dst.ChainProvider.SendCustomMessage(cmd.Context(), connectionAdd, msg, "")
if success {
fmt.Println("Fee claim request successful---")
}
return err

Check warning on line 1168 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L1157-L1168

Added lines #L1157 - L1168 were not covered by tests
},
}
return cmd
}
8 changes: 8 additions & 0 deletions relayer/chains/cosmos/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,14 @@ func (cc *CosmosProvider) MsgSubmitMisbehaviour(clientID string, misbehaviour ib
return NewCosmosMessage(msg), nil
}

func (ap *CosmosProvider) SendCustomMessage(ctx context.Context, contract string, msg provider.RelayerMessage, memo string) (*provider.RelayerTxResponse, bool, error) {
panic("Not implemented for Cosmos")

Check warning on line 1058 in relayer/chains/cosmos/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/cosmos/tx.go#L1057-L1058

Added lines #L1057 - L1058 were not covered by tests
}

func (cc *CosmosProvider) MsgClaimFees(dstChainID, dstAddress string) (provider.RelayerMessage, error) {
panic("Not implemented for Cosmos")

Check warning on line 1062 in relayer/chains/cosmos/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/cosmos/tx.go#L1061-L1062

Added lines #L1061 - L1062 were not covered by tests
}

// RelayPacketFromSequence relays a packet with a given seq on src and returns recvPacket msgs, timeoutPacketmsgs and error
func (cc *CosmosProvider) RelayPacketFromSequence(
ctx context.Context,
Expand Down
23 changes: 23 additions & 0 deletions relayer/chains/icon/fees.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package icon

import (
"github.com/cosmos/relayer/v2/relayer/chains/icon/types"
"github.com/cosmos/relayer/v2/relayer/provider"
)

type MsgClaimFees struct {
Nid string `json:"nid"`
Address types.HexBytes `json:"address"`
}

func (icp *IconProvider) MsgClaimFees(dstChainID, dstAddress string) (provider.RelayerMessage, error) {

params := MsgClaimFees{
Nid: dstChainID,
Address: types.NewHexBytes([]byte(dstAddress)),
}

msg := icp.NewIconMessage(params, MethodClaimFees)

return msg, nil

Check warning on line 22 in relayer/chains/icon/fees.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/fees.go#L13-L22

Added lines #L13 - L22 were not covered by tests
}
2 changes: 2 additions & 0 deletions relayer/chains/icon/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ const (
MethodTimeoutPacket = "timeoutPacket"

MethodGetAllPorts = "getAllPorts"

MethodClaimFees = "claimFees"
)
71 changes: 68 additions & 3 deletions relayer/chains/icon/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func (icp *IconProvider) SendMessagesToMempool(

for _, msg := range msgs {
if msg != nil {
err := icp.SendIconTransaction(ctx, msg, asyncCtx, asyncCallback)
err := icp.SendIconTransaction(ctx, types.Address(icp.PCfg.IbcHandlerAddress), msg, asyncCtx, asyncCallback)
if err != nil {
icp.log.Warn("Send Icon Transaction Error", zap.String("method", msg.Type()), zap.Error(err))
continue
Expand All @@ -703,8 +703,73 @@ func (icp *IconProvider) SendMessagesToMempool(
return nil
}

func (icp *IconProvider) SendCustomMessage(ctx context.Context, contract string, msg provider.RelayerMessage, memo string) (*provider.RelayerTxResponse, bool, error) {
var (
rlyResp *provider.RelayerTxResponse
callbackErr error
wg sync.WaitGroup
)

callback := func(rtr *provider.RelayerTxResponse, err error) {
rlyResp = rtr
callbackErr = err
wg.Done()
}

Check warning on line 717 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L706-L717

Added lines #L706 - L717 were not covered by tests

wg.Add(1)
if err := retry.Do(func() error {
return icp.SendCustomMessageToMempool(ctx, types.Address(contract), msg, memo, ctx, callback)
}, retry.Context(ctx), rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
icp.log.Info(
"Error building or broadcasting transaction",
zap.String("chain_id", icp.PCfg.ChainID),
zap.Uint("attempt", n+1),
zap.Uint("max_attempts", rtyAttNum),
zap.Error(err),
)
})); err != nil {
return nil, false, err
}

Check warning on line 732 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L719-L732

Added lines #L719 - L732 were not covered by tests

wg.Wait()

if callbackErr != nil {
return rlyResp, false, callbackErr
}

Check warning on line 738 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L734-L738

Added lines #L734 - L738 were not covered by tests

if rlyResp.Code != 1 {
return rlyResp, false, fmt.Errorf("transaction failed with code: %d", rlyResp.Code)
}

Check warning on line 742 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L740-L742

Added lines #L740 - L742 were not covered by tests

return rlyResp, true, callbackErr

Check warning on line 744 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L744

Added line #L744 was not covered by tests

}

func (icp *IconProvider) SendCustomMessageToMempool(
ctx context.Context,
contract types.Address,
msg provider.RelayerMessage,
memo string,
asyncCtx context.Context,
asyncCallback func(*provider.RelayerTxResponse, error),
) error {
icp.txMu.Lock()
defer icp.txMu.Unlock()

if msg != nil {
err := icp.SendIconTransaction(ctx, contract, msg, asyncCtx, asyncCallback)
if err != nil {
icp.log.Warn("Send Icon Transaction Error", zap.String("method", msg.Type()), zap.Error(err))
return err
}

Check warning on line 764 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L755-L764

Added lines #L755 - L764 were not covered by tests
}

return nil

Check warning on line 767 in relayer/chains/icon/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/tx.go#L767

Added line #L767 was not covered by tests
}

func (icp *IconProvider) SendIconTransaction(
ctx context.Context,
contract types.Address,
msg provider.RelayerMessage,
asyncCtx context.Context,
asyncCallback func(*provider.RelayerTxResponse, error)) error {
Expand All @@ -717,7 +782,7 @@ func (icp *IconProvider) SendIconTransaction(
txParamEst := &types.TransactionParamForEstimate{
Version: types.NewHexInt(types.JsonrpcApiVersion),
FromAddress: types.Address(wallet.Address().String()),
ToAddress: types.Address(icp.PCfg.IbcHandlerAddress),
ToAddress: contract,
NetworkID: types.NewHexInt(icp.PCfg.ICONNetworkID),
DataType: "call",
Data: types.CallData{
Expand All @@ -739,7 +804,7 @@ func (icp *IconProvider) SendIconTransaction(
txParam := &types.TransactionParam{
Version: types.NewHexInt(types.JsonrpcApiVersion),
FromAddress: types.Address(wallet.Address().String()),
ToAddress: types.Address(icp.PCfg.IbcHandlerAddress),
ToAddress: contract,
NetworkID: types.NewHexInt(icp.PCfg.ICONNetworkID),
StepLimit: stepLimit,
DataType: "call",
Expand Down
8 changes: 8 additions & 0 deletions relayer/chains/penumbra/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ func parseEventsFromTxResponse(resp *sdk.TxResponse) []provider.RelayerEvent {
return events
}

func (cc *PenumbraProvider) MsgClaimFees(dstChainID, dstAddress string) (provider.RelayerMessage, error) {
panic("Not implemented for Penumbra")
}

func (ap *PenumbraProvider) SendCustomMessage(ctx context.Context, contract string, msg provider.RelayerMessage, memo string) (*provider.RelayerTxResponse, bool, error) {
panic("Not implemented for Penumbra")
}

// CreateClient creates an sdk.Msg to update the client on src with consensus state from dst
func (cc *PenumbraProvider) MsgCreateClient(clientState ibcexported.ClientState, consensusState ibcexported.ConsensusState) (provider.RelayerMessage, error) {
signer, err := cc.Address()
Expand Down
2 changes: 2 additions & 0 deletions relayer/chains/wasm/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
MethodGetNextClientSequence = "get_next_client_sequence"
MethodGetNextChannelSequence = "get_next_channel_sequence"
MethodGetNextConnectionSequence = "get_next_connection_sequence"

MethodClaimFees = "claim_fees"
)

const (
Expand Down
35 changes: 35 additions & 0 deletions relayer/chains/wasm/fees.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package wasm

import (
"encoding/json"

"github.com/cosmos/relayer/v2/relayer/provider"
)

type ClaimFeesMsg struct {
ClaimFees struct {
Nid string `json:"nid"`
Address string `json:"address"`
} `json:"claim_fees"`
}

func (c *ClaimFeesMsg) Type() string {
return "claim_fees"

Check warning on line 17 in relayer/chains/wasm/fees.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/fees.go#L16-L17

Added lines #L16 - L17 were not covered by tests
}

func (c *ClaimFeesMsg) MsgBytes() ([]byte, error) {
return json.Marshal(c)

Check warning on line 21 in relayer/chains/wasm/fees.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/fees.go#L20-L21

Added lines #L20 - L21 were not covered by tests
}

func (ap *WasmProvider) MsgClaimFees(dstChainID, dstAddress string) (provider.RelayerMessage, error) {
params := &ClaimFeesMsg{
ClaimFees: struct {
Nid string "json:\"nid\""
Address string "json:\"address\""
}{
Nid: dstChainID,
Address: dstAddress,
},
}
return params, nil

Check warning on line 34 in relayer/chains/wasm/fees.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/fees.go#L24-L34

Added lines #L24 - L34 were not covered by tests
}
96 changes: 96 additions & 0 deletions relayer/chains/wasm/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"
"time"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/codec/types"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"

Expand Down Expand Up @@ -708,6 +709,101 @@ func (ap *WasmProvider) SendMessages(ctx context.Context, msgs []provider.Relaye
return rlyResp, true, callbackErr
}

func (ap *WasmProvider) SendCustomMessage(ctx context.Context, contract string, msg provider.RelayerMessage, memo string) (*provider.RelayerTxResponse, bool, error) {
var (
rlyResp *provider.RelayerTxResponse
callbackErr error
wg sync.WaitGroup
)

callback := func(rtr *provider.RelayerTxResponse, err error) {
callbackErr = err

if err != nil {
wg.Done()
return
}

Check warning on line 725 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L712-L725

Added lines #L712 - L725 were not covered by tests

for i, e := range rtr.Events {
if startsWithWasm(e.EventType) {
rtr.Events[i].EventType = findEventType(e.EventType)
}

Check warning on line 730 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L727-L730

Added lines #L727 - L730 were not covered by tests
}
rlyResp = rtr
wg.Done()

Check warning on line 733 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L732-L733

Added lines #L732 - L733 were not covered by tests
}

wg.Add(1)

if err := retry.Do(func() error {
return ap.SendTransactionCosmWasm(ctx, contract, msg, ctx, callback)
}, retry.Context(ctx), rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
ap.log.Info(
"Error building or broadcasting transaction",
zap.String("chain_id", ap.PCfg.ChainID),
zap.Uint("attempt", n+1),
zap.Uint("max_attempts", rtyAttNum),
zap.Error(err),
)
})); err != nil {
return nil, false, err
}

Check warning on line 750 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L736-L750

Added lines #L736 - L750 were not covered by tests

wg.Wait()

if callbackErr != nil {
return rlyResp, false, callbackErr
}

Check warning on line 756 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L752-L756

Added lines #L752 - L756 were not covered by tests

if rlyResp.Code != 0 {
return rlyResp, false, fmt.Errorf("transaction failed with code: %d", rlyResp.Code)
}

Check warning on line 760 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L758-L760

Added lines #L758 - L760 were not covered by tests

return rlyResp, true, callbackErr

Check warning on line 762 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L762

Added line #L762 was not covered by tests

}

func (ap *WasmProvider) SendTransactionCosmWasm(
ctx context.Context,
contract string,
msg provider.RelayerMessage,

asyncCtx context.Context,
asyncCallback func(*provider.RelayerTxResponse, error),
) error {
ap.txMu.Lock()
defer ap.txMu.Unlock()

cliCtx := ap.ClientContext()
factory, err := ap.PrepareFactory(ap.TxFactory())
if err != nil {
return err
}
msgByte, err := msg.MsgBytes()
if err != nil {
return err
}

Check warning on line 785 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L773-L785

Added lines #L773 - L785 were not covered by tests

signer, _ := ap.Address()
params := &wasmtypes.MsgExecuteContract{
Sender: signer,
Contract: contract,
Msg: msgByte,
}

txBytes, sequence, err := ap.buildMessages(cliCtx, factory, params)
if err != nil {
return err
}
if err := ap.BroadcastTx(cliCtx, txBytes, []provider.RelayerMessage{msg}, asyncCtx, defaultBroadcastWaitTimeout, asyncCallback, false); err != nil {
if strings.Contains(err.Error(), sdkerrors.ErrWrongSequence.Error()) {
ap.handleAccountSequenceMismatchError(err)
}

Check warning on line 801 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L787-L801

Added lines #L787 - L801 were not covered by tests
}
ap.updateNextAccountSequence(sequence + 1)
return nil

Check warning on line 804 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L803-L804

Added lines #L803 - L804 were not covered by tests
}

func (ap *WasmProvider) SendMessagesToMempool(
ctx context.Context,
msgs []provider.RelayerMessage,
Expand Down
3 changes: 3 additions & 0 deletions relayer/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ type ChainProvider interface {

Init(ctx context.Context) error

MsgClaimFees(dstChainID string, dstAddress string) (RelayerMessage, error)

// [Begin] Client IBC message assembly functions
NewClientState(dstChainID string, dstIBCHeader IBCHeader, dstTrustingPeriod, dstUbdPeriod time.Duration, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour bool) (ibcexported.ClientState, error)

Expand Down Expand Up @@ -391,6 +393,7 @@ type ChainProvider interface {
AcknowledgementFromSequence(ctx context.Context, dst ChainProvider, dsth, seq uint64, dstChanID, dstPortID, srcChanID, srcPortID string) (RelayerMessage, error)

SendMessage(ctx context.Context, msg RelayerMessage, memo string) (*RelayerTxResponse, bool, error)
SendCustomMessage(ctx context.Context, contract string, msg RelayerMessage, memo string) (*RelayerTxResponse, bool, error)
SendMessages(ctx context.Context, msgs []RelayerMessage, memo string) (*RelayerTxResponse, bool, error)
SendMessagesToMempool(
ctx context.Context,
Expand Down

0 comments on commit abf10d9

Please sign in to comment.