transactionDataFromV1 function

dynamic transactionDataFromV1(
  1. dynamic data
)

Implementation

dynamic transactionDataFromV1(dynamic data) {
  dynamic expiration;
  if (data["expiration"] != null) {
    if (data["expiration"]["Epoch"] != null) {
      expiration = { "Epoch": data["expiration"]["Epoch"] };
    } else {
      expiration = { "None": true };
    }
  }

	return {
		"version": 2,
		"sender": data["sender"],
		"expiration": expiration,
		"gasData": {
			"owner": data["gasConfig"]?["owner"],
			"budget": data["gasConfig"]?["budget"]?.toString(),
			"price": data["gasConfig"]?["price"]?.toString(),
			"payment":
				data["gasConfig"]?["payment"]?.map((ref) => ({
					"digest": ref["digest"],
					"objectId": ref["objectId"],
					"version": ref["version"]?.toString(),
				})),
		},
		"inputs": data["inputs"]?.map((input) {
			if (input["kind"] == 'Input') {
        if (input["value"] is Map && (input["value"]["Object"] != null || input["value"]["Pure"] != null)) {
          final value = input["value"];

					if (value["Object"] != null) {
						if (value["Object"]["ImmOrOwned"] != null) {
							return {
								"Object": {
									"ImmOrOwnedObject": {
										"objectId": value["Object"]["ImmOrOwned"]["objectId"],
										"version": value["Object"]["ImmOrOwned"]["version"]?.toString(),
										"digest": value["Object"]["ImmOrOwned"]["digest"],
									},
								},
							};
						}
						if (value["Object"]["Shared"] != null) {
							return {
								"Object": {
									"SharedObject": {
										"mutable": value["Object"]["Shared"]["mutable"],
										"initialSharedVersion": value["Object"]["Shared"]["initialSharedVersion"],
										"objectId": value["Object"]["Shared"]["objectId"],
									},
								},
							};
						}
						if (value["Object"]["Receiving"] != null) {
							return {
								"Object": {
									"Receiving": {
										"digest": value["Object"]["Receiving"]["digest"],
										"version": value["Object"]["Receiving"]["version"]?.toString(),
										"objectId": value["Object"]["Receiving"]["objectId"],
									},
								},
							};
						}

						throw ArgumentError('Invalid object input');
					}

					return {
						"Pure": {
              "bytes": toB64(Uint8List.fromList(value["Pure"].cast<int>())),
						},
					};
				}

				if (input["type"] == 'object') {
					return {
						"UnresolvedObject": {
							"objectId": input["value"]?.toString(),
						},
					};
				}

				return {
					"UnresolvedPure": {
						"value": input["value"],
					},
				};
			}

			throw ArgumentError('Invalid input');
		}),
		"commands": data["transactions"]?.map((transaction) {
			switch (transaction["kind"]) {
				case 'MakeMoveVec':
					return {
						"MakeMoveVec": {
							"type":
								transaction["type"]?["Some"] != null
									? TypeTagSerializer.tagToString(transaction["type"]["Some"])
									: null,
							"elements": transaction["objects"]?.map((arg) => parseV1TransactionArgument(arg)),
						},
					};
				case 'MergeCoins': {
					return {
						"MergeCoins": {
							"destination": parseV1TransactionArgument(transaction["destination"]),
							"sources": transaction["sources"]?.map((arg) => parseV1TransactionArgument(arg)),
						},
					};
				}
				case 'MoveCall': {
					final [pkg, mod, fn] = transaction["target"].split('::');
					return {
						"MoveCall": {
							"package": pkg,
							"module": mod,
							"function": fn,
							"typeArguments": transaction["typeArguments"],
							"arguments": transaction["arguments"]?.map((arg) => parseV1TransactionArgument(arg)),
						},
					};
				}
				case 'Publish': {
					return {
						"Publish": {
							"modules": transaction["modules"]?.map((mod) => toB64(mod)),
							"dependencies": transaction["dependencies"],
						},
					};
				}
				case 'SplitCoins': {
					return {
						"SplitCoins": {
							"coin": parseV1TransactionArgument(transaction["coin"]),
							"amounts": transaction["amounts"]?.map((arg) => parseV1TransactionArgument(arg)),
						},
					};
				}
				case 'TransferObjects': {
					return {
						"TransferObjects": {
							"objects": transaction["objects"]?.map((arg) => parseV1TransactionArgument(arg)),
							"address": parseV1TransactionArgument(transaction["address"]),
						},
					};
				}
				case 'Upgrade': {
					return {
						"Upgrade": {
							"modules": transaction["modules"]?.map((mod) => toB64(mod)),
							"dependencies": transaction["dependencies"],
							"package": transaction["packageId"],
							"ticket": parseV1TransactionArgument(transaction["ticket"]),
						},
					};
				}
			}

			throw Exception("Unknown transaction $transaction");
		}).toList(),
	};
}