@suilend/sdk 12.7.0 → 13.0.0-next.g16373c6

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 (88) hide show
  1. package/_generated/_dependencies/pyth-2/price-identifier/structs.d.ts +46 -0
  2. package/_generated/_dependencies/{source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e → pyth-2}/price-identifier/structs.js +45 -25
  3. package/_generated/_dependencies/std/ascii/structs.d.ts +100 -0
  4. package/_generated/_dependencies/{source/0x1 → std}/ascii/structs.js +153 -103
  5. package/_generated/_dependencies/{source/0x1 → std}/option/structs.d.ts +29 -21
  6. package/_generated/_dependencies/std/option/structs.js +155 -0
  7. package/_generated/_dependencies/std/type-name/structs.d.ts +65 -0
  8. package/_generated/_dependencies/{source/0x1 → std}/type-name/structs.js +49 -22
  9. package/_generated/_dependencies/sui/bag/structs.d.ts +75 -0
  10. package/_generated/_dependencies/sui/bag/structs.js +154 -0
  11. package/_generated/_dependencies/{source/0x2 → sui}/balance/structs.d.ts +80 -60
  12. package/_generated/_dependencies/sui/balance/structs.js +306 -0
  13. package/_generated/_dependencies/sui/object/structs.d.ts +104 -0
  14. package/_generated/_dependencies/{source/0x2 → sui}/object/structs.js +98 -46
  15. package/_generated/_dependencies/{source/0x2 → sui}/object-table/structs.d.ts +37 -30
  16. package/_generated/_dependencies/sui/object-table/structs.js +164 -0
  17. package/_generated/_envs/index.d.ts +3 -0
  18. package/_generated/_envs/index.js +18 -0
  19. package/_generated/_envs/mainnet.d.ts +2 -0
  20. package/_generated/_envs/mainnet.js +490 -0
  21. package/_generated/_framework/env.d.ts +137 -0
  22. package/_generated/_framework/env.js +261 -0
  23. package/_generated/_framework/reified.d.ts +68 -36
  24. package/_generated/_framework/reified.js +110 -98
  25. package/_generated/_framework/util.d.ts +4 -4
  26. package/_generated/_framework/util.js +82 -79
  27. package/_generated/_framework/vector.d.ts +11 -9
  28. package/_generated/_framework/vector.js +10 -10
  29. package/_generated/suilend/cell/structs.d.ts +24 -22
  30. package/_generated/suilend/cell/structs.js +68 -24
  31. package/_generated/suilend/decimal/structs.d.ts +28 -24
  32. package/_generated/suilend/decimal/structs.js +45 -24
  33. package/_generated/suilend/index.d.ts +5 -6
  34. package/_generated/suilend/index.js +10 -19
  35. package/_generated/suilend/lending-market/functions.d.ts +1123 -149
  36. package/_generated/suilend/lending-market/functions.js +1121 -207
  37. package/_generated/suilend/lending-market/structs.d.ts +985 -1290
  38. package/_generated/suilend/lending-market/structs.js +2482 -1282
  39. package/_generated/suilend/lending-market-registry/functions.d.ts +32 -3
  40. package/_generated/suilend/lending-market-registry/functions.js +30 -6
  41. package/_generated/suilend/liquidity-mining/structs.d.ts +200 -333
  42. package/_generated/suilend/liquidity-mining/structs.js +449 -319
  43. package/_generated/suilend/obligation/structs.d.ts +406 -918
  44. package/_generated/suilend/obligation/structs.js +786 -596
  45. package/_generated/suilend/rate-limiter/functions.d.ts +134 -15
  46. package/_generated/suilend/rate-limiter/functions.js +139 -21
  47. package/_generated/suilend/rate-limiter/structs.d.ts +72 -80
  48. package/_generated/suilend/rate-limiter/structs.js +98 -48
  49. package/_generated/suilend/reserve/structs.d.ts +706 -904
  50. package/_generated/suilend/reserve/structs.js +1798 -761
  51. package/_generated/suilend/reserve-config/functions.d.ts +718 -96
  52. package/_generated/suilend/reserve-config/functions.js +751 -157
  53. package/_generated/suilend/reserve-config/structs.d.ts +148 -163
  54. package/_generated/suilend/reserve-config/structs.js +260 -94
  55. package/client.d.ts +4 -2
  56. package/client.js +35 -39
  57. package/lib/initialize.d.ts +617 -83
  58. package/lib/initialize.js +151 -48
  59. package/lib/proCompatible.js +5 -5
  60. package/package.json +1 -1
  61. package/parsers/lendingMarket.d.ts +9 -9
  62. package/parsers/obligation.d.ts +14 -14
  63. package/parsers/obligation.js +4 -4
  64. package/parsers/rateLimiter.d.ts +1 -1
  65. package/parsers/reserve.d.ts +12 -12
  66. package/parsers/reserve.js +2 -2
  67. package/strategies.d.ts +14 -14
  68. package/utils/events.d.ts +20 -0
  69. package/utils/events.js +3 -1
  70. package/utils/obligation.d.ts +3 -0
  71. package/utils/obligation.js +10 -0
  72. package/utils/simulate.d.ts +13 -1
  73. package/utils/simulate.js +19 -5
  74. package/_generated/_dependencies/source/0x1/ascii/structs.d.ts +0 -84
  75. package/_generated/_dependencies/source/0x1/index.d.ts +0 -3
  76. package/_generated/_dependencies/source/0x1/index.js +0 -3
  77. package/_generated/_dependencies/source/0x1/option/structs.js +0 -110
  78. package/_generated/_dependencies/source/0x1/type-name/structs.d.ts +0 -47
  79. package/_generated/_dependencies/source/0x2/bag/structs.d.ts +0 -52
  80. package/_generated/_dependencies/source/0x2/bag/structs.js +0 -113
  81. package/_generated/_dependencies/source/0x2/balance/structs.js +0 -210
  82. package/_generated/_dependencies/source/0x2/index.d.ts +0 -3
  83. package/_generated/_dependencies/source/0x2/index.js +0 -3
  84. package/_generated/_dependencies/source/0x2/object/structs.d.ts +0 -82
  85. package/_generated/_dependencies/source/0x2/object-table/structs.js +0 -115
  86. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/index.d.ts +0 -4
  87. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/index.js +0 -4
  88. package/_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.d.ts +0 -46
