Last active
November 2, 2021 00:51
-
-
Save larry0x/a3f74934478fd2d9462c9ed1fce26cd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from "fs"; | |
import axios from "axios"; | |
const SPACELOOT_CONTRACT_ADDRESS = "terra13qrc9j00lk3x0rvpptzdmwtckfj64d5g6xnrv9"; | |
const START_ID = 0; | |
const END_ID = 8000; | |
const IDS_PER_QUERY = 1000; | |
type MantleResponse = { | |
data: { | |
[key: string]: { | |
Result: string; | |
}; | |
}; | |
}; | |
type OwnerOfResponse = { | |
owner: string; | |
approvals: object[]; | |
}; | |
function generateQuery(id: number) { | |
// NOTE: GraphQL does not allow using a number as the name of a query. Therefore instead of id, | |
// we use `id_${id}` | |
return ` | |
id_${id}: WasmContractsContractAddressStore( | |
ContractAddress: "${SPACELOOT_CONTRACT_ADDRESS}", | |
QueryMsg: "{\\"owner_of\\":{\\"token_id\\":\\"${id}\\"}}" | |
) { | |
Result | |
} | |
`; | |
} | |
function generateQueries(start: number, end: number) { | |
let queries: string[] = []; | |
for (let id = start; id < end; id++) { | |
queries.push(generateQuery(id)); | |
} | |
return ` | |
query { | |
${queries.join("\n")} | |
} | |
`; | |
} | |
async function fetchSpacelootOwners() { | |
let owners: { [key: number]: string } = {}; | |
let start = START_ID; | |
let end = start + IDS_PER_QUERY; | |
while (start < end) { | |
process.stdout.write(`querying owners of id ${start} to ${end - 1}... `); | |
const response: { data: MantleResponse } = await axios.post("https://mantle.terra.dev/", { | |
query: generateQueries(start, end), | |
}); | |
console.log("success!"); | |
// console.log(response.data); | |
for (const [key, value] of Object.entries(response.data.data)) { | |
const id = parseInt(key.slice(3)); | |
const ownerOfResponse: OwnerOfResponse = JSON.parse(value.Result); | |
owners[id] = ownerOfResponse.owner; | |
} | |
start = end; | |
end += IDS_PER_QUERY; | |
if (end > END_ID) end = END_ID; | |
} | |
return owners; | |
} | |
(async function () { | |
const owners = await fetchSpacelootOwners(); | |
fs.writeFileSync("./spaceloot_owners.json", JSON.stringify(owners, null, 2)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment