Exemplo n.º 1
0
        private Chain FindChainByInput(string chainInput)
        {
            var chain = Nexus.FindChainByName(chainInput);

            if (chain != null)
            {
                return(chain);
            }

            if (Address.IsValidAddress(chainInput))
            {
                return(Nexus.FindChainByAddress(Address.FromText(chainInput)));
            }

            return(null);
        }
Exemplo n.º 2
0
        private TransactionResult FillTransaction(Transaction tx)
        {
            var block = Nexus.FindBlockByTransaction(tx);
            var chain = Nexus.FindChainByAddress(block.ChainAddress);

            var result = new TransactionResult
            {
                hash          = tx.Hash.ToString(),
                chainAddress  = chain.Address.Text,
                timestamp     = block.Timestamp.Value,
                blockHeight   = block.Height,
                blockHash     = block.Hash.ToString(),
                confirmations = Nexus.GetConfirmationsOfBlock(block),
                script        = tx.Script.Encode()
            };

            var eventList = new List <EventResult>();

            var evts = block.GetEventsForTransaction(tx.Hash);

            foreach (var evt in evts)
            {
                var eventEntry = new EventResult
                {
                    address = evt.Address.Text,
                    data    = evt.Data.Encode(),
                    kind    = evt.Kind.ToString()
                };
                eventList.Add(eventEntry);
            }
            result.events = eventList.ToArray();

            var txResult = block.GetResultForTransaction(tx.Hash);

            result.result = txResult != null?Base16.Encode(txResult) : "";

            return(result);
        }
