def AsParameterType(type: ContractParameterType, item: StackItem): """ Convert a StackItem to a ContractParameter object of a specified ContractParameterType Args: type (neo.SmartContract.ContractParameterType): The ContractParameterType to convert to item (neo.VM.InteropService.StackItem): The item to convert to a ContractParameter object Returns: """ if type == ContractParameterType.Integer: return ContractParameter(type, value=item.GetBigInteger()) elif type == ContractParameterType.Boolean: return ContractParameter(type, value=item.GetBoolean()) elif type == ContractParameterType.Array: output = [ ContractParameter.ToParameter(subitem) for subitem in item.GetArray() ] return ContractParameter(type, value=output) elif type == ContractParameterType.String: return ContractParameter(type, value=item.GetString()) elif type == ContractParameterType.InteropInterface: return ContractParameter(type, value=item.GetInterface()) # all other types return a byte array else: return ContractParameter(type, value=item.GetByteArray())
def ToParameter(item: StackItem): """ Convert a StackItem to a ContractParameter object Args: item (neo.VM.InteropService.StackItem) The item to convert to a ContractParameter object Returns: ContractParameter """ if isinstance(item, Array) or isinstance(item, Struct): items = item.GetArray() output = [ ContractParameter.ToParameter(subitem) for subitem in items ] return ContractParameter(type=ContractParameterType.Array, value=output) elif isinstance(item, Boolean): return ContractParameter(type=ContractParameterType.Boolean, value=item.GetBoolean()) elif isinstance(item, ByteArray): return ContractParameter(type=ContractParameterType.ByteArray, value=item.GetByteArray()) elif isinstance(item, Integer): return ContractParameter(type=ContractParameterType.Integer, value=str(item.GetBigInteger())) elif isinstance(item, InteropInterface): return ContractParameter( type=ContractParameterType.InteropInterface, value=item.GetInterface())