evm/aave/dataprovider.go

63 lines
2.1 KiB
Go

package aave
import (
"git.lehouerou.net/laurent/evm"
"git.lehouerou.net/laurent/evm/aave/contracts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type DataProvider struct {
client evm.Client
contract *contracts.DataProvider
}
func NewDataProvider(client evm.Client, address common.Address) (*DataProvider, error) {
contract, err := contracts.NewDataProvider(address, client)
if err != nil {
return nil, errors.Wrap(err, "init contract")
}
return &DataProvider{
client: client,
contract: contract,
}, nil
}
type UserReserveData struct {
CurrentATokenBalance decimal.Decimal
CurrentStableDebt decimal.Decimal
CurrentVariableDebt decimal.Decimal
PrincipalStableDebt decimal.Decimal
ScaledVariableDebt decimal.Decimal
StableBorrowRate decimal.Decimal
LiquidityRate decimal.Decimal
StableRateLastUpdated decimal.Decimal
UsageAsCollateralEnabled bool
}
func (d *DataProvider) UserReserveData(asset common.Address, address common.Address) (UserReserveData, error) {
token, err := d.client.TokenService().TokenByAddress(asset)
if err != nil {
return UserReserveData{}, errors.Wrap(err, "get token")
}
rd, err := d.contract.GetUserReserveData(&bind.CallOpts{}, asset, address)
if err != nil {
return UserReserveData{}, errors.Wrap(err, "calling contract")
}
return UserReserveData{
CurrentATokenBalance: token.ValueFromBigInt(rd.CurrentATokenBalance),
CurrentStableDebt: token.ValueFromBigInt(rd.CurrentStableDebt),
CurrentVariableDebt: token.ValueFromBigInt(rd.CurrentVariableDebt),
PrincipalStableDebt: token.ValueFromBigInt(rd.PrincipalStableDebt),
ScaledVariableDebt: token.ValueFromBigInt(rd.ScaledVariableDebt),
StableBorrowRate: token.ValueFromBigInt(rd.StableBorrowRate),
LiquidityRate: token.ValueFromBigInt(rd.LiquidityRate),
StableRateLastUpdated: token.ValueFromBigInt(rd.StableRateLastUpdated),
UsageAsCollateralEnabled: rd.UsageAsCollateralEnabled,
}, nil
}