Exemplo n.º 3
0
        public ChainSimulator(KeyPair ownerKey, int seed, Logger logger = null)
        {
            this.Logger = logger != null ? logger : new DummyLogger();

            _owner     = ownerKey;
            this.Nexus = new Nexus();

            CurrentTime = new DateTime(2018, 8, 26);

            if (!Nexus.CreateGenesisBlock("simnet", _owner, CurrentTime))
            {
                throw new ChainException("Genesis block failure");
            }

            this.bankChain = Nexus.FindChainByName("bank");

            _rnd = new System.Random(seed);
            _keys.Add(_owner);

            var oneFuel      = UnitConversion.ToBigInteger(1, Nexus.FuelTokenDecimals);
            var localBalance = Nexus.RootChain.GetTokenBalance(Nexus.FuelTokenSymbol, _owner.Address);

            if (localBalance < oneFuel)
            {
                throw new Exception("Funds missing oops");
            }

            var nachoAddress = Address.FromText("PGasVpbFYdu7qERihCsR22nTDQp1JwVAjfuJ38T8NtrCB");
            var nachoFuel    = UnitConversion.ToBigInteger(5, Nexus.FuelTokenDecimals);
            var nachoChain   = Nexus.FindChainByName("nacho");

            var appsChain = Nexus.FindChainByName("apps");

            BeginBlock();
            GenerateSideChainSend(_owner, Nexus.FuelTokenSymbol, Nexus.RootChain, _owner.Address, appsChain, oneFuel, 0);
            GenerateSideChainSend(_owner, Nexus.FuelTokenSymbol, Nexus.RootChain, nachoAddress, nachoChain, nachoFuel, 9999);
            GenerateSideChainSend(_owner, Nexus.FuelTokenSymbol, Nexus.RootChain, Address.FromText("P27j1vgY1cjVYPnPDqjAVvqtxMmK9qjYvqz99EFp8vrPQ"), nachoChain, nachoFuel, 9999);
            var blockTx = EndBlock().First();

            BeginBlock();
            GenerateSideChainSettlement(_owner, Nexus.RootChain, appsChain, blockTx.Hash);
            GenerateSideChainSettlement(_owner, Nexus.RootChain, nachoChain, blockTx.Hash);
            EndBlock();

            BeginBlock();
            GenerateChain(_owner, Nexus.RootChain, "dex");
            GenerateChain(_owner, Nexus.RootChain, "market");
            EndBlock();

            BeginBlock();
            GenerateAppRegistration(_owner, "nachomen", "https://nacho.men", "Collect, train and battle against other players in Nacho Men!");
            GenerateAppRegistration(_owner, "mystore", "https://my.store", "The future of digital content distribution!");
            GenerateAppRegistration(_owner, "nftbazar", "https://nft.bazar", "A decentralized NFT market");

            GenerateToken(_owner, Constants.NACHO_SYMBOL, "Nachomen", 0, 0, TokenFlags.Transferable);
            GenerateToken(_owner, Constants.WRESTLER_SYMBOL, "Nachomen Luchador", 0, 0, TokenFlags.Transferable);
            GenerateToken(_owner, Constants.ITEM_SYMBOL, "Nachomen Item", 0, 0, TokenFlags.Transferable);
            EndBlock();

            var market = Nexus.FindChainByName("market");

            BeginBlock();

            var nachoSymbol = "NACHO";

            RandomSpreadNFT(nachoSymbol, 150);

            GenerateSetTokenMetadata(_owner, nachoSymbol, "details", "https://nacho.men/luchador/*");
            GenerateSetTokenMetadata(_owner, nachoSymbol, "viewer", "https://nacho.men/luchador/body/*");
            EndBlock();

            var nftSales = new List <KeyValuePair <KeyPair, BigInteger> >();

            BeginBlock();
            for (int i = 1; i < 7; i++)
            {
                BigInteger   ID = i + 100;
                TokenContent info;
                try
                {
                    info = Nexus.GetNFT(nachoSymbol, ID);
                }
                catch
                {
                    continue;
                }

                var chain = Nexus.FindChainByAddress(info.CurrentChain);
                if (chain == null)
                {
                    continue;
                }

                var nftOwner = chain.GetTokenOwner(nachoSymbol, ID);

                if (nftOwner == Address.Null)
                {
                    continue;
                }

                foreach (var key in _keys)
                {
                    if (key.Address == nftOwner)
                    {
                        nftSales.Add(new KeyValuePair <KeyPair, BigInteger>(key, ID));
                        // send some gas to the sellers
                        GenerateTransfer(_owner, key.Address, Nexus.RootChain, Nexus.FuelTokenSymbol, UnitConversion.ToBigInteger(0.01m, Nexus.FuelTokenDecimals));
                    }
                }
            }

            EndBlock();

            BeginBlock();
            foreach (var sale in nftSales)
            {
                // TODO this later should be the market chain instead of root
                GenerateNftSale(sale.Key, Nexus.RootChain, nachoSymbol, sale.Value, UnitConversion.ToBigInteger(100 + 5 * _rnd.Next() % 50, Nexus.FuelTokenDecimals));
            }
            EndBlock();

            BeginBlock();

            var newWrestler = new NachoWrestler()
            {
                auctionID          = 0,
                battleCount        = 0,
                comments           = new string[0],
                currentMojo        = 10,
                experience         = 10000,
                flags              = WrestlerFlags.None,
                genes              = new byte[] { 115, 169, 73, 21, 111, 3, 174, 90, 137, 58 }, //"Piece, 115, 169, 73, 21, 111, 3, 174, 90, 137, 58"
                gymBoostAtk        = byte.MaxValue,
                gymBoostDef        = byte.MaxValue,
                gymBoostStamina    = byte.MaxValue,
                gymTime            = 0,
                itemID             = 0,
                location           = WrestlerLocation.None,
                maskOverrideCheck  = byte.MaxValue,
                maskOverrideID     = byte.MaxValue,
                maskOverrideRarity = byte.MaxValue,
                maxMojo            = 10,
                mojoTime           = 0,
                moveOverrides      = new byte[0],
                nickname           = "testname",
                owner              = nachoAddress,
                perfumeTime        = 0,
                praticeLevel       = PraticeLevel.Gold,
                roomTime           = 0,
                score              = 0,
                stakeAmount        = 0,
                trainingStat       = StatKind.None,
                ua1 = byte.MaxValue,
                ua2 = byte.MaxValue,
                ua3 = byte.MaxValue,
                us1 = byte.MaxValue,
                us2 = byte.MaxValue,
                us3 = byte.MaxValue
            };

            var wrestlerBytes = newWrestler.Serialize();

            GenerateNft(_owner, nachoAddress, nachoSymbol, new byte[0], wrestlerBytes);

            EndBlock();
        }
