68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package beefy
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.lehouerou.net/laurent/evm"
|
|
"git.lehouerou.net/laurent/evm/beefy/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"
|
|
)
|
|
|
|
type VaultV6 struct {
|
|
evm.Token
|
|
client evm.Client
|
|
contract *contracts.VaultV6
|
|
|
|
Underlying evm.Token
|
|
}
|
|
|
|
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")
|
|
}
|
|
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")
|
|
}
|
|
|
|
return &VaultV6{
|
|
client: client,
|
|
contract: contract,
|
|
Token: token,
|
|
Underlying: want,
|
|
}, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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...)
|
|
}
|