Skip to content

Commit fd96158

Browse files
authored
Merge pull request #485 from icon-project/fix/sui-gas-issue
fix: pick smallest coin for execute params
2 parents 02e4b5c + 9ccc6a4 commit fd96158

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

relayer/chains/sui/tx.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/base64"
66
"encoding/hex"
77
"fmt"
8-
"math/big"
98
"strconv"
109
"strings"
1110
"time"
@@ -19,6 +18,7 @@ import (
1918
"github.com/icon-project/centralized-relay/relayer/events"
2019
relayertypes "github.com/icon-project/centralized-relay/relayer/types"
2120
"github.com/icon-project/centralized-relay/utils/hexstr"
21+
"github.com/icon-project/centralized-relay/utils/sorter"
2222
"go.uber.org/zap"
2323
)
2424

@@ -216,6 +216,24 @@ func (p *Provider) prepareTxMoveCall(msg *SuiMessage) (lib.Base64Data, error) {
216216
args = append(args, param.Val)
217217
}
218218

219+
coins, err := p.client.GetCoins(context.Background(), p.wallet.Address)
220+
if err != nil {
221+
return nil, err
222+
}
223+
224+
if len(coins) == 0 {
225+
return nil, fmt.Errorf("no sui coins found")
226+
}
227+
228+
sorter.Sort(coins, func(c1, c2 types.Coin) bool {
229+
return c1.Balance.Int64() > c2.Balance.Int64()
230+
})
231+
232+
coinAddress, err := move_types.NewAccountAddressHex(coins[0].CoinObjectId.String())
233+
if err != nil {
234+
return nil, fmt.Errorf("invalid coin address: %w", err)
235+
}
236+
219237
res, err := p.client.MoveCall(
220238
context.Background(),
221239
*accountAddress,
@@ -224,7 +242,7 @@ func (p *Provider) prepareTxMoveCall(msg *SuiMessage) (lib.Base64Data, error) {
224242
msg.Method,
225243
msg.TypeArgs,
226244
args,
227-
nil,
245+
coinAddress,
228246
types.NewSafeSuiBigInt(p.cfg.GasLimit),
229247
)
230248
if err != nil {
@@ -482,12 +500,16 @@ func (p *Provider) getExecuteParams(
482500
if err != nil {
483501
return nil, nil, err
484502
}
485-
_, coin, err := coins.PickSUICoinsWithGas(big.NewInt(int64(suiBaseFee)), p.cfg.GasLimit, types.PickBigger)
486-
if err != nil {
487-
return nil, nil, err
503+
if len(coins) == 0 {
504+
return nil, nil, fmt.Errorf("no sui coins found for execute params")
488505
}
506+
507+
sorter.Sort(coins, func(c1, c2 types.Coin) bool {
508+
return c1.Balance.Int64() < c2.Balance.Int64()
509+
})
510+
489511
callArgs = append(callArgs, SuiCallArg{
490-
Type: CallArgObject, Val: coin.CoinObjectId.String(),
512+
Type: CallArgObject, Val: coins[0].CoinObjectId.String(),
491513
})
492514
case "sn":
493515
snU128, err := bcs.NewUint128FromBigInt(message.Sn)

0 commit comments

Comments
 (0)