Skip to content

Commit

Permalink
Add AAVE available borrowing balance query.
Browse files Browse the repository at this point in the history
  • Loading branch information
perrornet committed Oct 10, 2024
1 parent edd562b commit ceebac8
Show file tree
Hide file tree
Showing 8 changed files with 4,634 additions and 19 deletions.
19 changes: 16 additions & 3 deletions internal/daemons/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"omni-balance/internal/daemons/market"
"omni-balance/internal/db"
"omni-balance/internal/models"
"omni-balance/utils/bot"
"omni-balance/utils/chains"
"omni-balance/utils/configs"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)

func Run(ctx context.Context, conf configs.Config) error {
Expand Down Expand Up @@ -95,12 +98,22 @@ func process(ctx context.Context, conf configs.Config, walletAddress, tokenName,
panic(fmt.Sprintf("%s botType not found", botType))
}
return func() ([]bot.Task, bot.ProcessType, error) {
// get token price
tokenPrices, err := models.FindTokenPrice(db.DB(), []string{tokenName})
if err != nil {
return nil, bot.Parallel, err
}
if len(tokenPrices) == 0 {
tokenPrices = make(map[string]decimal.Decimal)
}

tasks, processType, err := m.Check(ctx, bot.Params{
Conf: conf,
Info: bot.Config{
Wallet: conf.GetWallet(walletAddress),
TokenName: tokenName,
Chain: chainName,
Wallet: conf.GetWallet(walletAddress),
TokenName: tokenName,
Chain: chainName,
TokenPrice: tokenPrices[tokenName],
},
Client: client,
})
Expand Down
3 changes: 3 additions & 0 deletions internal/daemons/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func do(ctx context.Context, order models.Order, conf configs.Config) {
}
removeOrderError(order.ID)
order = models.GetOrder(ctx, db.DB(), order.ID)
if order.SourceChainName == "" || order.Tx == "" {
return
}
err = notice.Send(
provider.WithNotify(ctx, provider.WithNotifyParams{
TaskId: order.TaskId,
Expand Down
7 changes: 4 additions & 3 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ type Bot interface {
}

type Config struct {
Wallet wallets.Wallets
TokenName string `json:"token_name"`
Chain string `json:"chains"`
Wallet wallets.Wallets
TokenName string `json:"token_name"`
TokenPrice decimal.Decimal `json:"token_price"`
Chain string `json:"chains"`
}

type Task struct {
Expand Down
24 changes: 21 additions & 3 deletions utils/bot/helix_liquidity/aave_debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helix_liquidity

import (
"context"
"omni-balance/utils/bot/helix_liquidity/lendingpool"
"omni-balance/utils/chains"
"omni-balance/utils/constant"

Expand Down Expand Up @@ -350,7 +351,8 @@ var (
},
},
constant.ArbitrumSepolia: {
Chain: constant.ArbitrumSepolia,
Chain: constant.ArbitrumSepolia,
L2Pool: common.HexToAddress("0xBfC91D59fdAA134A4ED45f7B584cAf96D7792Eff"),
DebtTokens: map[string]debtTokens{
"USDC": {
Name: "USDC",
Expand Down Expand Up @@ -386,7 +388,7 @@ type Aave struct {
func (a Aave) BalanceOf(ctx context.Context, args DebtParams) (decimal.Decimal, error) {
conf, ok := aaveAddressBook[args.Chain]
if !ok || conf.Chain == "" || conf.DebtTokens[args.Token].Name == "" {
log.Warnf("chain %s not support", args.Chain)
log.Warnf("chain %s token %s not support", args.Chain, args.Token)
return decimal.Zero, nil
}
atokenBalance, err := chains.GetTokenBalance(ctx, args.Client, conf.DebtTokens[args.Token].AToken.Hex(),
Expand All @@ -400,7 +402,23 @@ func (a Aave) BalanceOf(ctx context.Context, args DebtParams) (decimal.Decimal,
if err != nil {
return decimal.Zero, errors.Wrap(err, "get vtoken balance error")
}
return atokenBalance.Sub(vtokenBalance), nil
if args.TokenPrice.InexactFloat64() <= 0 {
return atokenBalance.Sub(vtokenBalance), nil
}
pool, err := lendingpool.NewLendingpoolCaller(conf.L2Pool, args.Client)
if err != nil {
return decimal.Zero, errors.Wrap(err, "new lendingpool caller error")
}
user, err := pool.GetUserAccountData(nil, args.Address)
if err != nil {
return decimal.Zero, errors.Wrap(err, "get user account data error")
}
if user.AvailableBorrowsBase.Int64() <= 0 {
return atokenBalance.Sub(vtokenBalance), nil
}
AvailableBorrowsBalance := chains.WeiToEth(user.AvailableBorrowsBase, 8)
return AvailableBorrowsBalance.Div(args.TokenPrice), nil

}

func (a Aave) Name() string {
Expand Down
9 changes: 5 additions & 4 deletions utils/bot/helix_liquidity/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ type Debt interface {
}

type DebtParams struct {
Address common.Address
Token string
Client simulated.Client
Chain string
Address common.Address
Token string
TokenPrice decimal.Decimal
Client simulated.Client
Chain string
}

var (
Expand Down
9 changes: 5 additions & 4 deletions utils/bot/helix_liquidity/helix_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ func (b HelixLiquidity) Balance(ctx context.Context, args bot.Params) (decimal.D
}
for _, v := range debtImpl {
balance, err := v.BalanceOf(ctx, DebtParams{
Address: args.Info.Wallet.GetAddress(),
Chain: args.Info.Chain,
Client: args.Client,
Token: token.Name,
Address: args.Info.Wallet.GetAddress(),
Chain: args.Info.Chain,
Client: args.Client,
Token: token.Name,
TokenPrice: args.Info.TokenPrice,
})
if err != nil {
return decimal.Zero, errors.Wrapf(err, "get %s balance of %s error", token.Name, v.Name())
Expand Down
4,578 changes: 4,578 additions & 0 deletions utils/bot/helix_liquidity/lendingpool/lendingpool.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions utils/provider/bridge/li/li.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (l Li) Swap(ctx context.Context, args provider.SwapParams) (result provider
var (
targetChain = l.conf.GetChainConfig(args.TargetChain)
tokenOut = l.conf.GetTokenInfoOnChain(args.TargetToken, targetChain.Name)
tokenOutAmount = args.Amount
tokenOutAmountWei = decimal.NewFromBigInt(chains.EthToWei(tokenOutAmount, tokenOut.Decimals), 0)
tokenOutAmount decimal.Decimal
tokenOutAmountWei decimal.Decimal
tokenIn configs.Token
tokenInAmount decimal.Decimal
tokenInAmountWei decimal.Decimal
Expand Down

0 comments on commit ceebac8

Please sign in to comment.