Exemplo n.º 4
0
        public ChainSimulator(KeyPair ownerKey, int seed, int cacheSize, Logger logger = null)
        {
            this.Logger = logger != null ? logger : new DummyLogger();

            _owner     = ownerKey;
            this.Nexus = new Nexus("simnet", ownerKey.Address, cacheSize);

            CurrentTime = new DateTime(2018, 8, 26);

            if (!Nexus.CreateGenesisBlock(_owner, CurrentTime))
            {
                throw new ChainException("Genesis block failure");
            }

            this.bankChain = Nexus.FindChainByName("bank");

            _rnd = new System.Random(seed);
            _keys.Add(_owner);

            var oneFuel      = UnitConversion.ToBigInteger(1, Nexus.FuelTokenDecimals);
            var localBalance = Nexus.RootChain.GetTokenBalance(Nexus.FuelToken, _owner.Address);

            if (localBalance < oneFuel)
            {
                throw new Exception("Funds missing oops");
            }

            var appsChain = Nexus.FindChainByName("apps");

            BeginBlock();
            GenerateSideChainSend(_owner, Nexus.FuelToken, Nexus.RootChain, _owner.Address, appsChain, oneFuel, 0);
            var blockTx = EndBlock().First();

            BeginBlock();
            GenerateSideChainSettlement(_owner, Nexus.RootChain, appsChain, blockTx.Hash);
            GenerateChain(_owner, Nexus.RootChain, "dex");
            GenerateChain(_owner, Nexus.RootChain, "market");
            EndBlock();

            BeginBlock();
            GenerateAppRegistration(_owner, "nachomen", "https://nacho.men", "Collect, train and battle against other players in Nacho Men!");
            GenerateAppRegistration(_owner, "mystore", "https://my.store", "The future of digital content distribution!");
            GenerateAppRegistration(_owner, "nftbazar", "https://nft.bazar", "A decentralized NFT market");
            GenerateToken(_owner, "NACHO", "Nachomen", 0, 0, TokenFlags.Transferable);
            EndBlock();

            var market = Nexus.FindChainByName("market");

            BeginBlock();

            var nacho = Nexus.FindTokenBySymbol("NACHO");

            RandomSpreadNFT(nacho, 150);

            GenerateSetTokenMetadata(_owner, nacho, "details", "https://nacho.men/luchador/*");
            GenerateSetTokenMetadata(_owner, nacho, "viewer", "https://nacho.men/luchador/body/*");
            EndBlock();

            var nftSales = new List <KeyValuePair <KeyPair, BigInteger> >();

            BeginBlock();
            for (int i = 1; i < 7; i++)
            {
                BigInteger ID   = i + 100;
                var        info = Nexus.GetNFT(nacho, ID);
                if (info == null)
                {
                    continue;
                }

                var chain = Nexus.FindChainByAddress(info.CurrentChain);
                if (chain == null)
                {
                    continue;
                }

                var nftOwner = chain.GetTokenOwner(nacho, ID);

                if (nftOwner == Address.Null)
                {
                    continue;
                }

                foreach (var key in _keys)
                {
                    if (key.Address == nftOwner)
                    {
                        nftSales.Add(new KeyValuePair <KeyPair, BigInteger>(key, ID));
                        // send some gas to the sellers
                        GenerateTransfer(_owner, key.Address, Nexus.RootChain, Nexus.FuelToken, UnitConversion.ToBigInteger(0.01m, Nexus.FuelTokenDecimals));
                    }
                }
            }

            EndBlock();

            BeginBlock();
            foreach (var sale in nftSales)
            {
                // TODO this later should be the market chain instead of root
                GenerateNftSale(sale.Key, Nexus.RootChain, nacho, sale.Value, UnitConversion.ToBigInteger(100 + 5 * _rnd.Next() % 50, Nexus.FuelTokenDecimals));
            }
            EndBlock();
        }