github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/executiontester/blockchain/erc721_token.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package blockchain
     7  
     8  import (
     9  	"math/big"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/iotexproject/iotex-address/address"
    14  )
    15  
    16  type (
    17  	// Erc721Token erc721 token interface
    18  	Erc721Token interface {
    19  		Contract
    20  		CreateToken(string, string) (string, error)
    21  		Transfer(string, string, string, string, string) (string, error)
    22  	}
    23  
    24  	erc721Token struct {
    25  		Contract
    26  	}
    27  )
    28  
    29  // NewErc721Token creates a new Erc721Token
    30  func NewErc721Token(exp string) Erc721Token {
    31  	return &erc721Token{Contract: NewContract(exp)}
    32  }
    33  
    34  func (f *erc721Token) CreateToken(tokenid, creditor string) (string, error) {
    35  	TokenID, ok := new(big.Int).SetString(tokenid, 10)
    36  	if !ok {
    37  		return "", errors.Errorf("invalid tokenid = %s", tokenid)
    38  	}
    39  	addrCreditor, err := address.FromString(creditor)
    40  	if err != nil {
    41  		return "", errors.Errorf("invalid creditor address = %s", creditor)
    42  	}
    43  	owner := f.RunAsOwner()
    44  	h, err := owner.Call(CreateTo, addrCreditor.Bytes(), TokenID.Bytes())
    45  	if err != nil {
    46  		return h, errors.Wrapf(err, "call failed to create")
    47  	}
    48  
    49  	if _, err := f.CheckCallResult(h); err != nil {
    50  		return h, errors.Wrapf(err, "check failed to create")
    51  	}
    52  	return h, nil
    53  }
    54  
    55  func (f *erc721Token) Transfer(token, sender, prvkey, receiver string, tokenid string) (string, error) {
    56  	TokenID, ok := new(big.Int).SetString(tokenid, 10)
    57  	if !ok {
    58  		return "", errors.Errorf("invalid tokenid = %s", tokenid)
    59  	}
    60  	from, err := address.FromString(sender)
    61  	if err != nil {
    62  		return "", errors.Errorf("invalid account address = %s", sender)
    63  	}
    64  	addrReceiver, err := address.FromString(receiver)
    65  	if err != nil {
    66  		return "", errors.Errorf("invalid account address = %s", receiver)
    67  	}
    68  	// transfer to receiver
    69  	h, err := f.SetAddress(token).
    70  		SetExecutor(sender).
    71  		SetPrvKey(prvkey).
    72  		Call(TransferFrom, from.Bytes(), addrReceiver.Bytes(), TokenID.Bytes())
    73  	if err != nil {
    74  		return h, errors.Wrap(err, "call transfer failed")
    75  	}
    76  
    77  	if _, err := f.CheckCallResult(h); err != nil {
    78  		return h, errors.Wrap(err, "check transfer failed")
    79  	}
    80  	return h, nil
    81  }