Skip to content

Commit

Permalink
feat: refactor (#1)
Browse files Browse the repository at this point in the history
* chore: make imports explicit

* feat: add optional arguments to prankFulfill, split test file

* chore: fix CI

* chore: fix CI

* fix: incorrect feeData pass-through

* fix: resolve tsc issue

* feat: add comments

* chore: add natspec

* feat: simplifying test imports

* feat: use cli binary

* fix: minify executable

* fix: build cli during workflow

* fix: linux build sed command

* feat: add mac testing

* fix: run mac test on macos-14

* feat: use right binary based on platform

* fix: macos tests

* fix: remove diff checking for now

* feat: save build files

* feat: add linux build

* feat: use bun instead of executable

* fix: bun installation

* fix: don't install bun in ci

* try fixing ci

* try to fix install sleep

* run tests twice

* try to get tests working

* feat: use node to run cli

* fix: ci

* chore: remove unused fn

* chore: remove bun from .gitignore

---------

Co-authored-by: roshan <[email protected]>
  • Loading branch information
yi-sun and rpalakkal authored Feb 8, 2024
1 parent e2a96bb commit 2876edd
Show file tree
Hide file tree
Showing 16 changed files with 1,863 additions and 700 deletions.
56 changes: 51 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@ on:

jobs:
test:
name: Foundry Tests
name: Foundry Tests Linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-node@v4
with:
node-version: 18

- name: Build CLI
run: |
npm install
mv build/axiom-std-cli-build.js build/axiom-std-cli-temp-build.js
cp src/test/AxiomCli.sol src/test/AxiomCli.sol.temp
npm run build
diff build/axiom-std-cli-build.js build/axiom-std-cli-temp-build.js
diff src/test/AxiomCli.sol src/test/AxiomCli.sol.temp
rm build/axiom-std-cli-temp-build.js
rm src/test/AxiomCli.sol.temp
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
Expand All @@ -29,10 +40,45 @@ jobs:
- name: Check forge tree
run: forge tree

- name: Install NPM packages
run: npm install
- name: Run Foundry tests
run: |
export PROVIDER_URI_SEPOLIA=${{ secrets.PROVIDER_URI_SEPOLIA }}
RUST_BACKTRACE=1 forge test -vvv
test-mac:
name: Foundry Tests Mac
runs-on: macos-14
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-node@v4
with:
node-version: 18

- name: Build CLI
run: |
npm install
mv build/axiom-std-cli-build.js build/axiom-std-cli-temp-build.js
cp src/test/AxiomCli.sol src/test/AxiomCli.sol.temp
npm run build
diff build/axiom-std-cli-build.js build/axiom-std-cli-temp-build.js
diff src/test/AxiomCli.sol src/test/AxiomCli.sol.temp
rm build/axiom-std-cli-temp-build.js
rm src/test/AxiomCli.sol.temp
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Check forge version
run: forge --version

- name: Check forge tree
run: forge tree

- name: Run Foundry tests
run: |
export PROVIDER_URI_SEPOLIA=${{ secrets.PROVIDER_URI_SEPOLIA }}
RUST_BACKTRACE=1 forge test -vvv
RUST_BACKTRACE=1 forge test -vvv
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ docs/
node_modules/
.axiom

dist
dist
Empty file added axiom-std-marker
Empty file.
181 changes: 181 additions & 0 deletions build/axiom-std-cli-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
"use strict";
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};

// dist/utils.js
var require_utils = __commonJS({
"dist/utils.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.getSolidityType = void 0;
var getSolidityType = (type) => {
switch (type) {
case "CircuitValue":
return "uint256";
case "CircuitValue256":
return "uint256";
case "CircuitValue[]":
return "uint256[]";
case "CircuitValue256[]":
return "uint256[]";
default:
throw new Error(`Unknown type ${type}`);
}
};
exports2.getSolidityType = getSolidityType;
}
});

