22// SPDX-License-Identifier: Apache-2.0
33
44export const utilsContent = /* ts */ `
5- import { bcs, BcsType, TypeTag, TypeTagSerializer, BcsStruct, BcsEnum, BcsTuple } from '@mysten/sui/bcs';
5+ import {
6+ bcs,
7+ BcsType,
8+ TypeTag,
9+ TypeTagSerializer,
10+ BcsStruct,
11+ BcsEnum,
12+ BcsTuple,
13+ } from '@mysten/sui/bcs';
614import { normalizeSuiAddress } from '@mysten/sui/utils';
715import { TransactionArgument, isArgument } from '@mysten/sui/transactions';
16+ import { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';
817
918const MOVE_STDLIB_ADDRESS = normalizeSuiAddress('0x1');
1019const SUI_FRAMEWORK_ADDRESS = normalizeSuiAddress('0x2');
11- const SUI_SYSTEM_ADDRESS = normalizeSuiAddress('0x3');
1220
1321export type RawTransactionArgument<T> = T | TransactionArgument;
1422
23+ export interface GetOptions<
24+ Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {},
25+ > extends SuiClientTypes.GetObjectOptions<Include> {
26+ client: ClientWithCoreApi;
27+ }
28+
29+ export interface GetManyOptions<
30+ Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {},
31+ > extends SuiClientTypes.GetObjectsOptions<Include> {
32+ client: ClientWithCoreApi;
33+ }
34+
1535export function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null {
1636 const parsedTag = typeof typeTag === 'string' ? TypeTagSerializer.parseFromStr(typeTag) : typeTag;
1737
@@ -36,7 +56,7 @@ export function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null
3656 return type ? bcs.vector(type) : null;
3757 } else if ('struct' in parsedTag) {
3858 const structTag = parsedTag.struct;
39- const pkg = normalizeSuiAddress(parsedTag.struct .address);
59+ const pkg = normalizeSuiAddress(structTag .address);
4060
4161 if (pkg === MOVE_STDLIB_ADDRESS) {
4262 if (
@@ -47,20 +67,28 @@ export function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null
4767 }
4868
4969 if (structTag.module === 'option' && structTag.name === 'Option') {
50- const type = getPureBcsSchema(structTag.typeParams[0]! );
70+ const type = getPureBcsSchema(structTag.typeParams[0]);
5171 return type ? bcs.option(type) : null;
5272 }
5373 }
5474
55- if (pkg === SUI_FRAMEWORK_ADDRESS && structTag.module === 'object' && (structTag.name === 'ID' || structTag.name === 'UID')) {
75+ if (
76+ pkg === SUI_FRAMEWORK_ADDRESS &&
77+ structTag.module === 'object' &&
78+ (structTag.name === 'ID' || structTag.name === 'UID')
79+ ) {
5680 return bcs.Address;
5781 }
5882 }
5983
6084 return null;
6185}
6286
63- export function normalizeMoveArguments(args: unknown[] | object, argTypes: string[], parameterNames?: string[]) {
87+ export function normalizeMoveArguments(
88+ args: unknown[] | object,
89+ argTypes: readonly (string | null)[],
90+ parameterNames?: string[],
91+ ) {
6492 const argLen = Array.isArray(args) ? args.length : Object.keys(args).length;
6593 if (parameterNames && argLen !== parameterNames.length) {
6694 throw new Error(
@@ -72,30 +100,32 @@ export function normalizeMoveArguments(args: unknown[] | object, argTypes: strin
72100
73101 let index = 0;
74102 for (const [i, argType] of argTypes.entries()) {
75- if (argType === \`\${SUI_FRAMEWORK_ADDRESS}::deny_list::DenyList\` ) {
76- normalizedArgs.push((tx) => tx.object.denyList ());
103+ if (argType === '0x2::clock::Clock' ) {
104+ normalizedArgs.push((tx) => tx.object.clock ());
77105 continue;
78106 }
79107
80- if (argType === \`\${SUI_FRAMEWORK_ADDRESS} ::random::Random\` ) {
108+ if (argType === '0x2 ::random::Random' ) {
81109 normalizedArgs.push((tx) => tx.object.random());
82110 continue;
83111 }
84112
85- if (argType === \`\${SUI_FRAMEWORK_ADDRESS}::clock::Clock\` ) {
86- normalizedArgs.push((tx) => tx.object.clock ());
113+ if (argType === '0x2::deny_list::DenyList' ) {
114+ normalizedArgs.push((tx) => tx.object.denyList ());
87115 continue;
88116 }
89117
90- if (argType === \`\${SUI_SYSTEM_ADDRESS} ::sui_system::SuiSystemState\` ) {
118+ if (argType === '0x3 ::sui_system::SuiSystemState' ) {
91119 normalizedArgs.push((tx) => tx.object.system());
92120 continue;
93121 }
94122
95- let arg
123+ let arg;
96124 if (Array.isArray(args)) {
97125 if (index >= args.length) {
98- throw new Error(\`Invalid number of arguments, expected at least \${index + 1}, got \${args.length}\`);
126+ throw new Error(
127+ \`Invalid number of arguments, expected at least \${index + 1}, got \${args.length}\`,
128+ );
99129 }
100130 arg = args[index];
101131 } else {
@@ -117,8 +147,8 @@ export function normalizeMoveArguments(args: unknown[] | object, argTypes: strin
117147 continue;
118148 }
119149
120- const type = argTypes[i]! ;
121- const bcsType = getPureBcsSchema(type);
150+ const type = argTypes[i];
151+ const bcsType = type === null ? null : getPureBcsSchema(type);
122152
123153 if (bcsType) {
124154 const bytes = bcsType.serialize(arg as never);
@@ -138,7 +168,47 @@ export function normalizeMoveArguments(args: unknown[] | object, argTypes: strin
138168export class MoveStruct<
139169 T extends Record<string, BcsType<any>>,
140170 const Name extends string = string,
141- > extends BcsStruct<T, Name> {}
171+ > extends BcsStruct<T, Name> {
172+ async get<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}>({
173+ objectId,
174+ ...options
175+ }: GetOptions<Include>): Promise<
176+ SuiClientTypes.Object<Include & { content: true }> & { json: BcsStruct<T>['$inferType'] }
177+ > {
178+ const [res] = await this.getMany<Include>({
179+ ...options,
180+ objectIds: [objectId],
181+ });
182+
183+ return res;
184+ }
185+
186+ async getMany<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}>({
187+ client,
188+ ...options
189+ }: GetManyOptions<Include>): Promise<
190+ Array<SuiClientTypes.Object<Include & { content: true }> & { json: BcsStruct<T>['$inferType'] }>
191+ > {
192+ const response = (await client.core.getObjects({
193+ ...options,
194+ include: {
195+ ...options.include,
196+ content: true,
197+ },
198+ })) as SuiClientTypes.GetObjectsResponse<Include & { content: true }>;
199+
200+ return response.objects.map((obj) => {
201+ if (obj instanceof Error) {
202+ throw obj;
203+ }
204+
205+ return {
206+ ...obj,
207+ json: this.parse(obj.content),
208+ };
209+ });
210+ }
211+ }
142212
143213export class MoveEnum<
144214 T extends Record<string, BcsType<any> | null>,
0 commit comments