github.com/0chain/gosdk@v1.17.11/znft/storageerc721.go (about)

     1  package znft
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	storageerc721 "github.com/0chain/gosdk/znft/contracts/dstorageerc721/binding"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  const (
    14  	ContractStorageERC721Name       = "StorageERC721"
    15  	ContractStorageERC721FixedName  = "StorageERC721Fixed"
    16  	ContractStorageERC721PackName   = "StorageERC721Pack"
    17  	ContractStorageERC721RandomName = "StorageERC721Random"
    18  	Withdraw                        = "withdraw"
    19  	SetReceiver                     = "setReceiver"
    20  	SetRoyalty                      = "setRoyalty"
    21  	SetMintable                     = "setMintable"
    22  	SetAllocation                   = "setAllocation"
    23  	SetURI                          = "setURI"
    24  	TokenURIFallback                = "tokenURIFallback"
    25  	TokenURI                        = "tokenURI"
    26  	Price                           = "price"
    27  	Mint                            = "mint"
    28  	MintOwner                       = "mintOwner"
    29  	RoyaltyInfo                     = "royaltyInfo"
    30  )
    31  
    32  // Solidity Functions
    33  // - withdraw()
    34  // - setReceiver(address receiver_)
    35  // - setRoyalty(uint256 royalty_)
    36  // - setMintable(bool status_)
    37  // - setMax(uint256 max_)
    38  // - setAllocation(string calldata allocation_)
    39  // - setURI(string calldata uri_)
    40  // - setURIFallback(string calldata uri_)
    41  // - tokenURIFallback(uint256 tokenId)  returns (string memory)
    42  // - price() returns (uint256)
    43  // - mint(uint256 amount)
    44  // - mintOwner(uint256 amount)
    45  // - royaltyInfo(uint256 tokenId, uint256 salePrice) returns (address, uint256)
    46  // - freeze()
    47  // Fields:
    48  //    uint256 public total;
    49  //    uint256 public max;
    50  //    uint256 public batch;
    51  //    bool public mintable;
    52  //    string public allocation;
    53  //    string public uri;
    54  //    string public uriFallback;
    55  //    uint256 public royalty;
    56  //    address public receiver;
    57  
    58  type IStorageECR721 interface {
    59  	Withdraw() error
    60  	SetReceiver(receiver string) error
    61  	SetRoyalty(sum *big.Int) error
    62  	SetMintable(status bool) error
    63  	SetAllocation(allocation string) error
    64  	SetURI(uri string) error
    65  	SetURIFallback(uri string) error
    66  	TokenURIFallback(token *big.Int) (string, error)
    67  	Price() (*big.Int, error)
    68  	Mint(amount *big.Int) error
    69  	MintOwner(amount *big.Int) error
    70  	RoyaltyInfo(tokenId, salePrice *big.Int) (string, *big.Int, error)
    71  	Max() (*big.Int, error) // Fields
    72  	Total() (*big.Int, error)
    73  	Batch() (*big.Int, error)
    74  	Mintable() (bool, error)
    75  	Allocation() (string, error)
    76  	Uri() (string, error)
    77  	UriFallback() (string, error)
    78  	Royalty() (*big.Int, error)
    79  	Receiver() (string, error)
    80  }
    81  
    82  var (
    83  	_ IStorageECR721 = (*StorageECR721)(nil)
    84  )
    85  
    86  type StorageECR721 struct {
    87  	session *storageerc721.BindingSession
    88  	ctx     context.Context
    89  }
    90  
    91  func (s *StorageECR721) SetURIFallback(uri string) error {
    92  	evmTr, err := s.session.SetURIFallback(uri)
    93  	if err != nil {
    94  		err = errors.Wrapf(err, "failed to execute %s", "SetURIFallback")
    95  		Logger.Error(err)
    96  		return err
    97  	}
    98  
    99  	Logger.Info("Executed SetURIFallback, hash: ", evmTr.Hash().Hex())
   100  
   101  	return nil
   102  }
   103  
   104  func (s *StorageECR721) Total() (*big.Int, error) {
   105  	total, err := s.session.Total()
   106  	if err != nil {
   107  		err = errors.Wrapf(err, "failed to read %s", "Total")
   108  		Logger.Error(err)
   109  		return nil, err
   110  	}
   111  
   112  	return total, nil
   113  }
   114  
   115  func (s *StorageECR721) Batch() (*big.Int, error) {
   116  	batch, err := s.session.Batch()
   117  	if err != nil {
   118  		err = errors.Wrapf(err, "failed to read %s", "Batch")
   119  		Logger.Error(err)
   120  		return nil, err
   121  	}
   122  
   123  	return batch, nil
   124  }
   125  
   126  func (s *StorageECR721) Mintable() (bool, error) {
   127  	mintable, err := s.session.Mintable()
   128  	if err != nil {
   129  		err = errors.Wrapf(err, "failed to read %s", "Mintable")
   130  		Logger.Error(err)
   131  		return false, err
   132  	}
   133  
   134  	return mintable, nil
   135  }
   136  
   137  func (s *StorageECR721) Allocation() (string, error) {
   138  	allocation, err := s.session.Allocation()
   139  	if err != nil {
   140  		err = errors.Wrapf(err, "failed to read %s", "Allocation")
   141  		Logger.Error(err)
   142  		return "", err
   143  	}
   144  
   145  	return allocation, nil
   146  }
   147  
   148  func (s *StorageECR721) Uri() (string, error) {
   149  	uri, err := s.session.Uri()
   150  	if err != nil {
   151  		err = errors.Wrapf(err, "failed to read %s", "URI")
   152  		Logger.Error(err)
   153  		return "", err
   154  	}
   155  
   156  	return uri, nil
   157  }
   158  
   159  func (s *StorageECR721) UriFallback() (string, error) {
   160  	uri, err := s.session.UriFallback()
   161  	if err != nil {
   162  		err = errors.Wrapf(err, "failed to read %s", "URIFallback")
   163  		Logger.Error(err)
   164  		return "", err
   165  	}
   166  
   167  	return uri, nil
   168  }
   169  
   170  func (s *StorageECR721) Royalty() (*big.Int, error) {
   171  	value, err := s.session.Royalty()
   172  	if err != nil {
   173  		err = errors.Wrapf(err, "failed to read %s", "Royalty")
   174  		Logger.Error(err)
   175  		return nil, err
   176  	}
   177  
   178  	return value, nil
   179  }
   180  
   181  func (s *StorageECR721) Receiver() (string, error) {
   182  	value, err := s.session.Receiver()
   183  	if err != nil {
   184  		err = errors.Wrapf(err, "failed to read %s", "Receiver")
   185  		Logger.Error(err)
   186  		return "", err
   187  	}
   188  
   189  	return value.String(), nil
   190  }
   191  
   192  func (s *StorageECR721) Max() (*big.Int, error) {
   193  	max, err := s.session.Max()
   194  	if err != nil {
   195  		err = errors.Wrapf(err, "failed to read %s", "Max")
   196  		Logger.Error(err)
   197  		return nil, err
   198  	}
   199  
   200  	return max, nil
   201  }
   202  
   203  func (s *StorageECR721) Mint(amount *big.Int) error {
   204  	evmTr, err := s.session.Mint(amount)
   205  	if err != nil {
   206  		err = errors.Wrapf(err, "failed to execute %s", Mint)
   207  		Logger.Error(err)
   208  		return err
   209  	}
   210  
   211  	Logger.Info("Executed ", Mint, "hash: ", evmTr.Hash().Hex())
   212  
   213  	return nil
   214  }
   215  
   216  func (s *StorageECR721) RoyaltyInfo(tokenId, salePrice *big.Int) (string, *big.Int, error) {
   217  	address, sum, err := s.session.RoyaltyInfo(tokenId, salePrice)
   218  	if err != nil {
   219  		err = errors.Wrapf(err, "failed to read %s", RoyaltyInfo)
   220  		Logger.Error(err)
   221  		return "", nil, err
   222  	}
   223  
   224  	return address.Hex(), sum, nil
   225  }
   226  
   227  func (s *StorageECR721) MintOwner(amount *big.Int) error {
   228  	evmTr, err := s.session.MintOwner(amount)
   229  	if err != nil {
   230  		err = errors.Wrapf(err, "failed to execute %s", MintOwner)
   231  		Logger.Error(err)
   232  		return err
   233  	}
   234  
   235  	Logger.Info("Executed ", MintOwner, "hash: ", evmTr.Hash().Hex())
   236  
   237  	return nil
   238  }
   239  
   240  func (s *StorageECR721) TokenURIFallback(token *big.Int) (string, error) {
   241  	tokenURI, err := s.session.TokenURIFallback(token)
   242  	if err != nil {
   243  		err = errors.Wrapf(err, "failed to read %s", TokenURIFallback)
   244  		Logger.Error(err)
   245  		return "", err
   246  	}
   247  
   248  	return tokenURI, nil
   249  }
   250  
   251  // Price returns price
   252  func (s *StorageECR721) Price() (*big.Int, error) {
   253  	price, err := s.session.Price()
   254  	if err != nil {
   255  		err = errors.Wrapf(err, "failed to read %s", Price)
   256  		Logger.Error(err)
   257  		return nil, err
   258  	}
   259  
   260  	return price, nil
   261  }
   262  
   263  // SetURI updates uri
   264  func (s *StorageECR721) SetURI(uri string) error {
   265  	evmTr, err := s.session.SetURI(uri)
   266  	if err != nil {
   267  		err = errors.Wrapf(err, "failed to execute %s", SetURI)
   268  		Logger.Error(err)
   269  		return err
   270  	}
   271  
   272  	Logger.Info("Executed ", SetURI, "hash: ", evmTr.Hash().String())
   273  
   274  	return nil
   275  }
   276  
   277  // SetAllocation updates allocation
   278  func (s *StorageECR721) SetAllocation(allocation string) error {
   279  	evmTr, err := s.session.SetAllocation(allocation)
   280  	if err != nil {
   281  		err = errors.Wrapf(err, "failed to execute %s", SetAllocation)
   282  		Logger.Error(err)
   283  		return err
   284  	}
   285  
   286  	Logger.Info("Executed ", SetAllocation, " hash: ", evmTr.Hash())
   287  
   288  	return nil
   289  }
   290  
   291  // SetMintable updates mintable state
   292  func (s *StorageECR721) SetMintable(status bool) error {
   293  	evmTr, err := s.session.SetMintable(status)
   294  	if err != nil {
   295  		err = errors.Wrapf(err, "failed to execute %s", SetMintable)
   296  		Logger.Error(err)
   297  		return err
   298  	}
   299  
   300  	Logger.Info("Executed ", SetMintable, " hash: ", evmTr.Hash())
   301  
   302  	return nil
   303  }
   304  
   305  // SetRoyalty eth balance from token contract - setReceiver(address receiver_)
   306  func (s *StorageECR721) SetRoyalty(sum *big.Int) error {
   307  	evmTr, err := s.session.SetRoyalty(sum)
   308  	if err != nil {
   309  		err = errors.Wrapf(err, "failed to execute %s", SetRoyalty)
   310  		Logger.Error(err)
   311  		return err
   312  	}
   313  
   314  	Logger.Info("Executed ", SetRoyalty, "hash: ", evmTr.Hash())
   315  
   316  	return nil
   317  }
   318  
   319  // SetReceiver eth balance from token contract - setReceiver(address receiver_)
   320  func (s *StorageECR721) SetReceiver(receiver string) error {
   321  	address := common.HexToAddress(receiver)
   322  
   323  	evmTr, err := s.session.SetReceiver(address)
   324  	if err != nil {
   325  		err = errors.Wrapf(err, "failed to execute %s", SetReceiver)
   326  		Logger.Error(err)
   327  		return err
   328  	}
   329  
   330  	Logger.Info("Executed ", SetReceiver, "hash: ", evmTr.Hash())
   331  
   332  	return nil
   333  }
   334  
   335  // Withdraw eth balance from token contract - withdraw()
   336  func (s *StorageECR721) Withdraw() error {
   337  	evmTr, err := s.session.Withdraw()
   338  	if err != nil {
   339  		err = errors.Wrapf(err, "failed to execute %s", Withdraw)
   340  		Logger.Error(err)
   341  		return err
   342  	}
   343  
   344  	Logger.Info("Executed ", Withdraw, " hash: ", evmTr.Hash())
   345  
   346  	return nil
   347  }