Skip to content

Commit

Permalink
feat(sdk): As an Issuer, I want to pass my custom ABI to the SDK to g…
Browse files Browse the repository at this point in the history
…et contextualized errors (#796)
  • Loading branch information
Chirag-S-Kotian authored Nov 26, 2024
1 parent 7f57adb commit efde4d0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 29 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
98 changes: 70 additions & 28 deletions sdk/src/dataMapper/PortalDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Portal_filter, Portal_orderBy } from "../../.graphclient";
import { abiPortalRegistry } from "../abi/PortalRegistry";
import { handleError } from "../utils/errorHandler";
import { executeTransaction } from "../utils/transactionSender";
import { Abi } from "viem";

export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filter, Portal_orderBy> {
typeName = "portal";
Expand All @@ -27,6 +28,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayload: AttestationPayload,
validationPayloads: string[],
value: bigint = 0n,
customAbi?: Abi,
) {
const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId);
if (!matchingSchema) {
Expand All @@ -41,6 +43,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
validationPayloads,
],
value,
customAbi,
);
}

Expand All @@ -50,15 +53,17 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
validationPayloads: string[],
waitForConfirmation: boolean = false,
value: bigint = 0n,
customAbi?: Abi,
) {
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?: Abi,
) {
const attestationPayloadsArg = [];

Expand All @@ -76,7 +81,13 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationData,
]);
}
return this.simulatePortalContract(portalAddress, "bulkAttest", [attestationPayloadsArg, validationPayloads]);
return this.simulatePortalContract(
portalAddress,
"bulkAttest",
[attestationPayloadsArg, validationPayloads],
0n,
customAbi,
);
}

async simulateAttestV2(
Expand Down Expand Up @@ -117,26 +128,32 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
waitForConfirmation: boolean = false,
customAbi?: Abi,
) {
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?: Abi) {
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?: Abi) {
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?: Abi) {
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?: Abi,
) {
const request = await this.simulateBulkRevoke(portalAddress, attestationIds, customAbi);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

Expand All @@ -145,17 +162,24 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationId: string,
attestationPayload: AttestationPayload,
validationPayloads: string[],
customAbi?: Abi,
) {
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 +188,15 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayload: AttestationPayload,
validationPayloads: string[],
waitForConfirmation: boolean = false,
customAbi?: Abi,
) {
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 +205,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationIds: string[],
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
customAbi?: Abi,
) {
const attestationPayloadsArg = [];

Expand All @@ -190,11 +222,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,12 +237,14 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayloads: AttestationPayload[],
validationPayloads: string[][],
waitForConfirmation: boolean = false,
customAbi?: Abi,
) {
const request = await this.simulateBulkReplace(
portalAddress,
attestationIds,
attestationPayloads,
validationPayloads,
customAbi,
);
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}
Expand Down Expand Up @@ -257,8 +293,13 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
return executeTransaction(request, this.web3Client, this.walletClient, waitForConfirmation);
}

async getPortalByAddress(id: Address) {
return this.executePortalRegistryReadMethod("getPortalByAddress", [id]);
async getPortalByAddress(address: Address) {
return await this.web3Client.readContract({
address: this.conf.portalRegistryAddress,
abi: abiPortalRegistry,
functionName: "getPortal",
args: [address],
});
}

async getPortalsNumber() {
Expand All @@ -268,7 +309,6 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
async isPortalRegistered(id: Address) {
return this.executePortalRegistryReadMethod("isRegistered", [id]);
}

private async executePortalRegistryReadMethod(functionName: string, args: unknown[]) {
return this.web3Client.readContract({
abi: abiPortalRegistry,
Expand All @@ -294,24 +334,26 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
handleError(ActionType.Simulation, err);
}
}

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

const abi = [...abiDefaultPortal, ...(customAbi || [])];

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

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

0 comments on commit efde4d0

Please sign in to comment.