Skip to content

Commit

Permalink
fix(bridge): enhance error handling and update provider logic
Browse files Browse the repository at this point in the history
- Refactor bridge providers (bungee, helix_liquidity_claim, okx, routernitro) to improve error
handling and clarify messages.
- Remove unused 'SourceChain' field in market util and update query in helix_liquidity_claim.
- Simplify version information logging in main package.
- Ensure all provider functions return descriptive error messages and wrap errors accordingly.
  • Loading branch information
perrornet committed Aug 27, 2024
1 parent c1cee3c commit f35a2a4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"omni-balance/utils/configs"
"omni-balance/utils/notice"
"omni-balance/utils/provider"
_ "omni-balance/utils/provider/bridge/bungee"
_ "omni-balance/utils/provider/bridge/darwinia"
_ "omni-balance/utils/provider/bridge/helix_liquidity_claim"
_ "omni-balance/utils/provider/bridge/okx"
Expand Down Expand Up @@ -67,7 +68,9 @@ func Action(cli *cli.Context) error {
if err := initConfig(ctx, cli.Bool("placeholder"), cli.String("conf"), cli.String("port")); err != nil {
return errors.Wrap(err, "init config")
}
log.Infof("version: %s, commit: %s, commitTime: %s", version, commitMessage, commitTime)
if version != "" && commitTime != "" {
log.Infof("version: %s, commit: %s, commitTime: %s", version, commitMessage, commitTime)
}

if err := notice.Init(notice.Type(config.Notice.Type), config.Notice.Config, config.Notice.Interval); err != nil {
log.Warnf("init notice error: %v", err)
Expand Down
1 change: 1 addition & 0 deletions internal/daemons/market/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func providerSupportsOrder(ctx context.Context, p provider.Provider, order model
SourceToken: order.TokenInName,
Sender: conf.GetWallet(order.Wallet),
TargetToken: order.TokenOutName,
SourceChain: order.SourceChainName,
Receiver: order.Wallet,
TargetChain: order.TargetChainName,
Amount: order.Amount,
Expand Down
24 changes: 11 additions & 13 deletions utils/provider/bridge/bungee/bungee.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,20 @@ func (r Bungee) GetCost(ctx context.Context, args provider.SwapParams) (provider
costAmount decimal.Decimal
)
if _, ok := SupportedChain[args.TargetChain]; !ok {
return nil, error_types.ErrUnsupportedTokenAndChain
return nil, errors.Wrapf(error_types.ErrUnsupportedTokenAndChain, "target chain %s", args.TargetChain)
}

if args.SourceChain == "" || args.SourceToken == "" {
var quote gjson.Result
args.SourceToken, args.SourceChain, costAmount, quote, err = r.GetBestQuote(ctx, args)
if err != nil {
return nil, err
}
if len(quote.Get("result.routes").Array()) == 0 {
return nil, error_types.ErrUnsupportedTokenAndChain
}
var quote gjson.Result
args.SourceToken, args.SourceChain, costAmount, quote, err = r.GetBestQuote(ctx, args)
if err != nil {
return nil, errors.Wrap(err, "get best quote")
}
if len(quote.Get("result.routes").Array()) == 0 {
return nil, errors.Wrapf(error_types.ErrUnsupportedTokenAndChain, "%s -> %s not found any routes", args.SourceChain, args.TargetChain)
}

if args.SourceChain == "" || args.SourceToken == "" || costAmount.IsZero() {
return nil, error_types.ErrUnsupportedTokenAndChain
return nil, errors.Wrapf(error_types.ErrUnsupportedTokenAndChain, "%s -> %s tokenIn is 0", args.SourceChain, args.TargetChain)
}
return provider.TokenInCosts{
provider.TokenInCost{
Expand Down Expand Up @@ -86,7 +84,7 @@ func (r Bungee) CheckToken(ctx context.Context, tokenName, tokenInChainName, tok
ToTokenChainId: tokenOuChain.Id,
})
if len(quote.Get("result").Get("routes").Array()) == 0 {
return false, error_types.ErrUnsupportedTokenAndChain
return false, errors.Wrap(error_types.ErrUnsupportedTokenAndChain, "route not found")
}
return err == nil, err
}
Expand Down Expand Up @@ -136,7 +134,7 @@ func (r Bungee) Swap(ctx context.Context, args provider.SwapParams) (provider.Sw
}

if len(quotes.Get("result").Get("routes").Array()) == 0 {
return provider.SwapResult{}, error_types.ErrUnsupportedTokenAndChain
return provider.SwapResult{}, errors.Wrap(error_types.ErrUnsupportedTokenAndChain, "route not found")
}
quote = quotes.Get("result").Get("routes").Array()[0]
tokenOutAmountWei = decimal.RequireFromString(quote.Get("toAmount").String())
Expand Down
3 changes: 2 additions & 1 deletion utils/provider/bridge/bungee/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func (r Bungee) GetBestQuote(ctx context.Context, args provider.SwapParams) (tok
AmountWei: decimal.NewFromBigInt(chains.EthToWei(args.Amount, tokenOut.Decimals), 0),
FromTokenChainId: constant.GetChainId(args.TargetChain),
ToTokenChainId: chain.Id,
Sender: args.Sender.GetAddress(true),
Receiver: common.HexToAddress(args.Receiver),
})
if err != nil {
log.Debugf("#%d %s %s get quote error: %s", args.OrderId, msg, tokenIn.Name, err)
Expand Down Expand Up @@ -183,7 +185,6 @@ func (r Bungee) GetBestQuote(ctx context.Context, args provider.SwapParams) (tok
tokenInChainName = chainName
quote = quoteData
}

if args.SourceChain != "" && args.SourceToken != "" {
getQuote(args.SourceChain, args.SourceToken)
if tokenInChainName == "" || tokenInName == "" || tokenInAmount.IsZero() {
Expand Down
2 changes: 1 addition & 1 deletion utils/provider/bridge/helix_liquidity_claim/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c Claim) ListNeedWithdrawRecords(ctx context.Context, relayer common.Addre
}

func (c Claim) ListClaim(ctx context.Context, relayer, tokenAddress common.Address, toChain string) (map[string]map[string]*ClaimInfo, error) {
query := `{historyRecords(row: 100,bridges: ["lnv3"],needWithdrawLiquidity: true,relayer: "%s",toChains: ["%s"],recvTokenAddress: "%s") {total, records { id sender relayer sendTokenAddress lastRequestWithdraw sendAmount recvTokenAddress toChain fromChain }}}`
query := `{historyRecords(row: 100,bridges: ["lnv3"],needWithdrawLiquidity: true,relayer: "%s",fromChains: ["%s"],recvTokenAddress: "%s") {total, records { id sender relayer sendTokenAddress lastRequestWithdraw sendAmount recvTokenAddress toChain fromChain }}}`
query = fmt.Sprintf(query,
strings.ToLower(relayer.Hex()),
toChain,
Expand Down
8 changes: 3 additions & 5 deletions utils/provider/bridge/okx/okx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ func (o *OKX) GetCost(ctx context.Context, args provider.SwapParams) (provider.T
costAmount decimal.Decimal
)

if args.SourceChain == "" || args.SourceToken == "" {
args.SourceToken, args.SourceChain, costAmount, err = o.GetBestTokenInChain(ctx, args)
if err != nil {
return nil, err
}
args.SourceToken, args.SourceChain, costAmount, err = o.GetBestTokenInChain(ctx, args)
if err != nil {
return nil, err
}
if args.SourceChain == "" || args.SourceToken == "" || costAmount.IsZero() {
return nil, error_types.ErrUnsupportedTokenAndChain
Expand Down
8 changes: 3 additions & 5 deletions utils/provider/bridge/routernitro/routernitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ func (r Routernitro) GetCost(ctx context.Context, args provider.SwapParams) (pro
err error
costAmount decimal.Decimal
)
if args.SourceChain == "" || args.SourceToken == "" {
args.SourceToken, args.SourceChain, costAmount, _, err = r.GetBestQuote(ctx, args)
if err != nil {
return nil, err
}
args.SourceToken, args.SourceChain, costAmount, _, err = r.GetBestQuote(ctx, args)
if err != nil {
return nil, err
}
if args.SourceChain == "" || args.SourceToken == "" || costAmount.IsZero() {
return nil, error_types.ErrUnsupportedTokenAndChain
Expand Down

0 comments on commit f35a2a4

Please sign in to comment.