Open this in Axiom REPL →
Created
March 12, 2024 07:04
-
-
Save hackerchai/a291a92aaef0d1948bad0c33088b4f07 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// _ _____ ______ _____ _ | |
// /\ (_) | __ \| ____| __ \| | | |
// / \ __ ___ ___ _ __ ___ | |__) | |__ | |__) | | | |
// / /\ \ \ \/ / |/ _ \| '_ ` _ \| _ /| __| | ___/| | | |
// / ____ \ > <| | (_) | | | | | | | \ \| |____| | | |____ | |
// /_/ \_\/_/\_\_|\___/|_| |_| |_|_| \_\______|_| |______| | |
// | |
// Docs: https://docs.axiom.xyz/sdk/typescript-sdk/axiom-circuit/ | |
// Example Axiom REPL circuit to prove the average balance of an account over a fixed time period | |
// Number of samples to take. Note that this must be a constant value and NOT an input because the size of | |
// the circuit must be known at compile time. | |
const samples = 8; | |
// Number of blocks between each sample. | |
const spacing = 900; | |
// Validate that the block number is greater than the number of samples times the spacing | |
if (inputs.blockNumber.value() <= (samples * spacing)) { | |
throw new Error("Block number must be greater than the number of samples times the spacing"); | |
} | |
// Perform the block number validation in the circuit as well | |
checkLessThan(mul(samples, spacing), inputs.blockNumber); | |
// Get account balance at the sample block numbers | |
let sampledAccounts = new Array(samples); | |
for (let i = 0; i < samples; i++) { | |
const sampleBlockNumber: CircuitValue = sub(inputs.blockNumber, mul(spacing, i)); | |
const account = getAccount(sampleBlockNumber, inputs.address); | |
sampledAccounts[i] = account; | |
} | |
// Accumulate all of the balances to `total` | |
let total = constant(0); | |
for (const account of sampledAccounts) { | |
const balance: CircuitValue256 = await account.balance(); | |
total = add(total, balance.toCircuitValue()); | |
} | |
// Divide the total amount by the number of samples to get the average value | |
const average = div(total, samples); | |
// We call `addToCallback` on all values that we would like to be passed to our contract after the circuit has | |
// been proven in ZK. The values can then be handled by our contract once the prover calls the callback function. | |
addToCallback(inputs.blockNumber); | |
addToCallback(inputs.address); | |
addToCallback(average); |
This file contains hidden or 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
{ | |
"blockNumber": 5000000, | |
"address": "0xEaa455e4291742eC362Bc21a8C46E5F2b5ed4701" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment