github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/executiontester/blockchain/array-of-strings.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  	"encoding/hex"
    10  	"math/big"
    11  )
    12  
    13  type (
    14  	// ArrayString interface for array-of-strings.sol
    15  	ArrayString interface {
    16  		Contract
    17  		GetString() (ret string, err error)
    18  	}
    19  
    20  	arrayString struct {
    21  		Contract
    22  	}
    23  )
    24  
    25  // NewArrayString creates a new ArrayString contract
    26  func NewArrayString(exp string) ArrayString {
    27  	return &arrayString{Contract: NewContract(exp)}
    28  }
    29  
    30  // GetString is calling function bar() constant returns(string)
    31  func (f *arrayString) GetString() (ret string, err error) {
    32  	rets, err := f.RunAsOwner().SetAddress(f.Address()).Read(ArrayStringBar, []byte(Producer))
    33  	if err != nil {
    34  		return
    35  	}
    36  	retBytes, err := hex.DecodeString(rets)
    37  	if err != nil {
    38  		return
    39  	}
    40  	len := len(retBytes) / 32
    41  	lenOfString := new(big.Int).SetBytes(retBytes[1*32 : (1+1)*32])
    42  
    43  	for i := 2; i < len; i++ {
    44  		b := retBytes[i*32 : (i+1)*32]
    45  		ret += string(b[:lenOfString.Uint64()])
    46  	}
    47  	return
    48  }