evm/polygon/wmatic.go
2021-11-09 09:07:42 +04:00

45 lines
1.2 KiB
Go

package polygon
import (
"context"
"git.lehouerou.net/laurent/evm"
"git.lehouerou.net/laurent/evm/polygon/contracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type WMATIC struct {
evm.Token
client evm.Client
contract *contracts.Wmatic
}
func NewWrappedMaticService(client evm.Client) (*WMATIC, error) {
address, err := client.TokenService().TokenAddressBySymbol("WMATIC")
if err != nil {
return nil, errors.Wrap(err, "failed to get wmatic address")
}
t, err := evm.NewToken(client, address)
if err != nil {
return nil, errors.Wrap(err, "init token contract")
}
c, err := contracts.NewWmatic(address, client)
if err != nil {
return nil, errors.Wrap(err, "init wmatic contract")
}
return &WMATIC{
Token: t,
client: client,
contract: c,
}, nil
}
func (w *WMATIC) Unwrap(ctx context.Context, amount decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return w.client.Execute(ctx,
func(ctx context.Context, options *evm.TransactOpts) (*types.Transaction, error) {
return w.contract.Withdraw(options.TransactOpts, w.ValueToBigInt(amount))
}, opts...)
}