@show-karma/karma-gap-sdk 0.2.9 → 0.3.2

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.
@@ -1,4 +1,4 @@
1
- import { Hex, IAttestation, JSONStr, MultiAttestData, SignerOrProvider } from '../types';
1
+ import { Hex, IAttestation, JSONStr, MultiAttestData, SignerOrProvider, TNetwork } from '../types';
2
2
  import { Schema } from './Schema';
3
3
  import { SchemaItem, SchemaValue } from '@ethereum-attestation-service/eas-sdk';
4
4
  import { GapSchema } from './GapSchema';
@@ -20,13 +20,13 @@ export interface AttestationArgs<T = unknown, S extends Schema = Schema> {
20
20
  *
21
21
  * ```ts
22
22
  * const grantee = new Attestation({
23
- * schema: Schema.get("Grantee"), // Use GapSchema.find("SchemaName") if using default GAP schemas
23
+ * schema: Schema.get("Grantee", "network-name"), // Use this.schema.gap.findSchema("SchemaName") if using default GAP schemas
24
24
  * data: { grantee: true },
25
25
  * uid: "0xabc123",
26
26
  * });
27
27
  *
28
28
  * const granteeDetails = new Attestation({
29
- * schema: Schema.get("GranteeDetails"),
29
+ * schema: Schema.get("GranteeDetails", "optimism"),
30
30
  * data: {
31
31
  * name: "John Doe",
32
32
  * description: "A description",
@@ -55,7 +55,7 @@ export declare class Attestation<T = unknown, S extends Schema = GapSchema> impl
55
55
  readonly revoked?: boolean;
56
56
  readonly revocationTime?: Date;
57
57
  readonly createdAt: Date;
58
- readonly chainID: number;
58
+ private _chainID;
59
59
  private _reference?;
60
60
  constructor(args: AttestationArgs<T, S>);
61
61
  /**
@@ -149,13 +149,14 @@ export declare class Attestation<T = unknown, S extends Schema = GapSchema> impl
149
149
  /**
150
150
  * Transform attestation interface-based into class-based.
151
151
  */
152
- static fromInterface<T extends Attestation = Attestation>(attestations: IAttestation[]): T[];
152
+ static fromInterface<T extends Attestation = Attestation>(attestations: IAttestation[], network: TNetwork): T[];
153
153
  /**
154
154
  * Asserts if schema is valid.
155
155
  * > Does not check refUID if `strict = false`. To check refUID use `Schema.validate()`
156
156
  * @param args
157
157
  */
158
158
  protected assert(args: AttestationArgs, strict?: boolean): void;
159
+ get chainID(): number;
159
160
  get data(): T;
160
161
  get uid(): Hex;
161
162
  set uid(uid: Hex);
@@ -13,13 +13,13 @@ const GapContract_1 = require("./contract/GapContract");
13
13
  *
14
14
  * ```ts
15
15
  * const grantee = new Attestation({
16
- * schema: Schema.get("Grantee"), // Use GapSchema.find("SchemaName") if using default GAP schemas
16
+ * schema: Schema.get("Grantee", "network-name"), // Use this.schema.gap.findSchema("SchemaName") if using default GAP schemas
17
17
  * data: { grantee: true },
18
18
  * uid: "0xabc123",
19
19
  * });
20
20
  *
21
21
  * const granteeDetails = new Attestation({
22
- * schema: Schema.get("GranteeDetails"),
22
+ * schema: Schema.get("GranteeDetails", "optimism"),
23
23
  * data: {
24
24
  * name: "John Doe",
25
25
  * description: "A description",
@@ -50,7 +50,7 @@ class Attestation {
50
50
  this.revoked = args.revoked;
51
51
  this.revocationTime = (0, get_date_1.getDate)(args.revocationTime);
52
52
  this.createdAt = (0, get_date_1.getDate)(args.createdAt || Date.now() / 1000);
53
- this.chainID = args.chainID;
53
+ this._chainID = args.chainID || consts_1.Networks[this.schema.gap.network].chainId;
54
54
  }
55
55
  /**
56
56
  * Encodes the schema.
@@ -257,14 +257,15 @@ class Attestation {
257
257
  /**
258
258
  * Transform attestation interface-based into class-based.
259
259
  */
260
- static fromInterface(attestations) {
260
+ static fromInterface(attestations, network) {
261
261
  const result = [];
262
262
  attestations.forEach((attestation) => {
263
263
  try {
264
- const schema = Schema_1.Schema.get(attestation.schemaId);
264
+ const schema = Schema_1.Schema.get(attestation.schemaId, network);
265
265
  result.push(new Attestation({
266
266
  ...attestation,
267
267
  schema,
268
+ chainID: consts_1.Networks[network].chainId,
268
269
  data: attestation.decodedDataJson,
269
270
  }));
270
271
  }
@@ -288,7 +289,10 @@ class Attestation {
288
289
  throw new SchemaError_1.SchemaError('MISSING_FIELD', 'Schema uid is required');
289
290
  }
290
291
  if (strict)
291
- Schema_1.Schema.validate();
292
+ Schema_1.Schema.validate(this.schema.gap.network);
293
+ }
294
+ get chainID() {
295
+ return this._chainID;
292
296
  }
293
297
  get data() {
294
298
  return this._data;
@@ -315,6 +319,7 @@ class Attestation {
315
319
  schema,
316
320
  uid: '0x0',
317
321
  createdAt: new Date(),
322
+ chainID: consts_1.Networks[schema.gap.network].chainId,
318
323
  });
319
324
  }
320
325
  }
@@ -3,7 +3,11 @@ import { Attestation } from './Attestation';
3
3
  import { Community, Grant, MemberOf, Milestone, Project } from './entities';
4
4
  import { Grantee } from './types/attestations';
5
5
  import { AxiosGQL } from './GraphQL/AxiosGQL';
6
+ import { GAP } from './GAP';
6
7
  export declare abstract class Fetcher extends AxiosGQL {
8
+ protected gap: GAP;
9
+ constructor(url: string);
10
+ set gapInstance(gap: GAP);
7
11
  /**
8
12
  * Fetch a single attestation by its UID.
9
13
  * @param uid
@@ -3,5 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Fetcher = void 0;
4
4
  const AxiosGQL_1 = require("./GraphQL/AxiosGQL");
5
5
  class Fetcher extends AxiosGQL_1.AxiosGQL {
6
+ constructor(url) {
7
+ super(url);
8
+ }
9
+ set gapInstance(gap) {
10
+ this.gap = gap;
11
+ }
6
12
  }
7
13
  exports.Fetcher = Fetcher;
@@ -136,8 +136,7 @@ interface GAPArgs {
136
136
  *
137
137
  * const schemas = MountEntities(Networks.sepolia);
138
138
  *
139
- * // Use GAP.createClient to create a singleton GAP client
140
- * const gap = GAP.createClient({
139
+ * const gap = new GAP({
141
140
  * network: "sepolia",
142
141
  * owner: "0xd7d1DB401EA825b0325141Cd5e6cd7C2d01825f2",
143
142
  * schemas: Object.values(schemas),
@@ -152,7 +151,6 @@ interface GAPArgs {
152
151
  * ```
153
152
  */
154
153
  export declare class GAP extends Facade {
155
- private static client;
156
154
  private static remoteStorage?;
157
155
  readonly fetch: Fetcher;
158
156
  readonly network: TNetwork;
@@ -187,19 +185,22 @@ export declare class GAP extends Facade {
187
185
  */
188
186
  generateSlug: (text: string) => Promise<string>;
189
187
  /**
190
- * Creates or returns an existing GAP client.
191
- *
192
- * _Use the constructor only if multiple clients are needed._
193
- * @static
194
- * @param {GAPArgs} args
188
+ * Returns a copy of the original schema with no pointers.
189
+ * @param name
190
+ * @returns
191
+ */
192
+ findSchema(name: TSchemaName): GapSchema;
193
+ /**
194
+ * Find many schemas by name and return their copies as an array in the same order.
195
+ * @param names
195
196
  * @returns
196
197
  */
197
- static createClient(args: GAPArgs): GAP;
198
+ findManySchemas(names: TSchemaName[]): GapSchema[];
198
199
  /**
199
200
  * Get the multicall contract
200
201
  * @param signer
201
202
  */
202
- static getMulticall(signer: SignerOrProvider): ethers.Contract;
203
+ static getMulticall(signer: SignerOrProvider): Promise<ethers.Contract>;
203
204
  /**
204
205
  * Get the multicall contract
205
206
  * @param signer
package/core/class/GAP.js CHANGED
@@ -9,11 +9,11 @@ const ProjectResolver_json_1 = __importDefault(require("../abi/ProjectResolver.j
9
9
  const types_1 = require("../types");
10
10
  const Schema_1 = require("./Schema");
11
11
  const GapSchema_1 = require("./GapSchema");
12
- const GapEasClient_1 = require("./GraphQL/GapEasClient");
13
12
  const eas_sdk_1 = require("@ethereum-attestation-service/eas-sdk");
14
13
  const consts_1 = require("../consts");
15
14
  const ethers_1 = require("ethers");
16
15
  const package_json_1 = require("../../package.json");
16
+ const GraphQL_1 = require("./GraphQL");
17
17
  const get_web3_provider_1 = require("../utils/get-web3-provider");
18
18
  /**
19
19
  * GAP SDK Facade.
@@ -55,8 +55,7 @@ const get_web3_provider_1 = require("../utils/get-web3-provider");
55
55
  *
56
56
  * const schemas = MountEntities(Networks.sepolia);
57
57
  *
58
- * // Use GAP.createClient to create a singleton GAP client
59
- * const gap = GAP.createClient({
58
+ * const gap = new GAP({
60
59
  * network: "sepolia",
61
60
  * owner: "0xd7d1DB401EA825b0325141Cd5e6cd7C2d01825f2",
62
61
  * schemas: Object.values(schemas),
@@ -97,14 +96,19 @@ class GAP extends types_1.Facade {
97
96
  };
98
97
  const schemas = args.schemas || Object.values((0, consts_1.MountEntities)(consts_1.Networks[args.network]));
99
98
  this.network = args.network;
100
- GAP._eas = new eas_sdk_1.EAS(consts_1.Networks[args.network].contracts.eas);
101
- this.fetch = args.apiClient || new GapEasClient_1.GapEasClient({ network: args.network });
99
+ this._eas = new eas_sdk_1.EAS(consts_1.Networks[args.network].contracts.eas);
100
+ this.fetch =
101
+ args.apiClient ||
102
+ new GraphQL_1.GapEasClient({
103
+ network: args.network,
104
+ });
105
+ this.fetch.gapInstance = this;
102
106
  this.assertGelatoOpts(args);
103
107
  GAP._gelatoOpts = args.gelatoOpts;
104
108
  GAP.remoteStorage = args.remoteStorage;
105
- this._schemas = schemas.map((schema) => new GapSchema_1.GapSchema(schema, false, args.globalSchemas ? !args.globalSchemas : false));
106
- Schema_1.Schema.validate();
107
- console.info(`Loaded GAP SDK v${package_json_1.version}`);
109
+ this._schemas = schemas.map((schema) => new GapSchema_1.GapSchema(schema, this, false, args.globalSchemas ? !args.globalSchemas : false));
110
+ Schema_1.Schema.validate(this.network);
111
+ console.info(`Loaded GAP SDK v${package_json_1.version} for network ${this.network}`);
108
112
  }
109
113
  assertGelatoOpts(args) {
110
114
  if (args.gelatoOpts &&
@@ -131,7 +135,7 @@ class GAP extends types_1.Facade {
131
135
  * @param schema
132
136
  */
133
137
  async attest(attestation) {
134
- const schema = GapSchema_1.GapSchema.find(attestation.schemaName);
138
+ const schema = GapSchema_1.GapSchema.find(attestation.schemaName, this.network);
135
139
  return schema.attest(attestation);
136
140
  }
137
141
  /**
@@ -139,34 +143,43 @@ class GAP extends types_1.Facade {
139
143
  * @param schemas
140
144
  */
141
145
  replaceSchemas(schemas) {
142
- Schema_1.Schema.replaceAll(schemas);
146
+ Schema_1.Schema.replaceAll(schemas, this.network);
143
147
  }
144
148
  /**
145
149
  * Replaces a schema from the schema list.
146
150
  * @throws {SchemaError} if desired schema name does not exist.
147
151
  */
148
152
  replaceSingleSchema(schema) {
149
- Schema_1.Schema.replaceOne(schema);
153
+ Schema_1.Schema.replaceOne(schema, this.network);
150
154
  }
151
155
  /**
152
- * Creates or returns an existing GAP client.
153
- *
154
- * _Use the constructor only if multiple clients are needed._
155
- * @static
156
- * @param {GAPArgs} args
156
+ * Returns a copy of the original schema with no pointers.
157
+ * @param name
158
+ * @returns
159
+ */
160
+ findSchema(name) {
161
+ const found = Schema_1.Schema.get(name, this.network);
162
+ return GapSchema_1.GapSchema.clone(found);
163
+ }
164
+ /**
165
+ * Find many schemas by name and return their copies as an array in the same order.
166
+ * @param names
157
167
  * @returns
158
168
  */
159
- static createClient(args) {
160
- if (!this.client)
161
- this.client = new this(args);
162
- return this.client;
169
+ findManySchemas(names) {
170
+ const schemas = Schema_1.Schema.getMany(names, this.network);
171
+ return schemas.map((s) => GapSchema_1.GapSchema.clone(s));
163
172
  }
164
173
  /**
165
174
  * Get the multicall contract
166
175
  * @param signer
167
176
  */
168
- static getMulticall(signer) {
169
- const address = consts_1.Networks[this.client.network].contracts.multicall;
177
+ static async getMulticall(signer) {
178
+ const chain = (await signer.provider.getNetwork()) || signer.provider.network;
179
+ const network = Object.values(consts_1.Networks).find((n) => +n.chainId === Number(chain.chainId));
180
+ if (!network)
181
+ throw new Error(`Network ${chain.name || chain.chainId} not supported.`);
182
+ const address = network.contracts.multicall;
170
183
  return new ethers_1.ethers.Contract(address, MultiAttester_json_1.default, signer);
171
184
  }
172
185
  /**
@@ -175,7 +188,8 @@ class GAP extends types_1.Facade {
175
188
  */
176
189
  static getProjectResolver(signer, chainId) {
177
190
  const provider = chainId ? (0, get_web3_provider_1.getWeb3Provider)(chainId) : signer;
178
- const address = consts_1.Networks[this.client.network].contracts.projectResolver;
191
+ const network = Object.values(consts_1.Networks).find((n) => +n.chainId === Number(chainId));
192
+ const address = network.contracts.projectResolver;
179
193
  return new ethers_1.ethers.Contract(address, ProjectResolver_json_1.default, provider);
180
194
  }
181
195
  get schemas() {
@@ -1,5 +1,6 @@
1
- import { IGapSchema, SchemaInterface, TSchemaName } from "../types";
2
- import { Schema } from "./Schema";
1
+ import { IGapSchema, SchemaInterface, TNetwork, TSchemaName } from '../types';
2
+ import { Schema } from './Schema';
3
+ import { GAP } from './GAP';
3
4
  /**
4
5
  * Represents the GapSchema
5
6
  * @extends Schema
@@ -7,7 +8,7 @@ import { Schema } from "./Schema";
7
8
  export declare class GapSchema extends Schema implements IGapSchema {
8
9
  readonly name: TSchemaName;
9
10
  readonly references: TSchemaName;
10
- constructor(args: SchemaInterface<TSchemaName>, strict?: boolean, ignoreSchema?: boolean);
11
+ constructor(args: SchemaInterface<TSchemaName>, gap: GAP, strict?: boolean, ignoreSchema?: boolean);
11
12
  /**
12
13
  * Clones a schema without references to the original.
13
14
  * @param schema
@@ -19,13 +20,13 @@ export declare class GapSchema extends Schema implements IGapSchema {
19
20
  * @param name
20
21
  * @returns
21
22
  */
22
- static find(name: TSchemaName): GapSchema;
23
+ static find(name: TSchemaName, network: TNetwork): GapSchema;
23
24
  /**
24
25
  * Find many schemas by name and return their copies as an array in the same order.
25
26
  * @param names
26
27
  * @returns
27
28
  */
28
- static findMany(names: TSchemaName[]): GapSchema[];
29
+ static findMany(names: TSchemaName[], network: TNetwork): GapSchema[];
29
30
  /**
30
31
  * Get all schemas that references this schema.
31
32
  */
@@ -8,16 +8,16 @@ const Schema_1 = require("./Schema");
8
8
  * @extends Schema
9
9
  */
10
10
  class GapSchema extends Schema_1.Schema {
11
- constructor(args, strict = false, ignoreSchema = false) {
12
- super(args, strict, ignoreSchema);
11
+ constructor(args, gap, strict = false, ignoreSchema = false) {
12
+ super(args, gap, strict, ignoreSchema);
13
13
  if (!ignoreSchema)
14
- Schema_1.Schema.add(new GapSchema({
14
+ Schema_1.Schema.add(gap.network, new GapSchema({
15
15
  name: args.name,
16
16
  schema: args.schema.map((s) => ({ ...s })),
17
17
  uid: args.uid,
18
18
  references: args.references,
19
19
  revocable: args.revocable,
20
- }, strict, true));
20
+ }, gap, strict, true));
21
21
  }
22
22
  /**
23
23
  * Clones a schema without references to the original.
@@ -31,15 +31,15 @@ class GapSchema extends Schema_1.Schema {
31
31
  uid: schema.uid,
32
32
  references: schema.references,
33
33
  revocable: schema.revocable,
34
- }, false, true);
34
+ }, schema.gap, false, true);
35
35
  }
36
36
  /**
37
37
  * Returns a copy of the original schema with no pointers.
38
38
  * @param name
39
39
  * @returns
40
40
  */
41
- static find(name) {
42
- const found = Schema_1.Schema.get(name);
41
+ static find(name, network) {
42
+ const found = Schema_1.Schema.get(name, network);
43
43
  return this.clone(found);
44
44
  }
45
45
  /**
@@ -47,15 +47,15 @@ class GapSchema extends Schema_1.Schema {
47
47
  * @param names
48
48
  * @returns
49
49
  */
50
- static findMany(names) {
51
- const schemas = Schema_1.Schema.getMany(names);
50
+ static findMany(names, network) {
51
+ const schemas = Schema_1.Schema.getMany(names, network);
52
52
  return schemas.map((s) => this.clone(s));
53
53
  }
54
54
  /**
55
55
  * Get all schemas that references this schema.
56
56
  */
57
57
  get children() {
58
- return (0, utils_1.mapFilter)(GapSchema.schemas, (s) => s.references === this.name || s.references === this.uid, (s) => new GapSchema(s, false, true));
58
+ return (0, utils_1.mapFilter)(GapSchema.schemas[this.gap.network], (s) => s.references === this.name || s.references === this.uid, (s) => new GapSchema(s, s.gap, false, true));
59
59
  }
60
60
  }
61
61
  exports.GapSchema = GapSchema;