@mysten/sui 2.29.0 → 2.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/dist/bcs/bcs.d.mts +6 -6
  3. package/dist/bcs/bcs.d.mts.map +1 -1
  4. package/dist/bcs/bcs.mjs +5 -1
  5. package/dist/bcs/bcs.mjs.map +1 -1
  6. package/dist/bcs/index.d.mts +139 -47
  7. package/dist/bcs/index.d.mts.map +1 -1
  8. package/dist/bcs/types.d.mts.map +1 -1
  9. package/dist/client/core-resolver.d.mts.map +1 -1
  10. package/dist/client/core-resolver.mjs +4 -6
  11. package/dist/client/core-resolver.mjs.map +1 -1
  12. package/dist/client/transaction-resolver.mjs +35 -8
  13. package/dist/client/transaction-resolver.mjs.map +1 -1
  14. package/dist/cryptography/signature.d.mts +8 -8
  15. package/dist/graphql/generated/tada-env.d.mts +74 -0
  16. package/dist/grpc/proto/sui/rpc/v2/input.d.mts +21 -1
  17. package/dist/grpc/proto/sui/rpc/v2/input.d.mts.map +1 -1
  18. package/dist/grpc/proto/sui/rpc/v2/input.mjs +21 -0
  19. package/dist/grpc/proto/sui/rpc/v2/input.mjs.map +1 -1
  20. package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
  21. package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
  22. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
  23. package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
  24. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
  25. package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.client.d.mts +4 -4
  26. package/dist/jsonRpc/core.mjs +1 -1
  27. package/dist/jsonRpc/core.mjs.map +1 -1
  28. package/dist/transactions/Transaction.d.mts +48 -46
  29. package/dist/transactions/Transaction.d.mts.map +1 -1
  30. package/dist/transactions/Transaction.mjs +65 -24
  31. package/dist/transactions/Transaction.mjs.map +1 -1
  32. package/dist/transactions/data/internal.d.mts +12 -0
  33. package/dist/transactions/data/internal.d.mts.map +1 -1
  34. package/dist/transactions/data/internal.mjs +5 -1
  35. package/dist/transactions/data/internal.mjs.map +1 -1
  36. package/dist/transactions/data/v2.d.mts +5 -0
  37. package/dist/transactions/data/v2.d.mts.map +1 -1
  38. package/dist/transactions/index.d.mts +3 -2
  39. package/dist/transactions/intents/AllowanceBalance.mjs +147 -0
  40. package/dist/transactions/intents/AllowanceBalance.mjs.map +1 -0
  41. package/dist/transactions/intents/BalanceIntentNames.mjs +7 -0
  42. package/dist/transactions/intents/BalanceIntentNames.mjs.map +1 -0
  43. package/dist/transactions/intents/BalanceOptions.d.mts +27 -0
  44. package/dist/transactions/intents/BalanceOptions.d.mts.map +1 -0
  45. package/dist/transactions/intents/BalanceOptions.mjs +12 -0
  46. package/dist/transactions/intents/BalanceOptions.mjs.map +1 -0
  47. package/dist/transactions/intents/CoinWithBalance.d.mts.map +1 -1
  48. package/dist/transactions/intents/CoinWithBalance.mjs +35 -12
  49. package/dist/transactions/intents/CoinWithBalance.mjs.map +1 -1
  50. package/dist/transactions/intents/ResolveBalances.mjs +16 -0
  51. package/dist/transactions/intents/ResolveBalances.mjs.map +1 -0
  52. package/dist/transactions/resolution-utils.mjs +13 -0
  53. package/dist/transactions/resolution-utils.mjs.map +1 -0
  54. package/dist/transactions/resolve.d.mts +8 -0
  55. package/dist/transactions/resolve.d.mts.map +1 -1
  56. package/dist/transactions/resolve.mjs +7 -3
  57. package/dist/transactions/resolve.mjs.map +1 -1
  58. package/dist/version.mjs +1 -1
  59. package/dist/version.mjs.map +1 -1
  60. package/dist/zklogin/bcs.d.mts +14 -14
  61. package/docs/llms-index.md +1 -0
  62. package/docs/migrations/sui-2.0/json-rpc-migration.md +50 -1
  63. package/docs/transactions/allowances.md +176 -0
  64. package/docs/transactions/basics.md +19 -14
  65. package/docs/transactions/coins-and-balances.md +12 -9
  66. package/docs/transactions/offline.md +137 -88
  67. package/package.json +2 -2
  68. package/src/bcs/bcs.ts +4 -0
  69. package/src/bcs/types.ts +2 -1
  70. package/src/client/core-resolver.ts +8 -11
  71. package/src/client/transaction-resolver.ts +43 -9
  72. package/src/graphql/generated/schema.graphql +87 -42
  73. package/src/graphql/generated/tada-env.ts +84 -0
  74. package/src/grpc/proto/sui/rpc/v2/input.ts +22 -0
  75. package/src/jsonRpc/core.ts +3 -4
  76. package/src/transactions/Transaction.ts +71 -53
  77. package/src/transactions/data/internal.ts +4 -0
  78. package/src/transactions/index.ts +3 -0
  79. package/src/transactions/intents/AllowanceBalance.ts +204 -0
  80. package/src/transactions/intents/BalanceIntentNames.ts +5 -0
  81. package/src/transactions/intents/BalanceOptions.ts +45 -0
  82. package/src/transactions/intents/CoinWithBalance.ts +82 -32
  83. package/src/transactions/intents/ResolveBalances.ts +30 -0
  84. package/src/transactions/resolution-utils.ts +18 -0
  85. package/src/transactions/resolve.ts +35 -2
  86. package/src/version.ts +1 -1
