Skip to content

Commit

Permalink
fix(mx): nftData uri issue. add getTransactionStatus, fix fetchHttpOr…
Browse files Browse the repository at this point in the history
…Ipfs get image issue
  • Loading branch information
muhammadusmanuet committed Sep 19, 2024
1 parent 9e60ebd commit 9fa31ff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/handlers/multiversx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function multiversxHandler({
)
).data;
return {
metaData: atob(response.uris[0]),
metaData: atob(response.uris[1]),
royalties: response.royalties ?? 0,
};
};
Expand Down Expand Up @@ -541,6 +541,11 @@ export function multiversxHandler({
);
return bal;
},
async getTransactionStatus(txHash) {
return (await waitForTransaction(txHash)).isSuccessful()
? "success"
: "failed";
},
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/handlers/multiversx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export type TMultiversXHandler = TSingularNftChain<
string
> &
ReadClaimed721Event &
ReadClaimed1155Event;
ReadClaimed1155Event &
TGetTransactionStatus;

type TGetTransactionStatus = {
getTransactionStatus(txHash: string): Promise<string>;
};

export type TMultiversXParams = {
provider: INetworkProvider;
Expand Down
32 changes: 25 additions & 7 deletions src/handlers/utils/fetchHttpOrIpfs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import axios from "axios";

export async function fetchHttpOrIpfs(uri: string) {
async function fetchWithFallback(uri: string, fallbackUri: string) {
const http = axios.create();
try {
const response = await http.get(uri, { timeout: 10000 });
return response.data;
} catch (ex) {
try {
const response = await http.get(fallbackUri, { timeout: 10000 });
return response.data;
} catch (ex) {
return "";
}
}
}

export async function fetchHttpOrIpfs(uri: string) {
const url = new URL(uri);
if (url.protocol === "http:" || url.protocol === "https:") {
const response = await http.get(uri);
return response.data;
return fetchWithFallback(
uri,
`${uri.replace("ipfs.io", "xpnetwork.infura-ipfs.io")}`,
);
}
if (url.protocol === "ipfs:") {
const response = await http.get(
`https://ipfs.io/ipfs/${uri.replace("ipfs://", "")}`,
);
return response.data;
const ipfsUri = `https://ipfs.io/ipfs/${uri.replace("ipfs://", "")}`;
const fallbackIpfsUri = `https://xpnetwork.infura-ipfs.io/ipfs/${uri.replace(
"ipfs://",
"",
)}`;
return fetchWithFallback(ipfsUri, fallbackIpfsUri);
}
throw new Error("Unsupported protocol");
}

0 comments on commit 9fa31ff

Please sign in to comment.