Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
103 lines (98 loc) · 3.41 KB
/
Copy pathcli.ts
File metadata and controls
103 lines (98 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// =============================================================================
// cli.ts — Minimal local CLI to exercise the BondingV5 + UniV3 helpers.
//
// Requires the `acp` CLI binary on PATH and an ACP wallet keystore already
// initialized for the walletId you pass. Example:
//
// ACP_CONFIG_ROOT=/data/acp BASE_RPC_URL=https://mainnet.base.org \
// tsx examples/cli.ts balances 1 0xb2a99bc73c89b6bcbeb4650eedcd5f2776373c48
// tsx examples/cli.ts usdc-to-virtual 1
// tsx examples/cli.ts buy 1 0xb2a99bc73c... 1000000000000000000 # 1 VIRTUAL
// tsx examples/cli.ts sell 1 0xb2a99bc73c... 50000000000000000000 # 50 preTok
// tsx examples/cli.ts virtual-to-usdc 1
// =============================================================================
import {
bondingV5Trade,
usdcToVirtualSwap,
virtualToUsdcSwap,
balanceOf,
} from "../src/bondingV5.js";
import { ADDR } from "../src/abi.js";
import { runAcpJson } from "../src/acpCli.js";
async function getWalletAddress(walletId: number): Promise<`0x${string}`> {
const out = await runAcpJson<{ address: string }>(
walletId, false, ["wallet", "address", "--json"], 30_000,
);
return out.address as `0x${string}`;
}
async function main() {
const [cmd, ...args] = process.argv.slice(2);
if (!cmd) {
console.error("usage: cli.ts <balances|buy|sell|usdc-to-virtual|virtual-to-usdc> ...");
process.exit(1);
}
switch (cmd) {
case "balances": {
const [walletIdStr, preToken] = args;
const walletId = Number(walletIdStr);
const addr = await getWalletAddress(walletId);
const [usdc, virtual] = await Promise.all([
balanceOf(addr, ADDR.usdc),
balanceOf(addr, ADDR.virtual),
]);
const out: Record<string, string> = {
walletId: String(walletId),
acpWallet: addr,
usdcMicros: usdc.toString(),
usdcHuman: (Number(usdc) / 1e6).toFixed(6),
virtualWei: virtual.toString(),
virtualHuman: (Number(virtual) / 1e18).toFixed(6),
};
if (preToken && /^0x[a-fA-F0-9]{40}$/.test(preToken)) {
const tokBal = await balanceOf(addr, preToken as `0x${string}`);
out.preToken = preToken;
out.preTokenWei = tokBal.toString();
out.preTokenHuman = (Number(tokBal) / 1e18).toFixed(6);
}
console.log(JSON.stringify(out, null, 2));
break;
}
case "buy": {
const [walletIdStr, preToken, amountInWei] = args;
const r = await bondingV5Trade(Number(walletIdStr), {
side: "buy",
preToken: preToken as `0x${string}`,
amountInWei: BigInt(amountInWei),
});
console.log(JSON.stringify(r, null, 2));
break;
}
case "sell": {
const [walletIdStr, preToken, amountInWei] = args;
const r = await bondingV5Trade(Number(walletIdStr), {
side: "sell",
preToken: preToken as `0x${string}`,
amountInWei: BigInt(amountInWei),
});
console.log(JSON.stringify(r, null, 2));
break;
}
case "usdc-to-virtual": {
const r = await usdcToVirtualSwap(Number(args[0]));
console.log(JSON.stringify(r, null, 2));
break;
}
case "virtual-to-usdc": {
const r = await virtualToUsdcSwap(Number(args[0]));
console.log(JSON.stringify(r, null, 2));
break;
}
default:
console.error(`unknown command: ${cmd}`);
process.exit(1);
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});