|
| 1 | +import * as assert from "assert"; |
| 2 | + |
| 3 | +import { getDevnetApi } from "../src/substrate"; |
| 4 | +import { getPublicClient } from "../src/utils"; |
| 5 | +import { ETH_LOCAL_URL } from "../src/config"; |
| 6 | +import { devnet } from "@polkadot-api/descriptors"; |
| 7 | +import { PublicClient } from "viem"; |
| 8 | +import { TypedApi } from "polkadot-api"; |
| 9 | +import { toViemAddress } from "../src/address-utils"; |
| 10 | +import { IDrandABI, IDRAND_ADDRESS } from "../src/contracts/drand"; |
| 11 | + |
| 12 | +describe("Test Drand Precompile", () => { |
| 13 | + let publicClient: PublicClient; |
| 14 | + let api: TypedApi<typeof devnet>; |
| 15 | + |
| 16 | + before(async () => { |
| 17 | + publicClient = await getPublicClient(ETH_LOCAL_URL); |
| 18 | + api = await getDevnetApi(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("Drand Randomness Functions", () => { |
| 22 | + it("getLastStoredRound returns a value", async () => { |
| 23 | + const lastRound = await publicClient.readContract({ |
| 24 | + abi: IDrandABI, |
| 25 | + address: toViemAddress(IDRAND_ADDRESS), |
| 26 | + functionName: "getLastStoredRound", |
| 27 | + args: [], |
| 28 | + }); |
| 29 | + |
| 30 | + const lastRoundFromApi = await api.query.Drand.LastStoredRound.getValue({ at: "best" }); |
| 31 | + |
| 32 | + assert.ok(lastRound !== undefined, "getLastStoredRound should return a value"); |
| 33 | + assert.strictEqual( |
| 34 | + typeof lastRound, |
| 35 | + "bigint", |
| 36 | + "getLastStoredRound should return a bigint" |
| 37 | + ); |
| 38 | + assert.ok(lastRound === lastRoundFromApi, "Last stored round should match the value from the API"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("getRandomness returns bytes32 for a round", async () => { |
| 42 | + const lastRound = await publicClient.readContract({ |
| 43 | + abi: IDrandABI, |
| 44 | + address: toViemAddress(IDRAND_ADDRESS), |
| 45 | + functionName: "getLastStoredRound", |
| 46 | + args: [], |
| 47 | + }); |
| 48 | + |
| 49 | + const randomness = await publicClient.readContract({ |
| 50 | + abi: IDrandABI, |
| 51 | + address: toViemAddress(IDRAND_ADDRESS), |
| 52 | + functionName: "getRandomness", |
| 53 | + args: [lastRound], |
| 54 | + }); |
| 55 | + |
| 56 | + const pulseFromApi = await api.query.Drand.Pulses.getValue(lastRound, { at: "best" }); |
| 57 | + const randomnessFromApi = pulseFromApi?.randomness.asHex(); |
| 58 | + |
| 59 | + assert.ok(randomness !== undefined, "getRandomness should return a value"); |
| 60 | + assert.strictEqual( |
| 61 | + typeof randomness, |
| 62 | + "string", |
| 63 | + "getRandomness should return a hex string (bytes32)" |
| 64 | + ); |
| 65 | + assert.strictEqual( |
| 66 | + randomness.length, |
| 67 | + 66, |
| 68 | + "bytes32 should be 0x + 64 hex chars" |
| 69 | + ); |
| 70 | + assert.strictEqual( |
| 71 | + randomness, |
| 72 | + randomnessFromApi, |
| 73 | + "Randomness should match the value from the API" |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("getRandomness for non-existent round returns zero bytes", async () => { |
| 78 | + // Use a very high round number that will not have a stored pulse |
| 79 | + const nonExistentRound = BigInt(999999999); |
| 80 | + const randomness = await publicClient.readContract({ |
| 81 | + abi: IDrandABI, |
| 82 | + address: toViemAddress(IDRAND_ADDRESS), |
| 83 | + functionName: "getRandomness", |
| 84 | + args: [nonExistentRound], |
| 85 | + }); |
| 86 | + |
| 87 | + console.log("randomness", randomness); |
| 88 | + |
| 89 | + assert.ok(randomness !== undefined, "getRandomness should return a value"); |
| 90 | + const zeroBytes32 = "0x" + "0".repeat(64); |
| 91 | + assert.strictEqual( |
| 92 | + randomness.toLowerCase(), |
| 93 | + zeroBytes32, |
| 94 | + "getRandomness for non-existent round should return zero bytes32" |
| 95 | + ); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments