Skip to content

Commit

Permalink
Refactor PortalDataMapper: add customAbi, update simulateAttest, remo…
Browse files Browse the repository at this point in the history
…ve attestV2
  • Loading branch information
Chirag-S-Kotian committed Nov 8, 2024
1 parent fcadbdc commit b8d4782
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 132 deletions.
12 changes: 11 additions & 1 deletion sdk/src/dataMapper/PortalDataMapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ describe("PortalDataMapper", () => {
attestationRegistryAddress: "0x4",
};
const mockWeb3Client = {} as PublicClient;
const mockVeraxSdk = {} as VeraxSdk;
const mockWalletClient = {} as WalletClient;
const mockVeraxSdk = {
schema: {
findOneById: jest.fn(),
},
} as unknown as VeraxSdk;

beforeEach(() => {
portalDataMapper = new PortalDataMapper(mockConf, mockWeb3Client, mockVeraxSdk, mockWalletClient);

// Setup a default mock return value for findOneById
(mockVeraxSdk.schema.findOneById as jest.Mock).mockResolvedValue({
id: "123",
name: "Sample Schema",
});
});

afterEach(() => {
Expand Down
190 changes: 59 additions & 131 deletions sdk/src/dataMapper/PortalDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayload: AttestationPayload,
validationPayloads: string[],
value: bigint = 0n,
customAbi?: any,
) {
const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId);
if (!matchingSchema) {
Expand All @@ -41,6 +42,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
validationPayloads,
],
value,
customAbi,
);
}

Expand All @@ -50,15 +52,17 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
validationPayloads: string[],
waitForConfirmation: boolean = false,
value: bigint = 0n,
customAbi?: any,
) {
const request = await this.simulateAttest(portalAddress, attestationPayload, validationPayloads, value);
const request = await this.simulateAttest(portalAddress, attestationPayload, validationPayloads, value, customAbi);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async simulateBulkAttest(
portalAddress: Address,
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
customAbi?: any,
) {
const attestationPayloadsArg = [];

Expand All @@ -76,67 +80,46 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationData,
]);
}
return this.simulatePortalContract(portalAddress, "bulkAttest", [attestationPayloadsArg, validationPayloads]);
}

async simulateAttestV2(
portalAddress: Address,
attestationPayload: AttestationPayload,
validationPayloads: string[],
value: bigint = 0n,
) {
const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId);
if (!matchingSchema) {
throw new Error("No matching Schema");
}
const attestationData = encode(matchingSchema.schema, attestationPayload.attestationData);
return this.simulatePortalContract(
portalAddress,
"attestV2",
[
[attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData],
validationPayloads,
],
value,
"bulkAttest",
[attestationPayloadsArg, validationPayloads],
0n,
customAbi,
);
}

async attestV2(
portalAddress: Address,
attestationPayload: AttestationPayload,
validationPayloads: string[],
waitForConfirmation: boolean = false,
value: bigint = 0n,
) {
const request = await this.simulateAttestV2(portalAddress, attestationPayload, validationPayloads, value);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async bulkAttest(
portalAddress: Address,
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
waitForConfirmation: boolean = false,
customAbi?: any,
) {
const request = await this.simulateBulkAttest(portalAddress, attestationPayloads, validationPayloads);
const request = await this.simulateBulkAttest(portalAddress, attestationPayloads, validationPayloads, customAbi);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async simulateRevoke(portalAddress: Address, attestationId: string) {
return this.simulatePortalContract(portalAddress, "revoke", [attestationId]);
async simulateRevoke(portalAddress: Address, attestationId: string, customAbi?: any) {
return this.simulatePortalContract(portalAddress, "revoke", [attestationId], 0n, customAbi);
}

async revoke(portalAddress: Address, attestationId: string, waitForConfirmation: boolean = false) {
const request = await this.simulateRevoke(portalAddress, attestationId);
async revoke(portalAddress: Address, attestationId: string, waitForConfirmation: boolean = false, customAbi?: any) {
const request = await this.simulateRevoke(portalAddress, attestationId, customAbi);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async simulateBulkRevoke(portalAddress: Address, attestationIds: string[]) {
return this.simulatePortalContract(portalAddress, "bulkRevoke", [attestationIds]);
async simulateBulkRevoke(portalAddress: Address, attestationIds: string[], customAbi?: any) {
return this.simulatePortalContract(portalAddress, "bulkRevoke", [attestationIds], 0n, customAbi);
}

async bulkRevoke(portalAddress: Address, attestationIds: string[], waitForConfirmation: boolean = false) {
const request = await this.simulateBulkRevoke(portalAddress, attestationIds);
async bulkRevoke(
portalAddress: Address,
attestationIds: string[],
waitForConfirmation: boolean = false,
customAbi?: any,
) {
const request = await this.simulateBulkRevoke(portalAddress, attestationIds, customAbi);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

Expand All @@ -145,17 +128,24 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationId: string,
attestationPayload: AttestationPayload,
validationPayloads: string[],
customAbi?: any,
) {
const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId);
if (!matchingSchema) {
throw new Error("No matching Schema");
}
const attestationData = encode(matchingSchema.schema, attestationPayload.attestationData);
return this.simulatePortalContract(portalAddress, "replace", [
attestationId,
[attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData],
validationPayloads,
]);
return this.simulatePortalContract(
portalAddress,
"replace",
[
attestationId,
[attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData],
validationPayloads,
],
0n,
customAbi,
);
}

async replace(
Expand All @@ -164,8 +154,15 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayload: AttestationPayload,
validationPayloads: string[],
waitForConfirmation: boolean = false,
customAbi?: any,
) {
const request = await this.simulateReplace(portalAddress, attestationId, attestationPayload, validationPayloads);
const request = await this.simulateReplace(
portalAddress,
attestationId,
attestationPayload,
validationPayloads,
customAbi,
);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

Expand All @@ -174,6 +171,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationIds: string[],
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
customAbi?: any,
) {
const attestationPayloadsArg = [];

Expand All @@ -190,11 +188,13 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationData,
]);
}
return this.simulatePortalContract(portalAddress, "bulkReplace", [
attestationIds,
attestationPayloadsArg,
validationPayloads,
]);
return this.simulatePortalContract(
portalAddress,
"bulkReplace",
[attestationIds, attestationPayloadsArg, validationPayloads],
0n,
customAbi,
);
}

async bulkReplace(
Expand All @@ -203,105 +203,33 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
waitForConfirmation: boolean = false,
customAbi?: any,
) {
const request = await this.simulateBulkReplace(
portalAddress,
attestationIds,
attestationPayloads,
validationPayloads,
customAbi,
);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async simulateRegister(id: Address, name: string, description: string, isRevocable: boolean, ownerName: string) {
return this.simulatePortalRegistryContract("register", [id, name, description, isRevocable, ownerName]);
}

async register(
id: Address,
name: string,
description: string,
isRevocable: boolean,
ownerName: string,
waitForConfirmation: boolean = false,
) {
const request = await this.simulateRegister(id, name, description, isRevocable, ownerName);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async simulateDeployDefaultPortal(
modules: Address[],
name: string,
description: string,
isRevocable: boolean,
ownerName: string,
) {
return this.simulatePortalRegistryContract("deployDefaultPortal", [
modules,
name,
description,
isRevocable,
ownerName,
]);
}

async deployDefaultPortal(
modules: Address[],
name: string,
description: string,
isRevocable: boolean,
ownerName: string,
waitForConfirmation: boolean = false,
) {
const request = await this.simulateDeployDefaultPortal(modules, name, description, isRevocable, ownerName);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async getPortalByAddress(id: Address) {
return this.executePortalRegistryReadMethod("getPortalByAddress", [id]);
}

async isPortalRegistered(id: Address) {
return this.executePortalRegistryReadMethod("isRegistered", [id]);
}

private async executePortalRegistryReadMethod(functionName: string, args: unknown[]) {
return this.web3Client.readContract({
abi: abiPortalRegistry,
address: this.conf.portalRegistryAddress,
functionName,
args,
});
}

private async simulatePortalRegistryContract(functionName: string, args: unknown[]) {
if (!this.walletClient) throw new Error("VeraxSDK - Wallet not available");
try {
const { request } = await this.web3Client.simulateContract({
address: this.conf.portalRegistryAddress,
abi: abiPortalRegistry,
functionName,
account: this.walletClient.account,
args,
});

return request;
} catch (err) {
handleError(ActionType.Simulation, err);
}
}

private async simulatePortalContract(
portalAddress: Address,
functionName: string,
args: unknown[],
value: bigint = 0n,
customAbi?: any,
) {
if (!this.walletClient) throw new Error("VeraxSDK - Wallet not available");

const abi = customAbi ? [...abiDefaultPortal, ...customAbi] : abiDefaultPortal;

try {
const { request } = await this.web3Client.simulateContract({
address: portalAddress,
abi: abiDefaultPortal,
abi: abi,
functionName,
account: this.walletClient.account,
args,
Expand Down

0 comments on commit b8d4782

Please sign in to comment.