evm/polycat/masterchefv2.go
2021-11-09 09:45:18 +04:00

87 lines
3.0 KiB
Go

package polycat
import (
"context"
"math/big"
"git.lehouerou.net/laurent/evm/polygon"
"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/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
const (
Masterchefv2Address = "0x4ce9Ae2f5983e19AebF5b8Bae4460f2B9EcE811a"
)
type Masterchefv2Service struct {
client *polygon.Client
contract *contracts.MasterChefv2
}
func NewMasterchefv2Service(c *polygon.Client) (*Masterchefv2Service, error) {
contract, err := contracts.NewMasterChefv2(common.HexToAddress(Masterchefv2Address), c)
if err != nil {
return nil, errors.Wrap(err, "init contract")
}
return &Masterchefv2Service{
contract: contract,
client: c,
}, nil
}
func (s *Masterchefv2Service) PoolInfo(pid int) (*PoolInfo, error) {
pi, err := s.contract.PoolInfo(&bind.CallOpts{}, big.NewInt(int64(pid)))
if err != nil {
return nil, errors.Wrap(err, "gettting pool info")
}
ui, err := s.contract.UserInfo(&bind.CallOpts{}, big.NewInt(int64(pid)), s.client.PublicAddress())
if err != nil {
return nil, errors.Wrap(err, "getting user info")
}
pair, err := NewCatPair(s.client, pi.LpToken)
if err != nil {
return nil, errors.Wrap(err, "getting pair contract")
}
return &PoolInfo{
LpToken: pair,
Balance: pair.ValueFromBigInt(ui.Amount),
TotalDeposited: pair.ValueFromBigInt(pi.TotalDeposited),
}, nil
}
func (s *Masterchefv2Service) HarvestAll(ctx context.Context, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return s.client.Execute(ctx, func(ctx context.Context, options *evm.TransactOpts) (*types.Transaction, error) {
return s.contract.HarvestAll(options.TransactOpts)
}, opts...)
}
func (s *Masterchefv2Service) PendingRewards(pids []int) (decimal.Decimal, error) {
var tot decimal.Decimal
for _, pid := range pids {
pr, err := s.contract.PendingPaw(&bind.CallOpts{}, big.NewInt(int64(pid)), s.client.PublicAddress())
if err != nil {
return decimal.Zero, errors.Wrapf(err, "getting pending reward for pid %d", pid)
}
tot = tot.Add(decimal.NewFromBigInt(pr, -18))
}
return tot, nil
}
func (s *Masterchefv2Service) Deposit(ctx context.Context, pid int, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return s.client.Execute(ctx, func(ctx context.Context, options *evm.TransactOpts) (*types.Transaction, error) {
return s.contract.Deposit(options.TransactOpts, big.NewInt(int64(pid)), amount.Shift(18).BigInt(), false)
}, opts...)
}
func (s *Masterchefv2Service) Withdraw(ctx context.Context, pid int, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return s.client.Execute(ctx, func(ctx context.Context, options *evm.TransactOpts) (*types.Transaction, error) {
return s.contract.Withdraw(options.TransactOpts, big.NewInt(int64(pid)), amount.Shift(18).BigInt(), false)
}, opts...)
}