@mysten/sui
Version:
Sui TypeScript API
1 lines • 6.21 kB
Source Map (JSON)
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/transactions/utils.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { is } from 'valibot';\n\nimport type { SuiMoveNormalizedType } from '../jsonRpc/index.js';\nimport { normalizeSuiAddress } from '../utils/sui-types.js';\nimport { ArgumentSchema } from './data/internal.js';\nimport type { Argument, CallArg, Command } from './data/internal.js';\n\nexport function extractMutableReference(\n\tnormalizedType: SuiMoveNormalizedType,\n): SuiMoveNormalizedType | undefined {\n\treturn typeof normalizedType === 'object' && 'MutableReference' in normalizedType\n\t\t? normalizedType.MutableReference\n\t\t: undefined;\n}\n\nexport function extractReference(\n\tnormalizedType: SuiMoveNormalizedType,\n): SuiMoveNormalizedType | undefined {\n\treturn typeof normalizedType === 'object' && 'Reference' in normalizedType\n\t\t? normalizedType.Reference\n\t\t: undefined;\n}\n\nexport function extractStructTag(\n\tnormalizedType: SuiMoveNormalizedType,\n): Extract<SuiMoveNormalizedType, { Struct: unknown }> | undefined {\n\tif (typeof normalizedType === 'object' && 'Struct' in normalizedType) {\n\t\treturn normalizedType;\n\t}\n\n\tconst ref = extractReference(normalizedType);\n\tconst mutRef = extractMutableReference(normalizedType);\n\n\tif (typeof ref === 'object' && 'Struct' in ref) {\n\t\treturn ref;\n\t}\n\n\tif (typeof mutRef === 'object' && 'Struct' in mutRef) {\n\t\treturn mutRef;\n\t}\n\treturn undefined;\n}\n\nexport function getIdFromCallArg(arg: string | CallArg) {\n\tif (typeof arg === 'string') {\n\t\treturn normalizeSuiAddress(arg);\n\t}\n\n\tif (arg.Object) {\n\t\tif (arg.Object.ImmOrOwnedObject) {\n\t\t\treturn normalizeSuiAddress(arg.Object.ImmOrOwnedObject.objectId);\n\t\t}\n\n\t\tif (arg.Object.Receiving) {\n\t\t\treturn normalizeSuiAddress(arg.Object.Receiving.objectId);\n\t\t}\n\n\t\treturn normalizeSuiAddress(arg.Object.SharedObject.objectId);\n\t}\n\n\tif (arg.UnresolvedObject) {\n\t\treturn normalizeSuiAddress(arg.UnresolvedObject.objectId);\n\t}\n\n\treturn undefined;\n}\n\nexport function isArgument(value: unknown): value is Argument {\n\treturn is(ArgumentSchema, value);\n}\n\nexport function remapCommandArguments(\n\tcommand: Command,\n\tinputMapping: Map<number, number>,\n\tcommandMapping: Map<number, number>,\n) {\n\tconst remapArg = (arg: Argument): Argument => {\n\t\tswitch (arg.$kind) {\n\t\t\tcase 'Input': {\n\t\t\t\tconst newInputIndex = inputMapping.get(arg.Input);\n\t\t\t\tif (newInputIndex === undefined) {\n\t\t\t\t\tthrow new Error(`Input ${arg.Input} not found in input mapping`);\n\t\t\t\t}\n\t\t\t\treturn { ...arg, Input: newInputIndex };\n\t\t\t}\n\t\t\tcase 'Result': {\n\t\t\t\tconst newCommandIndex = commandMapping.get(arg.Result);\n\t\t\t\tif (newCommandIndex !== undefined) {\n\t\t\t\t\treturn { ...arg, Result: newCommandIndex };\n\t\t\t\t}\n\t\t\t\treturn arg;\n\t\t\t}\n\t\t\tcase 'NestedResult': {\n\t\t\t\tconst newCommandIndex = commandMapping.get(arg.NestedResult[0]);\n\t\t\t\tif (newCommandIndex !== undefined) {\n\t\t\t\t\treturn { ...arg, NestedResult: [newCommandIndex, arg.NestedResult[1]] };\n\t\t\t\t}\n\t\t\t\treturn arg;\n\t\t\t}\n\t\t\tdefault:\n\t\t\t\treturn arg;\n\t\t}\n\t};\n\n\tswitch (command.$kind) {\n\t\tcase 'MoveCall':\n\t\t\tcommand.MoveCall.arguments = command.MoveCall.arguments.map(remapArg);\n\t\t\tbreak;\n\t\tcase 'TransferObjects':\n\t\t\tcommand.TransferObjects.objects = command.TransferObjects.objects.map(remapArg);\n\t\t\tcommand.TransferObjects.address = remapArg(command.TransferObjects.address);\n\t\t\tbreak;\n\t\tcase 'SplitCoins':\n\t\t\tcommand.SplitCoins.coin = remapArg(command.SplitCoins.coin);\n\t\t\tcommand.SplitCoins.amounts = command.SplitCoins.amounts.map(remapArg);\n\t\t\tbreak;\n\t\tcase 'MergeCoins':\n\t\t\tcommand.MergeCoins.destination = remapArg(command.MergeCoins.destination);\n\t\t\tcommand.MergeCoins.sources = command.MergeCoins.sources.map(remapArg);\n\t\t\tbreak;\n\t\tcase 'MakeMoveVec':\n\t\t\tcommand.MakeMoveVec.elements = command.MakeMoveVec.elements.map(remapArg);\n\t\t\tbreak;\n\t\tcase 'Upgrade':\n\t\t\tcommand.Upgrade.ticket = remapArg(command.Upgrade.ticket);\n\t\t\tbreak;\n\t\tcase '$Intent': {\n\t\t\tconst inputs = command.$Intent.inputs;\n\t\t\tcommand.$Intent.inputs = {};\n\n\t\t\tfor (const [key, value] of Object.entries(inputs)) {\n\t\t\t\tcommand.$Intent.inputs[key] = Array.isArray(value) ? value.map(remapArg) : remapArg(value);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase 'Publish':\n\t\t\tbreak;\n\t}\n}\n"],"mappings":";;;;;AA8CA,SAAgB,iBAAiB,KAAuB;AACvD,KAAI,OAAO,QAAQ,SAClB,QAAO,oBAAoB,IAAI;AAGhC,KAAI,IAAI,QAAQ;AACf,MAAI,IAAI,OAAO,iBACd,QAAO,oBAAoB,IAAI,OAAO,iBAAiB,SAAS;AAGjE,MAAI,IAAI,OAAO,UACd,QAAO,oBAAoB,IAAI,OAAO,UAAU,SAAS;AAG1D,SAAO,oBAAoB,IAAI,OAAO,aAAa,SAAS;;AAG7D,KAAI,IAAI,iBACP,QAAO,oBAAoB,IAAI,iBAAiB,SAAS;;AAM3D,SAAgB,WAAW,OAAmC;AAC7D,QAAO,GAAG,gBAAgB,MAAM;;AAGjC,SAAgB,sBACf,SACA,cACA,gBACC;CACD,MAAM,YAAY,QAA4B;AAC7C,UAAQ,IAAI,OAAZ;GACC,KAAK,SAAS;IACb,MAAM,gBAAgB,aAAa,IAAI,IAAI,MAAM;AACjD,QAAI,kBAAk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