Skip to content

Commit c951bde

Browse files
authored
Merge pull request #139 from initia-labs/fix/funding-bigint-amount
fix: funding account with bigint amount
2 parents 4eae014 + b8d8156 commit c951bde

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

models/minitia/constants.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
DefaultMinitiaWebsocket string = "ws://localhost:26657/websocket"
3737
DefaultMinitiaGRPC string = "http://localhost:9090"
3838
DefaultMinitiaJsonRPC string = "http://localhost:8545"
39-
DefaultMinitiaJsonRPCWS string = "http://localhost:8546"
39+
DefaultMinitiaJsonRPCWS string = "ws://localhost:8546"
4040

4141
DefaultRollupDenom string = "umin"
4242
DefaultMinievmDenom string = "GAS"

models/minitia/tx.go

+32-20
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"math/big"
78
"os"
89
"os/exec"
910
"path/filepath"
10-
"strconv"
1111
"time"
1212

1313
"github.com/initia-labs/weave/common"
@@ -350,7 +350,10 @@ const FundMinitiaAccountsWithoutBatchTxInterface = `
350350
}
351351
`
352352

353-
func (lsk *L1SystemKeys) calculateTotalWantedCoins(state *LaunchState) (l1Want int64, daWant int64, err error) {
353+
func (lsk *L1SystemKeys) calculateTotalWantedCoins(state *LaunchState) (l1Want *big.Int, daWant *big.Int, err error) {
354+
l1Want = new(big.Int)
355+
daWant = new(big.Int)
356+
354357
for _, acc := range []*types.GenesisAccount{
355358
lsk.BridgeExecutor,
356359
lsk.OutputSubmitter,
@@ -361,22 +364,23 @@ func (lsk *L1SystemKeys) calculateTotalWantedCoins(state *LaunchState) (l1Want i
361364
continue
362365
}
363366

364-
amount, err := strconv.ParseInt(acc.Coins, 10, 64)
365-
if err != nil {
366-
return 0, 0, fmt.Errorf("failed to parse coin amount %s: %v", acc.Coins, err)
367+
amount := new(big.Int)
368+
_, ok := amount.SetString(acc.Coins, 10)
369+
if !ok {
370+
return nil, nil, fmt.Errorf("failed to parse coin amount '%s'", acc.Coins)
367371
}
368372

369373
if acc == lsk.Challenger && state.batchSubmissionIsCelestia {
370-
daWant += amount
374+
daWant.Add(daWant, amount)
371375
} else {
372-
l1Want += amount
376+
l1Want.Add(l1Want, amount)
373377
}
374378
}
375379

376380
return l1Want, daWant, nil
377381
}
378382

379-
func queryChainBalance(binaryPath, rpc, address string) (map[string]int64, error) {
383+
func queryChainBalance(binaryPath, rpc, address string) (map[string]string, error) {
380384
queryCmd := exec.Command(binaryPath, "query", "bank", "balances", address,
381385
"--node", rpc, "--output", "json")
382386
balanceRes, err := queryCmd.CombinedOutput()
@@ -394,13 +398,9 @@ func queryChainBalance(binaryPath, rpc, address string) (map[string]int64, error
394398
return nil, fmt.Errorf("failed to unmarshal balance: %v", err)
395399
}
396400

397-
balanceMap := make(map[string]int64)
401+
balanceMap := make(map[string]string)
398402
for _, bal := range balance.Balances {
399-
amount, err := strconv.ParseInt(bal.Amount, 10, 64)
400-
if err != nil {
401-
return nil, fmt.Errorf("failed to parse balance amount: %v", err)
402-
}
403-
balanceMap[bal.Denom] = amount
403+
balanceMap[bal.Denom] = bal.Amount
404404
}
405405

406406
return balanceMap, nil
@@ -425,9 +425,14 @@ func (lsk *L1SystemKeys) VerifyGasStationBalances(state *LaunchState) error {
425425
}
426426

427427
// Verify L1 balance
428-
if l1Available := l1Balances[DefaultL1GasDenom]; l1Available < l1Want {
429-
return fmt.Errorf("%w: insufficient initia balance: have %d uinit, want %d uinit",
430-
ErrInsufficientBalance, l1Available, l1Want)
428+
l1AvailableBig := new(big.Int)
429+
if _, ok := l1AvailableBig.SetString(l1Balances[DefaultL1GasDenom], 10); !ok {
430+
return fmt.Errorf("failed to parse L1 available balance: %s", l1Balances[DefaultL1GasDenom])
431+
}
432+
433+
if l1AvailableBig.Cmp(l1Want) < 0 {
434+
return fmt.Errorf("%w: insufficient initia balance: have %s uinit, want %s uinit",
435+
ErrInsufficientBalance, l1AvailableBig.String(), l1Want.String())
431436
}
432437

433438
// Check Celestia balance if needed
@@ -440,7 +445,7 @@ func (lsk *L1SystemKeys) VerifyGasStationBalances(state *LaunchState) error {
440445
return nil
441446
}
442447

443-
func (lsk *L1SystemKeys) verifyCelestiaBalance(state *LaunchState, daWant int64) error {
448+
func (lsk *L1SystemKeys) verifyCelestiaBalance(state *LaunchState, daWant *big.Int) error {
444449
gasStationKey, err := config.GetGasStationKey()
445450
if err != nil {
446451
return fmt.Errorf("failed to get gas station key: %v", err)
@@ -464,8 +469,15 @@ func (lsk *L1SystemKeys) verifyCelestiaBalance(state *LaunchState, daWant int64)
464469
}
465470

466471
// Verify Celestia balance
467-
if daAvailable := celestiaBalances[DefaultCelestiaGasDenom]; daAvailable < daWant {
468-
return fmt.Errorf("%w: have %d utia, want %d utia", ErrInsufficientBalance, daAvailable, daWant)
472+
daAvailableBig := new(big.Int)
473+
if _, ok := daAvailableBig.SetString(celestiaBalances[DefaultCelestiaGasDenom], 10); !ok {
474+
return fmt.Errorf("failed to parse DA available balance: %s", celestiaBalances[DefaultCelestiaGasDenom])
475+
}
476+
477+
if daAvailableBig.Cmp(daWant) < 0 {
478+
return fmt.Errorf("insufficient DA balance. Required: %s%s, Available: %s%s",
479+
daWant.String(), DefaultCelestiaGasDenom,
480+
daAvailableBig.String(), DefaultCelestiaGasDenom)
469481
}
470482

471483
return nil

models/relayer/init.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"math/big"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -1122,6 +1123,7 @@ func (m *FundDefaultPresetConfirmationInput) Update(msg tea.Msg) (tea.Model, tea
11221123
if err != nil {
11231124
return m, m.HandlePanic(err)
11241125
}
1126+
11251127
// Check L1 balance if needed
11261128
if state.l1FundingAmount != "0" {
11271129
l1ActiveRpc, err := GetL1ActiveRpc(m.Ctx)
@@ -1133,28 +1135,27 @@ func (m *FundDefaultPresetConfirmationInput) Update(msg tea.Msg) (tea.Model, tea
11331135
return m, m.HandlePanic(err)
11341136
}
11351137

1136-
l1Required, err := strconv.ParseInt(state.l1FundingAmount, 10, 64)
1137-
if err != nil {
1138-
return m, m.HandlePanic(fmt.Errorf("invalid L1 funding amount: %w", err))
1138+
l1Required := new(big.Int)
1139+
if _, ok := l1Required.SetString(state.l1FundingAmount, 10); !ok {
1140+
return m, m.HandlePanic(fmt.Errorf("invalid L1 funding amount: %s", state.l1FundingAmount))
11391141
}
11401142

11411143
// Get the available balance for the default denom
11421144
l1GasDenom, err := GetL1GasDenom(m.Ctx)
11431145
if err != nil {
11441146
m.HandlePanic(err)
11451147
}
1146-
l1Available := int64(0)
1148+
l1Available := new(big.Int)
11471149
for _, coin := range *l1Balances {
11481150
if coin.Denom == l1GasDenom {
1149-
l1Available, err = strconv.ParseInt(coin.Amount, 10, 64)
1150-
if err != nil {
1151-
return m, m.HandlePanic(fmt.Errorf("invalid L1 funding amount: %w", err))
1151+
if _, ok := l1Available.SetString(coin.Amount, 10); !ok {
1152+
return m, m.HandlePanic(fmt.Errorf("invalid L1 balance amount: %s", coin.Amount))
11521153
}
11531154
}
11541155
}
1155-
if l1Available < l1Required {
1156-
m.err = fmt.Errorf("insufficient balance in gas station on L1. Required: %d%s, Available: %d%s",
1157-
l1Required, l1GasDenom, l1Available, l1GasDenom)
1156+
if l1Available.Cmp(l1Required) < 0 {
1157+
m.err = fmt.Errorf("insufficient balance in gas station on L1. Required: %s%s, Available: %s%s",
1158+
l1Required.String(), l1GasDenom, l1Available.String(), l1GasDenom)
11581159
return m, cmd
11591160
}
11601161
}
@@ -1170,9 +1171,9 @@ func (m *FundDefaultPresetConfirmationInput) Update(msg tea.Msg) (tea.Model, tea
11701171
return m, m.HandlePanic(err)
11711172
}
11721173

1173-
l2Required, err := strconv.ParseInt(state.l2FundingAmount, 10, 64)
1174-
if err != nil {
1175-
return m, m.HandlePanic(fmt.Errorf("invalid L2 funding amount: %w", err))
1174+
l2Required := new(big.Int)
1175+
if _, ok := l2Required.SetString(state.l2FundingAmount, 10); !ok {
1176+
return m, m.HandlePanic(fmt.Errorf("invalid L2 funding amount: %s", state.l2FundingAmount))
11761177
}
11771178

11781179
// Get the gas denom for L2
@@ -1182,20 +1183,19 @@ func (m *FundDefaultPresetConfirmationInput) Update(msg tea.Msg) (tea.Model, tea
11821183
}
11831184

11841185
// Find the balance for the required denom
1185-
l2Available := int64(0)
1186+
l2Available := new(big.Int)
11861187
for _, coin := range *l2Balances {
11871188
if coin.Denom == l2GasDenom {
1188-
l2Available, err = strconv.ParseInt(coin.Amount, 10, 64)
1189-
if err != nil {
1190-
return m, m.HandlePanic(fmt.Errorf("invalid L2 balance amount: %w", err))
1189+
if _, ok := l2Available.SetString(coin.Amount, 10); !ok {
1190+
return m, m.HandlePanic(fmt.Errorf("invalid L2 balance amount: %s", coin.Amount))
11911191
}
11921192
break
11931193
}
11941194
}
11951195

1196-
if l2Available < l2Required {
1197-
m.err = fmt.Errorf("insufficient balance in gas station on L2. Required: %d%s, Available: %d%s",
1198-
l2Required, l2GasDenom, l2Available, l2GasDenom)
1196+
if l2Available.Cmp(l2Required) < 0 {
1197+
m.err = fmt.Errorf("insufficient balance in gas station on L2. Required: %s%s, Available: %s%s",
1198+
l2Required.String(), l2GasDenom, l2Available.String(), l2GasDenom)
11991199
return m, cmd
12001200
}
12011201
}

0 commit comments

Comments
 (0)