package com.odcchina.fai.util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import static com.ruoyi.common.config.datasource.DynamicDataSourceContextHolder.log;
/**
* nft解析
*/
@Service
public class NftUtil {
@Autowired
private RestTemplate restTemplate;
public JSONObject etherTokenURI(String contract, BigInteger tokenId) {
String uri = tokenURI("https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161", contract, tokenId);
String jsonUrl = decodeUrl(uri);
JSONObject resp = fetchUrl(jsonUrl);
if (resp == null) {
return new JSONObject();
}
//log.info(resp.toJSONString());
String image = resp.getString("image");
resp.put("image", decodeUrl(image));
return resp;
}
private String tokenURI(String rpcUrl, String contract, BigInteger tokenId) {
Web3j web3j = Web3j.build(new HttpService(rpcUrl));
Function function = new Function("tokenURI",
Arrays.asList(new Uint256(tokenId)),
Arrays.asList(TypeReference.create(Utf8String.class))
);
Transaction transaction = Transaction.createEthCallTransaction(contract,
contract, FunctionEncoder.encode(function));
try {
EthCall ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
if (ethCall.getError() != null) {
log.error("tokenURI - {}", ethCall.getError().getMessage());
return null;
}
List<Type> result = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
Utf8String uri = (Utf8String) result.get(0);
return uri.getValue();
} catch (IOException | NullPointerException e) {
log.error(e.getLocalizedMessage());
//throw new RuntimeException(e);
}
return null;
}
//解析URL,IPFS url转为https链接
//ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/18
private String decodeUrl(String ipfsUrl) {
if (ipfsUrl == null) {
return null;
}
if (ipfsUrl.substring(0, 4).equalsIgnoreCase("http")) {
return ipfsUrl;
}
if (ipfsUrl.substring(0, 12).equalsIgnoreCase("ipfs://ipfs/")) {
return "https://ipfs.io/ipfs/" + ipfsUrl.substring(12);
}
if (ipfsUrl.substring(0, 7).equalsIgnoreCase("ipfs://")) {
return "https://ipfs.io/ipfs/" + ipfsUrl.substring(7);
}
return null;
}
private JSONObject fetchUrl(String jsonUrl) {
if (jsonUrl == null) {
return null;
}
try {
return restTemplate.getForObject(jsonUrl, JSONObject.class);
} catch (Exception e) {
e.getLocalizedMessage();
}
return null;
}
}
nft analysis, ipfs to HTTPS link
Guess you like
Origin blog.csdn.net/qq_38935605/article/details/125204086
Recommended
Ranking