github.com/koko1123/flow-go-1@v0.29.6/fvm/utils/cadenceValues.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/onflow/cadence"
     5  	"github.com/onflow/cadence/runtime/common"
     6  
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  )
     9  
    10  func FlowAddressSliceToCadenceAddressSlice(addresses []flow.Address) []common.Address {
    11  	adds := make([]common.Address, 0, len(addresses))
    12  	for _, a := range addresses {
    13  		adds = append(adds, common.Address(a))
    14  	}
    15  	return adds
    16  }
    17  
    18  func AddressSliceToCadenceValue(addresses []common.Address) cadence.Value {
    19  	adds := make([]cadence.Value, 0, len(addresses))
    20  	for _, a := range addresses {
    21  		adds = append(adds, cadence.NewAddress(a))
    22  	}
    23  	return cadence.NewArray(adds)
    24  }
    25  
    26  func CadenceValueToAddressSlice(value cadence.Value) (addresses []common.Address, ok bool) {
    27  
    28  	// cast to array
    29  	v, ok := value.(cadence.Array)
    30  	if !ok {
    31  		return nil, false
    32  	}
    33  
    34  	// parse addresses
    35  	for _, value := range v.Values {
    36  		a, ok := value.(cadence.Address)
    37  		if !ok {
    38  			return nil, false
    39  		}
    40  		addresses = append(addresses, common.Address(a))
    41  	}
    42  	return addresses, true
    43  }
    44  
    45  // CadenceValueToWeights converts a cadence value to a map of weights used for metering
    46  func CadenceValueToWeights(value cadence.Value) (map[uint]uint64, bool) {
    47  	result := make(map[uint]uint64)
    48  
    49  	dict, ok := value.(cadence.Dictionary)
    50  	if !ok {
    51  		return nil, false
    52  	}
    53  
    54  	for _, p := range dict.Pairs {
    55  		key, ok := p.Key.(cadence.UInt64)
    56  		if !ok {
    57  			return nil, false
    58  		}
    59  
    60  		value, ok := p.Value.(cadence.UInt64)
    61  		if !ok {
    62  			return nil, false
    63  		}
    64  
    65  		result[uint(key.ToGoValue().(uint64))] = uint64(value)
    66  	}
    67  
    68  	return result, true
    69  }