@@ -1,44 +1,52 @@
1
- import * as reified from "../../../../_framework/reified";
2
- import { PhantomReified, Reified, StructClass, ToField, ToTypeArgument, ToTypeStr, TypeArgument } from "../../../../_framework/reified";
3
- import { FieldsWithTypes } from "../../../../_framework/util";
4
- import { Vector } from "../../../../_framework/vector";
5
- import { PKG_V13 } from "../index";
6
- import { BcsType } from "@mysten/sui/bcs";
7
- import { SuiGrpcClient } from "@mysten/sui/grpc";
1
+ /** This module defines the Option type and its methods to represent and handle an optional value. */
2
+ import type { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';
3
+ import { PhantomReified, Reified, StructClass, ToField, ToJSON, ToTypeArgument, ToTypeStr, TypeArgument } from '../../../_framework/reified';
4
+ import { FieldsWithTypes } from '../../../_framework/util';
5
+ import { Vector } from '../../../_framework/vector';
8
6
  export declare function isOption(type: string): boolean;
9
7
  export interface OptionFields<Element extends TypeArgument> {
10
8
  vec: ToField<Vector<Element>>;
11
9
  }
12
10
  export type OptionReified<Element extends TypeArgument> = Reified<Option<Element>, OptionFields<Element>>;
11
+ export type OptionJSONField<Element extends TypeArgument> = {
12
+ vec: ToJSON<Element>[];
13
+ };
14
+ export type OptionJSON<Element extends TypeArgument> = {
15
+ $typeName: typeof Option.$typeName;
16
+ $typeArgs: [ToTypeStr<Element>];
17
+ } & OptionJSONField<Element>;
18
+ /**
19
+ * Abstraction of a value that may or may not be present. Implemented with a vector of size
20
+ * zero or one because Move bytecode does not have ADTs.
21
+ */
13
22
  export declare class Option<Element extends TypeArgument> implements StructClass {
14
23
  __StructClass: true;
15
- static readonly $typeName = "0x1::option::Option";
24
+ static readonly $typeName: `0x1::option::Option`;
16
25
  static readonly $numTypeParams = 1;
17
26
  static readonly $isPhantom: readonly [false];
18
27
  __inner: Element;
19
- readonly $typeName = "0x1::option::Option";
20
- readonly $fullTypeName: `${typeof PKG_V13}::option::Option<${ToTypeStr<Element>}>`;
28
+ readonly $typeName: typeof Option.$typeName;
29
+ readonly $fullTypeName: `0x1::option::Option<${ToTypeStr<Element>}>`;
21
30
  readonly $typeArgs: [ToTypeStr<Element>];
22
- readonly $isPhantom: readonly [false];
31
+ readonly $isPhantom: typeof Option.$isPhantom;
23
32
  readonly vec: ToField<Vector<Element>>;
24
33
  private constructor();
25
34
  static reified<Element extends Reified<TypeArgument, any>>(Element: Element): OptionReified<ToTypeArgument<Element>>;
26
35
  static get r(): typeof Option.reified;
27
36
  static phantom<Element extends Reified<TypeArgument, any>>(Element: Element): PhantomReified<ToTypeStr<Option<ToTypeArgument<Element>>>>;
28
37
  static get p(): typeof Option.phantom;
29
- static get bcs(): <Element extends BcsType<any>>(Element: Element) => BcsType<any>;
38
+ private static instantiateBcs;
39
+ private static cachedBcs;
40
+ static get bcs(): ReturnType<typeof Option.instantiateBcs>;
30
41
  static fromFields<Element extends Reified<TypeArgument, any>>(typeArg: Element, fields: Record<string, any>): Option<ToTypeArgument<Element>>;
31
42
  static fromFieldsWithTypes<Element extends Reified<TypeArgument, any>>(typeArg: Element, item: FieldsWithTypes): Option<ToTypeArgument<Element>>;
32
43
  static fromBcs<Element extends Reified<TypeArgument, any>>(typeArg: Element, data: Uint8Array): Option<ToTypeArgument<Element>>;
33
- toJSONField(): {
34
- vec: reified.ToJSON<Element>[];
35
- };
36
- toJSON(): {
37
- vec: reified.ToJSON<Element>[];
38
- $typeName: string;
39
- $typeArgs: [reified.ToTypeStr<Element>];
40
- };
44
+ toJSONField(): OptionJSONField<Element>;
45
+ toJSON(): OptionJSON<Element>;
41
46
  static fromJSONField<Element extends Reified<TypeArgument, any>>(typeArg: Element, field: any): Option<ToTypeArgument<Element>>;
42
47
  static fromJSON<Element extends Reified<TypeArgument, any>>(typeArg: Element, json: Record<string, any>): Option<ToTypeArgument<Element>>;
43
- static fetch<Element extends Reified<TypeArgument, any>>(client: SuiGrpcClient, typeArg: Element, id: string): Promise<Option<ToTypeArgument<Element>>>;
48
+ static fromCoreObject<Element extends Reified<TypeArgument, any>>(typeArg: Element, obj: SuiClientTypes.Object<{
49
+ content: true;
50
+ }>): Option<ToTypeArgument<Element>>;
51
+ static fetch<Element extends Reified<TypeArgument, any>>(client: ClientWithCoreApi, typeArg: Element, id: string): Promise<Option<ToTypeArgument<Element>>>;
44
52
  }
@@ -0,0 +1,155 @@
1
+ /** This module defines the Option type and its methods to represent and handle an optional value. */
2
+ import { bcs } from '@mysten/sui/bcs';
3
+ import { assertFieldsWithTypesArgsMatch, assertReifiedTypeArgsMatch, decodeFromFields, decodeFromFieldsWithTypes, decodeFromJSONField, extractType, fieldToJSON, phantom, toBcs, vector, } from '../../../_framework/reified.js';
4
+ import { composeSuiType, compressSuiType, parseTypeName, } from '../../../_framework/util.js';
5
+ /* ============================== Option =============================== */
6
+ export function isOption(type) {
7
+ type = compressSuiType(type);
8
+ return type.startsWith(`0x1::option::Option` + '<');
9
+ }
10
+ /**
11
+ * Abstraction of a value that may or may not be present. Implemented with a vector of size
12
+ * zero or one because Move bytecode does not have ADTs.
13
+ */
14
+ export class Option {
15
+ __StructClass = true;
16
+ static $typeName = `0x1::option::Option`;
17
+ static $numTypeParams = 1;
18
+ static $isPhantom = [false];
19
+ __inner = null; // for type checking in reified.ts
20
+ $typeName = Option.$typeName;
21
+ $fullTypeName;
22
+ $typeArgs;
23
+ $isPhantom = Option.$isPhantom;
24
+ vec;
25
+ constructor(typeArgs, fields) {
26
+ this.$fullTypeName = composeSuiType(Option.$typeName, ...typeArgs);
27
+ this.$typeArgs = typeArgs;
28
+ this.vec = fields.vec;
29
+ }
30
+ static reified(Element) {
31
+ const reifiedBcs = Option.bcs(toBcs(Element));
32
+ return {
33
+ get typeName() {
34
+ return Option.$typeName;
35
+ },
36
+ get fullTypeName() {
37
+ return composeSuiType(Option.$typeName, ...[extractType(Element)]);
38
+ },
39
+ get typeArgs() {
40
+ return [extractType(Element)];
41
+ },
42
+ isPhantom: Option.$isPhantom,
43
+ reifiedTypeArgs: [Element],
44
+ fromFields: (fields) => Option.fromFields(Element, fields),
45
+ fromFieldsWithTypes: (item) => Option.fromFieldsWithTypes(Element, item),
46
+ fromBcs: (data) => Option.fromFields(Element, reifiedBcs.parse(data)),
47
+ bcs: reifiedBcs,
48
+ fromJSONField: (field) => Option.fromJSONField(Element, field),
49
+ fromJSON: (json) => Option.fromJSON(Element, json),
50
+ fromCoreObject: (obj) => Option.fromCoreObject(Element, obj),
51
+ fetch: async (client, id) => Option.fetch(client, Element, id),
52
+ new: (fields) => {
53
+ return new Option([extractType(Element)], fields);
54
+ },
55
+ kind: 'StructClassReified',
56
+ };
57
+ }
58
+ static get r() {
59
+ return Option.reified;
60
+ }
61
+ static phantom(Element) {
62
+ return phantom(Option.reified(Element));
63
+ }
64
+ static get p() {
65
+ return Option.phantom;
66
+ }
67
+ static instantiateBcs() {
68
+ return (Element) => bcs.struct(`Option<${Element.name}>`, {
69
+ vec: bcs.vector(Element),
70
+ });
71
+ }
72
+ static cachedBcs = null;
73
+ static get bcs() {
74
+ if (!Option.cachedBcs) {
75
+ Option.cachedBcs = Option.instantiateBcs();
76
+ }
77
+ return Option.cachedBcs;
78
+ }
79
+ static fromFields(typeArg, fields) {
80
+ return Option.reified(typeArg).new({
81
+ vec: decodeFromFields(vector(typeArg), fields.vec),
82
+ });
83
+ }
84
+ static fromFieldsWithTypes(typeArg, item) {
85
+ if (!isOption(item.type)) {
86
+ throw new Error('not a Option type');
87
+ }
88
+ assertFieldsWithTypesArgsMatch(item, [typeArg]);
89
+ return Option.reified(typeArg).new({
90
+ vec: decodeFromFieldsWithTypes(vector(typeArg), item.fields.vec),
91
+ });
92
+ }
93
+ static fromBcs(typeArg, data) {
94
+ const typeArgs = [typeArg];
95
+ return Option.fromFields(typeArg, Option.bcs(toBcs(typeArg)).parse(data));
96
+ }
97
+ toJSONField() {
98
+ return {
99
+ vec: fieldToJSON(`vector<${this.$typeArgs[0]}>`, this.vec),
100
+ };
101
+ }
102
+ toJSON() {
103
+ return { $typeName: this.$typeName, $typeArgs: this.$typeArgs, ...this.toJSONField() };
104
+ }
105
+ static fromJSONField(typeArg, field) {
106
+ return Option.reified(typeArg).new({
107
+ vec: decodeFromJSONField(vector(typeArg), field.vec),
108
+ });
109
+ }
110
+ static fromJSON(typeArg, json) {
111
+ if (json.$typeName !== Option.$typeName) {
112
+ throw new Error(`not a Option json object: expected '${Option.$typeName}' but got '${json.$typeName}'`);
113
+ }
114
+ assertReifiedTypeArgsMatch(composeSuiType(Option.$typeName, ...[extractType(typeArg)]), json.$typeArgs, [typeArg]);
115
+ return Option.fromJSONField(typeArg, json);
116
+ }
117
+ static fromCoreObject(typeArg, obj) {
118
+ if (!isOption(obj.type)) {
119
+ throw new Error(`object at ${obj.objectId} is not a Option object`);
120
+ }
121
+ const gotTypeArgs = parseTypeName(obj.type).typeArgs;
122
+ if (gotTypeArgs.length !== 1) {
123
+ throw new Error(`type argument mismatch: expected 1 type arguments but got '${gotTypeArgs.length}'`);
124
+ }
125
+ for (let i = 0; i < 1; i++) {
126
+ const gotTypeArg = compressSuiType(gotTypeArgs[i]);
127
+ const expectedTypeArg = compressSuiType(extractType([typeArg][i]));
128
+ if (gotTypeArg !== expectedTypeArg) {
129
+ throw new Error(`type argument mismatch at position ${i}: expected '${expectedTypeArg}' but got '${gotTypeArg}'`);
130
+ }
131
+ }
132
+ return Option.fromBcs(typeArg, obj.content);
133
+ }
134
+ static async fetch(client, typeArg, id) {
135
+ const { object } = await client.core.getObject({
136
+ objectId: id,
137
+ include: { content: true },
138
+ });
139
+ if (!isOption(object.type)) {
140
+ throw new Error(`object at id ${id} is not a Option object`);
141
+ }
142
+ const gotTypeArgs = parseTypeName(object.type).typeArgs;
143
+ if (gotTypeArgs.length !== 1) {
144
+ throw new Error(`type argument mismatch: expected 1 type arguments but got '${gotTypeArgs.length}'`);
145
+ }
146
+ for (let i = 0; i < 1; i++) {
147
+ const gotTypeArg = compressSuiType(gotTypeArgs[i]);
148
+ const expectedTypeArg = compressSuiType(extractType([typeArg][i]));
149
+ if (gotTypeArg !== expectedTypeArg) {
150
+ throw new Error(`type argument mismatch at position ${i}: expected '${expectedTypeArg}' but got '${gotTypeArg}'`);
151
+ }
152
+ }
153
+ return Option.fromBcs(typeArg, object.content);
154
+ }
155
+ }
@@ -0,0 +1,65 @@
1
+ /** Functionality for converting Move types into values. Use with care! */
2
+ import type { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';
3
+ import { PhantomReified, Reified, StructClass, ToField, ToTypeStr } from '../../../_framework/reified';
4
+ import { FieldsWithTypes } from '../../../_framework/util';
5
+ import { String } from '../ascii/structs';
6
+ export declare function isTypeName(type: string): boolean;
7
+ export interface TypeNameFields {
8
+ /**
9
+ * String representation of the type. All types are represented
10
+ * using their source syntax:
11
+ * "u8", "u64", "bool", "address", "vector", and so on for primitive types.
12
+ * Struct types are represented as fully qualified type names; e.g.
13
+ * `00000000000000000000000000000001::string::String` or
14
+ * `0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>>`
15
+ * Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
16
+ */
17
+ name: ToField<String>;
18
+ }
19
+ export type TypeNameReified = Reified<TypeName, TypeNameFields>;
20
+ export type TypeNameJSONField = {
21
+ name: string;
22
+ };
23
+ export type TypeNameJSON = {
24
+ $typeName: typeof TypeName.$typeName;
25
+ $typeArgs: [];
26
+ } & TypeNameJSONField;
27
+ export declare class TypeName implements StructClass {
28
+ __StructClass: true;
29
+ static readonly $typeName: `0x1::type_name::TypeName`;
30
+ static readonly $numTypeParams = 0;
31
+ static readonly $isPhantom: readonly [];
32
+ readonly $typeName: typeof TypeName.$typeName;
33
+ readonly $fullTypeName: `0x1::type_name::TypeName`;
34
+ readonly $typeArgs: [];
35
+ readonly $isPhantom: typeof TypeName.$isPhantom;
36
+ /**
37
+ * String representation of the type. All types are represented
38
+ * using their source syntax:
39
+ * "u8", "u64", "bool", "address", "vector", and so on for primitive types.
40
+ * Struct types are represented as fully qualified type names; e.g.
41
+ * `00000000000000000000000000000001::string::String` or
42
+ * `0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>>`
43
+ * Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
44
+ */
45
+ readonly name: ToField<String>;
46
+ private constructor();
47
+ static reified(): TypeNameReified;
48
+ static get r(): TypeNameReified;
49
+ static phantom(): PhantomReified<ToTypeStr<TypeName>>;
50
+ static get p(): PhantomReified<ToTypeStr<TypeName>>;
51
+ private static instantiateBcs;
52
+ private static cachedBcs;
53
+ static get bcs(): ReturnType<typeof TypeName.instantiateBcs>;
54
+ static fromFields(fields: Record<string, any>): TypeName;
55
+ static fromFieldsWithTypes(item: FieldsWithTypes): TypeName;
56
+ static fromBcs(data: Uint8Array): TypeName;
57
+ toJSONField(): TypeNameJSONField;
58
+ toJSON(): TypeNameJSON;
59
+ static fromJSONField(field: any): TypeName;
60
+ static fromJSON(json: Record<string, any>): TypeName;
61
+ static fromCoreObject(obj: SuiClientTypes.Object<{
62
+ content: true;
63
+ }>): TypeName;
64
+ static fetch(client: ClientWithCoreApi, id: string): Promise<TypeName>;
65
+ }
@@ -1,22 +1,31 @@
1
- import { decodeFromFields, decodeFromFieldsWithTypes, decodeFromJSONField, phantom, } from "../../../../_framework/reified.js";
2
- import { composeSuiType, compressSuiType, } from "../../../../_framework/util.js";
3
- import { String } from "../ascii/structs.js";
4
- import { PKG_V13 } from "../index.js";
5
- import { bcs } from "@mysten/sui/bcs";
1
+ /** Functionality for converting Move types into values. Use with care! */
2
+ import { bcs } from '@mysten/sui/bcs';
3
+ import { decodeFromFields, decodeFromFieldsWithTypes, decodeFromJSONField, phantom, } from '../../../_framework/reified.js';
4
+ import { composeSuiType, compressSuiType } from '../../../_framework/util.js';
5
+ import { String } from '../ascii/structs.js';
6
6
  /* ============================== TypeName =============================== */
7
7
  export function isTypeName(type) {
8
8
  type = compressSuiType(type);
9
- return type === `${PKG_V13}::type_name::TypeName`;
9
+ return type === `0x1::type_name::TypeName`;
10
10
  }
11
11
  export class TypeName {
12
12
  __StructClass = true;
13
- static $typeName = `${PKG_V13}::type_name::TypeName`;
13
+ static $typeName = `0x1::type_name::TypeName`;
14
14
  static $numTypeParams = 0;
15
15
  static $isPhantom = [];
16
16
  $typeName = TypeName.$typeName;
17
17
  $fullTypeName;
18
18
  $typeArgs;
19
19
  $isPhantom = TypeName.$isPhantom;
20
+ /**
21
+ * String representation of the type. All types are represented
22
+ * using their source syntax:
23
+ * "u8", "u64", "bool", "address", "vector", and so on for primitive types.
24
+ * Struct types are represented as fully qualified type names; e.g.
25
+ * `00000000000000000000000000000001::string::String` or
26
+ * `0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>>`
27
+ * Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
28
+ */
20
29
  name;
21
30
  constructor(typeArgs, fields) {
22
31
  this.$fullTypeName = composeSuiType(TypeName.$typeName, ...typeArgs);
@@ -24,23 +33,29 @@ export class TypeName {
24
33
  this.name = fields.name;
25
34
  }
26
35
  static reified() {
36
+ const reifiedBcs = TypeName.bcs;
27
37
  return {
28
- typeName: TypeName.$typeName,
29
- fullTypeName: composeSuiType(TypeName.$typeName, ...[]),
38
+ get typeName() {
39
+ return TypeName.$typeName;
40
+ },
41
+ get fullTypeName() {
42
+ return composeSuiType(TypeName.$typeName, ...[]);
43
+ },
30
44
  typeArgs: [],
31
45
  isPhantom: TypeName.$isPhantom,
32
46
  reifiedTypeArgs: [],
33
47
  fromFields: (fields) => TypeName.fromFields(fields),
34
48
  fromFieldsWithTypes: (item) => TypeName.fromFieldsWithTypes(item),
35
- fromBcs: (data) => TypeName.fromBcs(data),
36
- bcs: TypeName.bcs,
49
+ fromBcs: (data) => TypeName.fromFields(reifiedBcs.parse(data)),
50
+ bcs: reifiedBcs,
37
51
  fromJSONField: (field) => TypeName.fromJSONField(field),
38
52
  fromJSON: (json) => TypeName.fromJSON(json),
53
+ fromCoreObject: (obj) => TypeName.fromCoreObject(obj),
39
54
  fetch: async (client, id) => TypeName.fetch(client, id),
40
55
  new: (fields) => {
41
56
  return new TypeName([], fields);
42
57
  },
43
- kind: "StructClassReified",
58
+ kind: 'StructClassReified',
44
59
  };
45
60
  }
46
61
  static get r() {
@@ -52,11 +67,18 @@ export class TypeName {
52
67
  static get p() {
53
68
  return TypeName.phantom();
54
69
  }
55
- static get bcs() {
56
- return bcs.struct("TypeName", {
70
+ static instantiateBcs() {
71
+ return bcs.struct('TypeName', {
57
72
  name: String.bcs,
58
73
  });
59
74
  }
75
+ static cachedBcs = null;
76
+ static get bcs() {
77
+ if (!TypeName.cachedBcs) {
78
+ TypeName.cachedBcs = TypeName.instantiateBcs();
79
+ }
80
+ return TypeName.cachedBcs;
81
+ }
60
82
  static fromFields(fields) {
61
83
  return TypeName.reified().new({
62
84
  name: decodeFromFields(String.reified(), fields.name),
@@ -64,7 +86,7 @@ export class TypeName {
64
86
  }
65
87
  static fromFieldsWithTypes(item) {
66
88
  if (!isTypeName(item.type)) {
67
- throw new Error("not a TypeName type");
89
+ throw new Error('not a TypeName type');
68
90
  }
69
91
  return TypeName.reified().new({
70
92
  name: decodeFromFieldsWithTypes(String.reified(), item.fields.name),
@@ -79,11 +101,7 @@ export class TypeName {
79
101
  };
80
102
  }
81
103
  toJSON() {
82
- return {
83
- $typeName: this.$typeName,
84
- $typeArgs: this.$typeArgs,
85
- ...this.toJSONField(),
86
- };
104
+ return { $typeName: this.$typeName, $typeArgs: this.$typeArgs, ...this.toJSONField() };
87
105
  }
88
106
  static fromJSONField(field) {
89
107
  return TypeName.reified().new({
@@ -92,12 +110,21 @@ export class TypeName {
92
110
  }
93
111
  static fromJSON(json) {
94
112
  if (json.$typeName !== TypeName.$typeName) {
95
- throw new Error("not a WithTwoGenerics json object");
113
+ throw new Error(`not a TypeName json object: expected '${TypeName.$typeName}' but got '${json.$typeName}'`);
96
114
  }
97
115
  return TypeName.fromJSONField(json);
98
116
  }
117
+ static fromCoreObject(obj) {
118
+ if (!isTypeName(obj.type)) {
119
+ throw new Error(`object at ${obj.objectId} is not a TypeName object`);
120
+ }
121
+ return TypeName.fromBcs(obj.content);
122
+ }
99
123
  static async fetch(client, id) {
100
- const { object } = await client.getObject({ objectId: id, include: { content: true } });
124
+ const { object } = await client.core.getObject({
125
+ objectId: id,
126
+ include: { content: true },
127
+ });
101
128
  if (!isTypeName(object.type)) {
102
129
  throw new Error(`object at id ${id} is not a TypeName object`);
103
130
  }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * A bag is a heterogeneous map-like collection. The collection is similar to `sui::table` in that
3
+ * its keys and values are not stored within the `Bag` value, but instead are stored using Sui's
4
+ * object system. The `Bag` struct acts only as a handle into the object system to retrieve those
5
+ * keys and values.
6
+ * Note that this means that `Bag` values with exactly the same key-value mapping will not be
7
+ * equal, with `==`, at runtime. For example
8
+ * ```
9
+ * let bag1 = bag::new();
10
+ * let bag2 = bag::new();
11
+ * bag::add(&mut bag1, 0, false);
12
+ * bag::add(&mut bag1, 1, true);
13
+ * bag::add(&mut bag2, 0, false);
14
+ * bag::add(&mut bag2, 1, true);
15
+ * // bag1 does not equal bag2, despite having the same entries
16
+ * assert!(&bag1 != &bag2);
17
+ * ```
18
+ * At it's core, `sui::bag` is a wrapper around `UID` that allows for access to
19
+ * `sui::dynamic_field` while preventing accidentally stranding field values. A `UID` can be
20
+ * deleted, even if it has dynamic fields associated with it, but a bag, on the other hand, must be
21
+ * empty to be destroyed.
22
+ */
23
+ import type { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';
24
+ import { PhantomReified, Reified, StructClass, ToField, ToTypeStr } from '../../../_framework/reified';
25
+ import { FieldsWithTypes } from '../../../_framework/util';
26
+ import { UID } from '../object/structs';
27
+ export declare function isBag(type: string): boolean;
28
+ export interface BagFields {
29
+ /** the ID of this bag */
30
+ id: ToField<UID>;
31
+ /** the number of key-value pairs in the bag */
32
+ size: ToField<'u64'>;
33
+ }
34
+ export type BagReified = Reified<Bag, BagFields>;
35
+ export type BagJSONField = {
36
+ id: string;
37
+ size: string;
38
+ };
39
+ export type BagJSON = {
40
+ $typeName: typeof Bag.$typeName;
41
+ $typeArgs: [];
42
+ } & BagJSONField;
43
+ export declare class Bag implements StructClass {
44
+ __StructClass: true;
45
+ static readonly $typeName: `0x2::bag::Bag`;
46
+ static readonly $numTypeParams = 0;
47
+ static readonly $isPhantom: readonly [];
48
+ readonly $typeName: typeof Bag.$typeName;
49
+ readonly $fullTypeName: `0x2::bag::Bag`;
50
+ readonly $typeArgs: [];
51
+ readonly $isPhantom: typeof Bag.$isPhantom;
52
+ /** the ID of this bag */
53
+ readonly id: ToField<UID>;
54
+ /** the number of key-value pairs in the bag */
55
+ readonly size: ToField<'u64'>;
56
+ private constructor();
57
+ static reified(): BagReified;
58
+ static get r(): BagReified;
59
+ static phantom(): PhantomReified<ToTypeStr<Bag>>;
60
+ static get p(): PhantomReified<ToTypeStr<Bag>>;
61
+ private static instantiateBcs;
62
+ private static cachedBcs;
63
+ static get bcs(): ReturnType<typeof Bag.instantiateBcs>;
64
+ static fromFields(fields: Record<string, any>): Bag;
65
+ static fromFieldsWithTypes(item: FieldsWithTypes): Bag;
66
+ static fromBcs(data: Uint8Array): Bag;
67
+ toJSONField(): BagJSONField;
68
+ toJSON(): BagJSON;
69
+ static fromJSONField(field: any): Bag;
70
+ static fromJSON(json: Record<string, any>): Bag;
71
+ static fromCoreObject(obj: SuiClientTypes.Object<{
72
+ content: true;
73
+ }>): Bag;
74
+ static fetch(client: ClientWithCoreApi, id: string): Promise<Bag>;
75
+ }
@@ -0,0 +1,154 @@
1
+ /**
2
+ * A bag is a heterogeneous map-like collection. The collection is similar to `sui::table` in that
3
+ * its keys and values are not stored within the `Bag` value, but instead are stored using Sui's
4
+ * object system. The `Bag` struct acts only as a handle into the object system to retrieve those
5
+ * keys and values.
6
+ * Note that this means that `Bag` values with exactly the same key-value mapping will not be
7
+ * equal, with `==`, at runtime. For example
8
+ * ```
9
+ * let bag1 = bag::new();
10
+ * let bag2 = bag::new();
11
+ * bag::add(&mut bag1, 0, false);
12
+ * bag::add(&mut bag1, 1, true);
13
+ * bag::add(&mut bag2, 0, false);
14
+ * bag::add(&mut bag2, 1, true);
15
+ * // bag1 does not equal bag2, despite having the same entries
16
+ * assert!(&bag1 != &bag2);
17
+ * ```
18
+ * At it's core, `sui::bag` is a wrapper around `UID` that allows for access to
19
+ * `sui::dynamic_field` while preventing accidentally stranding field values. A `UID` can be
20
+ * deleted, even if it has dynamic fields associated with it, but a bag, on the other hand, must be
21
+ * empty to be destroyed.
22
+ */
23
+ import { bcs } from '@mysten/sui/bcs';
24
+ import { decodeFromFields, decodeFromFieldsWithTypes, decodeFromJSONField, phantom, } from '../../../_framework/reified.js';
25
+ import { composeSuiType, compressSuiType } from '../../../_framework/util.js';
26
+ import { UID } from '../object/structs.js';
27
+ /* ============================== Bag =============================== */
28
+ export function isBag(type) {
29
+ type = compressSuiType(type);
30
+ return type === `0x2::bag::Bag`;
31
+ }
32
+ export class Bag {
33
+ __StructClass = true;
34
+ static $typeName = `0x2::bag::Bag`;
35
+ static $numTypeParams = 0;
36
+ static $isPhantom = [];
37
+ $typeName = Bag.$typeName;
38
+ $fullTypeName;
39
+ $typeArgs;
40
+ $isPhantom = Bag.$isPhantom;
41
+ /** the ID of this bag */
42
+ id;
43
+ /** the number of key-value pairs in the bag */
44
+ size;
45
+ constructor(typeArgs, fields) {
46
+ this.$fullTypeName = composeSuiType(Bag.$typeName, ...typeArgs);
47
+ this.$typeArgs = typeArgs;
48
+ this.id = fields.id;
49
+ this.size = fields.size;
50
+ }
51
+ static reified() {
52
+ const reifiedBcs = Bag.bcs;
53
+ return {
54
+ get typeName() {
55
+ return Bag.$typeName;
56
+ },
57
+ get fullTypeName() {
58
+ return composeSuiType(Bag.$typeName, ...[]);
59
+ },
60
+ typeArgs: [],
61
+ isPhantom: Bag.$isPhantom,
62
+ reifiedTypeArgs: [],
63
+ fromFields: (fields) => Bag.fromFields(fields),
64
+ fromFieldsWithTypes: (item) => Bag.fromFieldsWithTypes(item),
65
+ fromBcs: (data) => Bag.fromFields(reifiedBcs.parse(data)),
66
+ bcs: reifiedBcs,
67
+ fromJSONField: (field) => Bag.fromJSONField(field),
68
+ fromJSON: (json) => Bag.fromJSON(json),
69
+ fromCoreObject: (obj) => Bag.fromCoreObject(obj),
70
+ fetch: async (client, id) => Bag.fetch(client, id),
71
+ new: (fields) => {
72
+ return new Bag([], fields);
73
+ },
74
+ kind: 'StructClassReified',
75
+ };
76
+ }
77
+ static get r() {
78
+ return Bag.reified();
79
+ }
80
+ static phantom() {
81
+ return phantom(Bag.reified());
82
+ }
83
+ static get p() {
84
+ return Bag.phantom();
85
+ }
86
+ static instantiateBcs() {
87
+ return bcs.struct('Bag', {
88
+ id: UID.bcs,
89
+ size: bcs.u64(),
90
+ });
91
+ }
92
+ static cachedBcs = null;
93
+ static get bcs() {
94
+ if (!Bag.cachedBcs) {
95
+ Bag.cachedBcs = Bag.instantiateBcs();
96
+ }
97
+ return Bag.cachedBcs;
98
+ }
99
+ static fromFields(fields) {
100
+ return Bag.reified().new({
101
+ id: decodeFromFields(UID.reified(), fields.id),
102
+ size: decodeFromFields('u64', fields.size),
103
+ });
104
+ }
105
+ static fromFieldsWithTypes(item) {
106
+ if (!isBag(item.type)) {
107
+ throw new Error('not a Bag type');
108
+ }
109
+ return Bag.reified().new({
110
+ id: decodeFromFieldsWithTypes(UID.reified(), item.fields.id),
111
+ size: decodeFromFieldsWithTypes('u64', item.fields.size),
112
+ });
113
+ }
114
+ static fromBcs(data) {
115
+ return Bag.fromFields(Bag.bcs.parse(data));
116
+ }
117
+ toJSONField() {
118
+ return {
119
+ id: this.id,
120
+ size: this.size.toString(),
121
+ };
122
+ }
123
+ toJSON() {
124
+ return { $typeName: this.$typeName, $typeArgs: this.$typeArgs, ...this.toJSONField() };
125
+ }
126
+ static fromJSONField(field) {
127
+ return Bag.reified().new({
128
+ id: decodeFromJSONField(UID.reified(), field.id),
129
+ size: decodeFromJSONField('u64', field.size),
130
+ });
131
+ }
132
+ static fromJSON(json) {
133
+ if (json.$typeName !== Bag.$typeName) {
134
+ throw new Error(`not a Bag json object: expected '${Bag.$typeName}' but got '${json.$typeName}'`);
135
+ }
136
+ return Bag.fromJSONField(json);
137
+ }
138
+ static fromCoreObject(obj) {
139
+ if (!isBag(obj.type)) {
140
+ throw new Error(`object at ${obj.objectId} is not a Bag object`);
141
+ }
142
+ return Bag.fromBcs(obj.content);
143
+ }
144
+ static async fetch(client, id) {
145
+ const { object } = await client.core.getObject({
146
+ objectId: id,
147
+ include: { content: true },
148
+ });
149
+ if (!isBag(object.type)) {
150
+ throw new Error(`object at id ${id} is not a Bag object`);
151
+ }
152
+ return Bag.fromBcs(object.content);
153
+ }
154
+ }