@seedprotocol/sdk 0.3.0 → 0.3.1

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 (200) hide show
  1. package/dist/{ArweaveClient-CmJYeuq1.js → ArweaveClient-C0g0es2v.js} +2 -2
  2. package/dist/{ArweaveClient-EXXrXUzz.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
  3. package/dist/{ArweaveClient-EXXrXUzz.js → ArweaveClient-CzkBgiZY.js} +2 -2
  4. package/dist/{ArweaveClient-CmJYeuq1.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
  5. package/dist/{Db-CESKJH-G.js → Db-C4oog-0n.js} +3 -3
  6. package/dist/{Db-CESKJH-G.js.map → Db-C4oog-0n.js.map} +1 -1
  7. package/dist/{Db-DiTPSbQ8.js → Db-VPouegkr.js} +3 -3
  8. package/dist/{Db-DiTPSbQ8.js.map → Db-VPouegkr.js.map} +1 -1
  9. package/dist/{EasClient-BesHK2bO.js → EasClient-B9a3NsTi.js} +2 -2
  10. package/dist/{EasClient-BesHK2bO.js.map → EasClient-B9a3NsTi.js.map} +1 -1
  11. package/dist/{EasClient-Cop6WtWl.js → EasClient-DQ93i2ci.js} +2 -2
  12. package/dist/{EasClient-Cop6WtWl.js.map → EasClient-DQ93i2ci.js.map} +1 -1
  13. package/dist/{FileManager-DTzkncLh.js → FileManager-_ErxVuwb.js} +8 -5
  14. package/dist/FileManager-_ErxVuwb.js.map +1 -0
  15. package/dist/{Item-B5Tz-Y7t.js → Item-D9HFPxsK.js} +3 -3
  16. package/dist/{Item-B5Tz-Y7t.js.map → Item-D9HFPxsK.js.map} +1 -1
  17. package/dist/{ItemProperty-DBV0Iib_.js → ItemProperty-BdtvKrEy.js} +3 -3
  18. package/dist/{ItemProperty-DBV0Iib_.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
  19. package/dist/{QueryClient-C7a0NHCt.js → QueryClient-Bmnv_moN.js} +2 -2
  20. package/dist/{QueryClient-C7a0NHCt.js.map → QueryClient-Bmnv_moN.js.map} +1 -1
  21. package/dist/{QueryClient-BnwbHtqe.js → QueryClient-hzOtFe99.js} +2 -2
  22. package/dist/{QueryClient-BnwbHtqe.js.map → QueryClient-hzOtFe99.js.map} +1 -1
  23. package/dist/bin.js +5 -5
  24. package/dist/bin.js.map +1 -1
  25. package/dist/{constants-CWg2B3Au.js → constants-Bhl5dZpN.js} +4 -1
  26. package/dist/constants-Bhl5dZpN.js.map +1 -0
  27. package/dist/{index-CQ9FS10O.js → index-BAtjd9W-.js} +3 -3
  28. package/dist/index-BAtjd9W-.js.map +1 -0
  29. package/dist/{index-BHGJ-Es9.js → index-BSR5hv7Q.js} +59 -27
  30. package/dist/index-BSR5hv7Q.js.map +1 -0
  31. package/dist/main.js +2 -2
  32. package/dist/{seed.schema.config-DDzfQguf.js → seed.schema.config-BxJYabim.js} +3 -3
  33. package/dist/{seed.schema.config-DDzfQguf.js.map → seed.schema.config-BxJYabim.js.map} +1 -1
  34. package/dist/src/AppStateSchema.ts +10 -0
  35. package/dist/src/ArweaveClient.ts +13 -0
  36. package/dist/src/Attestation.ts +21 -0
  37. package/dist/src/BaseArweaveClient.ts +13 -0
  38. package/dist/src/BaseClientManager.ts +35 -0
  39. package/dist/src/BaseDb.ts +35 -0
  40. package/dist/src/BaseEasClient.ts +14 -0
  41. package/dist/src/BaseFileManager.ts +60 -0
  42. package/dist/src/BaseItem.ts +413 -0
  43. package/dist/src/BaseItemProperty.ts +416 -0
  44. package/dist/src/BaseQueryClient.ts +13 -0
  45. package/dist/src/ClientManager.ts +63 -0
  46. package/dist/src/ConfigSchema.ts +15 -0
  47. package/dist/src/Db.ts +82 -0
  48. package/dist/src/EasClient.ts +13 -0
  49. package/dist/src/FileDownloader.ts +63 -0
  50. package/dist/src/FileManager.ts +59 -0
  51. package/dist/src/IDb.ts +7 -0
  52. package/dist/src/IEasClient.ts +2 -0
  53. package/dist/src/IItem.ts +26 -0
  54. package/dist/src/IItemProperty.ts +28 -0
  55. package/dist/src/IQueryClient.ts +11 -0
  56. package/dist/src/ImageResizer.ts +81 -0
  57. package/dist/src/Item.ts +12 -0
  58. package/dist/src/ItemProperty.ts +9 -0
  59. package/dist/src/MetadataSchema.ts +28 -0
  60. package/dist/src/ModelSchema.ts +46 -0
  61. package/dist/src/ModelUidSchema.ts +16 -0
  62. package/dist/src/PropertyUidSchema.ts +16 -0
  63. package/dist/src/QueryClient.ts +26 -0
  64. package/dist/src/Schema.ts +17 -0
  65. package/dist/src/SeedProtocolSdk.d.ts +19 -0
  66. package/dist/src/SeedSchema.ts +29 -0
  67. package/dist/src/VersionSchema.ts +16 -0
  68. package/dist/src/actors.ts +295 -0
  69. package/dist/src/addModelsToDb.ts +152 -0
  70. package/dist/src/allItems.ts +31 -0
  71. package/dist/src/analyzeInput.ts +144 -0
  72. package/dist/src/browser.app.db.config.ts +27 -0
  73. package/dist/src/browser.ts +30 -0
  74. package/dist/src/checkStatus.ts +57 -0
  75. package/dist/src/clientManagerMachine.ts +59 -0
  76. package/dist/src/configureFs.ts +98 -0
  77. package/dist/src/connectToDb.ts +41 -0
  78. package/dist/src/connectionManager.ts +67 -0
  79. package/dist/src/constants.ts +125 -0
  80. package/dist/src/content-hash.ts +30 -0
  81. package/dist/src/create.ts +39 -0
  82. package/dist/src/createMetadata.ts +77 -0
  83. package/dist/src/createNewItem.ts +61 -0
  84. package/dist/src/createPublishAttempt.ts +16 -0
  85. package/dist/src/createSeed.ts +31 -0
  86. package/dist/src/createSeeds.ts +24 -0
  87. package/dist/src/createVersion.ts +33 -0
  88. package/dist/src/dbMachine.ts +182 -0
  89. package/dist/src/deleteItem.ts +19 -0
  90. package/dist/src/download.ts +208 -0
  91. package/dist/src/drizzle.ts +78 -0
  92. package/dist/src/eas.ts +40 -0
  93. package/dist/src/environment.ts +35 -0
  94. package/dist/src/eventBus.ts +5 -0
  95. package/dist/src/events.ts +14 -0
  96. package/dist/src/fetchDataFromEas.ts +94 -0
  97. package/dist/src/fetchDbData.ts +19 -0
  98. package/dist/src/fetchRelatedItems.ts +184 -0
  99. package/dist/src/fetchSeeds.ts +45 -0
  100. package/dist/src/fetchVersions.ts +49 -0
  101. package/dist/src/files.ts +81 -0
  102. package/dist/src/filesDownload.ts +326 -0
  103. package/dist/src/fragment-masking.ts +87 -0
  104. package/dist/src/getExistingItem.ts +60 -0
  105. package/dist/src/getItem.ts +32 -0
  106. package/dist/src/getItemData.ts +99 -0
  107. package/dist/src/getItemProperties.ts +56 -0
  108. package/dist/src/getItemProperty.ts +34 -0
  109. package/dist/src/getItems.ts +70 -0
  110. package/dist/src/getMetadata.ts +40 -0
  111. package/dist/src/getModelSchemas.ts +89 -0
  112. package/dist/src/getPropertyData.ts +50 -0
  113. package/dist/src/getPublishPayload.ts +355 -0
  114. package/dist/src/getPublishUploads.ts +207 -0
  115. package/dist/src/getRelationValueData.ts +27 -0
  116. package/dist/src/getSchemaForItemProperty.ts +68 -0
  117. package/dist/src/getSchemaForModel.ts +42 -0
  118. package/dist/src/getSchemaUidForModel.ts +32 -0
  119. package/dist/src/getSeedData.ts +34 -0
  120. package/dist/src/getSegmentedItemProperties.ts +65 -0
  121. package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
  122. package/dist/src/getVersionData.ts +46 -0
  123. package/dist/src/getVersionsForVersionUids.ts +39 -0
  124. package/dist/src/globalMachine.ts +280 -0
  125. package/dist/src/gql.ts +118 -0
  126. package/dist/src/graphql.ts +3209 -0
  127. package/dist/src/helpers.ts +273 -0
  128. package/dist/src/hydrateExistingItem.ts +137 -0
  129. package/dist/src/hydrateFromDb.ts +270 -0
  130. package/dist/src/hydrateNewItem.ts +34 -0
  131. package/dist/src/imageResize.ts +507 -0
  132. package/dist/src/index.d.ts +5 -0
  133. package/dist/src/index.ts +4 -0
  134. package/dist/src/initialize.ts +124 -0
  135. package/dist/src/internalMachine.ts +241 -0
  136. package/dist/src/itemMachineAll.ts +157 -0
  137. package/dist/src/itemMachineSingle.ts +158 -0
  138. package/dist/src/loadAppDb.ts +44 -0
  139. package/dist/src/machines.ts +66 -0
  140. package/dist/src/metadataLatest.ts +34 -0
  141. package/dist/src/migrate.ts +57 -0
  142. package/dist/src/model.ts +72 -0
  143. package/dist/src/modelClass.ts +19 -0
  144. package/dist/src/node.app.db.config.ts +41 -0
  145. package/dist/src/prepareDb.ts +29 -0
  146. package/dist/src/preparePublishRequestData.ts +81 -0
  147. package/dist/src/processItems.ts +74 -0
  148. package/dist/src/property.ts +156 -0
  149. package/dist/src/propertyMachine.ts +202 -0
  150. package/dist/src/publish.ts +28 -0
  151. package/dist/src/publishMachine.ts +77 -0
  152. package/dist/src/queries.ts +13 -0
  153. package/dist/src/recoverDeletedItem.ts +14 -0
  154. package/dist/src/reload.ts +33 -0
  155. package/dist/src/request.ts +45 -0
  156. package/dist/src/requestAll.ts +157 -0
  157. package/dist/src/resolveRelatedValue.ts +367 -0
  158. package/dist/src/resolveRemoteStorage.ts +88 -0
  159. package/dist/src/saveAppState.ts +50 -0
  160. package/dist/src/saveConfig.ts +106 -0
  161. package/dist/src/saveDataToDb.ts +145 -0
  162. package/dist/src/saveImageSrc.ts +242 -0
  163. package/dist/src/saveItemStorage.ts +156 -0
  164. package/dist/src/saveMetadata.ts +18 -0
  165. package/dist/src/savePublishService.ts +30 -0
  166. package/dist/src/saveRelation.ts +112 -0
  167. package/dist/src/seed.schema.config.ts +25 -0
  168. package/dist/src/seed.ts +10 -0
  169. package/dist/src/seedProtocol.ts +17 -0
  170. package/dist/src/services.ts +365 -0
  171. package/dist/src/setAddresses.ts +16 -0
  172. package/dist/src/sqlWasmClient.ts +88 -0
  173. package/dist/src/syncDbWithEas.ts +628 -0
  174. package/dist/src/trash.ts +29 -0
  175. package/dist/src/ts-to-proto.ts +101 -0
  176. package/dist/src/types.ts +12 -0
  177. package/dist/src/updateItemPropertyValue.ts +243 -0
  178. package/dist/src/updateMachineContext.ts +21 -0
  179. package/dist/src/updateMetadata.ts +59 -0
  180. package/dist/src/upload.ts +86 -0
  181. package/dist/src/validate.ts +45 -0
  182. package/dist/src/validateInput.ts +43 -0
  183. package/dist/src/validateItemData.ts +20 -0
  184. package/dist/src/versionData.ts +24 -0
  185. package/dist/src/waitForDb.ts +25 -0
  186. package/dist/src/waitForFiles.ts +41 -0
  187. package/dist/src/wasm.d.ts +8300 -0
  188. package/dist/types/scripts/bin.d.ts.map +1 -1
  189. package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
  190. package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
  191. package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
  192. package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
  193. package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
  194. package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
  195. package/package.json +3 -3
  196. package/dist/FileManager-DTzkncLh.js.map +0 -1
  197. package/dist/constants-CWg2B3Au.js.map +0 -1
  198. package/dist/index-BHGJ-Es9.js.map +0 -1
  199. package/dist/index-CQ9FS10O.js.map +0 -1
  200. package/dist/package.json +0 -189
@@ -0,0 +1,70 @@
1
+ import { ItemData } from '@/types'
2
+ import { and, eq, gt, isNotNull, isNull, or, SQL, sql } from 'drizzle-orm'
3
+ import { seeds } from '@/seedSchema'
4
+ import { BaseDb } from '@/db/Db/BaseDb'
5
+ import { getVersionData } from './subqueries/versionData'
6
+
7
+ type GetItemsDataProps = {
8
+ modelName?: string
9
+ deleted?: boolean
10
+ }
11
+
12
+ type GetItemsData = (props: GetItemsDataProps) => Promise<ItemData[]>
13
+
14
+ export const getItemsData: GetItemsData = async ({
15
+ modelName,
16
+ deleted,
17
+ }): Promise<ItemData[]> => {
18
+ const appDb = BaseDb.getAppDb()
19
+
20
+ const conditions: SQL[] = []
21
+
22
+ if (modelName) {
23
+ conditions.push(eq(seeds.type, modelName.toLowerCase()))
24
+ }
25
+
26
+ if (deleted) {
27
+ conditions.push(
28
+ or(
29
+ isNotNull(seeds._markedForDeletion),
30
+ eq(seeds._markedForDeletion, 1),
31
+ ) as SQL,
32
+ )
33
+ }
34
+
35
+ if (!deleted) {
36
+ conditions.push(
37
+ or(
38
+ isNull(seeds._markedForDeletion),
39
+ eq(seeds._markedForDeletion, 0),
40
+ ) as SQL,
41
+ )
42
+ }
43
+
44
+ const versionData = getVersionData()
45
+
46
+ let query = appDb
47
+ .with(versionData)
48
+ .select({
49
+ seedLocalId: seeds.localId,
50
+ seedUid: seeds.uid,
51
+ schemaUid: seeds.schemaUid,
52
+ modelName: sql<string>`${modelName}`,
53
+ attestationCreatedAt: seeds.attestationCreatedAt,
54
+ versionsCount: versionData.versionsCount,
55
+ lastVersionPublishedAt: versionData.lastVersionPublishedAt,
56
+ lastLocalUpdateAt: versionData.lastLocalUpdateAt,
57
+ latestVersionUid: versionData.latestVersionUid,
58
+ latestVersionLocalId: versionData.latestVersionLocalId,
59
+ createdAt: seeds.createdAt,
60
+ })
61
+ .from(seeds)
62
+ .leftJoin(versionData, eq(seeds.localId, versionData.seedLocalId))
63
+ .where(and(gt(versionData.versionsCount, 0), ...conditions))
64
+ .orderBy(sql.raw('COALESCE(attestation_created_at, created_at) DESC'))
65
+ .groupBy(seeds.localId)
66
+
67
+ const itemsData = (await query) as ItemData[]
68
+
69
+ return itemsData
70
+ }
@@ -0,0 +1,40 @@
1
+ import { BaseDb } from '@/db/Db/BaseDb'
2
+ import { and, eq, SQL } from 'drizzle-orm'
3
+ import { metadata, MetadataType } from '@/seedSchema'
4
+
5
+ type GetMetadataProps =
6
+ | {
7
+ storageTransactionId?: string
8
+ }
9
+ | undefined
10
+
11
+ type GetMetadata = (
12
+ props: GetMetadataProps,
13
+ ) => Promise<MetadataType | undefined>
14
+
15
+ export const getMetadata: GetMetadata = async (props) => {
16
+ const appDb = BaseDb.getAppDb()
17
+
18
+ let storageTransactionId: string | undefined
19
+ if (props) {
20
+ storageTransactionId = props.storageTransactionId
21
+ }
22
+
23
+ const whereClauses: SQL[] = []
24
+
25
+ if (storageTransactionId) {
26
+ whereClauses.push(eq(metadata.propertyName, 'storageTransactionId'))
27
+ whereClauses.push(eq(metadata.propertyValue, storageTransactionId))
28
+ }
29
+
30
+ const metadataRows = await appDb
31
+ .select()
32
+ .from(metadata)
33
+ .where(and(...whereClauses))
34
+
35
+ if (!metadataRows || metadataRows.length === 0) {
36
+ return
37
+ }
38
+
39
+ return metadataRows[0]
40
+ }
@@ -0,0 +1,89 @@
1
+ import { getModels } from '@/stores/modelClass'
2
+ import { GetSchemasQuery, Schema } from '@/graphql/gql/graphql'
3
+ import { BaseDb } from '@/db/Db/BaseDb'
4
+ import { models as modelsTable, modelUids } from '@/seedSchema'
5
+ import { eq } from 'drizzle-orm'
6
+ import pluralize from 'pluralize'
7
+ import { toSnakeCase } from '@/helpers'
8
+ import { GET_SCHEMAS } from '@/Item/queries'
9
+ import { BaseEasClient } from '@/helpers/EasClient/BaseEasClient'
10
+ import { BaseQueryClient } from '@/helpers/QueryClient/BaseQueryClient'
11
+
12
+
13
+ type GetModelSchemasReturn = {
14
+ modelSchemas: GetSchemasQuery
15
+ schemaStringToModelRecord: Map<string, Schema>
16
+ modelRecords: Record<string, unknown>
17
+ }
18
+
19
+ type GetModelSchemas = () => Promise<GetModelSchemasReturn>
20
+
21
+ export const getModelSchemas: GetModelSchemas = async () => {
22
+ const models = getModels()
23
+ const modelRecords = [] as Record<string, unknown>[]
24
+
25
+ const schemaStringToModelRecord = new Map<string, Schema>()
26
+
27
+ const appDb = BaseDb.getAppDb()
28
+
29
+ const OR: Record<string, unknown>[] = []
30
+
31
+ for (const [modelName, _] of Object.entries(models)) {
32
+ const foundModelQuery = await appDb
33
+ .select({
34
+ id: modelsTable.id,
35
+ name: modelsTable.name,
36
+ uid: modelUids.uid,
37
+ })
38
+ .from(modelsTable)
39
+ .leftJoin(modelUids, eq(modelsTable.id, modelUids.modelId))
40
+ .where(eq(modelsTable.name, modelName))
41
+ .limit(1)
42
+
43
+ const foundModel: Schema = { ...foundModelQuery[0] }
44
+
45
+ if (!foundModel) {
46
+ console.error(
47
+ `[item/events] [syncDbWithEas] model ${modelName} not found in SDK DB`,
48
+ )
49
+ return
50
+ }
51
+
52
+ foundModel.tableName = pluralize(foundModel.name).toLowerCase()
53
+
54
+ modelRecords!.push(foundModel)
55
+
56
+ if (modelName === 'Seed' || modelName === 'Version') {
57
+ continue
58
+ }
59
+
60
+ const schemaString = `bytes32 ${toSnakeCase(modelName)}`
61
+
62
+ OR.push({
63
+ schema: {
64
+ equals: `bytes32 ${toSnakeCase(modelName)}`,
65
+ },
66
+ })
67
+
68
+ schemaStringToModelRecord.set(schemaString, foundModel)
69
+ }
70
+
71
+ const queryClient = BaseQueryClient.getQueryClient()
72
+ const easClient = BaseEasClient.getEasClient()
73
+
74
+ const modelSchemas = await queryClient.fetchQuery({
75
+ queryKey: [`getSchemasAllModels`],
76
+ queryFn: async () =>
77
+ easClient.request(GET_SCHEMAS, {
78
+ where: {
79
+ OR,
80
+ },
81
+ }),
82
+ })
83
+
84
+ return {
85
+ modelSchemas,
86
+ schemaStringToModelRecord,
87
+ modelRecords,
88
+ }
89
+ }
@@ -0,0 +1,50 @@
1
+ import { PropertyData } from "@/types"
2
+ import { BaseDb } from "@/db/Db/BaseDb"
3
+ import { metadata, MetadataType } from "@/seedSchema"
4
+ import { or, eq, and, sql } from "drizzle-orm"
5
+ import { startCase } from "lodash-es"
6
+ import { GetPropertyDataOptions } from "@/types/db"
7
+
8
+
9
+ export const getPropertyData = async ({
10
+ propertyName,
11
+ seedLocalId,
12
+ seedUid,
13
+ }: GetPropertyDataOptions): Promise<PropertyData | undefined> => {
14
+ const appDb = BaseDb.getAppDb()
15
+
16
+ const whereClauses: any[] = [
17
+ or(
18
+ eq(metadata.propertyName, propertyName),
19
+ eq(metadata.propertyName, propertyName + 'Id'),
20
+ eq(metadata.propertyName, propertyName + 'Ids'),
21
+ ),
22
+ ]
23
+
24
+ if (seedLocalId) {
25
+ whereClauses.push(eq(metadata.seedLocalId, seedLocalId))
26
+ }
27
+
28
+ if (seedUid) {
29
+ whereClauses.push(eq(metadata.seedUid, seedUid))
30
+ }
31
+
32
+ const rows = (await appDb
33
+ .select()
34
+ .from(metadata)
35
+ .where(and(...whereClauses))
36
+ .orderBy(
37
+ sql.raw(`COALESCE(attestation_created_at, created_at) DESC`),
38
+ )) as MetadataType[]
39
+
40
+ if (!rows || rows.length === 0) {
41
+ return
42
+ }
43
+
44
+ const row = rows[0]
45
+
46
+ return {
47
+ ...row,
48
+ modelName: startCase(row.modelType),
49
+ }
50
+ }
@@ -0,0 +1,355 @@
1
+ import { getItem } from '@/db/read/getItem'
2
+ import {
3
+ defaultAttestationData,
4
+ INTERNAL_DATA_TYPES,
5
+ VERSION_SCHEMA_UID_OPTIMISM_SEPOLIA,
6
+ } from '@/helpers/constants'
7
+ import {
8
+ AttestationRequest,
9
+ SchemaEncoder,
10
+ ZERO_BYTES32,
11
+ } from '@ethereum-attestation-service/eas-sdk'
12
+
13
+ import { getSchemaForItemProperty } from '@/helpers/getSchemaForItemProperty'
14
+ import { toSnakeCase } from '@/helpers'
15
+ import pluralize from 'pluralize'
16
+ import { getSchemaUidForModel } from '@/db/read/getSchemaUidForModel'
17
+ import { getSchemaUidForSchemaDefinition } from '@/stores/eas'
18
+ import { getCorrectId } from '@/helpers'
19
+ import { getSegmentedItemProperties } from '@/helpers/getSegmentedItemProperties'
20
+ import { IItemProperty } from '@/interfaces'
21
+ import { IItem } from '@/interfaces'
22
+ import { BaseItem } from '@/Item/BaseItem'
23
+
24
+ const getVersionUid = (item: IItem<any>) => {
25
+ let versionUid
26
+
27
+ if (
28
+ item.latestVersionUid &&
29
+ item.latestVersionUid !== 'NULL' &&
30
+ item.latestVersionUid !== 'undefined'
31
+ ) {
32
+ versionUid = item.latestVersionUid
33
+ }
34
+ return versionUid || ZERO_BYTES32
35
+ }
36
+
37
+ const getPropertyData = async (itemProperty: IItemProperty<any>) => {
38
+ const easDataType =
39
+ INTERNAL_DATA_TYPES[itemProperty.propertyDef!.dataType].eas
40
+
41
+ let schemaUid: string | undefined = itemProperty.schemaUid
42
+
43
+ const propertyNameForSchema = toSnakeCase(itemProperty.propertyName)
44
+
45
+ const schemaDef = `${easDataType} ${propertyNameForSchema}`
46
+
47
+ if (!schemaUid) {
48
+ schemaUid = getSchemaUidForSchemaDefinition(schemaDef)
49
+ if (!schemaUid) {
50
+ const schema = await getSchemaForItemProperty({
51
+ propertyName: 'version',
52
+ easDataType: 'bytes32',
53
+ })
54
+ if (schema) {
55
+ schemaUid = schema.id
56
+ }
57
+ }
58
+ }
59
+
60
+ return {
61
+ schemaUid,
62
+ easDataType,
63
+ schemaDef,
64
+ propertyNameForSchema,
65
+ }
66
+ }
67
+
68
+ const processBasicProperties = async (
69
+ itemBasicProperties: IItemProperty<any>[],
70
+ itemPublishData: PublishPayload,
71
+ ): Promise<PublishPayload> => {
72
+ for (const basicProperty of itemBasicProperties) {
73
+ const value = basicProperty.getService().getSnapshot().context.propertyValue
74
+
75
+ if (!value || basicProperty.uid) {
76
+ continue
77
+ }
78
+
79
+ const { schemaUid, easDataType, schemaDef } =
80
+ await getPropertyData(basicProperty)
81
+
82
+ const propertyNameForSchema = toSnakeCase(basicProperty.propertyName)
83
+
84
+ const data = [
85
+ {
86
+ name: propertyNameForSchema,
87
+ type: easDataType,
88
+ value,
89
+ },
90
+ ]
91
+
92
+ const dataEncoder = new SchemaEncoder(schemaDef)
93
+
94
+ const encodedData = dataEncoder.encodeData(data)
95
+
96
+ itemPublishData.listOfAttestations.push({
97
+ schema: schemaUid!,
98
+ data: [
99
+ {
100
+ ...defaultAttestationData,
101
+ data: encodedData,
102
+ },
103
+ ],
104
+ })
105
+ }
106
+
107
+ return itemPublishData
108
+ }
109
+
110
+ const processRelationProperty = async (
111
+ relationProperty: IItemProperty<any>,
112
+ multiPublishPayload: MultiPublishPayload,
113
+ uploadedTransactions: UploadedTransaction[],
114
+ originalSeedLocalId: string,
115
+ ): Promise<MultiPublishPayload> => {
116
+ const value = relationProperty.getService().getSnapshot()
117
+ .context.propertyValue
118
+ if (!value || relationProperty.uid) {
119
+ return multiPublishPayload
120
+ }
121
+
122
+ const { localId: seedLocalId, uid: seedUid } = getCorrectId(value)
123
+
124
+ const relatedItem = await getItem({
125
+ seedLocalId,
126
+ seedUid,
127
+ })
128
+
129
+ if (!relatedItem) {
130
+ throw new Error(
131
+ `No related item found for relation property: ${relationProperty.propertyName}`,
132
+ )
133
+ }
134
+
135
+ const versionUid = getVersionUid(relatedItem)
136
+
137
+ const seedSchemaUid = await getSchemaUidForModel(
138
+ relationProperty.propertyDef!.ref as string,
139
+ )
140
+
141
+ let publishPayload: PublishPayload = {
142
+ localId: relationProperty.localId,
143
+ seedIsRevocable: true,
144
+ versionSchemaUid: VERSION_SCHEMA_UID_OPTIMISM_SEPOLIA,
145
+ seedUid: seedUid || ZERO_BYTES32,
146
+ seedSchemaUid,
147
+ versionUid,
148
+ listOfAttestations: [],
149
+ propertiesToUpdate: [
150
+ {
151
+ publishLocalId: originalSeedLocalId,
152
+ propertySchemaUid: relationProperty.schemaUid,
153
+ },
154
+ ],
155
+ }
156
+
157
+ const { itemBasicProperties, itemUploadProperties } =
158
+ getSegmentedItemProperties(relatedItem)
159
+
160
+ if (itemUploadProperties.length === 1) {
161
+ const uploadProperty = itemUploadProperties[0]
162
+ const itemProperty = uploadProperty.itemProperty
163
+ const transactionData = uploadedTransactions.find(
164
+ (transaction) => transaction.seedLocalId === relatedItem.seedLocalId,
165
+ )
166
+ if (transactionData) {
167
+ itemProperty.value = transactionData.txId
168
+ await itemProperty.save()
169
+ itemBasicProperties.push(itemProperty)
170
+ }
171
+ }
172
+
173
+ publishPayload = await processBasicProperties(
174
+ itemBasicProperties,
175
+ publishPayload,
176
+ )
177
+
178
+ multiPublishPayload.push(publishPayload)
179
+
180
+ return multiPublishPayload
181
+ }
182
+
183
+ const processListProperty = async (
184
+ listProperty: IItemProperty<any>,
185
+ multiPublishPayload: MultiPublishPayload,
186
+ originalSeedLocalId: string,
187
+ ): Promise<MultiPublishPayload> => {
188
+ let value = listProperty.getService().getSnapshot().context.propertyValue
189
+ if (!value || listProperty.uid) {
190
+ return multiPublishPayload
191
+ }
192
+
193
+ const singularPropertyName = pluralize.singular(listProperty.propertyName)
194
+ const propertyNameForSchema = `${singularPropertyName}${listProperty.propertyDef!.ref}Ids`
195
+ if (typeof value === 'string' && value.length === 66) {
196
+ value = [value]
197
+ }
198
+ if (typeof value === 'string' && value.length > 66) {
199
+ try {
200
+ value = JSON.parse(value)
201
+ } catch (error) {
202
+ value = value.split(',')
203
+ }
204
+ }
205
+
206
+ for (const seedId of value) {
207
+ const { localId: seedLocalId, uid: seedUid } = getCorrectId(seedId)
208
+
209
+ const relatedItem = await getItem({
210
+ seedLocalId,
211
+ seedUid,
212
+ })
213
+
214
+ if (!relatedItem) {
215
+ console.error(
216
+ `No related item found for list property: ${listProperty.propertyName}`,
217
+ )
218
+ continue
219
+ }
220
+
221
+ if (relatedItem.seedUid) {
222
+ return multiPublishPayload
223
+ }
224
+
225
+ const versionUid = getVersionUid(relatedItem)
226
+
227
+ const seedSchemaUid = await getSchemaUidForModel(
228
+ listProperty.propertyDef!.ref as string,
229
+ )
230
+
231
+ let publishPayload: PublishPayload = {
232
+ localId: relatedItem.seedLocalId,
233
+ seedIsRevocable: true,
234
+ versionSchemaUid: VERSION_SCHEMA_UID_OPTIMISM_SEPOLIA,
235
+ seedUid: seedUid || ZERO_BYTES32,
236
+ seedSchemaUid,
237
+ versionUid,
238
+ listOfAttestations: [],
239
+ propertiesToUpdate: [
240
+ {
241
+ publishLocalId: originalSeedLocalId,
242
+ propertySchemaUid: listProperty.schemaUid,
243
+ },
244
+ ],
245
+ }
246
+
247
+ const { itemBasicProperties } = getSegmentedItemProperties(relatedItem)
248
+
249
+ publishPayload = await processBasicProperties(
250
+ itemBasicProperties,
251
+ publishPayload,
252
+ )
253
+
254
+ multiPublishPayload.push(publishPayload)
255
+ }
256
+
257
+ return multiPublishPayload
258
+ }
259
+
260
+ type PublishPayload = {
261
+ localId: string
262
+ seedIsRevocable: boolean
263
+ seedSchemaUid: string
264
+ seedUid: string
265
+ versionSchemaUid: string
266
+ versionUid: string
267
+ listOfAttestations: AttestationRequest[]
268
+ propertiesToUpdate: any[]
269
+ }
270
+
271
+ type MultiPublishPayload = PublishPayload[]
272
+
273
+ type UploadedTransaction = {
274
+ txId: string
275
+ itemPropertyLocalId?: string
276
+ seedLocalId?: string
277
+ versionLocalId?: string
278
+ itemPropertyName?: string
279
+ }
280
+
281
+ export const getPublishPayload = async (
282
+ item: BaseItem<any>,
283
+ uploadedTransactions: UploadedTransaction[],
284
+ ): Promise<MultiPublishPayload> => {
285
+
286
+ let multiPublishPayload: MultiPublishPayload = []
287
+
288
+ // Each PublishPayload is generated from a Seed that needs publishing
289
+
290
+ // First we need to determine all Seeds to publish
291
+
292
+ // That means the Seed of the Item, plus any Seeds pointed to by Relations
293
+
294
+ let itemPublishData: PublishPayload = {
295
+ localId: item.seedLocalId,
296
+ seedUid: item.seedUid || ZERO_BYTES32,
297
+ seedIsRevocable: true,
298
+ seedSchemaUid: item.schemaUid,
299
+ versionSchemaUid: VERSION_SCHEMA_UID_OPTIMISM_SEPOLIA,
300
+ versionUid: getVersionUid(item),
301
+ listOfAttestations: [],
302
+ propertiesToUpdate: [],
303
+ }
304
+
305
+ const {
306
+ itemBasicProperties,
307
+ itemRelationProperties,
308
+ itemListProperties,
309
+ itemUploadProperties,
310
+ } = getSegmentedItemProperties(item)
311
+
312
+ if (itemUploadProperties.length === 1) {
313
+ const uploadProperty = itemUploadProperties[0]
314
+ const itemProperty = uploadProperty.itemProperty
315
+ const transactionData = uploadedTransactions.find(
316
+ (transaction) => transaction.seedLocalId === item.seedLocalId,
317
+ )
318
+ if (transactionData) {
319
+ itemProperty.value = transactionData.txId
320
+ await itemProperty.save()
321
+ itemBasicProperties.push(itemProperty)
322
+ }
323
+ }
324
+
325
+ for (const relationProperty of itemRelationProperties) {
326
+ multiPublishPayload = await processRelationProperty(
327
+ relationProperty,
328
+ multiPublishPayload,
329
+ uploadedTransactions,
330
+ item.seedLocalId,
331
+ )
332
+ itemBasicProperties.push(relationProperty)
333
+ }
334
+
335
+ for (const listProperty of itemListProperties) {
336
+ multiPublishPayload = await processListProperty(
337
+ listProperty,
338
+ multiPublishPayload,
339
+ item.seedLocalId,
340
+ )
341
+ itemBasicProperties.push(listProperty)
342
+ }
343
+
344
+ itemPublishData = await processBasicProperties(
345
+ itemBasicProperties,
346
+ itemPublishData,
347
+ )
348
+
349
+ multiPublishPayload.push(itemPublishData)
350
+
351
+
352
+
353
+
354
+ return multiPublishPayload
355
+ }