Created
September 22, 2022 18:09
-
-
Save gsw85/74b5f1933f2db23ea89897cf32bd0cb9 to your computer and use it in GitHub Desktop.
Alchemy NFT API JS Version - Script to Get NFT Contract Creator Address
This file contains hidden or 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 { Network, Alchemy } from "alchemy-sdk"; | |
| const settings = { | |
| apiKey: process.env.ALCHEMY_API_KEY, // Replace with your Alchemy API Key. | |
| network: Network.ETH_MAINNET, // Replace with your network. | |
| }; | |
| const alchemy = new Alchemy(settings); | |
| export async function getCreatorFromContract(contractAddress) { | |
| // making sure there is contractAddress | |
| if (!contractAddress) return; | |
| // searching for the block | |
| async function binarySearch(low = 0, high = 0, contract_address) { | |
| // Check base case | |
| if (high >= low) { | |
| const mid = parseInt((high + low) / 2); | |
| const midHex = "0x" + mid.toString(16); | |
| if (high === low) return low; | |
| if ((await alchemy.core.getCode(contract_address, midHex)) !== "0x") { | |
| return await binarySearch(low, mid, contract_address); | |
| } else if ( | |
| (await alchemy.core.getCode(contract_address, midHex)) === "0x" | |
| ) { | |
| return binarySearch(mid + 1, high, contract_address); | |
| } | |
| } else return -1; | |
| } | |
| try { | |
| const currNum = await alchemy.core.getBlockNumber(); | |
| const arr = Array.from({ length: currNum }, (v, i) => i); | |
| const resultBlockNum = await binarySearch( | |
| 0, | |
| arr.length - 1, | |
| contractAddress | |
| ); | |
| // return number | |
| // convert result to hex number | |
| const resultBlockNumHex = "0x" + resultBlockNum.toString(16); | |
| // get all transaction from that block | |
| const transactionReceipts = await alchemy.core.getTransactionReceipts({ | |
| blockNumber: resultBlockNumHex, | |
| }); | |
| // transactionReceipts will return an object | |
| const receipts = transactionReceipts.receipts; | |
| // receipts will return an array of transactions | |
| const ownerAddress = receipts.filter( | |
| (receipt) => receipt.contractAddress === contractAddress | |
| )[0].from; | |
| return ownerAddress; | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| } | |
| // To get the creator from NFT Contract (using goldeneggclub collection as example) - https://opensea.io/collection/goldeneggclub | |
| getCreatorFromContract("0x2a870463492deec8b8f01489c4c476053d017eda").then(console.log); | |
| // result: 0xb43eebac012cb2f12e1ec258a6ece20a7aa4712f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment