{"version":3,"file":"common.mjs","names":["account.generateAuth"],"sources":["../../../src/predict/tx/common.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport { bcs } from '@mysten/sui/bcs';\nimport type { Transaction, TransactionArgument, TransactionResult } from '@mysten/sui/transactions';\nimport { deriveObjectID } from '@mysten/sui/utils';\nimport { toGeneratedConfig, type GeneratedConfig } from '../config/generated.js';\nimport type { PredictConfig } from '../config/index.js';\nimport { AccountContract, accountMoveCalls as account } from '../../account.js';\n\n// Predict's accounts ARE the shared on-chain account primitive (`packages/account`), so the\n// builders live in `@mysten/deepbook-v3/account` and this is the thin adapter that drives them with\n// Predict's deployed ids. The generated thunks are still used directly by `withAuth` below, which\n// wraps Predict's own entrypoints too — those take the same `Auth` hot potato.\nexport function accountContract(cfg: PredictConfig): AccountContract {\n\treturn new AccountContract({\n\t\taccountPackageId: cfg.packages.account,\n\t\taccountRegistry: cfg.objects.accountRegistry,\n\t\taccountPackageIdLatest: cfg.packages.accountLatest,\n\t});\n}\n\n/**\n * Owner authority is a hot-potato `Auth` minted from the tx sender (`ctx` is implicit in a PTB)\n * and consumed by the very next account-loading call (`load_account_mut` inside `deposit_funds` /\n * `withdraw_funds` / `mint` / …). It resolves to owner auth for whoever signs the transaction.\n * See `packages/account/sources/account.move`.\n */\nexport function generateAuth(cfg: PredictConfig): (tx: Transaction) => TransactionResult {\n\treturn account.generateAuth({ config: toGeneratedConfig(cfg) });\n}\n\n// The shape every generated binding shares: options in, one PTB command out.\ntype GeneratedCall<Options> = (options: Options) => (tx: Transaction) => TransactionResult;\n\n// The generated options of a call that consumes the hot-potato `Auth`. Codegen types\n// `arguments` as the named form OR a positional tuple; only the named form is used here.\ninterface AuthCallOptions {\n\targuments: { auth: TransactionArgument } | readonly unknown[];\n\tconfig?: object;\n\tpackage?: string;\n}\n\n// The named-`arguments` arm of such an options type (the tuple arm has no `auth` property).\ntype NamedArguments<Options extends AuthCallOptions> = Extract<\n\tOptions['arguments'],\n\t{ auth: TransactionArgument }\n>;\n\n/**\n * The options {@link withAuth} leaves to the caller: the generated ones, minus the `auth`\n * argument it supplies itself, with the projected config required (it is what mints the auth).\n * The config must also carry every key the wrapped call resolves, so a call into the order-flow\n * companion can't be handed a slice without its desk.\n */\nexport type WithAuthOptions<Options extends AuthCallOptions> = Omit<\n\tOptions,\n\t'arguments' | 'config'\n> & {\n\tconfig: GeneratedConfig & NonNullable<Options['config']>;\n\targuments: Omit<NamedArguments<Options>, 'auth'>;\n};\n\n/**\n * Every owner-authorized call in this SDK is the same two commands — mint the hot-potato `Auth`,\n * then make the generated call that consumes it. This lifts a generated binding into that pair,\n * so such a builder is declared rather than written: `withAuth(account.depositFunds)` takes\n * `deposit_funds`'s own options with the `auth` slot already filled.\n */\nexport function withAuth<Options extends AuthCallOptions>(\n\tcall: GeneratedCall<Options>,\n): (options: WithAuthOptions<Options>) => (tx: Transaction) => TransactionResult {\n\treturn (options) => (tx) => {\n\t\tconst auth = tx.add(account.generateAuth({ config: options.config }));\n\t\t// The one unchecked step: with `Options` still generic, TS cannot see that putting `auth`\n\t\t// back makes the arguments whole again. Callers get the fully checked type above.\n\t\treturn tx.add(\n\t\t\tcall({ ...options, arguments: { ...options.arguments, auth } } as unknown as Options),\n\t\t);\n\t};\n}\n\n// The derivation must agree with on-chain `derive_address`, so it is owned in one place:\n// `@mysten/deepbook-v3/account`. This projects Predict's config onto that contract.\nexport function deriveAccountWrapperIdFrom(\n\tconfig: Pick<GeneratedConfig, 'accountRegistry' | 'accountPackageId'>,\n\towner: string,\n): string {\n\t// The original ID: the key type's defining package, whatever account version runs.\n\treturn new AccountContract({\n\t\taccountPackageId: config.accountPackageId,\n\t\taccountRegistry: config.accountRegistry,\n\t}).deriveAccountWrapperId(owner);\n}\n\n/** The deterministic id of an owner's canonical account wrapper — no chain read needed. */\nexport function deriveAccountWrapperId(cfg: PredictConfig, owner: string): string {\n\treturn deriveAccountWrapperIdFrom(toGeneratedConfig(cfg), owner);\n}\n\n// `account_registry::AccountKey(owner)`: a one-field positional struct, so its BCS is the bare\n// 32-byte address.\nconst AccountKey = bcs.struct('AccountKey', { pos0: bcs.Address });\n\n/**\n * The owner's canonical ACCOUNT id (`Account.account_id`), a different derived object from the\n * wrapper: `derive_address(registry, AccountKey(owner))`. Queue records and events name accounts\n * by it, and `queue::waiting_orders` takes it.\n */\nexport function deriveAccountIdFrom(\n\tconfig: Pick<GeneratedConfig, 'accountRegistry' | 'accountPackageId'>,\n\towner: string,\n): string {\n\treturn deriveObjectID(\n\t\tconfig.accountRegistry,\n\t\t`${config.accountPackageId}::account_registry::AccountKey`,\n\t\tAccountKey.serialize({ pos0: owner }).toBytes(),\n\t);\n}\n"],"mappings":";;;;;;;AAaA,SAAgB,gBAAgB,KAAqC;AACpE,QAAO,IAAI,gBAAgB;EAC1B,kBAAkB,IAAI,SAAS;EAC/B,iBAAiB,IAAI,QAAQ;EAC7B,wBAAwB,IAAI,SAAS;EACrC,CAAC;;;;;;;;AASH,SAAgB,aAAa,KAA4D;AACxF,QAAOA,eAAqB,EAAE,QAAQ,kBAAkB,IAAI,EAAE,CAAC;;;;;;;;AAwChE,SAAgB,SACf,MACgF;AAChF,SAAQ,aAAa,OAAO;EAC3B,MAAM,OAAO,GAAG,IAAIA,eAAqB,EAAE,QAAQ,QAAQ,QAAQ,CAAC,CAAC;AAGrE,SAAO,GAAG,IACT,KAAK;GAAE,GAAG;GAAS,WAAW;IAAE,GAAG,QAAQ;IAAW;IAAM;GAAE,CAAuB,CACrF;;;AAMH,SAAgB,2BACf,QACA,OACS;AAET,QAAO,IAAI,gBAAgB;EAC1B,kBAAkB,OAAO;EACzB,iBAAiB,OAAO;EACxB,CAAC,CAAC,uBAAuB,MAAM;;;AAIjC,SAAgB,uBAAuB,KAAoB,OAAuB;AACjF,QAAO,2BAA2B,kBAAkB,IAAI,EAAE,MAAM;;AAKjE,MAAM,aAAa,IAAI,OAAO,cAAc,EAAE,MAAM,IAAI,SAAS,CAAC;;;;;;AAOlE,SAAgB,oBACf,QACA,OACS;AACT,QAAO,eACN,OAAO,iBACP,GAAG,OAAO,iBAAiB,iCAC3B,WAAW,UAAU,EAAE,MAAM,OAAO,CAAC,CAAC,SAAS,CAC/C"}