ListNftWebWallet.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.Text;
  5. using Models;
  6. using Newtonsoft.Json;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. using UnityEngine.UI;
  10. using Web3Unity.Scripts.Library.ETHEREUEM.Connect;
  11. using Web3Unity.Scripts.Library.Web3Wallet;
  12. namespace Web3Unity.Scripts.Prefabs.Minter
  13. {
  14. public class ListNftWebWallet : MonoBehaviour
  15. {
  16. private readonly string chain = "ethereum";
  17. private readonly string network = "goerli";
  18. private readonly string chainID = "5";
  19. private string _itemPrice = "0.001";
  20. private string _tokenType = "";
  21. private string _itemID = "";
  22. private string account;
  23. public Renderer textureObject;
  24. public Text description;
  25. public Text tokenURI;
  26. public Text contractAddr;
  27. public Text isApproved;
  28. public InputField itemPrice;
  29. public Text noListedItems;
  30. public Text playerAccount;
  31. public void Awake()
  32. {
  33. account = PlayerPrefs.GetString("Account");
  34. description.text = "";
  35. tokenURI.text = "";
  36. isApproved.text = "";
  37. contractAddr.text = "";
  38. }
  39. // Start is called before the first frame update
  40. private async void Start()
  41. {
  42. playerAccount.text = account;
  43. try
  44. {
  45. var response = await EVM.GetMintedNFT(chain, network, account);
  46. if (response[1].uri == null)
  47. {
  48. Debug.Log("Not Listed Items");
  49. return;
  50. }
  51. if (response[1].uri.StartsWith("ipfs://"))
  52. {
  53. response[1].uri = response[1].uri.Replace("ipfs://", "https://ipfs.chainsafe.io/ipfs/");
  54. }
  55. var webRequest = UnityWebRequest.Get(response[1].uri);
  56. await webRequest.SendWebRequest();
  57. var data = JsonConvert.DeserializeObject<RootGetNFT>(Encoding.UTF8.GetString(webRequest.downloadHandler.data));
  58. description.text = data.description;
  59. // parse json to get image uri
  60. var imageUri = data.image;
  61. if (imageUri.StartsWith("ipfs://"))
  62. {
  63. imageUri = imageUri.Replace("ipfs://", "https://ipfs.chainsafe.io/ipfs/");
  64. StartCoroutine(DownloadImage(imageUri));
  65. }
  66. else
  67. {
  68. StartCoroutine(DownloadImage(imageUri));
  69. }
  70. tokenURI.text = response[1].uri;
  71. Debug.Log(response[1].uri);
  72. contractAddr.text = response[1].nftContract;
  73. Debug.Log("NFT Contract: " + response[1].nftContract);
  74. isApproved.text = response[1].isApproved.ToString();
  75. _itemID = response[1].id;
  76. _itemPrice = itemPrice.text;
  77. Debug.Log("Token Type: " + response[1].tokenType);
  78. _tokenType = response[1].tokenType;
  79. }
  80. catch (Exception e)
  81. {
  82. noListedItems.text = "NO LISTED ITEM for " + account;
  83. Debug.Log("No Listed Items" + e);
  84. }
  85. }
  86. // ReSharper disable Unity.PerformanceAnalysis
  87. private IEnumerator DownloadImage(string mediaUrl)
  88. {
  89. var request = UnityWebRequestTexture.GetTexture(mediaUrl);
  90. yield return request.SendWebRequest();
  91. if (request.result == UnityWebRequest.Result.ProtocolError)
  92. {
  93. Debug.Log(request.error);
  94. }
  95. else
  96. {
  97. var webTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;
  98. var webSprite = SpriteFromTexture2D(webTexture);
  99. textureObject.GetComponent<Image>().sprite = webSprite;
  100. }
  101. }
  102. private Sprite SpriteFromTexture2D(Texture2D texture)
  103. {
  104. return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
  105. }
  106. public async void ListItem()
  107. {
  108. var eth = float.Parse(_itemPrice);
  109. float decimals = 1000000000000000000; // 18 decimals
  110. var wei = eth * decimals;
  111. Debug.Log("ItemID: " + _itemID);
  112. var response =
  113. await EVM.CreateListNftTransaction(chain, network, account, _itemID, Convert.ToDecimal(wei).ToString(CultureInfo.InvariantCulture),
  114. _tokenType);
  115. var value = Convert.ToInt32(response.tx.value.hex, 16);
  116. Debug.Log("Response: " + response);
  117. try
  118. {
  119. var responseNft = await Web3Wallet.SendTransaction(chainID, response.tx.to, value.ToString(),
  120. response.tx.data, response.tx.gasLimit, response.tx.gasPrice);
  121. if (responseNft == null) Debug.Log("Empty Response Object:");
  122. }
  123. catch (Exception e)
  124. {
  125. Debug.Log("Error: " + e);
  126. }
  127. }
  128. }
  129. }