我已经建立了 NFT 质押网站,它工作正常,除了我在质押或取消质押时强制显示图像更新。这是因为使用moralis getnftforcontract api 和opensea api 无法立即更新图像。代码如下。
const get_balanceUrls = async () => {
await Moralis.Web3API.initialize({ apiKey: WEB3APIKEY });
const options = { chain: CHAINETHID, address: walletAddress, token_address: G_ADDRESS, };
let ownedNFTs = await Moralis.Web3API.account.getNFTsForContract(options);
console.log("owning NFTS", ownedNFTs.result);
let stakedNFTs = Object.values(balanceofstakes);
if(ownedNFTs.result.length == 0 && nft_unstakeBalance.length == 0) {
setBalanceImageUrl([]);
return;
}
let token_ids = "";
ownedNFTs.result.map((value, index) => {
if(stakedNFTs.indexOf(value.token_id) < 0) {
token_ids += 'token_ids=' + value.token_id + "&";
}
})
nft_unstakeBalance.map((value, index) => {
token_ids += 'token_ids=' + value + "&";
});
if(token_ids == "") {
setBalanceImageUrl([]);
return;
}
console.log("token_ids", token_ids)
let assets = await Axios.get(`${OPENSEA_URL}assets?
order_direction=desc&offset=0&limit=20&${token_ids}asset_contract_address=${G_ADDRESS}`)
.then(res => {
console.log("balanceurl", res);
return res.data.assets;
})
.catch(err => {
console.log(err);
return [];
});
setBalanceImageUrl(assets);
}
我认为应该有更方便的方法来解决这个问题。如果您知道,请随时回答或与我讨论。