-
Notifications
You must be signed in to change notification settings - Fork 11k
/
TransactionBlock.ts
865 lines (756 loc) · 26.1 KB
/
TransactionBlock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
import type { SerializedBcs } from '@mysten/bcs';
import { fromB64, isSerializedBcs } from '@mysten/bcs';
import { is, mask } from 'superstruct';
import { bcs } from '../bcs/index.js';
import type { ProtocolConfig, SuiClient, SuiMoveNormalizedType } from '../client/index.js';
import type { SignatureWithBytes, Signer } from '../cryptography/index.js';
import { SUI_TYPE_ARG } from '../utils/index.js';
import { normalizeSuiAddress, normalizeSuiObjectId } from '../utils/sui-types.js';
import {
BuilderCallArg,
getIdFromCallArg,
Inputs,
isMutableSharedObjectInput,
ObjectCallArg,
PureCallArg,
SuiObjectRef,
} from './Inputs.js';
import { createPure } from './pure.js';
import { getPureSerializationType, isTxContext } from './serializer.js';
import type { TransactionExpiration } from './TransactionBlockData.js';
import { TransactionBlockDataBuilder } from './TransactionBlockData.js';
import type { MoveCallTransaction, TransactionArgument, TransactionType } from './Transactions.js';
import { TransactionBlockInput, Transactions } from './Transactions.js';
import { create, extractMutableReference, extractReference, extractStructTag } from './utils.js';
export type TransactionObjectArgument = Exclude<
TransactionArgument,
{ kind: 'Input'; type: 'pure' }
>;
export type TransactionResult = Extract<TransactionArgument, { kind: 'Result' }> &
Extract<TransactionArgument, { kind: 'NestedResult' }>[];
const DefaultOfflineLimits = {
maxPureArgumentSize: 16 * 1024,
maxTxGas: 50_000_000_000,
maxGasObjects: 256,
maxTxSizeBytes: 128 * 1024,
} satisfies Limits;
function createTransactionResult(index: number): TransactionResult {
const baseResult: TransactionArgument = { kind: 'Result', index };
const nestedResults: TransactionArgument[] = [];
const nestedResultFor = (resultIndex: number): TransactionArgument =>
(nestedResults[resultIndex] ??= {
kind: 'NestedResult',
index,
resultIndex,
});
return new Proxy(baseResult, {
set() {
throw new Error(
'The transaction result is a proxy, and does not support setting properties directly',
);
},
// TODO: Instead of making this return a concrete argument, we should ideally
// make it reference-based (so that this gets resolved at build-time), which
// allows re-ordering transactions.
get(target, property) {
// This allows this transaction argument to be used in the singular form:
if (property in target) {
return Reflect.get(target, property);
}
// Support destructuring:
if (property === Symbol.iterator) {
return function* () {
let i = 0;
while (true) {
yield nestedResultFor(i);
i++;
}
};
}
if (typeof property === 'symbol') return;
const resultIndex = parseInt(property, 10);
if (Number.isNaN(resultIndex) || resultIndex < 0) return;
return nestedResultFor(resultIndex);
},
}) as TransactionResult;
}
function isReceivingType(normalizedType: SuiMoveNormalizedType): boolean {
const tag = extractStructTag(normalizedType);
if (tag) {
return (
tag.Struct.address === '0x2' &&
tag.Struct.module === 'transfer' &&
tag.Struct.name === 'Receiving'
);
}
return false;
}
function expectClient(options: BuildOptions): SuiClient {
if (!options.client) {
throw new Error(
`No provider passed to Transaction#build, but transaction data was not sufficient to build offline.`,
);
}
return options.client;
}
const TRANSACTION_BRAND = Symbol.for('@mysten/transaction');
const LIMITS = {
// The maximum gas that is allowed.
maxTxGas: 'max_tx_gas',
// The maximum number of gas objects that can be selected for one transaction.
maxGasObjects: 'max_gas_payment_objects',
// The maximum size (in bytes) that the transaction can be:
maxTxSizeBytes: 'max_tx_size_bytes',
// The maximum size (in bytes) that pure arguments can be:
maxPureArgumentSize: 'max_pure_argument_size',
} as const;
type Limits = Partial<Record<keyof typeof LIMITS, number>>;
// An amount of gas (in gas units) that is added to transactions as an overhead to ensure transactions do not fail.
const GAS_SAFE_OVERHEAD = 1000n;
// The maximum objects that can be fetched at once using multiGetObjects.
const MAX_OBJECTS_PER_FETCH = 50;
const chunk = <T>(arr: T[], size: number): T[][] =>
Array.from({ length: Math.ceil(arr.length / size) }, (_, i) =>
arr.slice(i * size, i * size + size),
);
interface BuildOptions {
client?: SuiClient;
onlyTransactionKind?: boolean;
/** Define a protocol config to build against, instead of having it fetched from the provider at build time. */
protocolConfig?: ProtocolConfig;
/** Define limits that are used when building the transaction. In general, we recommend using the protocol configuration instead of defining limits. */
limits?: Limits;
}
interface SignOptions extends BuildOptions {
signer: Signer;
}
export function isTransactionBlock(obj: unknown): obj is TransactionBlock {
return !!obj && typeof obj === 'object' && (obj as any)[TRANSACTION_BRAND] === true;
}
export type TransactionObjectInput = string | ObjectCallArg | TransactionObjectArgument;
/**
* Transaction Builder
*/
export class TransactionBlock {
/**
* Converts from a serialize transaction kind (built with `build({ onlyTransactionKind: true })`) to a `Transaction` class.
* Supports either a byte array, or base64-encoded bytes.
*/
static fromKind(serialized: string | Uint8Array) {
const tx = new TransactionBlock();
tx.#blockData = TransactionBlockDataBuilder.fromKindBytes(
typeof serialized === 'string' ? fromB64(serialized) : serialized,
);
return tx;
}
/**
* Converts from a serialized transaction format to a `Transaction` class.
* There are two supported serialized formats:
* - A string returned from `Transaction#serialize`. The serialized format must be compatible, or it will throw an error.
* - A byte array (or base64-encoded bytes) containing BCS transaction data.
*/
static from(serialized: string | Uint8Array) {
const tx = new TransactionBlock();
// Check for bytes:
if (typeof serialized !== 'string' || !serialized.startsWith('{')) {
tx.#blockData = TransactionBlockDataBuilder.fromBytes(
typeof serialized === 'string' ? fromB64(serialized) : serialized,
);
} else {
tx.#blockData = TransactionBlockDataBuilder.restore(JSON.parse(serialized));
}
return tx;
}
setSender(sender: string) {
this.#blockData.sender = sender;
}
/**
* Sets the sender only if it has not already been set.
* This is useful for sponsored transaction flows where the sender may not be the same as the signer address.
*/
setSenderIfNotSet(sender: string) {
if (!this.#blockData.sender) {
this.#blockData.sender = sender;
}
}
setExpiration(expiration?: TransactionExpiration) {
this.#blockData.expiration = expiration;
}
setGasPrice(price: number | bigint) {
this.#blockData.gasConfig.price = String(price);
}
setGasBudget(budget: number | bigint) {
this.#blockData.gasConfig.budget = String(budget);
}
setGasOwner(owner: string) {
this.#blockData.gasConfig.owner = owner;
}
setGasPayment(payments: SuiObjectRef[]) {
this.#blockData.gasConfig.payment = payments.map((payment) => mask(payment, SuiObjectRef));
}
#blockData: TransactionBlockDataBuilder;
/** Get a snapshot of the transaction data, in JSON form: */
get blockData() {
return this.#blockData.snapshot();
}
// Used to brand transaction classes so that they can be identified, even between multiple copies
// of the builder.
get [TRANSACTION_BRAND]() {
return true;
}
// Temporary workaround for the wallet interface accidentally serializing transaction blocks via postMessage
get pure(): ReturnType<typeof createPure> {
Object.defineProperty(this, 'pure', {
enumerable: false,
value: createPure((value, type) => {
if (isSerializedBcs(value)) {
return this.#input('pure', {
Pure: Array.from(value.toBytes()),
});
}
// TODO: we can also do some deduplication here
return this.#input(
'pure',
value instanceof Uint8Array
? Inputs.Pure(value)
: type
? Inputs.Pure(value, type)
: value,
);
}),
});
return this.pure;
}
constructor(transaction?: TransactionBlock) {
this.#blockData = new TransactionBlockDataBuilder(
transaction ? transaction.blockData : undefined,
);
}
/** Returns an argument for the gas coin, to be used in a transaction. */
get gas(): TransactionObjectArgument {
return { kind: 'GasCoin' };
}
/**
* Dynamically create a new input, which is separate from the `input`. This is important
* for generated clients to be able to define unique inputs that are non-overlapping with the
* defined inputs.
*
* For `Uint8Array` type automatically convert the input into a `Pure` CallArg, since this
* is the format required for custom serialization.
*
*/
#input<T extends 'object' | 'pure'>(type: T, value?: unknown) {
const index = this.#blockData.inputs.length;
const input = create(
{
kind: 'Input',
// bigints can't be serialized to JSON, so just string-convert them here:
value: typeof value === 'bigint' ? String(value) : value,
index,
type,
},
TransactionBlockInput,
);
this.#blockData.inputs.push(input);
return input as Extract<typeof input, { type: T }>;
}
/**
* Add a new object input to the transaction.
*/
object(value: TransactionObjectInput) {
if (typeof value === 'object' && 'kind' in value) {
return value;
}
const id = getIdFromCallArg(value);
const inserted = this.#blockData.inputs.find(
(i) => i.type === 'object' && id === getIdFromCallArg(i.value),
) as Extract<TransactionArgument, { type?: 'object' }> | undefined;
// Upgrade shared object inputs to mutable if needed:
if (
inserted &&
is(inserted.value, ObjectCallArg) &&
'Shared' in inserted.value.Object &&
is(value, ObjectCallArg) &&
'Shared' in value.Object
) {
inserted.value.Object.Shared.mutable =
inserted.value.Object.Shared.mutable || value.Object.Shared.mutable;
}
return (
inserted ??
this.#input('object', typeof value === 'string' ? normalizeSuiAddress(value) : value)
);
}
/**
* Add a new object input to the transaction using the fully-resolved object reference.
* If you only have an object ID, use `builder.object(id)` instead.
*/
objectRef(...args: Parameters<(typeof Inputs)['ObjectRef']>) {
return this.object(Inputs.ObjectRef(...args));
}
/**
* Add a new receiving input to the transaction using the fully-resolved object reference.
* If you only have an object ID, use `builder.object(id)` instead.
*/
receivingRef(...args: Parameters<(typeof Inputs)['ReceivingRef']>) {
return this.object(Inputs.ReceivingRef(...args));
}
/**
* Add a new shared object input to the transaction using the fully-resolved shared object reference.
* If you only have an object ID, use `builder.object(id)` instead.
*/
sharedObjectRef(...args: Parameters<(typeof Inputs)['SharedObjectRef']>) {
return this.object(Inputs.SharedObjectRef(...args));
}
/** Add a transaction to the transaction block. */
add(transaction: TransactionType) {
const index = this.#blockData.transactions.push(transaction);
return createTransactionResult(index - 1);
}
#normalizeTransactionArgument(
arg: TransactionArgument | SerializedBcs<any>,
): TransactionArgument {
if (isSerializedBcs(arg)) {
return this.pure(arg);
}
return arg as TransactionArgument;
}
// Method shorthands:
splitCoins(
coin: TransactionObjectArgument | string,
amounts: (TransactionArgument | SerializedBcs<any> | number | string | bigint)[],
) {
return this.add(
Transactions.SplitCoins(
typeof coin === 'string' ? this.object(coin) : coin,
amounts.map((amount) =>
typeof amount === 'number' || typeof amount === 'bigint' || typeof amount === 'string'
? this.pure.u64(amount)
: this.#normalizeTransactionArgument(amount),
),
),
);
}
mergeCoins(
destination: TransactionObjectArgument | string,
sources: (TransactionObjectArgument | string)[],
) {
return this.add(
Transactions.MergeCoins(
typeof destination === 'string' ? this.object(destination) : destination,
sources.map((src) => (typeof src === 'string' ? this.object(src) : src)),
),
);
}
publish({ modules, dependencies }: { modules: number[][] | string[]; dependencies: string[] }) {
return this.add(
Transactions.Publish({
modules,
dependencies,
}),
);
}
upgrade({
modules,
dependencies,
packageId,
ticket,
}: {
modules: number[][] | string[];
dependencies: string[];
packageId: string;
ticket: TransactionObjectArgument | string;
}) {
return this.add(
Transactions.Upgrade({
modules,
dependencies,
packageId,
ticket: typeof ticket === 'string' ? this.object(ticket) : ticket,
}),
);
}
moveCall({
arguments: args,
typeArguments,
target,
}: {
arguments?: (TransactionArgument | SerializedBcs<any>)[];
typeArguments?: string[];
target: `${string}::${string}::${string}`;
}) {
return this.add(
Transactions.MoveCall({
arguments: args?.map((arg) => this.#normalizeTransactionArgument(arg)),
typeArguments,
target,
}),
);
}
transferObjects(
objects: (TransactionObjectArgument | string)[],
address: TransactionArgument | SerializedBcs<any> | string,
) {
return this.add(
Transactions.TransferObjects(
objects.map((obj) => (typeof obj === 'string' ? this.object(obj) : obj)),
typeof address === 'string'
? this.pure.address(address)
: this.#normalizeTransactionArgument(address),
),
);
}
makeMoveVec({
type,
objects,
}: {
objects: (TransactionObjectArgument | string)[];
type?: string;
}) {
return this.add(
Transactions.MakeMoveVec({
type,
objects: objects.map((obj) => (typeof obj === 'string' ? this.object(obj) : obj)),
}),
);
}
/**
* Serialize the transaction to a string so that it can be sent to a separate context.
* This is different from `build` in that it does not serialize to BCS bytes, and instead
* uses a separate format that is unique to the transaction builder. This allows
* us to serialize partially-complete transactions, that can then be completed and
* built in a separate context.
*
* For example, a dapp can construct a transaction, but not provide gas objects
* or a gas budget. The transaction then can be sent to the wallet, where this
* information is automatically filled in (e.g. by querying for coin objects
* and performing a dry run).
*/
serialize() {
return JSON.stringify(this.#blockData.snapshot());
}
#getConfig(key: keyof typeof LIMITS, { protocolConfig, limits }: BuildOptions) {
// Use the limits definition if that exists:
if (limits && typeof limits[key] === 'number') {
return limits[key]!;
}
if (!protocolConfig) {
return DefaultOfflineLimits[key];
}
// Fallback to protocol config:
const attribute = protocolConfig?.attributes[LIMITS[key]];
if (!attribute) {
throw new Error(`Missing expected protocol config: "${LIMITS[key]}"`);
}
const value =
'u64' in attribute ? attribute.u64 : 'u32' in attribute ? attribute.u32 : attribute.f64;
if (!value) {
throw new Error(`Unexpected protocol config value found for: "${LIMITS[key]}"`);
}
// NOTE: Technically this is not a safe conversion, but we know all of the values in protocol config are safe
return Number(value);
}
/** Build the transaction to BCS bytes, and sign it with the provided keypair. */
async sign(options: SignOptions): Promise<SignatureWithBytes> {
const { signer, ...buildOptions } = options;
const bytes = await this.build(buildOptions);
return signer.signTransactionBlock(bytes);
}
/** Build the transaction to BCS bytes. */
async build(options: BuildOptions = {}): Promise<Uint8Array> {
await this.#prepare(options);
return this.#blockData.build({
maxSizeBytes: this.#getConfig('maxTxSizeBytes', options),
onlyTransactionKind: options.onlyTransactionKind,
});
}
/** Derive transaction digest */
async getDigest(
options: {
client?: SuiClient;
} = {},
): Promise<string> {
await this.#prepare(options);
return this.#blockData.getDigest();
}
#validate(options: BuildOptions) {
const maxPureArgumentSize = this.#getConfig('maxPureArgumentSize', options);
// Validate all inputs are the correct size:
this.#blockData.inputs.forEach((input, index) => {
if (is(input.value, PureCallArg)) {
if (input.value.Pure.length > maxPureArgumentSize) {
throw new Error(
`Input at index ${index} is too large, max pure input size is ${maxPureArgumentSize} bytes, got ${input.value.Pure.length} bytes`,
);
}
}
});
}
// The current default is just picking _all_ coins we can which may not be ideal.
async #prepareGasPayment(options: BuildOptions) {
if (this.#blockData.gasConfig.payment) {
const maxGasObjects = this.#getConfig('maxGasObjects', options);
if (this.#blockData.gasConfig.payment.length > maxGasObjects) {
throw new Error(`Payment objects exceed maximum amount: ${maxGasObjects}`);
}
}
// Early return if the payment is already set:
if (options.onlyTransactionKind || this.#blockData.gasConfig.payment) {
return;
}
const gasOwner = this.#blockData.gasConfig.owner ?? this.#blockData.sender;
const coins = await expectClient(options).getCoins({
owner: gasOwner!,
coinType: SUI_TYPE_ARG,
});
const paymentCoins = coins.data
// Filter out coins that are also used as input:
.filter((coin) => {
const matchingInput = this.#blockData.inputs.find((input) => {
if (
is(input.value, BuilderCallArg) &&
'Object' in input.value &&
'ImmOrOwned' in input.value.Object
) {
return coin.coinObjectId === input.value.Object.ImmOrOwned.objectId;
}
return false;
});
return !matchingInput;
})
.slice(0, this.#getConfig('maxGasObjects', options) - 1)
.map((coin) => ({
objectId: coin.coinObjectId,
digest: coin.digest,
version: coin.version,
}));
if (!paymentCoins.length) {
throw new Error('No valid gas coins found for the transaction.');
}
this.setGasPayment(paymentCoins);
}
async #prepareGasPrice(options: BuildOptions) {
if (options.onlyTransactionKind || this.#blockData.gasConfig.price) {
return;
}
this.setGasPrice(await expectClient(options).getReferenceGasPrice());
}
async #prepareTransactions(options: BuildOptions) {
const { inputs, transactions } = this.#blockData;
const moveModulesToResolve: MoveCallTransaction[] = [];
// Keep track of the object references that will need to be resolved at the end of the transaction.
// We keep the input by-reference to avoid needing to re-resolve it:
const objectsToResolve: {
id: string;
input: TransactionBlockInput;
normalizedType?: SuiMoveNormalizedType;
}[] = [];
inputs.forEach((input) => {
if (input.type === 'object' && typeof input.value === 'string') {
// The input is a string that we need to resolve to an object reference:
objectsToResolve.push({ id: normalizeSuiAddress(input.value), input });
return;
}
});
transactions.forEach((transaction) => {
// Special case move call:
if (transaction.kind === 'MoveCall') {
// Determine if any of the arguments require encoding.
// - If they don't, then this is good to go.
// - If they do, then we need to fetch the normalized move module.
const needsResolution = transaction.arguments.some(
(arg) => arg.kind === 'Input' && !is(inputs[arg.index].value, BuilderCallArg),
);
if (needsResolution) {
moveModulesToResolve.push(transaction);
}
}
// Special handling for values that where previously encoded using the wellKnownEncoding pattern.
// This should only happen when transaction block data was hydrated from an old version of the SDK
if (transaction.kind === 'SplitCoins') {
transaction.amounts.forEach((amount) => {
if (amount.kind === 'Input') {
const input = inputs[amount.index];
if (typeof input.value !== 'object') {
input.value = Inputs.Pure(bcs.U64.serialize(input.value));
}
}
});
}
if (transaction.kind === 'TransferObjects') {
if (transaction.address.kind === 'Input') {
const input = inputs[transaction.address.index];
if (typeof input.value !== 'object') {
input.value = Inputs.Pure(bcs.Address.serialize(input.value));
}
}
}
});
if (moveModulesToResolve.length) {
await Promise.all(
moveModulesToResolve.map(async (moveCall) => {
const [packageId, moduleName, functionName] = moveCall.target.split('::');
const normalized = await expectClient(options).getNormalizedMoveFunction({
package: normalizeSuiObjectId(packageId),
module: moduleName,
function: functionName,
});
// Entry functions can have a mutable reference to an instance of the TxContext
// struct defined in the TxContext module as the last parameter. The caller of
// the function does not need to pass it in as an argument.
const hasTxContext =
normalized.parameters.length > 0 && isTxContext(normalized.parameters.at(-1)!);
const params = hasTxContext
? normalized.parameters.slice(0, normalized.parameters.length - 1)
: normalized.parameters;
if (params.length !== moveCall.arguments.length) {
throw new Error('Incorrect number of arguments.');
}
params.forEach((param, i) => {
const arg = moveCall.arguments[i];
if (arg.kind !== 'Input') return;
const input = inputs[arg.index];
// Skip if the input is already resolved
if (is(input.value, BuilderCallArg)) return;
const inputValue = input.value;
const serType = getPureSerializationType(param, inputValue);
if (serType) {
input.value = Inputs.Pure(inputValue, serType);
return;
}
const structVal = extractStructTag(param);
if (structVal != null || (typeof param === 'object' && 'TypeParameter' in param)) {
if (typeof inputValue !== 'string') {
throw new Error(
`Expect the argument to be an object id string, got ${JSON.stringify(
inputValue,
null,
2,
)}`,
);
}
objectsToResolve.push({
id: inputValue,
input,
normalizedType: param,
});
return;
}
throw new Error(
`Unknown call arg type ${JSON.stringify(param, null, 2)} for value ${JSON.stringify(
inputValue,
null,
2,
)}`,
);
});
}),
);
}
if (objectsToResolve.length) {
const dedupedIds = [...new Set(objectsToResolve.map(({ id }) => id))];
const objectChunks = chunk(dedupedIds, MAX_OBJECTS_PER_FETCH);
const objects = (
await Promise.all(
objectChunks.map((chunk) =>
expectClient(options).multiGetObjects({
ids: chunk,
options: { showOwner: true },
}),
),
)
).flat();
let objectsById = new Map(
dedupedIds.map((id, index) => {
return [id, objects[index]];
}),
);
const invalidObjects = Array.from(objectsById)
.filter(([_, obj]) => obj.error)
.map(([id, _]) => id);
if (invalidObjects.length) {
throw new Error(`The following input objects are invalid: ${invalidObjects.join(', ')}`);
}
objectsToResolve.forEach(({ id, input, normalizedType }) => {
const object = objectsById.get(id)!;
const owner = object.data?.owner;
const initialSharedVersion =
owner && typeof owner === 'object' && 'Shared' in owner
? owner.Shared.initial_shared_version
: undefined;
if (initialSharedVersion) {
// There could be multiple transactions that reference the same shared object.
// If one of them is a mutable reference or taken by value, then we should mark the input
// as mutable.
const isByValue =
normalizedType != null &&
extractMutableReference(normalizedType) == null &&
extractReference(normalizedType) == null;
const mutable =
isMutableSharedObjectInput(input.value) ||
isByValue ||
(normalizedType != null && extractMutableReference(normalizedType) != null);
input.value = Inputs.SharedObjectRef({
objectId: id,
initialSharedVersion,
mutable,
});
} else if (normalizedType && isReceivingType(normalizedType)) {
input.value = Inputs.ReceivingRef(object.data!);
} else {
input.value = Inputs.ObjectRef(object.data!);
}
});
}
}
/**
* Prepare the transaction by valdiating the transaction data and resolving all inputs
* so that it can be built into bytes.
*/
async #prepare(options: BuildOptions) {
if (!options.onlyTransactionKind && !this.#blockData.sender) {
throw new Error('Missing transaction sender');
}
if (!options.protocolConfig && !options.limits && options.client) {
options.protocolConfig = await options.client.getProtocolConfig();
}
await Promise.all([this.#prepareGasPrice(options), this.#prepareTransactions(options)]);
if (!options.onlyTransactionKind) {
await this.#prepareGasPayment(options);
if (!this.#blockData.gasConfig.budget) {
const dryRunResult = await expectClient(options).dryRunTransactionBlock({
transactionBlock: this.#blockData.build({
maxSizeBytes: this.#getConfig('maxTxSizeBytes', options),
overrides: {
gasConfig: {
budget: String(this.#getConfig('maxTxGas', options)),
payment: [],
},
},
}),
});
if (dryRunResult.effects.status.status !== 'success') {
throw new Error(
`Dry run failed, could not automatically determine a budget: ${dryRunResult.effects.status.error}`,
{ cause: dryRunResult },
);
}
const safeOverhead = GAS_SAFE_OVERHEAD * BigInt(this.blockData.gasConfig.price || 1n);
const baseComputationCostWithOverhead =
BigInt(dryRunResult.effects.gasUsed.computationCost) + safeOverhead;
const gasBudget =
baseComputationCostWithOverhead +
BigInt(dryRunResult.effects.gasUsed.storageCost) -
BigInt(dryRunResult.effects.gasUsed.storageRebate);
// Set the budget to max(computation, computation + storage - rebate)
this.setGasBudget(
gasBudget > baseComputationCostWithOverhead ? gasBudget : baseComputationCostWithOverhead,
);
}
}
// Perform final validation on the transaction:
this.#validate(options);
}
}