evm/bsc/wbnb.go
2021-11-09 09:07:42 +04:00

45 lines
1.1 KiB
Go

package bsc
import (
"context"
"git.lehouerou.net/laurent/evm"
"git.lehouerou.net/laurent/evm/bsc/contracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type WBNB struct {
evm.Token
client evm.Client
contract *contracts.WBnb
}
func NewWBNB(client evm.Client) (*WBNB, error) {
address, err := client.TokenService().TokenAddressBySymbol("WBNB")
if err != nil {
return nil, errors.Wrap(err, "getting wbnb address")
}
t, err := evm.NewToken(client, address)
if err != nil {
return nil, errors.Wrap(err, "init token contract")
}
c, err := contracts.NewWBnb(address, client)
if err != nil {
return nil, errors.Wrap(err, "init wbnb contract")
}
return &WBNB{
Token: t,
client: client,
contract: c,
}, nil
}
func (w *WBNB) UnwrapWBNB(ctx context.Context, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return w.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
return w.contract.Withdraw(opts.TransactOpts, w.ValueToBigInt(amount))
}, opts...)
}