@xyo-network/erc1967-witness 2.84.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 (78) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +13 -0
  3. package/dist/browser/Payload.d.cts +14 -0
  4. package/dist/browser/Payload.d.cts.map +1 -0
  5. package/dist/browser/Payload.d.mts +14 -0
  6. package/dist/browser/Payload.d.mts.map +1 -0
  7. package/dist/browser/Payload.d.ts +14 -0
  8. package/dist/browser/Payload.d.ts.map +1 -0
  9. package/dist/browser/Witness.d.cts +13 -0
  10. package/dist/browser/Witness.d.cts.map +1 -0
  11. package/dist/browser/Witness.d.mts +13 -0
  12. package/dist/browser/Witness.d.mts.map +1 -0
  13. package/dist/browser/Witness.d.ts +13 -0
  14. package/dist/browser/Witness.d.ts.map +1 -0
  15. package/dist/browser/index.cjs +122 -0
  16. package/dist/browser/index.cjs.map +1 -0
  17. package/dist/browser/index.d.cts +4 -0
  18. package/dist/browser/index.d.cts.map +1 -0
  19. package/dist/browser/index.d.mts +4 -0
  20. package/dist/browser/index.d.mts.map +1 -0
  21. package/dist/browser/index.d.ts +4 -0
  22. package/dist/browser/index.d.ts.map +1 -0
  23. package/dist/browser/index.js +102 -0
  24. package/dist/browser/index.js.map +1 -0
  25. package/dist/browser/lib/getErc1967SlotStatus.d.cts +22 -0
  26. package/dist/browser/lib/getErc1967SlotStatus.d.cts.map +1 -0
  27. package/dist/browser/lib/getErc1967SlotStatus.d.mts +22 -0
  28. package/dist/browser/lib/getErc1967SlotStatus.d.mts.map +1 -0
  29. package/dist/browser/lib/getErc1967SlotStatus.d.ts +22 -0
  30. package/dist/browser/lib/getErc1967SlotStatus.d.ts.map +1 -0
  31. package/dist/browser/lib/index.d.cts +2 -0
  32. package/dist/browser/lib/index.d.cts.map +1 -0
  33. package/dist/browser/lib/index.d.mts +2 -0
  34. package/dist/browser/lib/index.d.mts.map +1 -0
  35. package/dist/browser/lib/index.d.ts +2 -0
  36. package/dist/browser/lib/index.d.ts.map +1 -0
  37. package/dist/node/Payload.d.cts +14 -0
  38. package/dist/node/Payload.d.cts.map +1 -0
  39. package/dist/node/Payload.d.mts +14 -0
  40. package/dist/node/Payload.d.mts.map +1 -0
  41. package/dist/node/Payload.d.ts +14 -0
  42. package/dist/node/Payload.d.ts.map +1 -0
  43. package/dist/node/Witness.d.cts +13 -0
  44. package/dist/node/Witness.d.cts.map +1 -0
  45. package/dist/node/Witness.d.mts +13 -0
  46. package/dist/node/Witness.d.mts.map +1 -0
  47. package/dist/node/Witness.d.ts +13 -0
  48. package/dist/node/Witness.d.ts.map +1 -0
  49. package/dist/node/index.cjs +133 -0
  50. package/dist/node/index.cjs.map +1 -0
  51. package/dist/node/index.d.cts +4 -0
  52. package/dist/node/index.d.cts.map +1 -0
  53. package/dist/node/index.d.mts +4 -0
  54. package/dist/node/index.d.mts.map +1 -0
  55. package/dist/node/index.d.ts +4 -0
  56. package/dist/node/index.d.ts.map +1 -0
  57. package/dist/node/index.js +102 -0
  58. package/dist/node/index.js.map +1 -0
  59. package/dist/node/lib/getErc1967SlotStatus.d.cts +22 -0
  60. package/dist/node/lib/getErc1967SlotStatus.d.cts.map +1 -0
  61. package/dist/node/lib/getErc1967SlotStatus.d.mts +22 -0
  62. package/dist/node/lib/getErc1967SlotStatus.d.mts.map +1 -0
  63. package/dist/node/lib/getErc1967SlotStatus.d.ts +22 -0
  64. package/dist/node/lib/getErc1967SlotStatus.d.ts.map +1 -0
  65. package/dist/node/lib/index.d.cts +2 -0
  66. package/dist/node/lib/index.d.cts.map +1 -0
  67. package/dist/node/lib/index.d.mts +2 -0
  68. package/dist/node/lib/index.d.mts.map +1 -0
  69. package/dist/node/lib/index.d.ts +2 -0
  70. package/dist/node/lib/index.d.ts.map +1 -0
  71. package/package.json +68 -0
  72. package/src/Payload.ts +19 -0
  73. package/src/Witness.ts +60 -0
  74. package/src/index.ts +3 -0
  75. package/src/lib/getErc1967SlotStatus.ts +70 -0
  76. package/src/lib/index.ts +1 -0
  77. package/typedoc.json +5 -0
  78. package/xy.config.ts +14 -0
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@xyo-network/erc1967-witness",
3
+ "author": {
4
+ "email": "support@xyo.network",
5
+ "name": "XYO Development Team",
6
+ "url": "https://xyo.network"
7
+ },
8
+ "bugs": {
9
+ "email": "support@xyo.network",
10
+ "url": "https://github.com/XYOracleNetwork/plugins/issues"
11
+ },
12
+ "dependencies": {
13
+ "@xylabs/assert": "^2.13.20",
14
+ "@xylabs/hex": "^2.13.20",
15
+ "@xyo-network/open-zeppelin-typechain": "^3.0.5",
16
+ "@xyo-network/payload-model": "^2.84.3",
17
+ "@xyo-network/witness-blockchain-abstract": "^2.84.3"
18
+ },
19
+ "devDependencies": {
20
+ "@xylabs/jest-helpers": "^2.13.20",
21
+ "@xylabs/ts-scripts-yarn3": "^3.2.19",
22
+ "@xylabs/tsconfig": "^3.2.19",
23
+ "ethers": "^6.9.0",
24
+ "jest": "^29.7.0",
25
+ "typescript": "^5.3.3"
26
+ },
27
+ "description": "Typescript/Javascript Plugins for XYO Platform",
28
+ "types": "dist/node/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "browser": {
32
+ "require": {
33
+ "types": "./dist/browser/index.d.cts",
34
+ "default": "./dist/browser/index.cjs"
35
+ },
36
+ "import": {
37
+ "types": "./dist/browser/index.d.mts",
38
+ "default": "./dist/browser/index.js"
39
+ }
40
+ },
41
+ "node": {
42
+ "require": {
43
+ "types": "./dist/node/index.d.cts",
44
+ "default": "./dist/node/index.cjs"
45
+ },
46
+ "import": {
47
+ "types": "./dist/node/index.d.mts",
48
+ "default": "./dist/node/index.js"
49
+ }
50
+ }
51
+ },
52
+ "./package.json": "./package.json"
53
+ },
54
+ "main": "dist/node/index.cjs",
55
+ "module": "dist/node/index.js",
56
+ "homepage": "https://xyo.network",
57
+ "license": "LGPL-3.0-only",
58
+ "publishConfig": {
59
+ "access": "public"
60
+ },
61
+ "repository": {
62
+ "type": "git",
63
+ "url": "https://github.com/XYOracleNetwork/plugins.git"
64
+ },
65
+ "sideEffects": false,
66
+ "version": "2.84.1",
67
+ "type": "module"
68
+ }
package/src/Payload.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { Address } from '@xylabs/hex'
2
+ import { Payload } from '@xyo-network/payload-model'
3
+
4
+ import { Erc1967SlotStatus } from './lib'
5
+
6
+ export const Erc1967StatusSchema = 'network.xyo.erc1967.status'
7
+ export type Erc1967StatusSchema = typeof Erc1967StatusSchema
8
+
9
+ export type Erc1967Status = Payload<
10
+ {
11
+ address: Address
12
+ beacon?: Erc1967SlotStatus['beacon']
13
+ block: number
14
+ chainId: number
15
+ implementation?: Erc1967SlotStatus['implementation']
16
+ slots?: Erc1967SlotStatus['slots']
17
+ },
18
+ Erc1967StatusSchema
19
+ >
package/src/Witness.ts ADDED
@@ -0,0 +1,60 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
3
+ import {
4
+ AbstractBlockchainWitness,
5
+ BlockchainAddress,
6
+ BlockchainAddressSchema,
7
+ BlockchainWitnessConfig,
8
+ BlockchainWitnessParams,
9
+ } from '@xyo-network/witness-blockchain-abstract'
10
+
11
+ import { getErc1967SlotStatus } from './lib'
12
+ import { Erc1967Status, Erc1967StatusSchema } from './Payload'
13
+
14
+ export const BlockchainErc1967WitnessConfigSchema = 'network.xyo.blockchain.Erc1967.witness.config'
15
+ export type BlockchainErc1967WitnessConfigSchema = typeof BlockchainErc1967WitnessConfigSchema
16
+
17
+ export type BlockchainErc1967WitnessConfig = BlockchainWitnessConfig<{ address?: string }, BlockchainErc1967WitnessConfigSchema>
18
+
19
+ export type BlockchainErc1967WitnessParams = BlockchainWitnessParams<BlockchainErc1967WitnessConfig>
20
+
21
+ export class BlockchainErc1967Witness<
22
+ TParams extends BlockchainErc1967WitnessParams = BlockchainErc1967WitnessParams,
23
+ > extends AbstractBlockchainWitness<TParams, BlockchainAddress, Erc1967Status> {
24
+ static override configSchemas = [BlockchainErc1967WitnessConfigSchema]
25
+
26
+ protected override async observeHandler(inPayloads: BlockchainAddress[] = []): Promise<Erc1967Status[]> {
27
+ await this.started('throw')
28
+ //calling it here to make sure we rests the cache
29
+ await this.getProviders()
30
+ try {
31
+ const observations = await Promise.all(
32
+ inPayloads.filter(isPayloadOfSchemaType(BlockchainAddressSchema)).map(async ({ address }) => {
33
+ const validatedAddress = assertEx(address ?? this.config.address, 'Missing address')
34
+
35
+ const provider = await this.getProvider(true, true)
36
+
37
+ const block = await provider.getBlockNumber()
38
+
39
+ const { beacon, implementation, slots } = await getErc1967SlotStatus(provider, validatedAddress, block)
40
+
41
+ const observation: Erc1967Status = {
42
+ address: validatedAddress,
43
+ beacon,
44
+ block,
45
+ chainId: Number((await provider.getNetwork()).chainId),
46
+ implementation,
47
+ schema: Erc1967StatusSchema,
48
+ slots,
49
+ }
50
+ return observation
51
+ }),
52
+ )
53
+ return observations
54
+ } catch (ex) {
55
+ const error = ex as Error
56
+ console.log(`Error [${this.config.name}]: ${error.message}`)
57
+ throw error
58
+ }
59
+ }
60
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './lib'
2
+ export * from './Payload'
3
+ export * from './Witness'
@@ -0,0 +1,70 @@
1
+ import { Address, hexFromHexString, isHexZero } from '@xylabs/hex'
2
+ import { UpgradeableBeacon__factory } from '@xyo-network/open-zeppelin-typechain'
3
+ import { Provider } from 'ethers'
4
+
5
+ export const ERC1967_PROXY_IMPLEMENTATION_STORAGE_SLOT = '0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc'
6
+ export const ERC1967_PROXY_BEACON_STORAGE_SLOT = '0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50'
7
+ export const ERC1967_PROXY_ADMIN_STORAGE_SLOT = '0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103'
8
+ export const ERC1967_PROXY_ROLLBACK_STORAGE_SLOT = '0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143'
9
+
10
+ export interface Erc1967DataSlots {
11
+ admin?: Address
12
+ beacon?: Address
13
+ implementation?: Address
14
+ rollback?: Address
15
+ }
16
+
17
+ export interface Erc1967SlotStatus {
18
+ address: Address
19
+ beacon?: {
20
+ implementation?: Address
21
+ }
22
+ implementation: Address
23
+ slots: Erc1967DataSlots
24
+ }
25
+
26
+ const readAddressFromSlot = async (provider: Provider, address: string, slot: string, block?: number) => {
27
+ try {
28
+ const slotValue = await provider.getStorage(address, slot, block)
29
+ return hexFromHexString(slotValue, { prefix: true })
30
+ } catch (ex) {
31
+ return undefined
32
+ }
33
+ }
34
+
35
+ export const getErc1967SlotStatus = async (provider: Provider, address: string, block?: number): Promise<Erc1967SlotStatus> => {
36
+ const status: Erc1967SlotStatus = {
37
+ address,
38
+ implementation: address,
39
+ slots: {
40
+ admin: await readAddressFromSlot(provider, address, ERC1967_PROXY_ADMIN_STORAGE_SLOT, block),
41
+ beacon: await readAddressFromSlot(provider, address, ERC1967_PROXY_BEACON_STORAGE_SLOT, block),
42
+ implementation: await readAddressFromSlot(provider, address, ERC1967_PROXY_IMPLEMENTATION_STORAGE_SLOT, block),
43
+ rollback: await readAddressFromSlot(provider, address, ERC1967_PROXY_ROLLBACK_STORAGE_SLOT, block),
44
+ },
45
+ }
46
+
47
+ if (status.slots.implementation && !isHexZero(status.slots.implementation)) {
48
+ status.implementation = `0x${BigInt(status.slots.implementation as string)
49
+ .toString(16)
50
+ .padStart(40, '0')}`
51
+ } else {
52
+ if (status.slots.beacon && !isHexZero(status.slots.beacon)) {
53
+ const beacon = UpgradeableBeacon__factory.connect(status.slots.beacon as string, { provider })
54
+ try {
55
+ const implementation = await beacon.implementation(block ? { blockTag: block } : {})
56
+ if (implementation) {
57
+ status.beacon = { implementation }
58
+ if (!isHexZero(implementation)) {
59
+ status.implementation = implementation
60
+ }
61
+ }
62
+ } catch (ex) {
63
+ const error = ex as Error
64
+ console.log(error.message)
65
+ }
66
+ }
67
+ }
68
+
69
+ return status
70
+ }
@@ -0,0 +1 @@
1
+ export * from './getErc1967SlotStatus'
package/typedoc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": ["./src/index.ts"],
4
+ "tsconfig": "./tsconfig.typedoc.json"
5
+ }
package/xy.config.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { XyTsupConfig } from '@xylabs/ts-scripts-yarn3'
2
+ const config: XyTsupConfig = {
3
+ compile: {
4
+ browser: {
5
+ src: true,
6
+ },
7
+ node: {
8
+ src: true,
9
+ },
10
+ },
11
+ }
12
+
13
+ // eslint-disable-next-line import/no-default-export
14
+ export default config