Skip to content

Commit

Permalink
feat: added revoke and simulateRevoke
Browse files Browse the repository at this point in the history
  • Loading branch information
kolhapuresatyajeet committed Oct 12, 2023
1 parent d0b3281 commit eb7b3af
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
18 changes: 17 additions & 1 deletion sdk/examples/portal/portalExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ export default class PortalExamples {

if (methodName.toLowerCase() == "replace" || methodName == "") console.log(await this.veraxSdk.portal.replace());

if (methodName.toLowerCase() == "revoke" || methodName == "") console.log(await this.veraxSdk.portal.revoke());
if (methodName.toLowerCase() == "revoke" || methodName == "") {
console.log(
await this.veraxSdk.portal.revoke(
"0xeea25bc2ec56cae601df33b8fc676673285e12cc",
"0x0000000000000000000000000000000000000000000000000000000000000001",
),
);
}

if (methodName.toLowerCase() == "simulateRevoke".toLowerCase() || methodName == "") {
console.log(
await this.veraxSdk.portal.simulateRevoke(
"0xeea25bc2ec56cae601df33b8fc676673285e12cc",
"0x0000000000000000000000000000000000000000000000000000000000000001",
),
);
}

if (methodName.toLowerCase() == "bulkRevoke".toLowerCase() || methodName == "")
console.log(await this.veraxSdk.portal.bulkRevoke());
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/dataMapper/BaseDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default abstract class BaseDataMapper<T> {
return queryResult.data[this.typeName];
}

async findBy<T extends keyof FilterMap>(whereClause: Partial<FilterMap[T]>) {
async findBy<TFilter extends keyof FilterMap>(whereClause: Partial<FilterMap[TFilter]>) {
const queryResult = await this.apolloClient.query<QueryResult<Array<T>, typeof this.typeName>>({
query: gql(`query GetBy { ${this.typeName}s(where: ${stringifyWhereClause(whereClause)}) ${this.gqlInterface} }`),
});
Expand Down
49 changes: 37 additions & 12 deletions sdk/src/dataMapper/PortalDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal> {

return request;
} catch (err) {
if (err instanceof BaseError) {
const revertError = err.walk((err) => err instanceof ContractFunctionRevertedError);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
console.error(`Failing with ${errorName}`);
}
}
console.error(err);

throw new Error("Simulation failed");
this.handleError(err);
}
}

Expand All @@ -64,8 +55,29 @@ export default class PortalDataMapper extends BaseDataMapper<Portal> {
throw new Error("Not implemented");
}

async revoke() {
throw new Error("Not implemented");
async simulateRevoke(portalAddress: Address, attestationId: string) {
try {
const { request } = await this.web3Client.simulateContract({
address: portalAddress,
abi: abiDefaultPortal,
functionName: "revoke",
account: this.walletClient.account,
args: [attestationId],
});

return request;
} catch (err) {
this.handleError(err);
}
}

async revoke(portalAddress: Address, attestationId: string) {
const request = await this.simulateRevoke(portalAddress, attestationId);
const hash: Hash = await this.walletClient.writeContract(request);

console.log(`Transaction sent with hash ${hash}`);

return hash;
}

async bulkRevoke() {
Expand All @@ -83,4 +95,17 @@ export default class PortalDataMapper extends BaseDataMapper<Portal> {
async clone() {
throw new Error("Not implemented");
}

private handleError(err: unknown): never {
if (err instanceof BaseError) {
const revertError = err.walk((err) => err instanceof ContractFunctionRevertedError);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
console.error(`Failing with ${errorName}`);
}
}
console.error(err);

throw new Error("Simulation failed");
}
}

0 comments on commit eb7b3af

Please sign in to comment.