-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkdrop-agent.js
More file actions
executable file
·174 lines (151 loc) · 4.48 KB
/
linkdrop-agent.js
File metadata and controls
executable file
·174 lines (151 loc) · 4.48 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
#!/usr/bin/env node
/*
Usage:
Required env:
PRIVATE_KEY=0x...
Optional env:
LINKDROP_API_KEY=... (overrides built-in default)
RPC_URL=...
RPC_URL_POLYGON=...
RPC_URL_BASE=...
RPC_URL_ARBITRUM=...
RPC_URL_OPTIMISM=...
RPC_URL_AVALANCHE=...
LINKDROP_BASE_URL=https://p2p.linkdrop.io
LINKDROP_API_URL=https://escrow-api.linkdrop.io/v3
Commands:
node linkdrop-agent.js send --amount 0.01 [--token native|0xToken] [--chain polygon|base|arbitrum|optimism|avalanche]
node linkdrop-agent.js claim --url "<claimUrl>" --to 0xRecipient [--chain polygon|base|arbitrum|optimism|avalanche]
Default chain: base
Positional fallback:
node linkdrop-agent.js send <amount> [token] [chain]
node linkdrop-agent.js claim <claimUrl> <to> [chain]
*/
const {
claimTransfer,
sendClaimableTransfer,
serializeError,
} = require("./agentdrop-core");
const USAGE = [
"linkdrop-agent.js",
"Commands:",
" send --amount <decimal> [--token <native|0xERC20>] [--chain <polygon|base|arbitrum|optimism|avalanche>]",
" claim --url <claimUrl> --to <0xAddress> [--chain <polygon|base|arbitrum|optimism|avalanche>]",
" default chain: base",
"Positional fallback:",
" send <amount> [token] [chain]",
" claim <claimUrl> <to> [chain]",
"Required env:",
" PRIVATE_KEY",
"Optional env:",
" LINKDROP_API_KEY (override built-in default)",
].join("\n");
let printed = false;
function printJson(payload) {
if (printed) {
return;
}
printed = true;
process.stdout.write(`${JSON.stringify(payload)}\n`);
}
function fail(error, code) {
const serialized = serializeError(error);
printJson({
ok: false,
error: {
code: code || serialized.details?.code || "UNKNOWN_ERROR",
...serialized,
},
});
process.exitCode = 1;
}
process.on("unhandledRejection", (reason) => {
fail(reason, "UNHANDLED_REJECTION");
});
process.on("uncaughtException", (error) => {
fail(error, "UNCAUGHT_EXCEPTION");
});
function parseArgv(args) {
const flags = {};
const positionals = [];
for (let index = 0; index < args.length; index += 1) {
const token = args[index];
if (!token.startsWith("--")) {
positionals.push(token);
continue;
}
const withoutPrefix = token.slice(2);
if (withoutPrefix.length === 0) {
continue;
}
const eqIndex = withoutPrefix.indexOf("=");
if (eqIndex >= 0) {
const key = withoutPrefix.slice(0, eqIndex);
const value = withoutPrefix.slice(eqIndex + 1);
flags[key] = value;
continue;
}
const next = args[index + 1];
if (next && !next.startsWith("--")) {
flags[withoutPrefix] = next;
index += 1;
} else {
flags[withoutPrefix] = true;
}
}
return { flags, positionals };
}
function getHelpPayload() {
return {
ok: true,
usage: USAGE,
examples: [
"node linkdrop-agent.js send --amount 0.01 --token native --chain base",
"node linkdrop-agent.js send --amount 5 --token 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 --chain polygon",
"node linkdrop-agent.js claim --url \"https://...\" --to 0x000000000000000000000000000000000000dead --chain base",
],
};
}
async function runSendCommand(parsed) {
const amountInput = parsed.flags.amount || parsed.positionals[0];
const tokenInput = parsed.flags.token || parsed.positionals[1] || "native";
const chainInput = parsed.flags.chain || parsed.positionals[2] || "base";
return sendClaimableTransfer({
amount: amountInput,
token: tokenInput,
chain: chainInput,
});
}
async function runClaimCommand(parsed) {
const claimUrl = parsed.flags.url || parsed.positionals[0];
const destination = parsed.flags.to || parsed.positionals[1];
const chainInput = parsed.flags.chain || parsed.positionals[2] || "base";
return claimTransfer({
claimUrl,
to: destination,
chain: chainInput,
});
}
async function main() {
const argv = process.argv.slice(2);
if (argv.length === 0 || argv.includes("--help") || argv[0] === "help") {
printJson(getHelpPayload());
return;
}
const command = String(argv[0]).toLowerCase();
const parsed = parseArgv(argv.slice(1));
if (command === "send") {
const result = await runSendCommand(parsed);
printJson(result);
return;
}
if (command === "claim") {
const result = await runClaimCommand(parsed);
printJson(result);
return;
}
throw new Error(`Unknown command '${command}'. Use 'send' or 'claim'.`);
}
main().catch((error) => {
fail(error);
});