serializeV1TransactionData function

TransactionDataV1 serializeV1TransactionData(
  1. TransactionData transactionData
)

Implementation

TransactionDataV1 serializeV1TransactionData(TransactionData transactionData) {
  final inputs = <Map<String, dynamic>>[];
  final len = transactionData.inputs?.length ?? 0;
  for (var index = 0; index < len; index++) {
    final input = transactionData.inputs![index];
    if (input["Object"] != null) {
      inputs.add({
        "kind": 'Input',
        "index": index,
        "value": {
          'Object': input["Object"]["ImmOrOwnedObject"] != null
                    ? {"ImmOrOwned": input["Object"]["ImmOrOwnedObject"]}
                    : input["Object"]["Receiving"] != null
                      ?
                        {
                          "Receiving": {
                            "digest": input["Object"]["Receiving"]["digest"],
                            "version": input["Object"]["Receiving"]["version"],
                            "objectId": input["Object"]["Receiving"]["objectId"]
                          }
                        }
                      : {
                          "Shared": {
                            "mutable": input["Object"]["SharedObject"]["mutable"],
                            "initialSharedVersion": input["Object"]["SharedObject"]["initialSharedVersion"],
                            "objectId": input["Object"]["SharedObject"]["objectId"]
                          }
                        }
        },
        "type": 'object'
      });
    } else if (input["Pure"] != null) {
      inputs.add({
        "kind": 'Input',
        "index": index,
        "value": {
          "Pure": fromB64(input["Pure"]["bytes"]),
        },
        "type": 'pure',
      });
    } else if (input["UnresolvedPure"] != null) {
      inputs.add({
        "kind": 'Input',
        "type": 'pure',
        "index": index,
        "value": input["UnresolvedPure"]["value"],
      });
    } else if (input["UnresolvedObject"] != null) {
      inputs.add({
        "kind": 'Input',
        "type": 'object',
        "index": index,
        "value": input["UnresolvedObject"]["objectId"],
      });
    } else {
      throw ArgumentError('Invalid input');
    }
  }

  return TransactionDataV1(
    version: 1,
    sender: transactionData.sender,
    expiration: transactionData.expiration,
    gasConfig: GasConfig(
      owner: transactionData.gasData.owner,
      budget: transactionData.gasData.budget,
      price: transactionData.gasData.price,
      payment: transactionData.gasData.payment,
    ),
    inputs: inputs,
    transactions: transactionData.commands?.map<Map<String, dynamic>>((command) {
      if (command["MakeMoveVec"] != null) {
				return {
					"kind": 'MakeMoveVec',
					"type":
						command["MakeMoveVec"]["type"] == null
							? { "None": true }
							: { "Some": TypeTagSerializer.parseFromStr(command["MakeMoveVec"]["type"]) },
					"objects": command["MakeMoveVec"]["elements"].map((arg) =>
						convertTransactionArgument(arg, inputs)
					),
				};
			} else if (command["MergeCoins"] != null) {
				return {
					"kind": 'MergeCoins',
					"destination": convertTransactionArgument(command["MergeCoins"]["destination"], inputs),
					"sources": command["MergeCoins"]["sources"].map((arg) => convertTransactionArgument(arg, inputs)),
				};
			} else if (command["MoveCall"] != null) {
				return {
					"kind": 'MoveCall',
					"target": "${command["MoveCall"]["package"]}::${command["MoveCall"]["module"]}::${command["MoveCall"]["function"]}",
					"typeArguments": command["MoveCall"]["typeArguments"],
					"arguments": command["MoveCall"]["arguments"].map((arg) =>
						convertTransactionArgument(arg, inputs),
					),
				};
			} else if (command["Publish"] != null) {
				return {
					"kind": 'Publish',
					"modules": command["Publish"]["modules"].map((mod) => fromB64(mod)),
					"dependencies": command["Publish"]["dependencies"],
				};
			} else if (command["SplitCoins"] != null) {
				return {
					"kind": 'SplitCoins',
					"coin": convertTransactionArgument(command["SplitCoins"]["coin"], inputs),
					"amounts": command["SplitCoins"]["amounts"].map((arg) => convertTransactionArgument(arg, inputs)),
				};
			} else if (command["TransferObjects"] != null) {
				return {
					"kind": 'TransferObjects',
					"objects": command["TransferObjects"]["objects"].map((arg) =>
						convertTransactionArgument(arg, inputs),
					),
					"address": convertTransactionArgument(command["TransferObjects"]["address"], inputs),
				};
			} else if (command["Upgrade"] != null) {
				return {
					"kind": 'Upgrade',
					"modules": command["Upgrade"]["modules"].map((mod) => fromB64(mod)),
					"dependencies": command["Upgrade"]["dependencies"],
					"packageId": command["Upgrade"]["package"],
					"ticket": convertTransactionArgument(command["Upgrade"]["ticket"], inputs),
				};
			} else {
        throw ArgumentError("Unknown transaction $command");
      }
    }).toList(),
  );
}