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

     1  package assetcontract
     2  
     3  import (
     4  	"math/rand"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/pkg/errors"
    10  
    11  	"github.com/iotexproject/iotex-core/config"
    12  	"github.com/iotexproject/iotex-core/tools/executiontester/blockchain"
    13  )
    14  
    15  const (
    16  	// ChainIP is the ip address of iotex api endpoint
    17  	chainIP = "localhost"
    18  )
    19  
    20  // ReturnedContract include all contract as return value
    21  type ReturnedContract struct {
    22  	FpToken          blockchain.FpToken
    23  	StbToken         blockchain.StableToken
    24  	Erc721Token      blockchain.Erc721Token
    25  	ArrDeletePassing blockchain.ArrayDeletePassing
    26  	ArrString        blockchain.ArrayString
    27  }
    28  
    29  // StartContracts deploys and starts fp token smart contract and stable token smart contract,erc721 token smart contract
    30  func StartContracts(cfg config.Config) (ret ReturnedContract, err error) {
    31  	endpoint := chainIP + ":" + strconv.Itoa(cfg.API.GRPCPort)
    32  
    33  	// deploy allowance sheet
    34  	allowance, err := deployContract(blockchain.AllowanceSheetBinary, endpoint)
    35  	if err != nil {
    36  		return
    37  	}
    38  	// deploy balance sheet
    39  	balance, err := deployContract(blockchain.BalanceSheetBinary, endpoint)
    40  	if err != nil {
    41  		return
    42  	}
    43  	// deploy registry
    44  	reg, err := deployContract(blockchain.RegistryBinary, endpoint)
    45  	if err != nil {
    46  		return
    47  	}
    48  	// deploy global pause
    49  	pause, err := deployContract(blockchain.GlobalPauseBinary, endpoint)
    50  	if err != nil {
    51  		return
    52  	}
    53  	// deploy stable token
    54  	stable, err := deployContract(blockchain.StableTokenBinary, endpoint)
    55  	if err != nil {
    56  		return
    57  	}
    58  
    59  	// create stable token
    60  	// TODO: query total supply and call stbToken.SetTotal()
    61  	ret.StbToken = blockchain.NewStableToken(endpoint).
    62  		SetAllowance(allowance).
    63  		SetBalance(balance).
    64  		SetRegistry(reg).
    65  		SetPause(pause).
    66  		SetStable(stable)
    67  	ret.StbToken.SetOwner(blockchain.Producer, blockchain.ProducerPrivKey)
    68  
    69  	// stable token set-up
    70  	if err = ret.StbToken.Start(); err != nil {
    71  		return
    72  	}
    73  
    74  	// deploy fp token
    75  	fpReg, err := deployContract(blockchain.FpRegistryBinary, endpoint)
    76  	if err != nil {
    77  		return
    78  	}
    79  	cdp, err := deployContract(blockchain.CdpManageBinary, endpoint)
    80  	if err != nil {
    81  		return
    82  	}
    83  	manage, err := deployContract(blockchain.ManageBinary, endpoint)
    84  	if err != nil {
    85  		return
    86  	}
    87  	proxy, err := deployContract(blockchain.ManageProxyBinary, endpoint)
    88  	if err != nil {
    89  		return
    90  	}
    91  	eap, err := deployContract(blockchain.EapStorageBinary, endpoint)
    92  	if err != nil {
    93  		return
    94  	}
    95  	riskLock, err := deployContract(blockchain.TokenRiskLockBinary, endpoint)
    96  	if err != nil {
    97  		return
    98  	}
    99  
   100  	// create fp token
   101  	ret.FpToken = blockchain.NewFpToken(endpoint).
   102  		SetManagement(manage).
   103  		SetManagementProxy(proxy).
   104  		SetEapStorage(eap).
   105  		SetRiskLock(riskLock).
   106  		SetRegistry(fpReg).
   107  		SetCdpManager(cdp).
   108  		SetStableToken(stable)
   109  	ret.FpToken.SetOwner(blockchain.Producer, blockchain.ProducerPrivKey)
   110  
   111  	// fp token set-up
   112  	if err = ret.FpToken.Start(); err != nil {
   113  		return
   114  	}
   115  
   116  	// erc721 token set-up
   117  	addr, err := deployContract(blockchain.Erc721Binary, endpoint)
   118  	if err != nil {
   119  		return
   120  	}
   121  	ret.Erc721Token = blockchain.NewErc721Token(endpoint)
   122  	ret.Erc721Token.SetAddress(addr)
   123  	ret.Erc721Token.SetOwner(blockchain.Producer, blockchain.ProducerPrivKey)
   124  
   125  	if err = ret.Erc721Token.Start(); err != nil {
   126  		return
   127  	}
   128  	// array-delete-passing.sol set-up
   129  	addr, err = deployContract(blockchain.ArrayDeletePassingBin, endpoint)
   130  	if err != nil {
   131  		return
   132  	}
   133  	ret.ArrDeletePassing = blockchain.NewArrayDelete(endpoint)
   134  	ret.ArrDeletePassing.SetAddress(addr)
   135  	ret.ArrDeletePassing.SetOwner(blockchain.Producer, blockchain.ProducerPrivKey)
   136  	if err = ret.ArrDeletePassing.Start(); err != nil {
   137  		return
   138  	}
   139  	// array-of-strings.sol set-up
   140  	addr, err = deployContract(blockchain.ArrayStringBin, endpoint)
   141  	if err != nil {
   142  		return
   143  	}
   144  	ret.ArrString = blockchain.NewArrayString(endpoint)
   145  	ret.ArrString.SetAddress(addr)
   146  	ret.ArrString.SetOwner(blockchain.Producer, blockchain.ProducerPrivKey)
   147  	err = ret.ArrString.Start()
   148  	return
   149  }
   150  
   151  // GenerateAssetID generates an asset ID
   152  func GenerateAssetID() string {
   153  	for {
   154  		id := strconv.Itoa(rand.Int())
   155  		if len(id) >= 8 {
   156  			// attach 8-digit YYYYMMDD at front
   157  			t := time.Now().Format(time.RFC3339)
   158  			t = strings.Replace(t, "-", "", -1)
   159  			return t[:8] + id[:8]
   160  		}
   161  	}
   162  }
   163  
   164  func deployContract(code, endpoint string, args ...[]byte) (string, error) {
   165  	// deploy the contract
   166  	contract := blockchain.NewContract(endpoint)
   167  	h, err := contract.
   168  		SetExecutor(blockchain.Producer).
   169  		SetPrvKey(blockchain.ProducerPrivKey).
   170  		Deploy(code, args...)
   171  	if err != nil {
   172  		return "", errors.Wrapf(err, "failed to deploy contract, txhash = %s", h)
   173  	}
   174  
   175  	receipt, err := contract.CheckCallResult(h)
   176  	if err != nil {
   177  		return h, errors.Wrapf(err, "check failed to deploy contract, txhash = %s", h)
   178  	}
   179  	return receipt.ContractAddress, nil
   180  }