-
Notifications
You must be signed in to change notification settings - Fork 2
/
refineBurntPix.ts
205 lines (170 loc) · 6.1 KB
/
refineBurntPix.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
import { ethers, network } from 'hardhat';
import * as dotenv from 'dotenv';
import { ERC725 } from '@erc725/erc725.js';
import lsp4Schema from '@erc725/erc725.js/schemas/LSP4DigitalAsset.json';
import { ERC725YDataKeys } from '@lukso/lsp-smart-contracts';
import {
TOKEN_ID,
ITERATION_NUMBER,
SPLITTER,
BURNT_PIX_CONTRACT,
MIN_LYX_BALANCE,
MAX_GAS_PRICE,
} from '../consts/constants';
import BurntPixABI from '../consts/BurntPixABI.json';
interface CustomNetworkConfig {
url?: string;
}
interface BurntPixAttribute {
key: string;
type: string;
value: number | string;
}
interface BurntPixAttributeValue {
[key: string]: number | string;
}
// Load the environment variables
dotenv.config();
async function main() {
// Set your token ID and it's final refinenent number
const burntPix = TOKEN_ID.GLOBE_N_SQUARE;
const maximumRefines = 1000000;
// Import private key to create wallet
const { PRIVATE_KEY } = process.env;
// Setup network parameters for ERC725
const customNetworkConfig = network.config as CustomNetworkConfig;
const networkUrl = customNetworkConfig.url;
// Check if transaction is pending
let isWaitingForTransaction = false;
// Check if RPC URL is provided
if (!networkUrl) {
throw new Error('Network URL is not defined in the Hardhat configuration.');
}
// Check if private key is provided
if (!PRIVATE_KEY) {
throw new Error('No private key provided.');
}
// Create new wallet for EOA controller
const provider = new ethers.JsonRpcProvider(networkUrl);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
// Create instance of the BurntPix contract
const contractABI = BurntPixABI;
// Use address based on used Hardhat network
const contract = new ethers.Contract(
BURNT_PIX_CONTRACT[network.name as keyof typeof BURNT_PIX_CONTRACT],
contractABI,
wallet,
);
// Function to check conditions and call refine
const refineScript = async () => {
// Check for balance of the controller
const balanceinWei = await provider.getBalance(wallet.address);
const balanceInLYX = ethers.formatEther(balanceinWei);
console.log(SPLITTER);
if (isWaitingForTransaction) {
console.log(
`[${new Date().toLocaleTimeString()}] Previous refinement is still processing. Skipping this round.`,
);
console.log(SPLITTER);
return;
}
console.log('--- Current Controller: ', wallet.address);
console.log('--- Current LYX Balance: ', balanceInLYX);
// Get gas price of the network and convert to Gwei
const gasPrice = (await provider.getFeeData()).gasPrice;
if (gasPrice === null) {
throw new Error('Failed to fetch the gas price');
}
const gasPriceInGwei = ethers.formatUnits(gasPrice, 'gwei');
console.log('--- Current Gas Price: ', gasPriceInGwei);
// Get the metadata of the token ID
console.log('--- Getting Metadata for: ', burntPix);
console.log(SPLITTER);
const tokenIdMetadata = await contract.getDataForTokenId(
burntPix,
ERC725YDataKeys.LSP4['LSP4Metadata'],
);
const erc725js = new ERC725(lsp4Schema);
// Decode the content from the ERC725Y storage
const decodedMetadata = erc725js.decodeData([
{
keyName: 'LSP4Metadata',
value: tokenIdMetadata,
},
]);
// Get the actual data from the URL field
const urlContent = decodedMetadata[0].value.url;
// Get the inner JSON containing the metadata
const metadataString = urlContent.substring(
urlContent.indexOf('{'),
urlContent.lastIndexOf('}') + 1,
);
// Parse the content string to get it's JSON
const innerJsonObject = JSON.parse(metadataString);
const attributesArray: BurntPixAttribute[] = innerJsonObject.LSP4Metadata.attributes;
// Convert the array to an object with dynamic keys and values
// Convert the array to an object with dynamic keys and values
const attributes: BurntPixAttributeValue = attributesArray.reduce<BurntPixAttributeValue>(
(acc, attr: BurntPixAttribute) => {
acc[attr.key] = attr.value;
return acc;
},
{},
);
// Prepare attributes for terminal output
const attributesOutput = Object.entries(attributes)
.map(([key, value]) => `${key}: ${value}`)
.join('\n');
console.log(attributesOutput);
console.log(SPLITTER);
// Prepare values for comparison
const totalIterations = Number(attributes['Iterations']);
const balanceInLYXNumber = parseFloat(balanceInLYX);
const gasPriceInGweiNumber = parseFloat(gasPriceInGwei);
const stopScript = () => {
// Stop the script
console.log('Stopping the script...');
console.log(SPLITTER);
process.exit(1);
};
/**
* Metrix for refinement
* - Controller balance that must be fulfilled
* - Maximum iteration count for the token ID
* - Maximum gas price you want to refine at
*/
console.log('--- Checking conditions for refinement...');
if (balanceInLYXNumber <= MIN_LYX_BALANCE) {
console.log('XXX Requirement not met: Balance less than 0.3 LYX.');
stopScript();
} else if (totalIterations >= maximumRefines) {
console.log('XXX Requirement not met: Refines fulfilled');
stopScript();
}
if (gasPriceInGweiNumber <= MAX_GAS_PRICE) {
console.log('--- Calling refine...');
isWaitingForTransaction = true;
try {
const tx = await contract.refine(burntPix, ITERATION_NUMBER);
await tx.wait();
console.log('Refine called successfully. Waiting for next round...');
} catch (error) {
console.error('Transaction reverted. Waiting for next round...');
} finally {
// Reset the flag after transaction completes or fails
isWaitingForTransaction = false;
}
} else {
console.log('XXX Requirement not met: Gas price too high');
console.log('Skipping this round...');
console.log(SPLITTER);
}
};
// Check conditions and call refine periodically
setInterval(refineScript, 10000); // 10 seconds
// Initial call
await refineScript();
}
main();
// Prevent the process from exiting
process.stdin.resume();