// dist/compile.js
var require_compile = __commonJS({
"dist/compile.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.compile = void 0;
var js_12 = require("@axiom-crypto/circuit/js");
var utils_12 = require("@axiom-crypto/circuit/cliHandler/utils");
var viem_12 = require("viem");
var utils_22 = require_utils();
var compile = async (circuitPath, inputs2, providerUri2) => {
let circuitFunction = "circuit";
const f = await (0, utils_12.getFunctionFromTs)(circuitPath, circuitFunction);
const provider2 = (0, utils_12.getProvider)(providerUri2);
const circuit2 = new js_12.AxiomBaseCircuit({
f: f.circuit,
mock: true,
provider: provider2,
shouldTime: false,
inputSchema: f.inputSchema
});
let abi2 = [];
let inputSchemaJson2 = JSON.parse(f.inputSchema);
let keys2 = Object.keys(inputSchemaJson2);
for (let i = 0; i < keys2.length; i++) {
abi2.push({ "name": keys2[i], "type": (0, utils_22.getSolidityType)(inputSchemaJson2[keys2[i]]) });
}
const rawInputs2 = (0, viem_12.decodeAbiParameters)(abi2, inputs2);
const circuitInputs2 = {};
for (let i = 0; i < keys2.length; i++) {
circuitInputs2[keys2[i]] = rawInputs2[i].toString();
}
try {
const res = await circuit2.mockCompile(circuitInputs2);
const circuitFn = `const ${f.importName} = AXIOM_CLIENT_IMPORT
${f.circuit.toString()}`;
const encoder = new TextEncoder();
const circuitBuild = encoder.encode(circuitFn);
const build = {
...res,
circuit: Buffer.from(circuitBuild).toString("base64")
};
console.log(JSON.stringify(build));
} catch (e) {
console.error(e);
}
};
exports2.compile = compile;
}
});

// dist/prove.js
var require_prove = __commonJS({
"dist/prove.js"(exports, module) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.prove = void 0;
var js_1 = require("@axiom-crypto/circuit/js");
var utils_1 = require("@axiom-crypto/circuit/cliHandler/utils");
var utils_2 = require_utils();
var viem_1 = require("viem");
var core_1 = require("@axiom-crypto/core");
var client_1 = require("@axiom-crypto/client");
var utils_3 = require("@axiom-crypto/client/axiom/utils");
var prove = async (compiledJson, inputs, providerUri, sourceChainId, callbackTarget, callbackExtraData, refundAddress, maxFeePerGas, callbackGasLimit, caller) => {
const decoder = new TextDecoder();
const provider = (0, utils_1.getProvider)(providerUri);
let compiled = JSON.parse(compiledJson);
const decodedArray = Buffer.from(compiled.circuit, "base64");
const raw = decoder.decode(decodedArray);
const AXIOM_CLIENT_IMPORT = require("@axiom-crypto/client");
const circuit = new js_1.AxiomBaseCircuit({
f: eval(raw),
mock: true,
// chainId: options.sourceChainId,
provider,
shouldTime: false,
inputSchema: compiled.inputSchema
});
let abi = [];
let decodedInputSchema = Buffer.from(compiled.inputSchema, "base64");
let inputSchemaJson = JSON.parse(decoder.decode(decodedInputSchema));
let keys = Object.keys(inputSchemaJson);
for (let i = 0; i < keys.length; i++) {
abi.push({ "name": keys[i], "type": (0, utils_2.getSolidityType)(inputSchemaJson[keys[i]]) });
}
const rawInputs = (0, viem_1.decodeAbiParameters)(abi, inputs);
const circuitInputs = {};
for (let i = 0; i < keys.length; i++) {
circuitInputs[keys[i]] = rawInputs[i].toString();
}
const axiom = new core_1.AxiomSdkCore({
providerUri: provider,
chainId: sourceChainId,
version: "v2"
});
try {
let computeQuery;
circuit.loadSavedMock(compiled);
computeQuery = await circuit.mockProve(circuitInputs);
const computeResults = circuit.getComputeResults();
const dataQuery = circuit.getDataQuery();
const res = {
sourceChainId: circuit.getChainId(),
computeQuery,
computeResults,
dataQuery
};
let build = await (0, client_1.buildSendQuery)({
axiom,
dataQuery: res.dataQuery,
computeQuery: res.computeQuery,
callback: {
target: callbackTarget,
extraData: callbackExtraData
},
options: {
refundee: refundAddress,
maxFeePerGas,
callbackGasLimit: Number(callbackGasLimit)
},
caller
});
build.value = build.value.toString();
const query = {
value: build.value,
mock: build.mock,
queryId: build.queryId,
args: (0, utils_3.argsArrToObj)(build.args),
calldata: build.calldata,
computeResults
};
console.log(JSON.stringify(query));
} catch (e) {
console.error(e);
}
};
exports.prove = prove;
}
});

