Skip to content

Commit

Permalink
Merge branch 'dev' into chore/add-datamapper-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
satyajeetkolhapure authored Oct 10, 2023
2 parents b1e58a8 + 1be2180 commit 3d62674
Show file tree
Hide file tree
Showing 8 changed files with 2,179 additions and 51 deletions.
2,053 changes: 2,004 additions & 49 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions sdk/examples/utils/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import VeraxSdk from "../../src/VeraxSdk";

const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET);

// This example relies on a complex Schema written by Clique
console.log(
JSON.stringify(
veraxSdk.utils.decode(
"((string type, ((string name, uint256 count)[] GameGenres, (string name, uint256 count)[] GameSetting, (string name, uint256 count)[] PlayStyle) items, (string type, (string level, string hoursPlayedSteam) items) subHoursPlayedSteam, (string type, (string topTag, string hoursSteam) items) gameGenresTopTagByHoursSteam, (string type, (string playstyle, string hoursSteam) items) playstyleTopTagByHoursSteam, string sumHourByTop10GamesSteam) userGamingPieChartSteam)",
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000004e000000000000000000000000000000000000000000000000000000000000006200000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000000352504700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000000967616d6567656e72650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000007c000000000000000000000000000000000000000000000000000000000000000873657474696e6731000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000007d0000000000000000000000000000000000000000000000000000000000000009706c61795374796c65000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000001774797065537562486f757273506c61796564537465616d0000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000053e39393939000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000331323500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000207479706567616d6547656e726573546f705461674279486f757273537465616d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000006746f7054616700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004313236380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000001f74797065706c61797374796c65546f705461674279486f757273537465616d00000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000009706c61797374796c6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006313235383734000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000093132353839363534310000000000000000000000000000000000000000000000",
),
(key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged
2,
),
);
59 changes: 59 additions & 0 deletions sdk/examples/utils/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import VeraxSdk from "../../src/VeraxSdk";

const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET);

// This example relies on a complex Schema written by Clique
console.log(
veraxSdk.utils.encode(
"((string type, ((string name, uint256 count)[] GameGenres, (string name, uint256 count)[] GameSetting, (string name, uint256 count)[] PlayStyle) items, (string type, (string level, string hoursPlayedSteam) items) subHoursPlayedSteam, (string type, (string topTag, string hoursSteam) items) gameGenresTopTagByHoursSteam, (string type, (string playstyle, string hoursSteam) items) playstyleTopTagByHoursSteam, string sumHourByTop10GamesSteam) userGamingPieChartSteam)",
[
{
userGamingPieChartSteam: {
type: "RPG",
items: {
GameGenres: [
{
name: "gamegenre",
count: 123,
},
],
GameSetting: [
{
name: "setting1",
count: 124,
},
],
PlayStyle: [
{
name: "playStyle",
count: 125,
},
],
},
subHoursPlayedSteam: {
type: "typeSubHoursPlayedSteam",
items: {
level: ">9999",
hoursPlayedSteam: "125",
},
},
gameGenresTopTagByHoursSteam: {
type: "typegameGenresTopTagByHoursSteam",
items: {
topTag: "topTag",
hoursSteam: "1268",
},
},
playstyleTopTagByHoursSteam: {
type: "typeplaystyleTopTagByHoursSteam",
items: {
playstyle: "playstyle",
hoursSteam: "125874",
},
},
sumHourByTop10GamesSteam: "125896541",
},
},
],
),
);
5 changes: 5 additions & 0 deletions sdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
// ... other Jest configuration options
};
9 changes: 8 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
"portal:one": "ts-node examples/portal/findOneById.ts",
"schema:all": "ts-node examples/schema/findBy.ts",
"schema:one": "ts-node examples/schema/findOneById.ts",
"test": "jest",
"utils:attestation:counter": "ts-node examples/utils/getAttestationIdCounter.ts",
"utils:attestation:version": "ts-node examples/utils/getVersionNumber.ts",
"utils:decode": "ts-node examples/utils/decode.ts",
"utils:encode": "ts-node examples/utils/encode.ts",
"utils:module:counter": "ts-node examples/utils/getModulesNumber.ts",
"utils:portal:counter": "ts-node examples/utils/getPortalsCount.ts",
"utils:schema:counter": "ts-node examples/utils/getSchemasNumber.ts"
Expand All @@ -40,6 +43,10 @@
"viem": "^1.14.0"
},
"devDependencies": {
"ts-node": "^10.9.1"
"@types/jest": "^29.5.5",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"vite": "^4.4.9"
}
}
11 changes: 10 additions & 1 deletion sdk/src/dataMapper/UtilsDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { abiAttestationRegistry } from "../abi/AttestationRegistry";
import { abiModuleRegistry } from "../abi/ModuleRegistry";
import { abiPortalRegistry } from "../abi/PortalRegistry";
import { abiSchemaRegistry } from "../abi/SchemaRegistry";
import { decode, encode } from "../utils/abiCoder";

