@gearbox-protocol/testnets-sdk
Typed client for the anvil-manager v1 API, which runs the anvil forks Gearbox calls testnets. The types are generated from the Go backend (backend/manager/core) by tygo; run pnpm generate:types in frontend after changing them.
@gearbox-protocol/sdk (>=18) and viem (^2) are peer dependencies.
Usage
One client sends one token. Create with a forks:create API key, then switch to the created testnet's accessToken. It grants forks:read and forks:write on that testnet only, and expires with it:
import { TestnetsAPI } from "@gearbox-protocol/testnets-sdk";
const api = new TestnetsAPI({ token: process.env.ANVIL_MANAGER_TOKEN });
const created = await api.create({ network: "Mainnet", timeout: "2h" });
// or: await api.clone("mainnet-persistent", { description: "my clone" });
const fork = api.withToken(created.accessToken);
const status = await fork.waitUntilReady(created.executionId, { strict: true });
// ... use status.rpcUrl ...
const task = await fork.startTask(created.executionId, { id: "optimist" });
const done = await fork.waitForTask(created.executionId, task.id);
const result = await fork.getTaskResult<unknown>(created.executionId, done.id);
const faucet = await fork.getScriptResult<unknown>(
created.executionId,
"deploy-faucet",
);
const file = await fork.getArtifact(
created.executionId,
"deploy-faucet/output/addresses.json",
);
await fork.kill(created.executionId);
getScriptResult and getTaskResult return a step's output.json, parsed with json_parse. A script result needs forks:read; a task result is public. Nothing a step writes is secret: inputs, outputs and the shared dir are synced to S3.
Writing a step
Every script and task gets the same env (SHARED_DIR, WORK_DIR, INPUT_DIR, OUTPUT_DIR, OUTPUT_JSON, and INPUT_JSON when it has an input), typed as StepEnv; the path constants are exported from the root. STEP_CONVENTION.md has the full layout. Node steps can use @gearbox-protocol/testnets-sdk/node:
import {
assertWorkDir,
readInputJson,
writeOutputJson,
} from "@gearbox-protocol/testnets-sdk/node";
assertWorkDir();
const input = await readInputJson<MyInput>(); // undefined when the step has no input
await writeOutputJson(result); // json_stringify to $OUTPUT_JSON
Webhooks
waitUntilReady and waitForTask poll for up to 10 minutes and 1 hour. Where a process cannot live that long, e.g. in a serverless function, let anvil-manager call back instead:
const created = await api.create({
network: "Mainnet",
webhook: "https://hooks.gearbox.fi/fork?secret=...",
});
// later, in the handler of fork:ready:
await fork.startTask(created.executionId, {
id: "optimist",
webhook: "https://hooks.gearbox.fi/task?secret=...",
});
clone takes webhook too, and so do the PermissionlessTestnetsAPI presets. A fork webhook gets fork:ready, and finally fork:failed or fork:terminated. A task webhook gets task:finished. Every body is a WebhookEvent.
A webhook is never stored, so its URL may carry a secret. It must go to a host anvil-manager allows. Events can be lost, so keep a deadline and fall back to one get or getTask call. The backend README documents when each event fires and how it is delivered.
Errors
Every failure is a TestnetsError subclass: TestnetStatusError (non-2xx, with TestnetNotFoundError for 404), TestnetRequestFailedError, TestnetFailedError, TestnetWaitTimeoutError and IpfsDownloadError.
Permissionless testnets
@gearbox-protocol/testnets-sdk/permissionless adds PermissionlessTestnetsAPI, a TestnetsAPI that has some testnet presets:
import { PermissionlessTestnetsAPI } from "@gearbox-protocol/testnets-sdk/permissionless";
const api = new PermissionlessTestnetsAPI({
token: process.env.ANVIL_MANAGER_TOKEN,
});
// the input is any JSON with a chainId and, optionally, a marketConfigurator
const created = await api.createPermissionless({ input: timelockTxs });
// or from IPFS, on top of a persistent testnet:
// await api.createPermissionless({ cid }, { cloneFrom: "mainnet-persistent", scriptVersion: "staging" });
const fork = api.withToken(created.accessToken);
await fork.waitUntilReady(created.executionId);
const state = await fork.getState(created.executionId); // GearboxState
The input is sent as the input of the execute_txs script (EXECUTE_SCRIPT_ID), and getState is getScriptResult of that script.
Manual testing
ANVIL_MANAGER_TOKEN=... pnpm create-testnet
# against a local backend
ANVIL_MANAGER_URL=http://localhost:8080 ANVIL_MANAGER_TOKEN=... pnpm create-testnet
Publishing
The package is published by hand to the next channel. The version bump stays out of git:
cd frontend/packages/testnets-sdk && pnpm build
npm version 0.1.0-next.0 --no-git-tag-version
pnpm publish --tag next --no-git-checks && git checkout package.json