65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package polycat
|
|
|
|
import (
|
|
"git.lehouerou.net/laurent/evm"
|
|
"git.lehouerou.net/laurent/evm/polycat/contracts"
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/pkg/errors"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
type CatPair struct {
|
|
evm.Token
|
|
paircontract *contracts.CatPair
|
|
}
|
|
|
|
func NewCatPair(client evm.Client, address common.Address) (*CatPair, error) {
|
|
t, err := client.TokenService().TokenByAddress(address)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "init contract")
|
|
}
|
|
|
|
pc, err := contracts.NewCatPair(address, client)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "init pair contract")
|
|
}
|
|
|
|
return &CatPair{
|
|
Token: t,
|
|
paircontract: pc,
|
|
}, nil
|
|
}
|
|
|
|
func (p *CatPair) GetReserves() (decimal.Decimal, decimal.Decimal, error) {
|
|
res, err := p.paircontract.GetReserves(&bind.CallOpts{})
|
|
if err != nil {
|
|
return decimal.Zero, decimal.Zero, errors.Wrap(err, "calling pair contract")
|
|
}
|
|
return p.ValueFromBigInt(res.Reserve0), p.ValueFromBigInt(res.Reserve1), nil
|
|
}
|
|
|
|
func (p *CatPair) getToken0() (evm.Token, error) {
|
|
t0address, err := p.paircontract.Token0(&bind.CallOpts{})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "getting token0 address")
|
|
}
|
|
t0, err := p.Client().TokenService().TokenByAddress(t0address)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "getting token0 contract")
|
|
}
|
|
return t0, nil
|
|
}
|
|
|
|
func (p *CatPair) getToken1() (evm.Token, error) {
|
|
t1address, err := p.paircontract.Token0(&bind.CallOpts{})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "getting token1 address")
|
|
}
|
|
t1, err := p.Client().TokenService().TokenByAddress(t1address)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "getting token1 contract")
|
|
}
|
|
return t1, nil
|
|
}
|