Skip to content

Commit

Permalink
add coupon-burn type (#43)
Browse files Browse the repository at this point in the history
* add vscode setting to use project typescript version

* add coupon-burn type

* add test for coupon type
  • Loading branch information
jdville03 authored Jan 26, 2023
1 parent bddfb65 commit 057e3e4
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
39 changes: 35 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,19 @@ export const getDomainDefinition = (
case "coupon-kyc":
return getCouponKycDomainDefinition(verifyingContract, actionId, chainId);
case "manager-coupon":
return getManagerCouponDomainDefinition(verifyingContract, actionId, chainId);
return getManagerCouponDomainDefinition(
verifyingContract,
actionId,
chainId
);
case "coupon-nft":
return getNftCouponDomainDefinition(verifyingContract, actionId, chainId);
case "coupon-burn":
return getCouponBurnDomainDefinition(
verifyingContract,
actionId,
chainId
);
default:
throw new Error("unknown type " + message.type);
}
Expand Down Expand Up @@ -273,8 +283,8 @@ export const getCouponKycDomainDefinition = (

const types = {
Message: [
{ name: "kycedMember", type: "address" },
{ name: "memberNonce", type: "uint256"}
{ name: "kycedMember", type: "address" },
{ name: "memberNonce", type: "uint256" },
],
EIP712Domain: getDomainType(),
};
Expand Down Expand Up @@ -336,6 +346,25 @@ export const getNftCouponDomainDefinition = (
return { domain, types };
};

export const getCouponBurnDomainDefinition = (
verifyingContract: string,
actionId: string,
chainId: number
) => {
const domain = getMessageDomainType(chainId, verifyingContract, actionId);

const types = {
Message: [
{ name: "authorizedMember", type: "address" },
{ name: "amount", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
EIP712Domain: getDomainType(),
};

return { domain, types };
};

export const getDomainType = () => {
return [
{ name: "name", type: "string" },
Expand Down Expand Up @@ -367,7 +396,9 @@ export const prepareMessage = (message: MessageWithType) => {
return message;
case "manager-coupon":
return message;
case "coupon-nft":
case "coupon-nft":
return message;
case "coupon-burn":
return message;
default:
throw new Error("unknown type " + message.type);
Expand Down
34 changes: 34 additions & 0 deletions tests/getCouponBurnDomainDefinition.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
DEFAULT_ACTION_ID,
DEFAULT_CHAIN_ID,
DEFAULT_VERIFYING_CONTRACT,
} from "./utils";
import { getDomainType, getCouponBurnDomainDefinition } from "../index";

describe("getCouponBurnDomainDefinition unit tests", () => {
test("should return correct data", () => {
expect(
getCouponBurnDomainDefinition(
DEFAULT_VERIFYING_CONTRACT,
DEFAULT_ACTION_ID,
DEFAULT_CHAIN_ID
)
).toEqual({
domain: {
actionId: DEFAULT_ACTION_ID,
chainId: DEFAULT_CHAIN_ID,
name: "Snapshot Message",
verifyingContract: DEFAULT_VERIFYING_CONTRACT,
version: "4",
},
types: {
Message: [
{ name: "authorizedMember", type: "address" },
{ name: "amount", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
EIP712Domain: getDomainType(),
},
});
});
});
34 changes: 34 additions & 0 deletions tests/getCouponDomainDefinition.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
DEFAULT_ACTION_ID,
DEFAULT_CHAIN_ID,
DEFAULT_VERIFYING_CONTRACT,
} from "./utils";
import { getDomainType, getCouponDomainDefinition } from "../index";

describe("getCouponDomainDefinition unit tests", () => {
test("should return correct data", () => {
expect(
getCouponDomainDefinition(
DEFAULT_VERIFYING_CONTRACT,
DEFAULT_ACTION_ID,
DEFAULT_CHAIN_ID
)
).toEqual({
domain: {
actionId: DEFAULT_ACTION_ID,
chainId: DEFAULT_CHAIN_ID,
name: "Snapshot Message",
verifyingContract: DEFAULT_VERIFYING_CONTRACT,
version: "4",
},
types: {
Message: [
{ name: "authorizedMember", type: "address" },
{ name: "amount", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
EIP712Domain: getDomainType(),
},
});
});
});
9 changes: 8 additions & 1 deletion types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ export type PrepareProposalPayloadReturn = {
};

export type MessageWithType = Record<string, any> & {
type: SnapshotType | "result" | "coupon" | "coupon-kyc" | "manager-coupon" | "coupon-nft";
type:
| SnapshotType
| "result"
| "coupon"
| "coupon-kyc"
| "manager-coupon"
| "coupon-nft"
| "coupon-burn";
};

export type VoteEntry = {
Expand Down

0 comments on commit 057e3e4

Please sign in to comment.