Skip to content

Commit

Permalink
Upgrade to the new Irys SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Oct 26, 2024
1 parent 5e572b9 commit c7e8c0a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
34 changes: 19 additions & 15 deletions templates/nft-minting-dapp-template/frontend/utils/Irys.ts
Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -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);
Expand All @@ -60,7 +64,7 @@ export const uploadFile = async (
aptosWallet: any,
fileToUpload: File,
): Promise<string> => {
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}`;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion templates/nft-minting-dapp-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 18 additions & 23 deletions templates/token-minting-dapp-template/frontend/utils/Irys.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -63,7 +58,7 @@ export const fundNode = async (aptosWallet: WalletContextState, amount?: number)
};

export const uploadFile = async (aptosWallet: WalletContextState, fileToUpload: File): Promise<string> => {
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}`;
Expand Down
3 changes: 2 additions & 1 deletion templates/token-minting-dapp-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c7e8c0a

Please sign in to comment.