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

44 lines
1.3 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/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type TankChefService struct {
client *polygon.Client
contract *contracts.TankChef
}
func NewTankChefService(c *polygon.Client, address string) (*TankChefService, error) {
contract, err := contracts.NewTankChef(common.HexToAddress(address), c)
if err != nil {
return nil, errors.Wrap(err, "init contract")
}
return &TankChefService{
contract: contract,
client: c,
}, nil
}
func (s *TankChefService) Harvest(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.Withdraw(options.TransactOpts, big.NewInt(0))
}, opts...)
}
func (s *TankChefService) Deposit(ctx context.Context, 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, amount.Shift(18).BigInt())
}, opts...)
}