export default class ModuleDataMapper extends BaseDataMapper {
export default class UtilsDataMapper extends BaseDataMapper {
typeName = "counter";
gqlInterface = `{
attestations
Expand Down Expand Up @@ -52,4 +53,12 @@ export default class ModuleDataMapper extends BaseDataMapper {
functionName: "getAttestationIdCounter",
});
}

encode(schema: string, values: unknown[]): `0x${string}` {
return encode(schema, values);
}

decode(schema: string, attestationData: `0x${string}`): readonly unknown[] {
return decode(schema, attestationData);
}
}
9 changes: 9 additions & 0 deletions sdk/src/utils/abiCoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { decodeAbiParameters, encodeAbiParameters, parseAbiParameters } from "viem";

export function encode(schema: string, values: unknown[]): `0x${string}` {
return encodeAbiParameters(parseAbiParameters(schema), values);
}

export function decode(schema: string, attestationData: `0x${string}`): readonly unknown[] {
return decodeAbiParameters(parseAbiParameters(schema), attestationData);
}
69 changes: 69 additions & 0 deletions sdk/test/dataMapper/AttestationDataMapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import AttestationDataMapper from "../../src/dataMapper/AttestationDataMapper";
import VeraxSdk from "../../src/VeraxSdk";
import { createPublicClient, PublicClient, http } from "viem";
import { ApolloClient, InMemoryCache, ApolloQueryResult, gql } from "@apollo/client/core";
//TODO : This is a basic test example. mock data and assertions should be more precise
describe("AttestationDataMapper", () => {
let mockApolloClient: ApolloClient<object>;
let web3Client: PublicClient;
let attestationDataMapper: AttestationDataMapper;
const typeName: string = "attestation";
const gqlInterface: string = `{
id
schemaId
replacedBy
attester
portal
attestedDate
expirationDate
revocationDate
version
revoked
subject
attestationData
schemaString
decodedData
}`;

beforeAll(() => {
// Create web3Client
web3Client = createPublicClient({
chain: VeraxSdk.DEFAULT_LINEA_TESTNET.chain,
transport: http(),
});

// Create a mock Apollo Client with an in-memory cache
mockApolloClient = new ApolloClient({
cache: new InMemoryCache(),
});

attestationDataMapper = new AttestationDataMapper(VeraxSdk.DEFAULT_LINEA_TESTNET, web3Client, mockApolloClient);
});

afterEach(() => {
// Clear the mock function's call history after each test
jest.clearAllMocks();
});

describe("findOneById", () => {
it("should return the expected attestation", async () => {
const attestationId = "attestationId1";
// Mock the behavior of the query method
const queryMock = jest.spyOn(mockApolloClient, "query");
queryMock.mockResolvedValueOnce({
data: {
attestation: { result: "success" },
},
} as ApolloQueryResult<unknown>);

const result = await attestationDataMapper.findOneById(attestationId);

// Assert
expect(result).toBe('{"result":"success"}');
expect(mockApolloClient.query).toHaveBeenCalledWith({
query: gql(`query GetOne($id: ID!) { ${typeName}(id: $id) ${gqlInterface} }`),
variables: { id: attestationId },
});
});
});
});

0 comments on commit 3d62674

Please sign in to comment.