2021-11-08 18:20:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:07:42 +00:00
|
|
|
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)
|
2021-11-08 18:20:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "init token contract")
|
|
|
|
}
|
2021-11-09 05:07:42 +00:00
|
|
|
c, err := contracts.NewWBnb(address, client)
|
2021-11-08 18:20:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "init wbnb contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &WBNB{
|
|
|
|
Token: t,
|
|
|
|
client: client,
|
|
|
|
contract: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 04:11:05 +00:00
|
|
|
func (w *WBNB) UnwrapWBNB(ctx context.Context, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
|
2021-11-08 18:20:31 +00:00
|
|
|
return w.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
|
|
|
|
return w.contract.Withdraw(opts.TransactOpts, w.ValueToBigInt(amount))
|
2021-11-09 04:11:05 +00:00
|
|
|
}, opts...)
|
2021-11-08 18:20:31 +00:00
|
|
|
}
|