-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute-b.ts
More file actions
291 lines (269 loc) ยท 14.9 KB
/
Copy pathroute-b.ts
File metadata and controls
291 lines (269 loc) ยท 14.9 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env -S npx tsx
/**
* Backstop โ ROUTE B: real end-to-end redemption-default claim on Coston2.
*
* Produces a genuine on-chain `Claimed` event WITHOUT running your own FAssets
* agent (which is whitelist-gated on Coston2). Instead it redeems faucet-minted
* FXRP from a public agent and insures it; if that agent doesn't pay the
* underlying XRP within the ~15-min window (common on testnet), the keeper
* produces the real FDC ReferencedPaymentNonexistence proof and `claim()` fires.
*
* One attempt = one run of this script, then `npm run keeper:once` after the
* deadline. If the agent DID pay, nothing to claim โ just run it again.
*
* Steps (all as account #1 = PRIVATE_KEY = the redeemer/LP/owner):
* 1. sanity: FXRP balance >= 1 lot (else tells you to hit the faucet)
* 2. fund the Backstop pool so it can back the guard (idempotent-ish)
* 3. AssetManager.redeem(1 lot, yourXRPL, executor=0) -> redemptionRequestId
* 4. Backstop.buyGuard(requestId, coverageUsd) with the quoted premium
* 5. print guardId + the exact deadline to wait for, then the keeper command
*
* Run:
* npx tsx scripts/route-b.ts # full attempt (fund + redeem + guard)
* npx tsx scripts/route-b.ts --fund-only # just top up the pool
* npx tsx scripts/route-b.ts --dry-run # read + size everything, no writes
*
* Env (build/.env): COSTON2_RPC_URL, PRIVATE_KEY (#1), BACKSTOP_ADDRESS.
* Optional: REDEEMER_XRPL_ADDRESS (defaults to the Route-A user XRPL account).
*/
import "dotenv/config";
import {
createPublicClient,
createWalletClient,
http,
parseEther,
formatEther,
parseEventLogs,
zeroAddress,
type Address,
type PublicClient,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { FLARE_CONTRACT_REGISTRY, registryAbi } from "./flare.js";
// โโ CLI + palette (same as keeper.ts) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const FLAGS = new Set(process.argv.slice(2).filter((a) => a.startsWith("--")));
const FUND_ONLY = FLAGS.has("--fund-only");
const DRY_RUN = FLAGS.has("--dry-run");
const C = {
green: (s: string) => `\x1b[32m${s}\x1b[0m`,
red: (s: string) => `\x1b[31m${s}\x1b[0m`,
yellow: (s: string) => `\x1b[33m${s}\x1b[0m`,
cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
dim: (s: string) => `\x1b[2m${s}\x1b[0m`,
bold: (s: string) => `\x1b[1m${s}\x1b[0m`,
};
const line = (k: string, v: unknown) => console.log(` ${C.dim(k.padEnd(24))} ${v}`);
const log = (m: string) => console.log(`${C.dim(new Date().toISOString())} ${m}`);
const usd = (x: bigint) => `$${(Number(x) / 1e18).toFixed(4)}`;
// โโ Config + clients โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const RPC = process.env.COSTON2_RPC_URL;
const BACKSTOP = process.env.BACKSTOP_ADDRESS as Address | undefined;
if (!RPC || !BACKSTOP) {
console.error(C.red("COSTON2_RPC_URL and BACKSTOP_ADDRESS must be set in build/.env"));
process.exit(2);
}
const REDEEMER_XRPL =
process.env.REDEEMER_XRPL_ADDRESS || "r4TgaZzJ1AZaQUfAGGraSBeA5ZZo3Bgj7C";
// leave this much native for gas + premium + executor fee; deposit the rest to the pool
const GAS_RESERVE_FLR = parseEther("40");
const COVERAGE_FRACTION_BIPS = 7000n; // insure 70% of pool value โ margin under the 80% cap
const coston2 = {
id: 114,
name: "Coston2",
nativeCurrency: { name: "Coston2 Flare", symbol: "C2FLR", decimals: 18 },
rpcUrls: { default: { http: [RPC] } },
} as const;
const publicClient: PublicClient = createPublicClient({ transport: http(RPC) }) as PublicClient;
const pk = process.env.PRIVATE_KEY;
if (!pk) {
console.error(C.red("PRIVATE_KEY (account #1, the redeemer) is not set."));
process.exit(2);
}
const account = privateKeyToAccount(
pk.startsWith("0x") ? (pk as `0x${string}`) : (`0x${pk}` as `0x${string}`),
);
const wallet = createWalletClient({ account, chain: coston2, transport: http(RPC) });
// โโ Minimal ABIs (only what we call) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const backstopAbi = [
{ type: "function", name: "pool", stateMutability: "view", inputs: [], outputs: [{ type: "address" }] },
{ type: "function", name: "owner", stateMutability: "view", inputs: [], outputs: [{ type: "address" }] },
{ type: "function", name: "poolValueUsd", stateMutability: "nonpayable", inputs: [], outputs: [{ type: "uint256" }] },
{ type: "function", name: "maxUtilizationBips", stateMutability: "view", inputs: [], outputs: [{ type: "uint256" }] },
{ type: "function", name: "totalActiveCoverageUsd", stateMutability: "view", inputs: [], outputs: [{ type: "uint256" }] },
{ type: "function", name: "expectedUsd", stateMutability: "nonpayable", inputs: [{ name: "valueUBA", type: "uint256" }], outputs: [{ type: "uint256" }] },
{ type: "function", name: "quotePremiumFlr", stateMutability: "nonpayable", inputs: [{ name: "coverageUsd", type: "uint256" }], outputs: [{ type: "uint256" }] },
{
type: "function", name: "buyGuard", stateMutability: "payable",
inputs: [{ name: "redemptionRequestId", type: "uint256" }, { name: "coverageUsd", type: "uint256" }],
outputs: [{ name: "guardId", type: "uint256" }],
},
{
type: "event", name: "GuardBought", inputs: [
{ name: "guardId", type: "uint256", indexed: true },
{ name: "redeemer", type: "address", indexed: true },
{ name: "agentVault", type: "address", indexed: true },
{ name: "coverageUsd", type: "uint256", indexed: false },
{ name: "premium", type: "uint256", indexed: false },
],
},
] as const;
const poolAbi = [
{ type: "function", name: "deposit", stateMutability: "payable", inputs: [], outputs: [{ name: "minted", type: "uint256" }] },
] as const;
const erc20Abi = [
{ type: "function", name: "balanceOf", stateMutability: "view", inputs: [{ name: "a", type: "address" }], outputs: [{ type: "uint256" }] },
{ type: "function", name: "decimals", stateMutability: "view", inputs: [], outputs: [{ type: "uint8" }] },
{ type: "function", name: "symbol", stateMutability: "view", inputs: [], outputs: [{ type: "string" }] },
] as const;
const assetManagerAbi = [
{ type: "function", name: "fAsset", stateMutability: "view", inputs: [], outputs: [{ type: "address" }] },
{ type: "function", name: "lotSize", stateMutability: "view", inputs: [], outputs: [{ type: "uint256" }] },
{
type: "function", name: "redeem", stateMutability: "payable",
inputs: [
{ name: "_lots", type: "uint256" },
{ name: "_redeemerUnderlyingAddressString", type: "string" },
{ name: "_executor", type: "address" },
],
outputs: [{ name: "_redeemedAmountUBA", type: "uint256" }],
},
{
type: "event", name: "RedemptionRequested", inputs: [
{ name: "agentVault", type: "address", indexed: true },
{ name: "redeemer", type: "address", indexed: true },
{ name: "requestId", type: "uint256", indexed: true },
{ name: "paymentAddress", type: "string", indexed: false },
{ name: "valueUBA", type: "uint256", indexed: false },
{ name: "feeUBA", type: "uint256", indexed: false },
{ name: "firstUnderlyingBlock", type: "uint256", indexed: false },
{ name: "lastUnderlyingBlock", type: "uint256", indexed: false },
{ name: "lastUnderlyingTimestamp", type: "uint256", indexed: false },
{ name: "paymentReference", type: "bytes32", indexed: false },
{ name: "executor", type: "address", indexed: false },
{ name: "executorFeeNatWei", type: "uint256", indexed: false },
],
},
] as const;
const read = <T>(address: Address, abi: any, functionName: string, args: any[] = []) =>
publicClient.readContract({ address, abi, functionName, args }) as Promise<T>;
async function main() {
console.log(C.bold("\nโญโ Backstop Route B โ real redemption-default attempt โโโโโโโโโโโโโโฎ"));
line("network", "Coston2 (114)");
line("account #1 (redeemer)", account.address);
line("Backstop", BACKSTOP);
if (DRY_RUN) log(C.yellow("DRY-RUN โ no transactions will be sent"));
// Resolve AssetManagerFXRP the same way Backstop does, then the FXRP token.
const am = await read<Address>(FLARE_CONTRACT_REGISTRY, registryAbi, "getContractAddressByName", ["AssetManagerFXRP"]);
const fxrp = await read<Address>(am, assetManagerAbi, "fAsset");
const lot = await read<bigint>(am, assetManagerAbi, "lotSize");
const dec = await read<number>(fxrp, erc20Abi, "decimals");
const sym = await read<string>(fxrp, erc20Abi, "symbol");
const bal = await read<bigint>(fxrp, erc20Abi, "balanceOf", [account.address]);
const fmt = (v: bigint) => (Number(v) / 10 ** dec).toFixed(dec);
line("AssetManagerFXRP", am);
line(`${sym} token`, fxrp);
line("lot size", `${fmt(lot)} ${sym}`);
line("your FXRP balance", `${fmt(bal)} ${sym}`);
// โโ Step 1: need โฅ 1 lot โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if (bal < lot) {
console.log(
C.red(`\nโ Need โฅ ${fmt(lot)} ${sym} to redeem 1 lot; you have ${fmt(bal)}.`),
);
console.log(
` Click ${C.bold("Request FXRP")} at https://faucet.flare.network/coston2 to\n` +
` ${C.bold(account.address)}, then re-run this script.`,
);
process.exit(1);
}
// โโ Step 2: fund the pool โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const pool = await read<Address>(BACKSTOP, backstopAbi, "pool");
let poolVal = await read<bigint>(BACKSTOP, backstopAbi, "poolValueUsd");
const nativeBal = await publicClient.getBalance({ address: account.address });
line("pool", pool);
line("pool value (pre)", usd(poolVal));
line("your native", `${formatEther(nativeBal)} C2FLR`);
const depositAmt = nativeBal > GAS_RESERVE_FLR ? nativeBal - GAS_RESERVE_FLR : 0n;
if (depositAmt > 0n) {
log(`funding pool with ${formatEther(depositAmt)} C2FLR (leaving ${formatEther(GAS_RESERVE_FLR)} for gas/premium)`);
if (!DRY_RUN) {
const tx = await wallet.writeContract({ address: pool, abi: poolAbi, functionName: "deposit", value: depositAmt });
await publicClient.waitForTransactionReceipt({ hash: tx });
line("deposit tx", tx);
poolVal = await read<bigint>(BACKSTOP, backstopAbi, "poolValueUsd");
line("pool value (post)", usd(poolVal));
}
} else {
log(C.yellow("native balance below reserve โ skipping pool top-up"));
}
if (FUND_ONLY) {
log(C.green("--fund-only: done."));
return;
}
// โโ Step 3: redeem 1 lot โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
log(`redeeming 1 lot -> underlying ${REDEEMER_XRPL}`);
if (DRY_RUN) {
log(C.yellow("[dry-run] would call AssetManager.redeem(1, <yourXRPL>, 0x0) and buyGuard"));
return;
}
const redeemTx = await wallet.writeContract({
address: am, abi: assetManagerAbi, functionName: "redeem",
args: [1n, REDEEMER_XRPL, zeroAddress], value: 0n,
});
const redeemRcpt = await publicClient.waitForTransactionReceipt({ hash: redeemTx });
line("redeem tx", redeemTx);
const events = parseEventLogs({ abi: assetManagerAbi, eventName: "RedemptionRequested", logs: redeemRcpt.logs });
if (events.length === 0) {
console.log(C.red("โ No RedemptionRequested event โ redemption may have been rejected (address not normalized?) or split incomplete."));
console.log(` Inspect tx: https://coston2-explorer.flare.network/tx/${redeemTx}`);
process.exit(1);
}
const ev = events[0].args as any;
const requestId: bigint = ev.requestId;
const deadlineTs: bigint = ev.lastUnderlyingTimestamp;
line("redemptionRequestId", requestId.toString());
line("assigned agentVault", ev.agentVault);
line("value / fee (UBA)", `${ev.valueUBA} / ${ev.feeUBA}`);
line("payment deadline", new Date(Number(deadlineTs) * 1000).toISOString());
// โโ Step 4: size coverage + buyGuard โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const maxUsd = await read<bigint>(BACKSTOP, backstopAbi, "expectedUsd", [ev.valueUBA]);
const byPool = (poolVal * COVERAGE_FRACTION_BIPS) / 10_000n;
let coverageUsd = byPool < maxUsd ? byPool : maxUsd;
if (coverageUsd === 0n) {
console.log(C.red("โ Computed zero coverage โ pool value too low. Fund the pool (Request C2FLR to #1) and retry."));
process.exit(1);
}
const premium = await read<bigint>(BACKSTOP, backstopAbi, "quotePremiumFlr", [coverageUsd]);
const value = premium + premium / 50n + 1n; // +2% buffer; buyGuard refunds overpayment
line("coverage (max for lot)", usd(maxUsd));
line("coverage (chosen)", `${usd(coverageUsd)} (${Number(COVERAGE_FRACTION_BIPS) / 100}% of pool)`);
line("premium", `${formatEther(premium)} C2FLR`);
log("buying guardโฆ");
const guardTx = await wallet.writeContract({
address: BACKSTOP, abi: backstopAbi, functionName: "buyGuard",
args: [requestId, coverageUsd], value,
});
const guardRcpt = await publicClient.waitForTransactionReceipt({ hash: guardTx });
line("buyGuard tx", guardTx);
const bought = parseEventLogs({ abi: backstopAbi, eventName: "GuardBought", logs: guardRcpt.logs });
const guardId = bought.length ? (bought[0].args as any).guardId : "(check tx)";
// โโ Summary โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const waitS = Number(deadlineTs) - Math.floor(Date.now() / 1000);
console.log(C.bold(C.green("\nโญโ Guard live โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ")));
line("guardId", guardId.toString());
line("redemptionRequestId", requestId.toString());
line("deadline", `${new Date(Number(deadlineTs) * 1000).toISOString()} (~${Math.max(0, Math.ceil(waitS / 60))} min)`);
console.log(C.bold("โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ"));
console.log(
`\nNext:\n` +
` 1. If the agent PAYS the XRP before the deadline, there's nothing to claim โ\n` +
` just run this script again for another attempt.\n` +
` 2. After the deadline passes (+ a minute), run:\n` +
C.bold(` npm run keeper:once\n`) +
` It builds the real FDC non-existence proof and fires Backstop.claim(${guardId}).\n` +
` 3. A CLAIMED event = your real end-to-end artifact. Grab the claim tx hash.`,
);
}
main().catch((e) => {
console.error(C.red(`\nโ ${e?.shortMessage || e?.message || e}`));
process.exit(1);
});