Skip to content

Commit

Permalink
Merge branch 'v2/cosmos/stargate' of github.com:forbole/bdjuno into v…
Browse files Browse the repository at this point in the history
…2/cosmos/v0.44.x

� Conflicts:
�	cmd/fix/ibc-transfer/receive.go
  • Loading branch information
RiccardoM committed Jan 17, 2022
2 parents d6533b9 + aa4c278 commit 53e57db
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions database/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,35 @@ func (db *Db) SaveAccountBalances(balances []types.AccountBalance) error {
}

func (db *Db) saveUpToDateBalances(paramsNumber int, balances []types.AccountBalance) error {
stmt := `INSERT INTO account_balance (address, coins, height) VALUES `
var params []interface{}
var accounts []types.Account

balStmt := `INSERT INTO account_balance (address, coins, height) VALUES `
var balParams []interface{}

for i, bal := range balances {
accounts = append(accounts, types.NewAccount(bal.Address))

bi := i * paramsNumber
stmt += fmt.Sprintf("($%d, $%d, $%d),", bi+1, bi+2, bi+3)
balStmt += fmt.Sprintf("($%d, $%d, $%d),", bi+1, bi+2, bi+3)

coins := pq.Array(dbtypes.NewDbCoins(bal.Balance))
params = append(params, bal.Address, coins, bal.Height)
balParams = append(balParams, bal.Address, coins, bal.Height)
}

// Store the accounts
err := db.SaveAccounts(accounts)
if err != nil {
return err
}

stmt = stmt[:len(stmt)-1]
stmt += `
balStmt = balStmt[:len(balStmt)-1]
balStmt += `
ON CONFLICT (address) DO UPDATE
SET coins = excluded.coins,
height = excluded.height
WHERE account_balance.height <= excluded.height`

_, err := db.Sql.Exec(stmt, params...)
_, err = db.Sql.Exec(balStmt, balParams...)
if err != nil {
return fmt.Errorf("error while storing up-to-date balances: %s", err)
}
Expand Down

0 comments on commit 53e57db

Please sign in to comment.