// dist/axiom-std-cli.js
Object.defineProperty(exports, "__esModule", { value: true });
var commander_1 = require("commander");
var compile_1 = require_compile();
var prove_1 = require_prove();
var program = new commander_1.Command("axiom-std");
program.name("axiom-std").usage("axiom-std CLI");
program.command("readCircuit").description("Read and compile a circuit").argument("<circuitPath>", "path to the typescript circuit file").argument("<inputs>", "inputs to the circuit").argument("<providerUri>", "provider to use").action(compile_1.compile);
program.command("prove").description("Prove a circuit").argument("<compiledJson>", "compiled json string").argument("<inputs>", "inputs to the circuit").argument("<providerUri>", "provider to use").argument("<sourceChainId>", "source chain id").argument("<callbackTarget>", "callback target").argument("<callbackExtraData>", "callback extra data").argument("<refundAddress>", "refund address").argument("<maxFeePerGas>", "max fee per gas").argument("<callbackGasLimit>", "callback gas limit").argument("<caller>", "caller").action(prove_1.prove);
program.parseAsync(process.argv);
1 change: 0 additions & 1 deletion cli/index.ts → cli/axiom-std-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Command } from "commander";
import { compile } from "./compile";
import { prove } from "./prove";


const program = new Command("axiom-std");

program.name("axiom-std").usage("axiom-std CLI");
Expand Down
4 changes: 1 addition & 3 deletions cli/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ export const compile = async (
}
// console.log(abi);
// console.log(encodeAbiParameters(abi, [4205938, "0x8018fe32fCFd3d166E8b4c4E37105318A84BA11b"]))
const rawInputs = decodeAbiParameters(abi, inputs as `0x${string}`);
const rawInputs: any[] = decodeAbiParameters(abi, inputs as `0x${string}`);
const circuitInputs: any = {};
for (let i = 0; i < keys.length; i++) {
circuitInputs[keys[i]] = rawInputs[i].toString();
}
// console.log(circuitInputs);



try {
const res = await circuit.mockCompile(circuitInputs);
const circuitFn = `const ${f.importName} = AXIOM_CLIENT_IMPORT\n${f.circuit.toString()}`;
Expand Down
5 changes: 1 addition & 4 deletions cli/prove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { AxiomSdkCore } from "@axiom-crypto/core";
import { buildSendQuery } from "@axiom-crypto/client";
import { argsArrToObj } from '@axiom-crypto/client/axiom/utils';



export const prove = async (
compiledJson: string,
inputs: string,
Expand All @@ -35,7 +33,6 @@ export const prove = async (
const raw = decoder.decode(decodedArray);
const AXIOM_CLIENT_IMPORT = require("@axiom-crypto/client");


const circuit = new AxiomBaseCircuit({
f: eval(raw),
mock: true,
Expand All @@ -54,7 +51,7 @@ export const prove = async (
}
// console.log(abi);
// console.log(encodeAbiParameters(abi, [4205938, "0x8018fe32fCFd3d166E8b4c4E37105318A84BA11b"]))
const rawInputs = decodeAbiParameters(abi, inputs as `0x${string}`);
const rawInputs: any[] = decodeAbiParameters(abi, inputs as `0x${string}`);
const circuitInputs: any = {};
for (let i = 0; i < keys.length; i++) {
circuitInputs[keys[i]] = rawInputs[i].toString();
Expand Down
Loading

0 comments on commit 2876edd

Please sign in to comment.