Exemplo n.º 1
0
        /**
         * 获取渔场信息
         */
        public static NFTInfo tokenData(BigInteger tokenId)
        {
            object[] objInfo = _getNFTInfo(tokenId.AsByteArray());
            NFTInfo  info    = (NFTInfo)(object)objInfo;

            return(info);
        }
Exemplo n.º 2
0
        public static bool approve(byte[] approved, BigInteger tokenId)
        {
            if (approved.Length != 20)
            {
                return(false);
            }

            object[] objInfo = _getNFTInfo(tokenId.AsByteArray());
            NFTInfo  info    = (NFTInfo)(object)objInfo;

            byte[] tokenOwner = info.owner;
            if (tokenOwner.Length != 20)
            {
                Runtime.Log("Token does not exist");
                return(false);
            }

            if (Runtime.CheckWitness(tokenOwner))
            {
                string approvalKey = "apr/" + tokenId;

                // only one third-party spender can be approved
                // at any given time for a specific token

                Storage.Put(Storage.CurrentContext, approvalKey, approved);
                Approved(tokenOwner, approved, tokenId);

                return(true);
            }

            Runtime.Log("Incorrect permission");
            return(false);
        }
Exemplo n.º 3
0
        IEnumerator INFTMarket.FetchNFTInfo(string assetContractAddress, string tokenId, Action <NFTInfo> onSuccess, Action <string> onError)
        {
            string nftId = $"{assetContractAddress}/{tokenId}";

            if (cachedResponses.ContainsKey(nftId))
            {
                onSuccess?.Invoke(cachedResponses[nftId]);
                yield break;
            }

            OpenSeaRequest request = requestController.AddRequest(assetContractAddress, tokenId);

            yield return(request.OnResolved(
                             (assetResponse) =>
            {
                NFTInfo nftInfo = ResponseToNFTInfo(assetResponse);
                if (!cachedResponses.ContainsKey(nftId))
                {
                    cachedResponses.Add(nftId, nftInfo);
                }
                onSuccess?.Invoke(nftInfo);
            },
                             (error) =>
            {
                onError?.Invoke($"{openSeaMarketInfo.name} error fetching {assetContractAddress}/{tokenId} {error}");
            }));
        }
Exemplo n.º 4
0
        private NFTInfo ResponseToNFTInfo(AssetResponse response)
        {
            NFTInfo ret = NFTInfo.defaultNFTInfo;

            ret.marketInfo       = openSeaMarketInfo;
            ret.name             = response.name;
            ret.description      = response.description;
            ret.thumbnailUrl     = response.image_thumbnail_url;
            ret.previewImageUrl  = response.image_preview_url;
            ret.originalImageUrl = response.image_original_url;
            ret.assetLink        = response.external_link;
            ret.marketLink       = response.permalink;

            if (!string.IsNullOrEmpty(response.owner?.address))
            {
                ret.owner = response.owner.address;
            }

            if (response.num_sales != null)
            {
                ret.numSales = response.num_sales.Value;
            }

            if (response.last_sale != null)
            {
                ret.lastSaleDate = response.last_sale.event_timestamp;

                if (response.last_sale.payment_token != null)
                {
                    ret.lastSaleAmount = PriceToFloatingPointString(response.last_sale);
                    ret.lastSaleToken  = new NFT.PaymentTokenInfo()
                    {
                        symbol = response.last_sale.payment_token.symbol
                    };
                }
            }

            UnityEngine.Color backgroundColor;
            if (UnityEngine.ColorUtility.TryParseHtmlString("#" + response.background_color, out backgroundColor))
            {
                ret.backgroundColor = backgroundColor;
            }

            OrderInfo sellOrder = GetSellOrder(response.sell_orders, response.owner.address);

            if (sellOrder != null)
            {
                ret.currentPrice      = PriceToFloatingPointString(sellOrder.current_price, sellOrder.payment_token_contract);
                ret.currentPriceToken = new NFT.PaymentTokenInfo()
                {
                    symbol = sellOrder.payment_token_contract.symbol
                };
            }

            return(ret);
        }
Exemplo n.º 5
0
 public CatalogItem(NFTInfo nFTInfo)
 {
     asset_pack_id = BuilderInWorldSettings.ASSETS_COLLECTIBLES;
     id            = nFTInfo.assetContract.address;
     thumbnail     = nFTInfo.thumbnailUrl;
     SetBaseURL(nFTInfo.originalImageUrl);
     name     = nFTInfo.name;
     category = nFTInfo.assetContract.name;
     model    = BuilderInWorldSettings.COLLECTIBLE_MODEL_PROTOCOL + nFTInfo.assetContract.address + "/" + nFTInfo.tokenId;
     tags     = new List <string>();
     contents = new Dictionary <string, string>();
     metrics  = new SceneObject.ObjectMetrics();
 }
Exemplo n.º 6
0
        public static bool transfer(byte[] from, byte[] to, BigInteger tokenId)
        {
            if (from.Length != 20 || to.Length != 20)
            {
                return(false);
            }

            StorageContext ctx = Storage.CurrentContext;

            if (from == to)
            {
                //Runtime.Log("Transfer to self!");
                return(true);
            }

            object[] objInfo = _getNFTInfo(tokenId.AsByteArray());
            if (objInfo.Length == 0)
            {
                return(false);
            }

            NFTInfo info = (NFTInfo)(object)objInfo;

            byte[] ownedBy = info.owner;

            if (from != ownedBy)
            {
                //Runtime.Log("Token is not owned by tx sender");
                return(false);
            }
            //bool res = _subOwnerToken(from, tokenId);
            //if (!res)
            //{
            //    //Runtime.Log("Unable to transfer token");
            //    return false;
            //}
            //_addOwnerToken(to, tokenId);

            info.owner = to;
            _putNFTInfo(tokenId.AsByteArray(), info);

            //remove any existing approvals for this token
            byte[] approvalKey = "apr/".AsByteArray().Concat(tokenId.AsByteArray());
            Storage.Delete(ctx, approvalKey);

            //记录交易信息
            _setTxInfo(from, to, tokenId);

            Transferred(from, to, tokenId);
            return(true);
        }
Exemplo n.º 7
0
        /**
         * 获取渔场拥有者
         */
        public static byte[] ownerOf(BigInteger tokenId)
        {
            object[] objInfo = _getNFTInfo(tokenId.AsByteArray());
            NFTInfo  info    = (NFTInfo)(object)objInfo;

            if (info.owner.Length > 0)
            {
                return(info.owner);
            }
            else
            {
                //return System.Text.Encoding.ASCII.GetBytes("token does not exist");
                return(new byte[] { });
            }
        }
Exemplo n.º 8
0
    private IEnumerator FetchNFTImage(NFTInfo nftInfo)
    {
        spinnerNftImage.SetActive(true);

        ITexture nftImageAsset = null;

        yield return(WrappedTextureUtils.Fetch(nftInfo.previewImageUrl,
                                               (downloadedTex, texturePromise) =>
        {
            nftImageAsset = downloadedTex;
            this.texturePromise = texturePromise;
        }));

        if (nftImageAsset == null)
        {
            yield return(WrappedTextureUtils.Fetch(nftInfo.originalImageUrl,
                                                   (downloadedTex, texturePromise) =>
            {
                nftImageAsset = downloadedTex;
                this.texturePromise = texturePromise;
            }, Asset_Gif.MaxSize._256));
        }

        if (nftImageAsset != null)
        {
            imageAsset       = nftImageAsset;
            imageNft.texture = nftImageAsset.texture;

            if (nftImageAsset is Asset_Gif gifAsset)
            {
                gifAsset.OnFrameTextureChanged += (texture) => { imageNft.texture = texture; };
                gifAsset.Play();
            }
            else
            {
                if (!backgroundColorSet)
                {
                    SetSmartBackgroundColor(nftImageAsset.texture);
                }
            }

            SetNFTImageSize(nftImageAsset.texture);

            imageNft.gameObject.SetActive(true);
            spinnerNftImage.SetActive(false);
        }
    }
