-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.ts
320 lines (281 loc) · 11.7 KB
/
cli.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env node
import { select, input, confirm, password } from '@inquirer/prompts';
import { ethers, utils } from "ethers";
import { getAxieIdsFromAccount } from "./lib/axie";
import { refreshToken } from "./lib/marketplace/access-token";
import { batchTransferAxies, transferAxie } from "./lib/transfers";
import { getAxieContract, getWETHContract } from "./lib/contracts"
import { MARKETPLACE_GATEWAY_V2 } from "@roninbuilders/contracts"
import { askToContinue, ensureMarketplaceToken, getAxieId, } from './lib/utils';
import { approveMarketplaceContract, approveWETH } from './lib/marketplace/approve';
import buyMarketplaceOrder from "./lib/marketplace/settle-order";
import cancelMarketplaceOrder from "./lib/marketplace/cancel-order";
import createMarketplaceOrder from "./lib/marketplace/create-order";
import 'dotenv/config'
async function main() {
// Ask for Skymavis API key at startup if not in env (optional)
let skyMavisApiKey = process.env.SKYMAVIS_DAPP_KEY;
if (!skyMavisApiKey) {
const shouldProvideKey = await confirm({
message: '🔑 Would you like to provide a Skymavis DApp API key? (Recommended for better rate limits)',
default: false
});
if (shouldProvideKey) {
skyMavisApiKey = await input({
message: '🔑 Enter your Skymavis DApp API key:',
validate: (value) => value !== undefined && value !== ''
});
}
}
// Check for PRIVATE_KEY before the main loop
let privateKey = process.env.PRIVATE_KEY;
if (!privateKey) {
privateKey = await password({
message: '🔐 Enter your private key:',
validate: (value) => {
if (!value) return false;
// Private keys are 32 bytes (64 characters) + optional "0x" prefix
return utils.isHexString(value, 32) || utils.isHexString(`0x${value}`, 32);
}
});
// Ensure "0x" prefix
privateKey = privateKey.startsWith('0x') ? privateKey : `0x${privateKey}`;
}
// Initialize provider with the API key from above
const provider = new ethers.providers.JsonRpcProvider(
skyMavisApiKey
? `https://api-gateway.skymavis.com/rpc?apikey=${skyMavisApiKey}`
: "https://api.roninchain.com/rpc"
);
// Initialize wallet with the previously obtained private key
const wallet = new ethers.Wallet(privateKey, provider);
const address = await wallet.getAddress();
while (true) {
try {
const action = await select({
message: 'What would you like to do?',
choices: [
{ name: 'Get account info', value: 'account' },
{ name: 'Refresh access token', value: 'refresh-token' },
{ name: 'Approve WETH', value: 'approve-weth' },
{ name: 'Approve marketplace', value: 'approve-marketplace' },
{ name: 'Buy axie', value: 'buy' },
{ name: 'Delist axie', value: 'delist' },
{ name: 'Delist all axies', value: 'delist-all' },
{ name: 'List axie', value: 'list' },
{ name: 'List all axies', value: 'list-all' },
{ name: 'Transfer axie', value: 'transfer' },
{ name: 'Transfer all axies', value: 'transfer-all' },
]
});
switch (action) {
case 'account': {
const axieIds = await getAxieIdsFromAccount(address, provider);
console.log(`📬 Address: ${address}`);
const wethContract = getWETHContract(wallet)
// Get RON balance
const balance = await provider.getBalance(address);
const balanceInEther = utils.formatEther(balance);
console.log('💰 RON Balance:', balanceInEther);
// Get WETH balance
const wethBalance = await wethContract.balanceOf(address);
const wethBalanceInEther = utils.formatEther(wethBalance);
console.log('💰 WETH Balance:', wethBalanceInEther);
// WETH allowance
const allowance = await wethContract.allowance(address, MARKETPLACE_GATEWAY_V2.address);
console.log('🛒 Marketplace WETH allowance:', !allowance.eq(0) ? '✅ Granted' : '❌ Not granted')
// get axie contract
const axieContract = getAxieContract(provider)
// check if marketplace is approved for axies
const isApprovedForAll = await axieContract.isApprovedForAll(address, MARKETPLACE_GATEWAY_V2.address)
console.log('🔐 Marketplace approval for Axies:', isApprovedForAll ? '✅ Approved' : '❌ Not approved')
console.log(`🐾 Number of Axies: ${axieIds.length}`);
if (axieIds.length > 0) {
console.log(`🆔 Axie IDs: ${axieIds.join(', ')}`);
}
break;
}
case 'refresh-token': {
let refreshTokenValue = process.env.MARKETPLACE_REFRESH_TOKEN;
if (!refreshTokenValue) {
refreshTokenValue = await input({
message: 'Enter refresh token',
validate: (value) => value.length > 0
});
}
const result = await refreshToken(refreshTokenValue);
console.log('New access token:', result.newAccessToken);
console.log('New refresh token:', result.newRefreshToken);
process.env.MARKETPLACE_ACCESS_TOKEN = result.newAccessToken;
process.env.MARKETPLACE_REFRESH_TOKEN = result.newRefreshToken;
break;
}
case 'approve-weth': {
await approveWETH(wallet);
break;
}
case 'approve-marketplace': {
await approveMarketplaceContract(wallet);
break;
}
case 'buy': {
const token = await ensureMarketplaceToken();
const axieId = await getAxieId();
if (!axieId) break;
const receipt = await buyMarketplaceOrder(
axieId,
wallet,
token,
process.env.SKYMAVIS_DAPP_KEY // This can be undefined
);
if (receipt) {
console.log('🚀 Transaction successful! Hash:', receipt.transactionHash);
console.log('🔗 View transaction: https://app.roninchain.com/tx/' + receipt.transactionHash);
}
break;
}
case 'delist': {
const axieId = await getAxieId();
if (!axieId) break;
const receipt = await cancelMarketplaceOrder(
axieId,
wallet,
process.env.SKYMAVIS_DAPP_KEY // This can be undefined
);
if (receipt) {
console.log('✅ Axie delisted! Transaction hash:', receipt.transactionHash);
console.log('🔗 View transaction: https://app.roninchain.com/tx/' + receipt.transactionHash);
}
break;
}
case 'delist-all': {
const fromAddress = await wallet.getAddress();
let axieIds = await getAxieIdsFromAccount(fromAddress, provider);
if (axieIds.length > 100) {
console.log('⚠️ Warning: Can only transfer up to 100 Axies at once, only delisting the first 100');
axieIds = axieIds.slice(0, 100);
}
const receipt = await batchTransferAxies(wallet, fromAddress, axieIds);
if (receipt) {
console.log('✅ Axies delisted! Transaction hash:', receipt.transactionHash);
console.log('🔗 View transaction: https://app.roninchain.com/tx/' + receipt.transactionHash);
}else{
console.log('❌ Error delisting Axies');
}
break;
}
case 'list': {
const axieId = await getAxieId();
if (!axieId) break;
const token = await ensureMarketplaceToken();
const basePrice = await input({
message: 'Enter base price in ETH',
validate: (value) => utils.parseEther(value).gt(0)
});
const address = await wallet.getAddress();
const currentBlock = await provider.getBlock('latest');
const startedAt = currentBlock.timestamp;
const expiredAt = startedAt + 15634800; // ~6 months
const orderData = {
address,
axieId: axieId.toString(),
basePrice: utils.parseEther(basePrice).toString(),
endedPrice: '0',
startedAt,
endedAt: 0,
expiredAt,
};
const result = await createMarketplaceOrder(
orderData,
token,
wallet,
process.env.SKYMAVIS_DAPP_KEY // This can be undefined
);
if (result === null || result.errors || !result.data) {
console.error('❌ Error:', result?.errors?.[0]?.message || 'Unknown error');
break;
}
console.log(`✅ Listed Axie ${axieId}! Current price in USD: ${result.data.createOrder.currentPriceUsd}`);
break;
}
case 'list-all': {
const token = await ensureMarketplaceToken();
const basePrice = await input({
message: 'Enter base price in ETH (for all Axies)',
validate: (value) => utils.parseEther(value).gt(0)
});
const address = await wallet.getAddress();
let axieIds = await getAxieIdsFromAccount(address, provider);
if (axieIds.length > 100) {
console.log('⚠️ Warning: Can only list up to 100 Axies at once, only listing the first 100');
axieIds = axieIds.slice(0, 100);
}
const currentBlock = await provider.getBlock('latest');
const startedAt = currentBlock.timestamp;
const expiredAt = startedAt + 15634800; // ~6 months
for (const axieId of axieIds) {
const orderData = {
address,
axieId: axieId.toString(),
basePrice: utils.parseEther(basePrice).toString(),
endedPrice: '0',
startedAt,
endedAt: 0,
expiredAt,
};
const result = await createMarketplaceOrder(
orderData,
token,
wallet,
process.env.SKYMAVIS_DAPP_KEY
);
if (result === null || result.errors || !result.data) {
console.error(`❌ Error listing Axie ${axieId}:`, result?.errors?.[0]?.message || 'Unknown error');
continue;
}
console.log(`✅ Listed Axie ${axieId}! Current price in USD: ${result.data.createOrder.currentPriceUsd}`);
}
break;
}
case 'transfer': {
const axieId = await getAxieId();
if (!axieId) break;
const address = await input({
message: 'Enter recipient address',
validate: (value) => value.length > 0
});
const receipt = await transferAxie(wallet, address, axieId);
if (receipt) {
console.log('✅ Axie transferred! Transaction hash:', receipt.transactionHash);
}
break;
}
case 'transfer-all': {
const address = await input({
message: 'Enter recipient address',
validate: (value) => value.length > 0
});
let axieIds = await getAxieIdsFromAccount(address, provider);
if (axieIds.length > 100) {
console.log('⚠️ Warning: Can only transfer up to 100 Axies at once, only transfering the first 100');
axieIds = axieIds.slice(0, 100);
}
const receipt = await batchTransferAxies(wallet, address, axieIds);
if (receipt) {
console.log('✅ Axies transferred! Transaction hash:', receipt.transactionHash);
}
break;
}
}
} catch (error) {
if (error instanceof Error) {
console.error('❌ Error:', error.message);
} else {
console.error('❌ Error:', error);
}
} finally {
await askToContinue();
}
}
}
main();