evm/bsc/wbnb.go

42 lines
1.0 KiB
Go
Raw Normal View History

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/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) (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))
})
}