evm/bsc/wbnb.go
2021-11-09 08:11:05 +04:00

42 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/common"
"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(address string, client evm.Client) (*WBNB, error) {
t, err := evm.NewToken(client, common.HexToAddress(address))
if err != nil {
return nil, errors.Wrap(err, "init token contract")
}
c, err := contracts.NewWBnb(common.HexToAddress(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...)
}