evm/pancakeswap/nftmarket.go
2021-11-09 13:23:41 +04:00

81 lines
3.1 KiB
Go

package pancakeswap
import (
"context"
"math/big"
"git.lehouerou.net/laurent/evm/bsc"
"git.lehouerou.net/laurent/evm"
"git.lehouerou.net/laurent/evm/pancakeswap/contracts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
const (
MarketAddress = "0x17539cCa21C7933Df5c980172d22659B8C345C5A"
)
type NftMarket struct {
client *bsc.Client
contract *contracts.NftMarket
wbnb evm.Token
}
func NewNftMarket(client *bsc.Client) (*NftMarket, error) {
contract, err := contracts.NewNftMarket(common.HexToAddress(MarketAddress), client)
if err != nil {
return nil, errors.Wrap(err, "init contract")
}
wbnb, err := client.TokenService().TokenBySymbol("WBNB")
if err != nil {
return nil, errors.Wrap(err, "init wbnb contract")
}
return &NftMarket{
client: client,
contract: contract,
wbnb: wbnb,
}, nil
}
func (m *NftMarket) GetAskPriceForToken(collection common.Address, tokenid int) (decimal.Decimal, error) {
res, err := m.contract.ViewAsksByCollectionAndTokenIds(&bind.CallOpts{}, collection, []*big.Int{big.NewInt(int64(tokenid))})
if err != nil {
return decimal.Zero, errors.Wrap(err, "calling contract")
}
if len(res.AskInfo) == 0 {
return decimal.Zero, errors.New("no info found for this token")
}
return m.wbnb.ValueFromBigInt(res.AskInfo[0].Price), nil
}
func (m *NftMarket) BuyTokenWithBNB(ctx context.Context, collection common.Address, tokenid int, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return m.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
opts.SetGasLimit(1000000)
return m.contract.BuyTokenUsingBNB(opts.TransactOpts, collection, big.NewInt(int64(tokenid)))
}, opts...)
}
func (m *NftMarket) BuyTokenWithWBNB(ctx context.Context, collection common.Address, tokenid int, price decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return m.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
opts.SetGasLimit(1000000)
return m.contract.BuyTokenUsingWBNB(opts.TransactOpts, collection, big.NewInt(int64(tokenid)), m.wbnb.ValueToBigInt(price))
}, opts...)
}
func (m *NftMarket) CreateAskOrder(ctx context.Context, collection common.Address, tokenid int, price decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return m.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
return m.contract.CreateAskOrder(opts.TransactOpts, collection, big.NewInt(int64(tokenid)), m.wbnb.ValueToBigInt(price))
}, opts...)
}
func (m *NftMarket) ModifyAskOrder(ctx context.Context, collection common.Address, tokenid int, price decimal.Decimal, opts ...evm.ExecutionOption) (evm.Transaction, error) {
return m.client.Execute(ctx, func(ctx context.Context, opts *evm.TransactOpts) (*types.Transaction, error) {
return m.contract.ModifyAskOrder(opts.TransactOpts, collection, big.NewInt(int64(tokenid)), m.wbnb.ValueToBigInt(price))
}, opts...)
}