HostFunction.fromXdr constructor

HostFunction.fromXdr(
  1. XdrHostFunction xdr
)

Implementation

factory HostFunction.fromXdr(XdrHostFunction xdr) {
  XdrHostFunctionType type = xdr.type;
  switch (type) {
    // Account effects
    case XdrHostFunctionType.HOST_FUNCTION_TYPE_UPLOAD_CONTRACT_WASM:
      if (xdr.wasm != null) {
        return UploadContractWasmHostFunction(xdr.wasm!.dataValue);
      }
      break;
    case XdrHostFunctionType.HOST_FUNCTION_TYPE_INVOKE_CONTRACT:
      if (xdr.invokeContract != null) {
        XdrInvokeContractArgs invokeArgs = xdr.invokeContract!;
        if (invokeArgs.contractAddress.contractId == null) {
          throw UnimplementedError();
        }
        String contractID =
            Util.bytesToHex(invokeArgs.contractAddress.contractId!.hash);
        String functionName = invokeArgs.functionName;
        List<XdrSCVal> funcArgs = invokeArgs.args;
        return InvokeContractHostFunction(contractID, functionName,
            arguments: funcArgs);
      }
      break;
    case XdrHostFunctionType.HOST_FUNCTION_TYPE_CREATE_CONTRACT:
      if (xdr.createContract != null) {
        if (xdr.createContract!.contractIDPreimage.type ==
            XdrContractIDPreimageType.CONTRACT_ID_PREIMAGE_FROM_ADDRESS) {
          if (xdr.createContract!.executable.type ==
                  XdrContractExecutableType.CONTRACT_EXECUTABLE_WASM &&
              xdr.createContract!.executable.wasmHash != null) {
            String wasmId = Util.bytesToHex(
                xdr.createContract!.executable.wasmHash!.hash);
            return CreateContractHostFunction(
                Address.fromXdr(
                    xdr.createContract!.contractIDPreimage.address!),
                wasmId,
                salt: xdr.createContract!.contractIDPreimage.salt!);
          } else if (xdr.createContract!.executable.type ==
              XdrContractExecutableType.CONTRACT_EXECUTABLE_TOKEN) {
            return DeploySACWithSourceAccountHostFunction(
                Address.fromXdr(
                    xdr.createContract!.contractIDPreimage.address!),
                salt: xdr.createContract!.contractIDPreimage.salt!);
          }
        } else if (xdr.createContract!.contractIDPreimage.type ==
                XdrContractIDPreimageType.CONTRACT_ID_PREIMAGE_FROM_ASSET &&
            xdr.createContract!.executable.type ==
                XdrContractExecutableType.CONTRACT_EXECUTABLE_TOKEN) {
          return DeploySACWithAssetHostFunction(Asset.fromXdr(
              xdr.createContract!.contractIDPreimage.fromAsset!));
        }
      }
      break;
  }
  throw UnimplementedError();
}