evm/beefy/vaultv6.go

68 lines
2.0 KiB
Go
Raw Normal View History

2021-11-12 11:05:57 +00:00
package beefy
import (
2021-11-15 10:09:43 +00:00
"context"
2021-11-12 11:05:57 +00:00
"git.lehouerou.net/laurent/evm"
"git.lehouerou.net/laurent/evm/beefy/contracts"
2021-11-14 19:08:37 +00:00
"github.com/ethereum/go-ethereum/accounts/abi/bind"
2021-11-12 11:05:57 +00:00
"github.com/ethereum/go-ethereum/common"
2021-11-15 10:09:43 +00:00
"github.com/ethereum/go-ethereum/core/types"
2021-11-12 11:05:57 +00:00
"github.com/pkg/errors"
2021-11-14 19:08:37 +00:00
"github.com/shopspring/decimal"
2021-11-12 11:05:57 +00:00
)
type VaultV6 struct {
evm.Token
client evm.Client
contract *contracts.VaultV6
2021-11-15 10:09:43 +00:00
Underlying evm.Token
2021-11-12 11:05:57 +00:00
}
func NewVaultV6(client evm.Client, address common.Address) (*VaultV6, error) {
contract, err := contracts.NewVaultV6(address, client)
if err != nil {
return nil, errors.Wrap(err, "init vault contract")
}
token, err := client.TokenService().TokenByAddress(address)
if err != nil {
return nil, errors.Wrap(err, "init token contract")
}
2021-11-15 10:09:43 +00:00
wantaddress, err := contract.Want(&bind.CallOpts{})
if err != nil {
return nil, errors.Wrap(err, "getting underlying token address")
}
want, err := client.TokenService().TokenByAddress(wantaddress)
if err != nil {
return nil, errors.Wrap(err, "init underlying token contract")
}
2021-11-12 11:05:57 +00:00
return &VaultV6{
2021-11-15 10:09:43 +00:00
client: client,
contract: contract,
Token: token,
Underlying: want,
2021-11-12 11:05:57 +00:00
}, nil
}
2021-11-14 19:08:37 +00:00
func (v *VaultV6) PricePerFullShare() (decimal.Decimal, error) {
ppfs, err := v.contract.GetPricePerFullShare(&bind.CallOpts{})
if err != nil {
return decimal.Zero, errors.Wrap(err, "calling contract")
}
return v.ValueFromBigInt(ppfs), nil
}
2021-11-15 10:09:43 +00:00
func (v *VaultV6) Deposit(ctx context.Context, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return v.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
return v.contract.Deposit(opts.TransactOpts, v.Underlying.ValueToBigInt(amount))
}, opts...)
}
func (v *VaultV6) Withdraw(ctx context.Context, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return v.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
return v.contract.Withdraw(opts.TransactOpts, v.ValueToBigInt(amount))
}, opts...)
}