Skip to content

Instantly share code, notes, and snippets.

@tchayen
Last active September 5, 2021 08:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchayen/258101341b7c18f2ab8b97ccf187037f to your computer and use it in GitHub Desktop.
Save tchayen/258101341b7c18f2ab8b97ccf187037f to your computer and use it in GitHub Desktop.
/**
* Usage:
* 1. Paste this into a main.js file in some empty directory.
* 2. Run: `yarn add web3 ethers abi-decoder`.
* 3. Go to contract's page
* https://etherscan.io/address/0x25ed58c027921E14D86380eA2646E3a1B5C55A8b
* and then to code and then copy "Contract ABI" and save it in the same dir
* as abi.json.
* 4. Create empty file backup.txt.
* 5. Download CSV data with transactions from etherscan page and save as
* data.csv. Please note that this solution will fall apart once more than
* 5k transactions happen since that's the limit for etherscan CSV.
* 6. Register for free on infura.io, get your token and create a file
* API_KEY.json with content
* { "key": "THE_KEY" }
* 7. Make sure about your preferred top of the range in line 83.
* 8. Run: `node main.js` (line 73 limits to the first 10 entries, it is for
* safety to check that setup works).
* 9. Re-run again without line 73 and claimed IDs will be saved to results.txt
* as the execution proceeds and the unclaimed ones will be sorted and
* printed after fetching is over.
* 10. Now you can use your chosen free token on the contract page. Enjoy!
*/
const fs = require("fs");
const Web3 = require("web3");
const ethers = require("ethers");
const abiDecoder = require("abi-decoder");
const API_KEY = require("./API_KEY.json").key;
const ABI = require("./abi.json");
abiDecoder.addABI(ABI);
const data = fs.readFileSync("./data.csv", "utf-8");
const lines = data
.split("\n")
.filter((_, index) => index > 0)
.map((line) =>
line
.split(",")
.map((string) => string.replaceAll('"', "").replaceAll("\r", ""))
);
const columns = [
"Txhash",
"Blockno",
"UnixTimestamp",
"DateTime",
"From",
"To",
"ContractAddress",
"Value_IN(ETH)",
"Value_OUT(ETH)",
"CurrentValue @ $3850.44/Eth",
"TxnFee(ETH)",
"TxnFee(USD)",
"Historical $Price/Eth",
"Status",
"ErrCode",
"Method",
"PrivateNote",
];
const reverseKeys = columns.reduce((accumulator, column, index) => {
accumulator[column] = index;
return accumulator;
}, {});
const web3 = new Web3(`wss://mainnet.infura.io/ws/v3/${API_KEY}`);
const eth = new ethers.providers.Web3Provider(web3.currentProvider);
const claimed = [];
const run = async () => {
const hashes = lines
.filter((line) => line[reverseKeys["Method"]] === "Claim")
.filter((_, i) => i < 10)
.map((line) => line[reverseKeys["Txhash"]]);
for await (const hash of hashes) {
const tx = await eth.getTransaction(hash);
const input = abiDecoder.decodeMethod(tx.data);
const id = input.params[0].value;
claimed.push(id);
fs.appendFileSync("backup.txt", `${id}\n`);
}
const BOTTOM_OF_THE_RANGE = 0;
const TOP_OF_THE_RANGE = 1000;
const set = new Set(claimed.map((a) => Number(a)).sort((a, b) => a - b));
for (let i = BOTTOM_OF_THE_RANGE; i < TOP_OF_THE_RANGE; i++) {
if (!set.has(i)) {
console.log(i);
}
}
process.exit(0);
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment