clawchain/sdk/examples/wagmi-adapter.ts
caelum0x 042e7379f1 feat(sdk,rust): vendored-integration V2 wagmi + V3 alloy + examples/docs
Implements docs/plans/2026-05-31-vendored-integration.md (Option B: adapt libs to
Cosmos, no EVM). V1 viem adapter was already done; this adds:

V2 wagmi (sdk/src/wagmi.ts): defineClawChain, Keplr/Leap connectors, createClawWagmiConfig,
and wagmi-style actions (connect/disconnect/getAccount/getBalance/getBlockNumber/read+
writeContract) over the viem client. signingClientFromConnector wires a wallet's offline
signer; ClawChainClient now accepts an offlineSigner option (client.ts/types.ts) so
Keplr/Leap-signed txs work without a mnemonic. Tests: sdk/src/wagmi.test.ts (7/7) via a
viemClient injection seam.

V3 alloy (clawchain-alloy/): standalone Rust crate (NOT in the vendored alloy/ workspace)
— alloy-style ClawProvider read path (chain_id, block_number, get_balance, query_contract)
over Tendermint RPC + Cosmos REST + CosmWasm. URL-building/parsing are pure, fixture-tested
offline (cargo test: 11 + 1 doctest, no network). tx signing scoped as an honest follow-up.

V4: sdk/examples/wagmi-adapter.ts; viem+wagmi sections in reference-integrations.md and
integrator-quickstart.md.

Verified: sdk npm run build 0 errors + 303 tests pass; cargo build+test pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 00:18:38 +03:00

60 lines
2.0 KiB
TypeScript

import {
createClawWagmiConfig,
createKeplrConnector,
defineClawChain,
connect,
getAccount,
getBalance,
getBlockNumber,
readContract,
type InjectedCosmosWallet,
} from "../src/wagmi.js";
// Define the ClawChain network (wagmi-style chain descriptor).
const chain = defineClawChain({
id: process.env.CLAW_CHAIN_ID ?? "clawchain-testnet-1",
rpcUrl: process.env.CLAW_RPC_URL ?? "http://localhost:26657",
});
// In a real browser, `createKeplrConnector()` reads `window.keplr`. Here we keep the
// example node-runnable: without an injected wallet, connect() throws, so we only
// connect when a wallet is provided. Pass a wallet via the connector factory to test.
const wallet = (globalThis as { keplr?: InjectedCosmosWallet }).keplr;
const config = createClawWagmiConfig({
chain,
connectors: [createKeplrConnector()],
rpcUrl: chain.rpcUrls.default.http[0],
});
// Read-only path works without any wallet (Tendermint + bank + CosmWasm).
await config.client.connect();
const height = await getBlockNumber(config);
console.log(`connected ${chain.id} at height ${height}`);
if (wallet) {
const { address, connector } = await connect(config, "keplr");
console.log(`connected ${connector} as ${address}`);
const acct = getAccount(config);
console.log(`account: ${acct.address} (isConnected=${acct.isConnected})`);
const balance = await getBalance(config);
console.log(`balance: ${balance.value}${balance.denom}`);
// Read a CosmWasm contract (maps to a smart query, not eth_call).
const contract = process.env.CLAW_CONTRACT;
if (contract) {
const cfg = await readContract(config, { address: contract, functionName: "config" });
console.log("contract config:", cfg);
} else {
console.log("set CLAW_CONTRACT to demo readContract against a CosmWasm contract");
}
} else {
console.log(
"no Keplr wallet detected (this is a node example); in a browser, " +
"createKeplrConnector() reads window.keplr to enable connect/getBalance/writeContract",
);
}
await config.client.disconnect();