[英]How can I fetch data from OpenSea in real time?
我構建了一個簡單的 web 應用程序,它根據我輸入的錢包地址從 opensea 輸出一個“NFT 錢包”。
現在假設發生交易並且錢包的所有者出售或購買了一個 nft,我如何實時更新我的 web 應用程序?
換句話說,我如何實時監聽來自 api 和 output 的錢包變化?
應用程序.js:
async function getNFT(address) {
const response = await fetch("https://api.opensea.io/api/v1/assets?owner=" + address + "&order_direction=desc&offset=0&limit=20");
const data = await response.json();
console.log(data);
let NFTWallet = [];
for (const asset of data.assets) {
const NFT = {
url: asset.image_url,
name: asset.asset_contract.name
}
NFTWallet.push(NFT);
let nft = document.createElement("span");
let image = document.createElement("span");
image.innerHTML = "<img src='" + NFT.url + "' width=\"84px\" height=\"84px\" title=\"" + NFT.name + "\"/>";
nft.appendChild(image);
document.getElementById("nft-wallet").appendChild(nft);
}
console.log(NFTWallet);
}
window.addEventListener("load", getNFT("wallet-adress"));//input wallet in getNft()
索引.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenSea Connection</title>
<script src="app.js"></script>
</head>
<body>
<h1>NFT Wallet</h1>
<div id="nft-wallet"></div>
</body>
</html>
You need to store the results of the first response from the function getNFT locally for example using Window.localStorage, and use setInterval() to call again the function getNFT with a delay in between executions and compare the results with the previous values.
對此有什么答案嗎? 我很感興趣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.