Skip to content

Update to use the new SDK #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions alchemy-sdk-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This script demonstrates access to the NFT API via the Alchemy SDK.
import {
Network,
initializeAlchemy,
getNftsForOwner,
getNftMetadata,
BaseNft,
NftTokenType,
} from "@alch/alchemy-sdk";

// Optional Config object, but defaults to demo api-key and eth-mainnet.
const settings = {
apiKey: "demo", // Replace with your Alchemy API Key.
network: Network.ETH_MAINNET, // Replace with your network.
maxRetries: 10,
};

const alchemy = initializeAlchemy(settings);

// Print owner's wallet address:
const ownerAddr = "0xshah.eth";
console.log("fetching NFTs for address:", ownerAddr);
console.log("...");

// Print total NFT count returned in the response:
const nftsForOwner = await getNftsForOwner(alchemy, "0xshah.eth");
console.log("number of NFTs found:", nftsForOwner.totalCount);
console.log("...");

// Print contract address and tokenId for each NFT:
for (const nft of nftsForOwner.ownedNfts) {
console.log("===");
console.log("contract address:", nft.contract.address);
console.log("token ID:", nft.tokenId);
}
console.log("===");

// Fetch metadata for a particular NFT:
console.log("fetching metadata for a Crypto Coven NFT...");
const response = await getNftMetadata(
alchemy,
"0x5180db8F5c931aaE63c74266b211F580155ecac8",
"1590"
);

// Uncomment this line to see the full api response:
// console.log(response);

// Print some commonly used fields:
console.log("NFT name: ", response.title);
console.log("token type: ", response.tokenType);
console.log("tokenUri: ", response.tokenUri.gateway);
console.log("image url: ", response.rawMetadata.image);
console.log("time last updated: ", response.timeLastUpdated);
console.log("===");
11 changes: 6 additions & 5 deletions alchemy-web3-script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This script demonstrates access to the NFT API via the AlchemyWeb3.js package.
// alchemy-nft-api/alchemy-web3-script.js
import { createAlchemyWeb3 } from "@alch/alchemy-web3";

Expand All @@ -6,14 +7,14 @@ const apiKey = "demo";

// Initialize an alchemy-web3 instance:
const web3 = createAlchemyWeb3(
`https://eth-mainnet.alchemyapi.io/v2/${apiKey}`,
`https://eth-mainnet.alchemyapi.io/v2/${apiKey}`
);

// The wallet address we want to query for NFTs:
const ownerAddr = "0xC33881b8FD07d71098b440fA8A3797886D831061";
const nfts = await web3.alchemy.getNfts({
owner: ownerAddr
})
owner: ownerAddr,
});

// Print owner's wallet address:
console.log("fetching NFTs for address:", ownerAddr);
Expand All @@ -35,8 +36,8 @@ console.log("===");
console.log("fetching metadata for a crypto coven NFT...");
const response = await web3.alchemy.getNftMetadata({
contractAddress: "0x5180db8F5c931aaE63c74266b211F580155ecac8",
tokenId: "1590"
})
tokenId: "1590",
});

// Uncomment this line to see the full api response:
// console.log(metadata);
Expand Down
Loading