Exemplo n.º 9
0
        /**
         * 生成新的渔场数据,生成事件
         */
        private static BigInteger createFOF(byte[] tokenOwner, byte isBinded, BigInteger bindId,
                                            BigInteger Id
                                            )
        {
            if (tokenOwner.Length != 20)
            {
                // Owner error.
                return(0);
            }

            //
            if (Runtime.CheckWitness(MintOwner))
            {
                //判断下是否超过总量
                byte[] tokenaId = Storage.Get(Storage.CurrentContext, KEY_ALL);
                byte[] tokenId  = Storage.Get(Storage.CurrentContext, KEY_TOTAL);
                if (tokenId.AsBigInteger() >= tokenaId.AsBigInteger())
                {
                    return(0);
                }
                BigInteger newToken = tokenId.AsBigInteger() + 1;
                tokenId = newToken.AsByteArray();

                NFTInfo newInfo = new NFTInfo();
                newInfo.owner    = tokenOwner;
                newInfo.isBinded = 0;
                newInfo.bindID   = 0;
                newInfo.Id       = tokenId


                                   _putNFTInfo(tokenId, newInfo);

                //_addOwnerToken(tokenOwner, tokenId.AsBigInteger());

                Storage.Put(Storage.CurrentContext, KEY_TOTAL, tokenId);

                // notify
                Birthed(tokenId.AsBigInteger(), newInfo.owner, newInfo.isBinded, newInfo.bindID, newInfo.Id);
                return(tokenId.AsBigInteger());
            }
            else
            {
                Runtime.Log("Only the contract owner may mint new tokens.");
                return(0);
            }
        }
Exemplo n.º 10
0
    SceneObject NFTInfoToSceneObject(NFTInfo nftInfo)
    {
        SceneObject sceneObject = new SceneObject();

        sceneObject.asset_pack_id = BuilderInWorldSettings.ASSETS_COLLECTIBLES;
        sceneObject.id            = nftInfo.assetContract.address;
        sceneObject.thumbnail     = nftInfo.thumbnailUrl;
        sceneObject.SetBaseURL(nftInfo.originalImageUrl);
        sceneObject.name     = nftInfo.name;
        sceneObject.category = nftInfo.assetContract.name;
        sceneObject.model    = BuilderInWorldSettings.COLLECTIBLE_MODEL_PROTOCOL + nftInfo.assetContract.address + "/" + nftInfo.tokenId;
        sceneObject.tags     = new List <string>();
        sceneObject.contents = new Dictionary <string, string>();
        sceneObject.metrics  = new SceneObject.ObjectMetrics();

        return(sceneObject);
    }
Exemplo n.º 11
0
    public static CatalogItem CreateCatalogItem(NFTInfo nFTInfo)
    {
        CatalogItem catalogItem = new CatalogItem();

        catalogItem.itemType = CatalogItem.ItemType.NFT;

        catalogItem.id           = nFTInfo.assetContract.address;
        catalogItem.thumbnailURL = nFTInfo.thumbnailUrl;
        catalogItem.name         = nFTInfo.name;
        catalogItem.category     = nFTInfo.assetContract.name;
        catalogItem.model        = $"{BuilderInWorldSettings.COLLECTIBLE_MODEL_PROTOCOL}{nFTInfo.assetContract.address}/{nFTInfo.tokenId}";
        catalogItem.tags         = new List <string>();
        catalogItem.contents     = new Dictionary <string, string>();
        catalogItem.metrics      = new SceneObject.ObjectMetrics();

        return(catalogItem);
    }
Exemplo n.º 12
0
    private IEnumerator FetchNFTImage(NFTInfo nftInfo)
    {
        spinnerNftImage.SetActive(true);

        IWrappedTextureAsset nftImageAsset = null;

        yield return(Utils.FetchWrappedTextureAsset(nftInfo.previewImageUrl,
                                                    (asset) =>
        {
            nftImageAsset = asset;
        }));

        if (nftImageAsset == null)
        {
            yield return(Utils.FetchWrappedTextureAsset(nftInfo.originalImageUrl,
                                                        (asset) =>
            {
                nftImageAsset = asset;
            }, WrappedTextureMaxSize._256));
        }

        if (nftImageAsset != null)
        {
            imageAsset       = nftImageAsset;
            imageNft.texture = nftImageAsset.texture;

            var gifAsset = nftImageAsset as WrappedGif;
            if (gifAsset != null)
            {
                gifAsset.SetUpdateTextureCallback((texture) =>
                {
                    imageNft.texture = texture;
                });
            }
            SetNFTImageSize(nftImageAsset.texture);
            if (!backgroundColorSet)
            {
                SetSmartBackgroundColor(nftImageAsset.texture);
            }

            imageNft.gameObject.SetActive(true);
            spinnerNftImage.SetActive(false);
        }
    }
    private IEnumerator FetchNFTImage(NFTInfo nftInfo)
    {
        spinnerNftImage.SetActive(true);

        bool imageFound = false;

        yield return(WrappedTextureUtils.Fetch(nftInfo.previewImageUrl,
                                               promise =>
        {
            imagePromise = promise;
            imageFound = true;
        }));

        if (!imageFound)
        {
            yield return(WrappedTextureUtils.Fetch(nftInfo.originalImageUrl,
                                                   promise =>
            {
                imagePromise = promise;
                imageFound = true;
            }));
        }

        if (imageFound && imagePromise?.asset != null)
        {
            Texture2D texture = imagePromise.asset.texture;
            imageNft.texture = texture;

            if ((imagePromise.asset is Asset_Gif gif))
            {
                SetupGifPlayer(gif);
            }
            else if (!backgroundColorSet)
            {
                SetSmartBackgroundColor(texture);
            }

            SetNFTImageSize(texture);

            imageNft.gameObject.SetActive(true);
            spinnerNftImage.SetActive(false);
        }
    IEnumerator FetchNFTImage()
    {
        if (spinner != null)
        {
            spinner.SetActive(true);
        }

        NFTInfo nftInfo = new NFTInfo();

        bool isError = false;

        yield return(NFTHelper.FetchNFTInfo(darURLRegistry, darURLAsset,
                                            (info) =>
        {
            nftInfo = info;
        },
                                            (error) =>
        {
            Debug.LogError($"Couldn't fetch NFT: '{darURLRegistry}/{darURLAsset}' {error}");
            OnLoadingAssetFail?.Invoke();
            isError = true;
        }));

        if (isError)
        {
            yield break;
        }

        yield return(new DCL.WaitUntil(() => (CommonScriptableObjects.playerUnityPosition - transform.position).sqrMagnitude < (config.loadingMinDistance * config.loadingMinDistance)));

        // We download the "preview" 256px image
        bool foundDCLImage = false;

        if (!string.IsNullOrEmpty(nftInfo.previewImageUrl))
        {
            yield return(WrappedTextureUtils.Fetch(nftInfo.previewImageUrl, (promise) =>
            {
                foundDCLImage = true;
                this.assetPromise = promise;
                SetFrameImage(promise.asset, resizeFrameMesh: true);
                SetupGifPlayer(promise.asset);

                var hqImageHandlerConfig = new NFTShapeHQImageConfig()
                {
                    controller = this,
                    nftConfig = config,
                    nftInfo = nftInfo,
                    asset = NFTAssetFactory.CreateAsset(promise.asset, config, UpdateTexture, gifPlayer)
                };
                hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig);
            }));
        }

        //We fall back to the nft original image which can have a really big size
        if (!foundDCLImage && !string.IsNullOrEmpty(nftInfo.originalImageUrl))
        {
            yield return(WrappedTextureUtils.Fetch(nftInfo.originalImageUrl,
                                                   (promise) =>
            {
                foundDCLImage = true;
                this.assetPromise = promise;
                SetFrameImage(promise.asset, resizeFrameMesh: true);
                SetupGifPlayer(promise.asset);
            }, () => isError = true));
        }

        if (isError)
        {
            Debug.LogError($"Couldn't fetch NFT image for: '{darURLRegistry}/{darURLAsset}' {nftInfo.originalImageUrl}");
            OnLoadingAssetFail?.Invoke();
            yield break;
        }

        FinishLoading(foundDCLImage);
    }
Exemplo n.º 15
0
    private void SetNFTInfo(NFTInfo info, string comment)
    {
        spinnerGeneral.SetActive(false);

        imageNftBackground.color = Color.white;
        backgroundColorSet       = info.backgroundColor != null;
        if (backgroundColorSet)
        {
            imageNftBackground.color = info.backgroundColor.Value;
        }

        textNftName.text = info.name;
        textNftName.gameObject.SetActive(true);

        textOwner.text = FormatOwnerAddress(info.owner);
        textOwner.gameObject.SetActive(true);

        if (!string.IsNullOrEmpty(info.lastSaleAmount))
        {
            textLastSalePrice.text = ShortDecimals(info.lastSaleAmount, 4);
            textLastSalePrice.gameObject.SetActive(true);
        }
        else
        {
            textLastSaleNeverSold.gameObject.SetActive(true);
        }

        if (!string.IsNullOrEmpty(info.currentPrice))
        {
            textPrice.text = ShortDecimals(info.currentPrice, 4);
            textPrice.gameObject.SetActive(true);

            if (info.currentPriceToken != null)
            {
                SetTokenSymbol(textPriceSymbol, info.currentPriceToken.Value.symbol);
            }
        }
        else
        {
            textPriceNotForSale.gameObject.SetActive(true);
        }

        if (info.lastSaleToken != null)
        {
            SetTokenSymbol(textLastSaleSymbol, info.lastSaleToken.Value.symbol);
        }

        if (!string.IsNullOrEmpty(info.description))
        {
            textDescription.text = info.description;
            containerDescription.SetActive(true);
        }

        if (!string.IsNullOrEmpty(comment))
        {
            textComment.text = comment;
            containerComment.SetActive(true);
        }

        textOpenMarketButton.text = "VIEW";
        if (info.marketInfo != null)
        {
            textOpenMarketButton.text = $"{textOpenMarketButton.text} ON {info.marketInfo.Value.name.ToUpper()}";
        }

        marketUrl = null;
        if (!string.IsNullOrEmpty(info.marketLink))
        {
            marketUrl = info.marketLink;
        }
        else if (!string.IsNullOrEmpty(info.assetLink))
        {
            marketUrl = info.assetLink;
        }

        buttonCancel.gameObject.SetActive(true);
        buttonOpenMarket.gameObject.SetActive(true);

        fetchNFTImageRoutine = StartCoroutine(FetchNFTImage(info));
    }
Exemplo n.º 16
0
        /**
         * 存储渔场信息
         */
        private static void _putNFTInfo(byte[] tokenId, NFTInfo info)
        {
            byte[] nftInfo = Helper.Serialize(info);

            Storage.Put(Storage.CurrentContext, tokenId, nftInfo);
        }
