github.com/ChainSafe/chainbridge-core@v1.4.2/chains/evm/calls/contracts/erc721/erc721.go (about)

     1  package erc721
     2  
     3  import (
     4  	"github.com/ChainSafe/chainbridge-core/chains/evm/calls"
     5  	"github.com/ChainSafe/chainbridge-core/chains/evm/calls/consts"
     6  	"github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts"
     7  	"math/big"
     8  	"strings"
     9  
    10  	"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
    11  	"github.com/ethereum/go-ethereum/accounts/abi"
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/rs/zerolog/log"
    14  )
    15  
    16  type ERC721Contract struct {
    17  	contracts.Contract
    18  }
    19  
    20  func NewErc721Contract(
    21  	client calls.ContractCallerDispatcher,
    22  	erc721ContractAddress common.Address,
    23  	t transactor.Transactor,
    24  ) *ERC721Contract {
    25  	a, _ := abi.JSON(strings.NewReader(consts.ERC721PresetMinterPauserABI))
    26  	b := common.FromHex(consts.ERC721PresetMinterPauserBin)
    27  	return &ERC721Contract{contracts.NewContract(erc721ContractAddress, a, b, client, t)}
    28  }
    29  
    30  func (c *ERC721Contract) AddMinter(
    31  	minter common.Address, opts transactor.TransactOptions,
    32  ) (*common.Hash, error) {
    33  	log.Debug().Msgf("Adding new minter %s", minter.String())
    34  	role, err := c.MinterRole()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return c.ExecuteTransaction("grantRole", opts, role, minter)
    39  }
    40  
    41  func (c *ERC721Contract) Approve(
    42  	tokenId *big.Int, recipient common.Address, opts transactor.TransactOptions,
    43  ) (*common.Hash, error) {
    44  	log.Debug().Msgf("Approving %s token for %s", tokenId.String(), recipient.String())
    45  	return c.ExecuteTransaction("approve", opts, recipient, tokenId)
    46  }
    47  
    48  func (c *ERC721Contract) Mint(
    49  	tokenId *big.Int, metadata string, destination common.Address, opts transactor.TransactOptions,
    50  ) (*common.Hash, error) {
    51  	log.Debug().Msgf("Minting tokens %s to %s", tokenId.String(), destination.String())
    52  	return c.ExecuteTransaction("mint", opts, destination, tokenId, metadata)
    53  }
    54  
    55  func (c *ERC721Contract) Owner(tokenId *big.Int) (*common.Address, error) {
    56  	log.Debug().Msgf("Getting owner of %s", tokenId.String())
    57  	res, err := c.CallContract("ownerOf", tokenId)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	ownerAddr := abi.ConvertType(res[0], new(common.Address)).(*common.Address)
    63  	return ownerAddr, nil
    64  }
    65  
    66  func (c *ERC721Contract) MinterRole() ([32]byte, error) {
    67  	res, err := c.CallContract("MINTER_ROLE")
    68  	if err != nil {
    69  		return [32]byte{}, err
    70  	}
    71  	out := *abi.ConvertType(res[0], new([32]byte)).(*[32]byte)
    72  	return out, nil
    73  }