-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from openlawteam/offchain-proof-helpers
Added offchain proof helpers
- Loading branch information
Showing
11 changed files
with
566 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.class | ||
*.log | ||
.env | ||
|
||
# sbt specific | ||
.cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
getOffchainVotingProof, | ||
SnapshotOffchainProofResponse, | ||
} from "../utils"; | ||
import { DEFAULT_SNAPSHOT_HUB_API_URL } from "./msw-mocks/helpers"; | ||
import { rest, server } from "./msw-mocks/server"; | ||
import { snapshotAPIOffchainProofResponse } from "./msw-mocks/rest-responses/snapshot-api"; | ||
|
||
const DEFAULT_MERKLE_ROOT_HEX: string = | ||
"0x2f6a1ec9f67c87e7956228a0838b0980748f2dda936a0ebaf3e929f192fa7b6c"; | ||
|
||
describe("getOffchainVotingProof unit tests", () => { | ||
test("can fetch proof", async () => { | ||
let testResponse: SnapshotOffchainProofResponse | undefined; | ||
|
||
testResponse = await getOffchainVotingProof( | ||
DEFAULT_SNAPSHOT_HUB_API_URL, | ||
"space", | ||
DEFAULT_MERKLE_ROOT_HEX | ||
); | ||
|
||
expect(testResponse).toMatchObject(snapshotAPIOffchainProofResponse); | ||
}); | ||
|
||
test('can return "undefined" if no proof exists', async () => { | ||
server.use( | ||
rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`, | ||
(_req, res, ctx) => res(ctx.status(404)) | ||
) | ||
); | ||
|
||
let testResponse: SnapshotOffchainProofResponse | undefined; | ||
let testError: Error | undefined = undefined; | ||
|
||
try { | ||
testResponse = await getOffchainVotingProof( | ||
DEFAULT_SNAPSHOT_HUB_API_URL, | ||
"space", | ||
DEFAULT_MERKLE_ROOT_HEX | ||
); | ||
} catch (error) { | ||
testError = error; | ||
} | ||
|
||
expect(testResponse).toBe(undefined); | ||
expect(testError).toBe(undefined); | ||
}); | ||
|
||
test("can throw error when server error", async () => { | ||
server.use( | ||
rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`, | ||
(_req, res, ctx) => res(ctx.status(500)) | ||
) | ||
); | ||
|
||
let testError: any; | ||
|
||
try { | ||
await getOffchainVotingProof( | ||
DEFAULT_SNAPSHOT_HUB_API_URL, | ||
"space", | ||
DEFAULT_MERKLE_ROOT_HEX | ||
); | ||
} catch (error) { | ||
testError = error; | ||
} | ||
|
||
expect(testError?.message).toMatch( | ||
/something went wrong while getting the off-chain vote proof\./i | ||
); | ||
}); | ||
|
||
test("can throw error when client error", async () => { | ||
server.use( | ||
rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`, | ||
(_req, res, ctx) => res(ctx.status(400)) | ||
) | ||
); | ||
|
||
let testError: any; | ||
|
||
try { | ||
// Using fake data | ||
await getOffchainVotingProof( | ||
DEFAULT_SNAPSHOT_HUB_API_URL, | ||
"space", | ||
DEFAULT_MERKLE_ROOT_HEX | ||
); | ||
} catch (error) { | ||
testError = error; | ||
} | ||
|
||
expect(testError?.message).toMatch( | ||
/something went wrong while getting the off-chain vote proof\./i | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,83 @@ | ||
import { rest } from "msw"; | ||
|
||
const snapshotAPIStatusHandler = rest.get("*/api", (_req, res, ctx) => | ||
res( | ||
ctx.json({ | ||
data: { | ||
name: "snapshot-hub", | ||
network: "testnet", | ||
version: "0.1.2", | ||
tag: "alpha", | ||
relayer: "0xEd7B3f2902f2E1B17B027bD0c125B674d293bDA0", | ||
}, | ||
}) | ||
) | ||
); | ||
|
||
// Export all mock handlers | ||
export const handlers = [snapshotAPIStatusHandler]; | ||
import { | ||
snapshotAPIDraftResponse, | ||
snapshotAPIOffchainProofResponse, | ||
snapshotAPIProposalResponse, | ||
snapshotAPIRootResponse, | ||
snapshotAPISpaceResponse, | ||
snapshotAPISubmitMessage, | ||
} from "./rest-responses/snapshot-api"; | ||
import { DEFAULT_SNAPSHOT_HUB_API_URL } from "./helpers"; | ||
|
||
/** | ||
* Snapshot API | ||
*/ | ||
|
||
const getSnapshotAPIStatus = rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api`, | ||
(_req, res, ctx) => | ||
res( | ||
ctx.json({ | ||
data: { | ||
name: "snapshot-hub", | ||
network: "testnet", | ||
version: "0.1.2", | ||
tag: "alpha", | ||
relayer: "0xEd7B3f2902f2E1B17B027bD0c125B674d293bDA0", | ||
}, | ||
}) | ||
) | ||
); | ||
|
||
const getSnapshotAPIRoot = rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api`, | ||
async (_req, res, ctx) => res(ctx.json(snapshotAPIRootResponse)) | ||
); | ||
|
||
const getSnapshotAPISpace = rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/spaces/:spaceName`, | ||
async (_req, res, ctx) => res(ctx.json(snapshotAPISpaceResponse)) | ||
); | ||
|
||
const getSnapshotAPIDraft = rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/draft/:id`, | ||
async (_req, res, ctx) => res(ctx.json(snapshotAPIDraftResponse)) | ||
); | ||
|
||
const getSnapshotAPIProposal = rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/proposal/:id`, | ||
async (_req, res, ctx) => res(ctx.json(snapshotAPIProposalResponse)) | ||
); | ||
|
||
const postSnapshotAPIMessage = rest.post( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/message`, | ||
async (_req, res, ctx) => res(ctx.json(snapshotAPISubmitMessage)) | ||
); | ||
|
||
const postSnapshotAPIOffchainProof = rest.post( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proofs`, | ||
(_req, res, ctx) => res(ctx.status(201)) | ||
); | ||
|
||
const getSnapshotAPIOffchainProof = rest.get( | ||
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`, | ||
(_req, res, ctx) => res(ctx.json(snapshotAPIOffchainProofResponse)) | ||
); | ||
|
||
/** | ||
* HANDLERS TO EXPORT | ||
*/ | ||
|
||
const handlers = [ | ||
getSnapshotAPIDraft, | ||
getSnapshotAPIOffchainProof, | ||
getSnapshotAPIProposal, | ||
getSnapshotAPIRoot, | ||
getSnapshotAPIStatus, | ||
getSnapshotAPISpace, | ||
postSnapshotAPIMessage, | ||
postSnapshotAPIOffchainProof, | ||
]; | ||
|
||
export { handlers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const DEFAULT_ETH_ADDRESS: string = | ||
"0x04028Df0Cea639E97fDD3fC01bA5CC172613211D"; | ||
|
||
export const DEFAULT_SPACE: string = "tributetest"; | ||
|
||
export const DEFAULT_PROPOSAL_HASH: string = | ||
"0x4662dd46b8ca7ce0852426f20bc53b02335432089bbe3a4c510b36741d81ca75"; | ||
|
||
export const DEFAULT_DRAFT_HASH: string = | ||
"0xb22ca9af120bfddfc2071b5e86a9edee6e0e2ab76399e7c2d96a9d502f5c1241"; | ||
|
||
export const DEFAULT_SIG: string = | ||
"0x042bfdd90cddaf67978fe3e3294f1c1aad017a06dde072c02ad1549914b8bf8e316c1addbad510247d2e5175006c272632a3b79120898482cf5e0187a1283b811b"; | ||
|
||
export const DEFAULT_DAO_NAME: string = "test-dao"; | ||
|
||
export const DEFAULT_ADAPTER_OR_EXTENSION_ID: string = | ||
"0x86e4d415f8203aaabba34505e3ceb7b05f5255120bc6ae19dd5f4823ce4e1fb9"; | ||
|
||
export const DEFAULT_DELEGATED_ADDRESS: string = | ||
"0xA51086a07AE92Ae8c29E7CD34421c16fd666595c"; | ||
|
||
// Fake Snapshot Hub host | ||
export const DEFAULT_SNAPSHOT_HUB_API_URL: string = | ||
"http://localhost:8080/snapshot-hub"; |
Oops, something went wrong.