Exemplo n.º 17
0
    private void SetNFTInfo(NFTInfo info, string comment)
    {
        spinnerGeneral.SetActive(false);

        imageNftBackground.color = Color.white;
        backgroundColorSet       = info.backgroundColor != null;
        if (backgroundColorSet)
        {
            imageNftBackground.color = info.backgroundColor.Value;
        }

        textNftName.text = info.name;
        textNftName.gameObject.SetActive(true);

        textOwner.text = FormatOwnerAddress(info.owner);
        textOwner.gameObject.SetActive(true);

        if (!string.IsNullOrEmpty(info.lastSaleAmount))
        {
            textLastSalePrice.text = ShortDecimals(info.lastSaleAmount, 4);
            textLastSalePrice.gameObject.SetActive(true);
        }
        else
        {
            textLastSaleNeverSold.gameObject.SetActive(true);
        }

        if (!string.IsNullOrEmpty(info.currentPrice))
        {
            textPrice.text = ShortDecimals(info.currentPrice, 4);
            textPrice.gameObject.SetActive(true);

            if (info.currentPriceToken != null)
            {
                SetTokenSymbol(textPriceSymbol, info.currentPriceToken.Value.symbol);
            }
        }
        else
        {
            textPriceNotForSale.gameObject.SetActive(true);
        }

        if (info.lastSaleToken != null)
        {
            SetTokenSymbol(textLastSaleSymbol, info.lastSaleToken.Value.symbol);
        }

        if (!string.IsNullOrEmpty(info.description))
        {
            textDescription.text = info.description;
            containerDescription.SetActive(true);
        }

        if (!string.IsNullOrEmpty(comment))
        {
            textComment.text = comment;
            containerComment.SetActive(true);
        }

        textOpenMarketButton.text = "VIEW";
        if (info.marketInfo != null)
        {
            textOpenMarketButton.text = $"{textOpenMarketButton.text} IN {info.marketInfo.Value.name.ToUpper()}";
        }

        marketUrl = null;
        if (!string.IsNullOrEmpty(info.marketLink))
        {
            marketUrl = info.marketLink;
        }
        else if (!string.IsNullOrEmpty(info.assetLink))
        {
            marketUrl = info.assetLink;
        }

        buttonCancel.gameObject.SetActive(true);
        buttonOpenMarket.gameObject.SetActive(true);

        if (!string.IsNullOrEmpty(info.thumbnailUrl))
        {
            spinnerNftImage.SetActive(true);
            fetchNFTImageRoutine = StartCoroutine(Utils.FetchWrappedTextureAsset(info.thumbnailUrl, (asset) =>
            {
                imageAsset       = asset;
                imageNft.texture = asset.texture;

                var gifAsset = asset as WrappedGif;
                if (gifAsset != null)
                {
                    gifAsset.SetUpdateTextureCallback((texture) =>
                    {
                        imageNft.texture = texture;
                    });
                }
                SetNFTImageSize(asset.texture);
                if (!backgroundColorSet)
                {
                    SetSmartBackgroundColor(asset.texture);
                }

                imageNft.gameObject.SetActive(true);
                spinnerNftImage.SetActive(false);
            }));
        }
    }
Exemplo n.º 18
0
        public static bool transferFrom(byte[] tokenFrom, byte[] tokenTo, BigInteger tokenId)
        {
            if (tokenFrom.Length != 20)
            {
                return(false);
            }
            if (tokenTo.Length != 20)
            {
                return(false);
            }

            if (tokenFrom == tokenTo)
            {
                Runtime.Log("Transfer to self!");
                return(true);
            }

            object[] objInfo = _getNFTInfo(tokenId.AsByteArray());
            if (objInfo.Length == 0)
            {
                return(false);
            }

            NFTInfo info = (NFTInfo)(object)objInfo;

            byte[] tokenOwner = info.owner;
            if (tokenOwner.Length != 20)
            {
                Runtime.Log("Token does not exist");
                return(false);
            }
            if (tokenFrom != tokenOwner)
            {
                Runtime.Log("From address is not the owner of this token");
                return(false);
            }

            byte[] approvalKey       = "apr/".AsByteArray().Concat(tokenId.AsByteArray());
            byte[] authorizedSpender = Storage.Get(Storage.CurrentContext, approvalKey);

            if (authorizedSpender.Length == 0)
            {
                Runtime.Log("No approval exists for this token");
                return(false);
            }

            if (Runtime.CheckWitness(authorizedSpender))
            {
                //bool res = _subOwnerToken(tokenFrom, tokenId);
                //if (res == false)
                //{
                //    Runtime.Log("Unable to transfer token");
                //    return false;
                //}
                //_addOwnerToken(tokenTo, tokenId);

                info.owner = tokenTo;
                _putNFTInfo(tokenId.AsByteArray(), info);

                // remove the approval for this token
                Storage.Delete(Storage.CurrentContext, approvalKey);

                Runtime.Log("Transfer complete");

                //记录交易信息
                _setTxInfo(tokenFrom, tokenTo, tokenId);

                Transferred(tokenFrom, tokenTo, tokenId);
                return(true);
            }

            Runtime.Log("Transfer by tx sender not approved by token owner");
            return(false);
        }