@@ -2,69 +2,132 @@
2
2
 
3
3
  > Build transactions without a network connection
4
4
 
5
- Normally the SDK resolves object versions, estimates gas, and fills in other details by querying the
6
- network. For offline building of backend services, air-gapped signing, or pre-built transactions,
7
- you must provide this information yourself. See also the Sui documentation on
5
+ Normally `build()` queries the network to resolve object versions, resolve intents like `tx.coin()`,
6
+ fetch the gas price, estimate the gas budget, pick gas coins, and set an expiration when gas is paid
7
+ from address balance. To build without a client, provide that information yourself. See also the Sui
8
+ documentation on
8
9
  [offline signing](https://docs.sui.io/guides/developer/transactions/transaction-auth/offline-signing)
9
10
  for the protocol-level details.
10
11
 
11
- ## Transactions without owned object inputs
12
+ ## Building only the transaction kind
12
13
 
13
- When your transaction only uses shared objects, party objects, and/or address balance withdrawals,
14
- there are no owned object versions to look up. Use `tx.withdrawal()` directly instead of `tx.coin()`
15
- or `coinWithBalance` because they require a client to resolve coin objects at build time.
14
+ If the next step only needs the inputs and commands (for example, a sponsor or a backend that fills
15
+ in gas), build with `onlyTransactionKind`. Sender, gas data, and expiration are left out:
16
16
 
17
17
  ```typescript
18
18
 
19
19
  const tx = new Transaction();
20
-
21
- // Use tx.withdrawal() + coin::redeem_funds to withdraw from address balance.
22
- // No coin object lookups needed — fully offline.
23
- const [coin] = tx.moveCall({
24
- target: '0x2::coin::redeem_funds',
25
- typeArguments: ['0x2::sui::SUI'],
26
- arguments: [tx.withdrawal({ amount: 1_000_000_000 })],
20
+ tx.moveCall({
21
+ target: '0xPackage::module::function',
22
+ arguments: [tx.pure.u64(100)],
27
23
  });
28
- tx.transferObjects([coin], '0xRecipientAddress');
29
24
 
30
- // Shared/party objects only need objectId + initialSharedVersion (both stable)
25
+ const kindBytes = await tx.build({ onlyTransactionKind: true });
26
+ ```
27
+
28
+ Inputs and intents still need to be resolved. Use full object references, `tx.withdrawal()`, or
29
+ [`assumeSufficientAddressBalances`](#coin-and-balance-intents) so nothing needs a lookup.
30
+
31
+ ## Building full transaction bytes
32
+
33
+ A full offline build needs the sender and all gas data:
34
+
35
+ | Method | Description |
36
+ | ----------------- | --------------------------------------------------------------- |
37
+ | `setSender()` | The address executing the transaction |
38
+ | `setGasPrice()` | Reference gas price (query `getReferenceGasPrice()` beforehand) |
39
+ | `setGasBudget()` | Maximum gas to spend (in MIST). Estimating it requires a client |
40
+ | `setGasPayment()` | Coin object references, or `[]` to pay gas from address balance |
41
+ | `setGasOwner()` | Only for sponsored transactions. Defaults to the sender |
42
+
43
+ ### Paying gas from address balance
44
+
45
+ With `setGasPayment([])`, gas is paid from the gas owner's SUI address balance. Nothing ties the
46
+ transaction to a specific object version, so it also needs a `ValidDuring` expiration for replay
47
+ protection:
48
+
49
+ ```typescript
50
+
51
+ // Look these up before going offline
52
+ const referenceGasPrice = 1000n;
53
+ const currentEpoch = 100;
54
+ const chainIdentifier = 'Base58ChainIdentifier'; // from getChainIdentifier()
55
+
56
+ const tx = new Transaction();
57
+
58
+ // FundsWithdrawal inputs contain the amount and type, so no object lookup is needed
31
59
  tx.moveCall({
32
- target: '0xPackage::module::function',
33
- arguments: [
34
- tx.sharedObjectRef({
35
- objectId: '0xSharedObjectId',
36
- initialSharedVersion: '1',
37
- mutable: true,
38
- }),
39
- ],
60
+ target: '0x2::balance::send_funds',
61
+ typeArguments: ['0x2::sui::SUI'],
62
+ arguments: [tx.withdrawal({ amount: 1_000_000_000 }), tx.pure.address('0xRecipientAddress')],
40
63
  });
41
64
 
42
- // Required configuration for all offline builds
43
65
  tx.setSender('0xSenderAddress');
44
- tx.setGasPrice(1000); // query getReferenceGasPrice() beforehand, or use a known value
66
+ tx.setGasPrice(referenceGasPrice);
45
67
  tx.setGasBudget(50_000_000);
46
- tx.setGasPayment([]); // empty array = pay gas from address balance
68
+ tx.setGasPayment([]);
47
69
 
48
- // Expiration is required when there are no owned objects for gas or inputs
49
70
  tx.setExpiration({
50
71
  ValidDuring: {
51
- minEpoch: 100, // current epoch
52
- maxEpoch: 101, // current epoch + 1
72
+ minEpoch: currentEpoch,
73
+ maxEpoch: currentEpoch + 1,
53
74
  minTimestamp: null,
54
75
  maxTimestamp: null,
55
- chain: 'mainnet', // or 'testnet', 'devnet'
56
- nonce: 0,
76
+ chain: chainIdentifier,
77
+ // Must be unique for each transaction in the validity window, including across restarts
78
+ nonce: await nonceStore.next(),
57
79
  },
58
80
  });
59
81
 
60
- // Build without a client
61
82
  const bytes = await tx.build();
62
83
  ```
63
84
 
64
- This enables fully stateless construction. You only need the sender address, reference gas price,
65
- epoch, and chain identifier.
85
+ Two otherwise-identical transactions with the same nonce have the same digest, and the second one is
86
+ rejected as a duplicate. Use a counter that survives restarts, not a hard-coded value.
87
+
88
+ The address balance needs to cover the gas budget on top of any withdrawals. Don't add the budget to
89
+ the withdrawal amount.
90
+
91
+ `{ Epoch: n }` expiration does not provide replay protection. Use `ValidDuring`.
66
92
 
67
- ## Party objects
93
+ ### Paying gas with coin objects
94
+
95
+ Gas coins need an exact version and digest. Other SUI can still come from address balance:
96
+
97
+ ```typescript
98
+
99
+ const tx = new Transaction();
100
+ tx.moveCall({
101
+ target: '0x2::balance::send_funds',
102
+ typeArguments: ['0x2::sui::SUI'],
103
+ arguments: [
104
+ tx.balance({ balance: 1_000_000, useGasCoin: false }),
105
+ tx.pure.address('0xRecipientAddress'),
106
+ ],
107
+ });
108
+
109
+ tx.setSender('0xSenderAddress');
110
+ tx.setGasPrice(1000);
111
+ tx.setGasBudget(50_000_000);
112
+ tx.setGasPayment([{ objectId: '0xGasCoinId', version: '3', digest: 'Base58GasCoinDigest' }]);
113
+
114
+ // assumeSufficientAddressBalances resolves tx.balance() without a client (see below)
115
+ const bytes = await tx.build({ assumeSufficientAddressBalances: true });
116
+ ```
117
+
118
+ ## Object inputs
119
+
120
+ ### Shared and party objects
121
+
122
+ Shared objects only need `objectId` and `initialSharedVersion`, both of which are stable:
123
+
124
+ ```typescript
125
+ tx.sharedObjectRef({
126
+ objectId: '0xSharedObjectId',
127
+ initialSharedVersion: '1',
128
+ mutable: true,
129
+ });
130
+ ```
68
131
 
69
132
  Party objects are address-owned but consensus-versioned, with per-address permissions. They are
70
133
  referenced the same way as shared objects:
@@ -83,12 +146,11 @@ Key properties for offline building:
83
146
  becomes a party object
84
147
  - **Enable pipelining**: Submit multiple transactions on the same party object without waiting for
85
148
  each one to finalize
86
- - **Cannot be used for gas**: Use address balance for gas payment (`setGasPayment([])`)
149
+ - **Cannot be used for gas**: Pay gas from address balance or with a SUI coin object
87
150
 
88
- ## Transactions with owned object inputs
151
+ ### Owned and immutable objects
89
152
 
90
- When your transaction uses owned or immutable objects, you must provide the exact version and digest
91
- for each one:
153
+ Owned and immutable objects need the exact version and digest:
92
154
 
93
155
  ```typescript
94
156
 
@@ -122,71 +184,58 @@ tx.moveCall({
122
184
  }),
123
185
  ],
124
186
  });
125
-
126
- // Gas payment with specific coin objects
127
- tx.setGasPayment([{ objectId: '0xGasCoinId', version: '3', digest: 'jkl012...' }]);
128
-
129
- tx.setSender('0xSenderAddress');
130
- tx.setGasPrice(1000);
131
- tx.setGasBudget(50_000_000);
132
-
133
- const bytes = await tx.build();
134
187
  ```
135
188
 
136
- ## Required configuration for all offline builds
189
+ ## Coin and balance intents
137
190
 
138
- Every offline-built transaction must have the following:
191
+ `tx.coin()` and `tx.balance()` normally look up the sender's address balance and coin objects. Pass
192
+ `assumeSufficientAddressBalances` to skip the lookups and withdraw from address balance instead:
139
193
 
140
- | Method | Description |
141
- | ----------------- | --------------------------------------------------------------- |
142
- | `setSender()` | The address executing the transaction |
143
- | `setGasPrice()` | Reference gas price (query `getReferenceGasPrice()` beforehand) |
144
- | `setGasBudget()` | Maximum gas to spend (in MIST) |
145
- | `setGasPayment()` | Coin object references, or `[]` for address balance |
194
+ ```typescript
146
195
 
147
- ### Expiration
196
+ const tx = new Transaction();
197
+ tx.moveCall({
198
+ target: '0x2::balance::send_funds',
199
+ typeArguments: ['0xPackage::module::TOKEN'],
200
+ arguments: [
201
+ tx.balance({ type: '0xPackage::module::TOKEN', balance: 1_000_000 }),
202
+ tx.pure.address('0xRecipientAddress'),
203
+ ],
204
+ });
148
205
 
149
- Set an expiration when your transaction uses no owned objects for gas or inputs. This applies when
150
- you use address balances for gas (`setGasPayment([])`) with only shared and party object inputs:
206
+ // The sender is still required, since the withdrawal comes from its address balance
207
+ tx.setSender('0xSenderAddress');
151
208
 
152
- ```typescript
153
- tx.setExpiration({
154
- ValidDuring: {
155
- minEpoch: 100, // current epoch
156
- maxEpoch: 101, // typically current epoch + 1
157
- minTimestamp: null,
158
- maxTimestamp: null,
159
- chain: 'mainnet',
160
- nonce: 0, // increment for multiple transactions in the same epoch
161
- },
209
+ const kindBytes = await tx.build({
210
+ onlyTransactionKind: true,
211
+ assumeSufficientAddressBalances: true,
162
212
  });
163
213
  ```
164
214
 
165
- You can also use epoch-based expiration:
215
+ > **Warning:** Nothing is checked. The transaction builds, then fails at execution if the address balance doesn't
216
+ > cover it.
166
217
 
167
- ```typescript
168
- tx.setExpiration({ Epoch: 100 });
169
- ```
218
+ SUI intents withdraw from address balance too, whether or not the transaction also uses `tx.gas`.
219
+ This matches an online build when the address balance is sufficient. Mixing default and
220
+ `useGasCoin: false` SUI intents in one transaction is still an error.
170
221
 
171
- > **Note:** When building with a client, the SDK sets expiration automatically. You only need the manual
172
- > configuration above for fully offline builds.
222
+ On a full build, the option also sets an unset gas payment to `[]`, but only when nothing else needs
223
+ a client, the transaction doesn't use `tx.gas`, and a `ValidDuring` or `Validity` expiration is
224
+ already set. Sender, gas price, and gas budget still need to be provided. Once set, the empty
225
+ payment is part of the transaction, the same as calling `setGasPayment([])`.
173
226
 
174
227
  ## Serialization
175
228
 
176
- ### Building to bytes
229
+ `toJSON()` resolves async thunks and intents but does not fill in gas or object versions. Pass
230
+ `supportedIntents` to keep an intent for another system to resolve:
177
231
 
178
232
  ```typescript
179
- // Build to BCS bytes (Uint8Array) fully offline, all data must be provided
180
- const bytes = await tx.build();
181
-
182
- // Build with a client — only makes network requests when there is unresolved data to look up
183
- const bytes = await tx.build({ client: grpcClient });
184
- ```
233
+ // Intents are resolved, so tx.coin() needs a client or assumeSufficientAddressBalances
234
+ const json = await tx.toJSON({ assumeSufficientAddressBalances: true });
235
+ const restored = Transaction.from(json);
185
236
 
186
- ### Converting bytes back to a Transaction
187
-
188
- ```typescript
189
- const tx = Transaction.from(bytes);
237
+ // Or keep tx.coin() intents for the receiver to resolve
238
+ const jsonWithIntents = await tx.toJSON({ supportedIntents: ['CoinWithBalance'] });
190
239
  ```
191
240
 
192
- This works with BCS byte arrays, base64-encoded strings, and JSON strings (from `toJSON()`).
241
+ `Transaction.from()` accepts JSON strings, BCS bytes, and base64-encoded BCS.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Mysten Labs <build@mystenlabs.com>",
4
4
  "description": "Sui TypeScript API",
5
5
  "homepage": "https://sdk.mystenlabs.com",
6
- "version": "2.29.0",
6
+ "version": "2.31.0",
7
7
  "license": "Apache-2.0",
8
8
  "sideEffects": false,
9
9
  "files": [
@@ -151,7 +151,7 @@
151
151
  "typescript": "^7.0.2",
152
152
  "vite": "^8.2.1",
153
153
  "vite-tsconfig-paths": "^6.0.4",
154
- "vitest": "^4.1.10",
154
+ "vitest": "^4.1.11",
155
155
  "wait-on": "^9.1.0"
156
156
  },
157
157
  "dependencies": {
package/src/bcs/bcs.ts CHANGED
@@ -96,6 +96,10 @@ export const WithdrawalType = bcs.enum('WithdrawalType', {
96
96
  export const WithdrawFrom = bcs.enum('WithdrawFrom', {
97
97
  Sender: null,
98
98
  Sponsor: null,
99
+ SenderAllowance: bcs.struct('SenderAllowance', {
100
+ funder: Address,
101
+ allowance: Address,
102
+ }),
99
103
  });
100
104
 
101
105
  // Rust: crates/sui-types/src/transaction.rs
package/src/bcs/types.ts CHANGED
@@ -44,7 +44,8 @@ export function isPureArg(arg: any): arg is PureArg {
44
44
 
45
45
  type Reservation = { MaxAmountU64: number | string };
46
46
  type WithdrawalTypeArg = { Balance: TypeTag };
47
- type WithdrawFrom = { Sender: null } | { Sponsor: null };
47
+ type WithdrawFrom =
48
+ { Sender: null } | { Sponsor: null } | { SenderAllowance: { funder: string; allowance: string } };
48
49
  type FundsWithdrawalArg = {
49
50
  reservation: Reservation;
50
51
  typeArg: WithdrawalTypeArg;
@@ -17,6 +17,7 @@ import { getPureBcsSchema, isTxContext } from '../transactions/serializer.js';
17
17
  import type { TransactionDataBuilder } from '../transactions/TransactionData.js';
18
18
  import { chunk } from '@mysten/utils';
19
19
  import type { BuildTransactionOptions } from '../transactions/index.js';
20
+ import { transactionUsesGasCoin } from '../transactions/resolution-utils.js';
20
21
 
21
22
  // The maximum objects that can be fetched at once using multiGetObjects.
22
23
  const MAX_OBJECTS_PER_FETCH = 50;
@@ -54,27 +55,23 @@ export async function coreClientResolveTransactionPlugin(
54
55
  next: () => Promise<void>,
55
56
  ) {
56
57
  const client = getClient(options);
57
-
58
58
  const needsGasPrice = !options.onlyTransactionKind && !transactionData.gasData.price;
59
59
  const needsPayment = !options.onlyTransactionKind && !transactionData.gasData.payment;
60
60
  const gasPayer = transactionData.gasData.owner ?? transactionData.sender;
61
61
 
62
- let usesGasCoin = false;
62
+ const usesGasCoin = transactionUsesGasCoin(transactionData);
63
63
  let withdrawals = 0n;
64
64
 
65
- transactionData.mapArguments((arg) => {
66
- if (arg.$kind === 'GasCoin') usesGasCoin = true;
67
- return arg;
68
- });
69
-
70
65
  const normalizedGasPayer = gasPayer ? normalizeSuiAddress(gasPayer) : null;
71
66
  for (const input of transactionData.inputs) {
72
67
  if (input.$kind !== 'FundsWithdrawal' || !normalizedGasPayer) continue;
73
68
  if (normalizeStructTag(input.FundsWithdrawal.typeArg.Balance) !== SUI_TYPE_ARG) continue;
74
-
75
- const withdrawalOwner = input.FundsWithdrawal.withdrawFrom.Sender
76
- ? transactionData.sender
77
- : gasPayer;
69
+ const source = input.FundsWithdrawal.withdrawFrom;
70
+ const withdrawalOwner = source.SenderAllowance
71
+ ? source.SenderAllowance.funder
72
+ : source.Sender
73
+ ? transactionData.sender
74
+ : gasPayer;
78
75
  if (
79
76
  withdrawalOwner &&
80
77
  normalizeSuiAddress(withdrawalOwner) === normalizedGasPayer &&
@@ -20,7 +20,7 @@ import {
20
20
  TransactionExpiration_TransactionExpirationKind,
21
21
  } from '../grpc/proto/sui/rpc/v2/transaction.js';
22
22
  import type { ObjectReference } from '../grpc/proto/sui/rpc/v2/object_reference.js';
23
- import type { Input } from '../grpc/proto/sui/rpc/v2/input.js';
23
+ import type { FundsWithdrawal, Input } from '../grpc/proto/sui/rpc/v2/input.js';
24
24
  import { FundsWithdrawal_Source, Input_InputKind } from '../grpc/proto/sui/rpc/v2/input.js';
25
25
  import type { Command } from '../grpc/proto/sui/rpc/v2/transaction.js';
26
26
  import type { Argument } from '../grpc/proto/sui/rpc/v2/argument.js';
@@ -102,10 +102,7 @@ function callArgToGrpcInput(arg: CallArg): Input {
102
102
  ? BigInt(withdrawal.reservation.MaxAmountU64)
103
103
  : undefined,
104
104
  coinType: withdrawal.typeArg.$kind === 'Balance' ? withdrawal.typeArg.Balance : undefined,
105
- source:
106
- withdrawal.withdrawFrom.$kind === 'Sponsor'
107
- ? FundsWithdrawal_Source.SPONSOR
108
- : FundsWithdrawal_Source.SENDER,
105
+ ...tsWithdrawFromToGrpc(withdrawal.withdrawFrom),
109
106
  },
110
107
  };
111
108
  }
@@ -115,6 +112,46 @@ function callArgToGrpcInput(arg: CallArg): Input {
115
112
  }
116
113
  }
117
114
 
115
+ function tsWithdrawFromToGrpc(
116
+ withdrawFrom: Extract<CallArg, { FundsWithdrawal: unknown }>['FundsWithdrawal']['withdrawFrom'],
117
+ ): Pick<FundsWithdrawal, 'source' | 'funder' | 'allowance'> {
118
+ switch (withdrawFrom.$kind) {
119
+ case 'Sender':
120
+ return { source: FundsWithdrawal_Source.SENDER };
121
+ case 'Sponsor':
122
+ return { source: FundsWithdrawal_Source.SPONSOR };
123
+ case 'SenderAllowance':
124
+ return {
125
+ source: FundsWithdrawal_Source.SENDER_ALLOWANCE,
126
+ funder: withdrawFrom.SenderAllowance.funder,
127
+ allowance: withdrawFrom.SenderAllowance.allowance,
128
+ };
129
+ default:
130
+ throw new Error(`Unknown WithdrawFrom kind: ${JSON.stringify(withdrawFrom)}`);
131
+ }
132
+ }
133
+
134
+ function grpcWithdrawFromToTs(
135
+ withdrawal: FundsWithdrawal | undefined,
136
+ ): Extract<CallArg, { FundsWithdrawal: unknown }>['FundsWithdrawal']['withdrawFrom'] {
137
+ switch (withdrawal?.source) {
138
+ case FundsWithdrawal_Source.SPONSOR:
139
+ return { $kind: 'Sponsor', Sponsor: true };
140
+ case FundsWithdrawal_Source.SENDER_ALLOWANCE:
141
+ if (!withdrawal.funder || !withdrawal.allowance) {
142
+ throw new Error(
143
+ `SENDER_ALLOWANCE withdrawal is missing funder or allowance: ${JSON.stringify(withdrawal)}`,
144
+ );
145
+ }
146
+ return {
147
+ $kind: 'SenderAllowance',
148
+ SenderAllowance: { funder: withdrawal.funder, allowance: withdrawal.allowance },
149
+ };
150
+ default:
151
+ return { $kind: 'Sender', Sender: true };
152
+ }
153
+ }
154
+
118
155
  /**
119
156
  * Converts a TypeScript Argument to gRPC Argument
120
157
  */
@@ -429,10 +466,7 @@ function grpcInputToCallArg(input: Input): CallArg {
429
466
  $kind: 'Balance' as const,
430
467
  Balance: input.fundsWithdrawal?.coinType ?? '0x2::sui::SUI',
431
468
  },
432
- withdrawFrom:
433
- input.fundsWithdrawal?.source === FundsWithdrawal_Source.SPONSOR
434
- ? { $kind: 'Sponsor' as const, Sponsor: true as const }
435
- : { $kind: 'Sender' as const, Sender: true as const },
469
+ withdrawFrom: grpcWithdrawFromToTs(input.fundsWithdrawal),
436
470
  },
437
471
  };
438
472