diff --git a/CHANGELOG.md b/CHANGELOG.md index f928ffca..4c85dc0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to the create-aptos-dapp tool will be captured in this file. # Unreleased - Remove `uniqueHolders` from NFT minting dapp template due to inefficient indexer query +- Upgrade to the new Irys SDK # 0.0.35 (2024-10-23) diff --git a/templates/nft-minting-dapp-template/frontend/utils/Irys.ts b/templates/nft-minting-dapp-template/frontend/utils/Irys.ts index 1d93d272..fe56a9dd 100644 --- a/templates/nft-minting-dapp-template/frontend/utils/Irys.ts +++ b/templates/nft-minting-dapp-template/frontend/utils/Irys.ts @@ -1,24 +1,28 @@ -import { WebIrys } from "@irys/sdk"; import { WalletContextState } from "@aptos-labs/wallet-adapter-react"; +import { WebUploader } from "@irys/web-upload"; +import { WebAptos } from "@irys/web-upload-aptos"; + import { getAccountAPTBalance } from "@/view-functions/getAccountAPTBalance"; import { NETWORK } from "@/constants"; -const getWebIrys = async (aptosWallet: WalletContextState) => { - const network = NETWORK === "testnet" ? "devnet" : "mainnet"; // Irys network - const token = "aptos"; - const rpcUrl = NETWORK; // Aptos network "mainnet" || "testnet" - const wallet = { rpcUrl: rpcUrl, name: "aptos", provider: aptosWallet }; - const webIrys = new WebIrys({ network, token, wallet }); - await webIrys.ready(); - return webIrys; +const getIrys = async (aptosWallet: WalletContextState) => { + // If dapp's network is testnet, use the devnet irys node, otherwise use the mainnet irys node + const irysNode = NETWORK === "testnet" ? "devnet" : "mainnet"; + const irys = WebUploader(WebAptos).withProvider(aptosWallet).network(irysNode); + // Irys requires to configure a rpc provider for the devnet node + if (irysNode === "devnet") { + irys.withRpc(NETWORK); + } + return await irys; }; export const checkIfFund = async (aptosWallet: WalletContextState, files: File[]) => { // 1. estimate the gas cost based on the data size https://docs.irys.xyz/developer-docs/irys-sdk/api/getPrice - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); + const costToUpload = await webIrys.utils.estimateFolderPrice(files.map((f) => f.size)); - // 2. check the wallet balance on the irys node: irys.getLoadedBalance() - const irysBalance = await webIrys.getLoadedBalance(); + // 2. check the wallet balance on the irys node + const irysBalance = await webIrys.getBalance(); // 3. if balance is enough, then upload without funding if (irysBalance.toNumber() > costToUpload.toNumber()) { @@ -44,7 +48,7 @@ export const checkIfFund = async (aptosWallet: WalletContextState, files: File[] }; export const fundNode = async (aptosWallet: WalletContextState, amount?: number) => { - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); try { const fundTx = await webIrys.fund(amount ?? 1000000); @@ -60,7 +64,7 @@ export const uploadFile = async ( aptosWallet: any, fileToUpload: File, ): Promise => { - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); try { const receipt = await webIrys.uploadFile(fileToUpload, { tags: [] }); return `https://gateway.irys.xyz/${receipt.id}`; @@ -71,7 +75,7 @@ export const uploadFile = async ( }; export const uploadFolder = async (aptosWallet: WalletContextState, files: File[]) => { - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); try { const receipt = await webIrys.uploadFolder(files); //returns the manifest ID diff --git a/templates/nft-minting-dapp-template/package.json b/templates/nft-minting-dapp-template/package.json index f62eaf86..f58ce9d0 100644 --- a/templates/nft-minting-dapp-template/package.json +++ b/templates/nft-minting-dapp-template/package.json @@ -19,7 +19,8 @@ "dependencies": { "@aptos-labs/ts-sdk": "^1.30.0", "@aptos-labs/wallet-adapter-react": "^3.6.2", - "@irys/sdk": "^0.2.1", + "@irys/web-upload": "^0.0.12", + "@irys/web-upload-aptos": "^0.0.12", "@radix-ui/react-accordion": "^1.1.2", "@radix-ui/react-alert-dialog": "^1.0.5", "@radix-ui/react-aspect-ratio": "^1.0.3", diff --git a/templates/token-minting-dapp-template/frontend/utils/Irys.ts b/templates/token-minting-dapp-template/frontend/utils/Irys.ts index 08e6c55d..05ebaecf 100644 --- a/templates/token-minting-dapp-template/frontend/utils/Irys.ts +++ b/templates/token-minting-dapp-template/frontend/utils/Irys.ts @@ -1,32 +1,27 @@ -import { WebIrys } from "@irys/sdk"; +import { WebUploader } from "@irys/web-upload"; +import { WebAptos } from "@irys/web-upload-aptos"; + +import { NETWORK } from "@/constants"; import { WalletContextState } from "@aptos-labs/wallet-adapter-react"; import { getAccountAPTBalance } from "@/view-functions/getAccountAPTBalance"; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -const getWebIrys = async (aptosWallet: any) => { - const network = "devnet"; // Irys network - const token = "aptos"; - const rpcUrl = "testnet"; // Aptos network "mainnet" || "testnet" - const wallet = { rpcUrl: rpcUrl, name: "aptos", provider: aptosWallet }; - const webIrys = new WebIrys({ network, token, wallet }); - await webIrys.ready(); - return webIrys; +const getIrys = async (aptosWallet: WalletContextState) => { + // If dapp's network is testnet, use the devnet irys node, otherwise use the mainnet irys node + const irysNode = NETWORK === "testnet" ? "devnet" : "mainnet"; + const irys = WebUploader(WebAptos).withProvider(aptosWallet).network(irysNode); + // Irys requires to configure a rpc provider for the devnet node + if (irysNode === "devnet") { + irys.withRpc(NETWORK); + } + return await irys; }; -/* TODO, the steps would be: -1. estimate the gas cost based on the data size https://docs.irys.xyz/developer-docs/irys-sdk/api/getPrice -2. check the wallet balance on the irys node: irys.getLoadedBalance() -3. if balance is enough, then upload without funding -4. if balance is not enough, check the payer balance -5. if payer balance > the amount based on the estimation, fund the irys node irys.fund, then upload -6. if payer balance < the amount, replenish the payer balance*/ - export const checkIfFund = async (aptosWallet: WalletContextState, fileSize: number) => { // 1. estimate the gas cost based on the data size https://docs.irys.xyz/developer-docs/irys-sdk/api/getPrice - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); const costToUpload = await webIrys.getPrice(fileSize); - // 2. check the wallet balance on the irys node: irys.getLoadedBalance() - const irysBalance = await webIrys.getLoadedBalance(); + // 2. check the wallet balance on the irys node + const irysBalance = await webIrys.getBalance(); // 3. if balance is enough, then upload without funding if (irysBalance.toNumber() > costToUpload.toNumber()) { return true; @@ -51,7 +46,7 @@ export const checkIfFund = async (aptosWallet: WalletContextState, fileSize: num }; export const fundNode = async (aptosWallet: WalletContextState, amount?: number) => { - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); try { const fundTx = await webIrys.fund(amount ?? 1000000); @@ -63,7 +58,7 @@ export const fundNode = async (aptosWallet: WalletContextState, amount?: number) }; export const uploadFile = async (aptosWallet: WalletContextState, fileToUpload: File): Promise => { - const webIrys = await getWebIrys(aptosWallet); + const webIrys = await getIrys(aptosWallet); try { const receipt = await webIrys.uploadFile(fileToUpload, { tags: [] }); return `https://gateway.irys.xyz/${receipt.id}`; diff --git a/templates/token-minting-dapp-template/package.json b/templates/token-minting-dapp-template/package.json index 3510457d..43328fb9 100644 --- a/templates/token-minting-dapp-template/package.json +++ b/templates/token-minting-dapp-template/package.json @@ -19,7 +19,8 @@ "dependencies": { "@aptos-labs/ts-sdk": "^1.30.0", "@aptos-labs/wallet-adapter-react": "^3.6.2", - "@irys/sdk": "^0.2.1", + "@irys/web-upload": "^0.0.12", + "@irys/web-upload-aptos": "^0.0.12", "@radix-ui/react-accordion": "^1.1.2", "@radix-ui/react-alert-dialog": "^1.0.5", "@radix-ui/react-checkbox": "^1.0.4",