Exemplo n.º 1
0
 private void TopUpChannel(NexusSimulator simulator, PhantasmaKeys from, BigInteger amount)
 {
     simulator.BeginBlock();
     simulator.GenerateCustomTransaction(from, ProofOfWork.None, () =>
                                         ScriptUtils.BeginScript().AllowGas(from.Address, Address.Null, 1, 9999)
                                         .CallContract("relay", "OpenChannel", from.Address, from.PublicKey)
                                         .CallContract("relay", "TopUpChannel", from.Address, amount / RelayFeePerMessage)
                                         .SpendGas(from.Address).EndScript());
     simulator.EndBlock();
 }
Exemplo n.º 2
0
        protected void OnSetValue(string[] args)
        {
            var chain     = _cli.Nexus.GetChainByName(_cli.Nexus.RootChain.Name);
            var fuelToken = _cli.Nexus.GetTokenInfo(_cli.Nexus.RootStorage, DomainSettings.FuelTokenSymbol);
            var balance   = chain.GetTokenBalance(chain.Storage, fuelToken, _cli.NodeKeys.Address);

            if (balance == 0)
            {
                Console.WriteLine("Node wallet needs gas to create a platform token!");
                return;
            }

            var key = args[0];

            if (string.IsNullOrEmpty(key))
            {
                Console.WriteLine("Key has to be set!");
                return;
            }

            Phantasma.Numerics.BigInteger value;
            try
            {
                value = Phantasma.Numerics.BigInteger.Parse(args[1]);
            }
            catch
            {
                Console.WriteLine("Value has to be set!");
                return;
            }

            var script = ScriptUtils.BeginScript()
                         .AllowGas(_cli.NodeKeys.Address, Address.Null, 100000, 15000)
                         .CallContract("governance", "SetValue", key, value)
                         .SpendGas(_cli.NodeKeys.Address).EndScript();

            var expire = Timestamp.Now + TimeSpan.FromMinutes(2);
            var tx     = new Phantasma.Blockchain.Transaction(_cli.Nexus.Name, _cli.Nexus.RootChain.Name, script, expire, Spook.TxIdentifier);

            tx.Mine((int)ProofOfWork.Minimal);
            tx.Sign(_cli.NodeKeys);

            if (_cli.Mempool != null)
            {
                _cli.Mempool.Submit(tx);
                Console.WriteLine($"Transaction {tx.Hash} submitted to mempool.");
            }
            else
            {
                Console.WriteLine("No mempool available");
                return;
            }

            Console.WriteLine($"SetValue {key}:{value} ts: {tx.Hash}");
        }
Exemplo n.º 3
0
        public Transaction GenerateSwap(KeyPair source, Chain chain, string fromSymbol, string toSymbol, BigInteger amount)
        {
            var script = ScriptUtils.BeginScript().
                         CallContract("swap", "SwapTokens", source.Address, fromSymbol, toSymbol, amount).
                         AllowGas(source.Address, Address.Null, 1, 9999).
                         SpendGas(source.Address).
                         EndScript();
            var tx = MakeTransaction(source, chain, script);

            return(tx);
        }
Exemplo n.º 4
0
        public void TimeSkipToDate(DateTime date)
        {
            CurrentTime = date;

            BeginBlock();
            var tx = GenerateCustomTransaction(_owner, ProofOfWork.None, () =>
                ScriptUtils.BeginScript().AllowGas(_owner.Address, Address.Null, MinimumFee, 9999)
                    .CallContract(Nexus.StakeContractName, "GetUnclaimed", _owner.Address).
                    SpendGas(_owner.Address).EndScript());
            EndBlock();
        }
Exemplo n.º 5
0
        public Transaction GenerateTransfer(KeyPair source, Address dest, Chain chain, string tokenSymbol, BigInteger amount)
        {
            var script = ScriptUtils.BeginScript().
                         AllowGas(source.Address, Address.Null, 1, 9999).
                         CallContract("token", "TransferTokens", source.Address, dest, tokenSymbol, amount).
                         SpendGas(source.Address).
                         EndScript();
            var tx = MakeTransaction(source, chain, script);

            return(tx);
        }
Exemplo n.º 6
0
 public Transaction GenerateSideChainSettlement(PhantasmaKeys source, Chain sourceChain, Chain destChain, Transaction transaction)
 {
     var script = ScriptUtils.
         BeginScript().
         CallContract(Nexus.BlockContractName, "SettleTransaction", sourceChain.Address, transaction.Hash).
         AllowGas(source.Address, Address.Null, MinimumFee, 800).
         SpendGas(source.Address).
         EndScript();
     var tx = MakeTransaction(source, ProofOfWork.None, destChain, script);
     return tx;
 }
Exemplo n.º 7
0
        public void BlockSerialization()
        {
            var keysA = KeyPair.Generate();

            var txs    = new List <Transaction>();
            var symbol = DomainSettings.FuelTokenSymbol;

            int count   = 5;
            var amounts = new BigInteger[count];

            for (int i = 0; i < count; i++)
            {
                var keysB = KeyPair.Generate();
                amounts[i] = UnitConversion.ToBigInteger(20 + (i + 1), DomainSettings.FuelTokenDecimals);

                var script = ScriptUtils.BeginScript().
                             AllowGas(keysA.Address, Address.Null, 1, 9999).
                             TransferTokens(symbol, keysA.Address, keysB.Address, amounts[i]).
                             SpendGas(keysA.Address).
                             EndScript();

                var tx = new Transaction("simnet", "main", script, Timestamp.Now - TimeSpan.FromMinutes(i));

                txs.Add(tx);
            }

            var  chainKeys = KeyPair.Generate();
            var  hashes    = txs.Select(x => x.Hash);
            uint protocol  = 42;
            var  block     = new Block(1, chainKeys.Address, Timestamp.Now, hashes, Hash.Null, protocol);

            int index = 0;

            foreach (var hash in hashes)
            {
                var data = new TokenEventData()
                {
                    symbol = symbol, chainAddress = keysA.Address, value = amounts[index]
                };
                var dataBytes = Serialization.Serialize(data);
                block.Notify(hash, new Event(EventKind.TokenSend, keysA.Address, "test", dataBytes));
                index++;
            }

            var bytes = block.ToByteArray();

            Assert.IsTrue(bytes != null);

            var block2 = Block.Unserialize(bytes);

            Assert.IsTrue(block2 != null);

            Assert.IsTrue(block.Hash == block2.Hash);
        }
Exemplo n.º 8
0
        public Transaction GenerateSwap(PhantasmaKeys source, Chain chain, string fromSymbol, string toSymbol, BigInteger amount)
        {
            var script = ScriptUtils.BeginScript().
                         CallContract(NativeContractKind.Swap, nameof(SwapContract.SwapTokens), source.Address, fromSymbol, toSymbol, amount).
                         AllowGas(source.Address, Address.Null, MinimumFee, 9999).
                         SpendGas(source.Address).
                         EndScript();
            var tx = MakeTransaction(source, ProofOfWork.None, chain, script);

            return(tx);
        }
Exemplo n.º 9
0
        public Transaction GenerateChain(KeyPair source, Chain parentchain, string name, params string[] contracts)
        {
            var script = ScriptUtils.BeginScript().
                         AllowGas(source.Address, Address.Null, 1, 9999).
                         CallContract("nexus", "CreateChain", source.Address, name, parentchain.Name, contracts).
                         SpendGas(source.Address).
                         EndScript();
            var tx = MakeTransaction(source, Nexus.RootChain, script);

            return(tx);
        }
Exemplo n.º 10
0
        public void TransferScriptMethodExtraction()
        {
            var source = PhantasmaKeys.Generate();
            var dest   = PhantasmaKeys.Generate();
            var amount = UnitConversion.GetUnitValue(DomainSettings.StakingTokenDecimals);
            var script = ScriptUtils.BeginScript().AllowGas(source.Address, Address.Null, 1, 999).TransferTokens(DomainSettings.StakingTokenSymbol, source.Address, dest.Address, amount).SpendGas(source.Address).EndScript();

            var table   = DisasmUtils.GetDefaultDisasmTable();
            var methods = DisasmUtils.ExtractMethodCalls(script, table);

            Assert.IsTrue(methods != null && methods.Count() == 3);
        }
Exemplo n.º 11
0
        public Transaction GenerateNft(KeyPair source, Address destAddress, Chain chain, string tokenSymbol, byte[] rom, byte[] ram)
        {
            var script = ScriptUtils.
                         BeginScript().
                         AllowGas(source.Address, Address.Null, 1, 9999).
                         CallContract("token", "MintToken", destAddress, tokenSymbol, rom, ram).
                         SpendGas(source.Address).
                         EndScript();

            var tx = MakeTransaction(source, chain, script);

            return(tx);
        }
Exemplo n.º 12
0
        public VMObject InvokeContractAtTimestamp(StorageContext storage, Timestamp time, string contractName, string methodName, params object[] args)
        {
            var script = ScriptUtils.BeginScript().CallContract(contractName, methodName, args).EndScript();

            var result = InvokeScript(storage, script, time);

            if (result == null)
            {
                throw new ChainException($"Invocation of method '{methodName}' of contract '{contractName}' failed");
            }

            return(result);
        }
Exemplo n.º 13
0
        public Transaction GenerateChain(PhantasmaKeys source, string organization, string parentchain, string name)
        {
            Throw.IfNull(parentchain, nameof(parentchain));

            var script = ScriptUtils.BeginScript().
                         AllowGas(source.Address, Address.Null, MinimumFee, 9999).
                         CallInterop("Nexus.CreateChain", source.Address, organization, name, parentchain).
                         SpendGas(source.Address).EndScript();

            var tx = MakeTransaction(source, ProofOfWork.Minimal, Nexus.RootChain, script);

            return(tx);
        }
Exemplo n.º 14
0
        public void TimeSkipYears(int years)
        {
            CurrentTime = CurrentTime.AddYears(years);

            BeginBlock();
            var tx = GenerateCustomTransaction(_owner, ProofOfWork.None, () =>
                ScriptUtils.BeginScript().AllowGas(_owner.Address, Address.Null, MinimumFee, 9999)
                    .CallContract(Nexus.StakeContractName, "GetUnclaimed", _owner.Address).
                    SpendGas(_owner.Address).EndScript());
            EndBlock();

            var txCost = Nexus.RootChain.GetTransactionFee(tx);
        }
Exemplo n.º 15
0
        public void TimeSkipToDate(DateTime date)
        {
            CurrentTime = date;
            DateTime.SpecifyKind(CurrentTime, DateTimeKind.Utc);

            BeginBlock();
            var tx = GenerateCustomTransaction(_owner, ProofOfWork.None, () =>
                                               ScriptUtils.BeginScript().AllowGas(_owner.Address, Address.Null, MinimumFee, 9999)
                                               .CallContract(NativeContractKind.Stake, nameof(StakeContract.GetUnclaimed), _owner.Address).
                                               SpendGas(_owner.Address).EndScript());

            EndBlock();
        }
Exemplo n.º 16
0
        public Transaction MintNonFungibleToken(PhantasmaKeys owner, Address destination, string tokenSymbol, byte[] rom, byte[] ram)
        {
            var chain = Nexus.RootChain;
            var script = ScriptUtils.
                BeginScript().
                AllowGas(owner.Address, Address.Null, MinimumFee, 9999).
                CallInterop("Runtime.MintToken", owner.Address, destination, tokenSymbol, rom, ram).  
                SpendGas(owner.Address).
                EndScript();

            var tx = MakeTransaction(owner, ProofOfWork.None, chain, script);
            return tx;
        }
Exemplo n.º 17
0
        public async Task <string> TransferTokensCustomNFT(PhantasmaKeys keyPair, string addressTo, string chainName, string symbol, string amountId, string payload, bool donation, MultisigSettings settings = new MultisigSettings())
        {
            try
            {
                int    decimals     = PhantasmaTokens.SingleOrDefault(t => t.Symbol == symbol).Decimals;
                var    bigIntAmount = UnitConversion.ToBigInteger(decimal.Parse(amountId), decimals);
                byte[] script;

                var    destinationAddress         = Address.FromText(addressTo);
                var    bigIntAmountDonation       = 500000000;
                string symbolDonation             = "SOUL";
                var    destinationAddressDonation = Address.FromText("P2K61GfcUbfWqCur644iLECZ62NAefuKgBkB6FrpMsqYHv6");
                if (donation)
                {
                    script = ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .TransferTokens(symbol, keyPair.Address, destinationAddress, bigIntAmount)
                             .TransferTokens(symbolDonation, keyPair.Address, destinationAddressDonation, bigIntAmountDonation)
                             .SpendGas(keyPair.Address)
                             .EndScript();
                }
                else
                {
                    script = ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .TransferTokens(symbol, keyPair.Address, destinationAddress, bigIntAmount)
                             .SpendGas(keyPair.Address)
                             .EndScript();
                }

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, chainName, script,
                                                                     DateTime.UtcNow + TimeSpan.FromMinutes(30), payload);
                tx.Sign(keyPair);

                var txResult = await _phantasmaRpcService.SendRawTx.SendRequestAsync(tx.ToByteArray(true).Encode());

                Log.Information("txResult send: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Information($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return("");
            }
            catch (Exception ex)
            {
                Log.Information($"Exception occurred: {ex.Message}");
                return("");
            }
        }
Exemplo n.º 18
0
        public Transaction MintNonFungibleToken(KeyPair source, Address destAddress, string tokenSymbol, byte[] rom, byte[] ram, BigInteger value)
        {
            var chain  = Nexus.RootChain;
            var script = ScriptUtils.
                         BeginScript().
                         AllowGas(source.Address, Address.Null, 1, 9999).
                         CallContract("token", "MintToken", destAddress, tokenSymbol, rom, ram, value).
                         SpendGas(source.Address).
                         EndScript();

            var tx = MakeTransaction(source, chain, script);

            return(tx);
        }
Exemplo n.º 19
0
        public Transaction GenerateSideChainSettlement(KeyPair source, Chain sourceChain, Chain destChain, Hash targetHash)
        {
            _pendingBlocks.RemoveAll(x => x.hash == targetHash);

            var script = ScriptUtils.
                         BeginScript().
                         CallInterop("Runtime.SettleBlock", sourceChain.Address, targetHash).
                         AllowGas(source.Address, Address.Null, MinimumFee, 9999).
                         SpendGas(source.Address).
                         EndScript();
            var tx = MakeTransaction(source, ProofOfWork.None, destChain, script);

            return(tx);
        }
Exemplo n.º 20
0
        protected void OnPlatformAddressAdd(string[] args)
        {
            var platform        = args[0];
            var externalAddress = args[1];

            Address localAddress;

            switch (platform)
            {
            case NeoWallet.NeoPlatform:
                localAddress = NeoWallet.EncodeAddress(externalAddress);
                break;

            case EthereumWallet.EthereumPlatform:
                localAddress = EthereumWallet.EncodeAddress(externalAddress);
                break;

            case BSCWallet.BSCPlatform:
                localAddress = BSCWallet.EncodeAddress(externalAddress);
                break;

            default:
                throw new Exception("Unknown platform: " + platform);
            }

            var minimumFee = _cli.Settings.Node.MinimumFee;
            var script     = ScriptUtils.BeginScript()
                             .AllowGas(_cli.NodeKeys.Address, Address.Null, minimumFee, 1500)
                             .CallContract("interop", nameof(InteropContract.RegisterAddress), _cli.NodeKeys.Address, platform, localAddress, externalAddress)
                             .SpendGas(_cli.NodeKeys.Address).EndScript();

            var expire = Timestamp.Now + TimeSpan.FromMinutes(2);
            var tx     = new Phantasma.Blockchain.Transaction(_cli.Nexus.Name, _cli.Nexus.RootChain.Name, script, expire, Spook.TxIdentifier);

            tx.Mine((int)ProofOfWork.Minimal);
            tx.Sign(_cli.NodeKeys);

            if (_cli.Mempool != null)
            {
                _cli.Mempool.Submit(tx);
                Console.WriteLine($"Transaction {tx.Hash} submitted to mempool.");
            }
            else
            {
                Console.WriteLine("No mempool available");
                return;
            }
            Console.WriteLine($"Added address {externalAddress} to {platform}");
            Spook.Logger.Message($"Added address {externalAddress} to {platform}");
        }
Exemplo n.º 21
0
        public Transaction GenerateSideChainSettlement(KeyPair source, Chain sourceChain, Chain destChain, Hash targetHash)
        {
            _pendingBlocks.RemoveAll(x => x.hash == targetHash);

            var script = ScriptUtils.
                         BeginScript().
                         CallContract("token", "SettleBlock", sourceChain.Address, targetHash).
                         AllowGas(source.Address, Address.Null, 1, 9999).
                         SpendGas(source.Address).
                         EndScript();
            var tx = MakeTransaction(source, destChain, script);

            return(tx);
        }
Exemplo n.º 22
0
        private Transaction ChainCreateTx(KeyPair owner, string name, params string[] contracts)
        {
            var script = ScriptUtils.
                         BeginScript().
                         AllowGas(owner.Address, Address.Null, 1, 9999).
                         CallContract(ScriptBuilderExtensions.NexusContract, "CreateChain", owner.Address, name, RootChain.Name, contracts).
                         SpendGas(owner.Address).
                         EndScript();

            var tx = new Transaction(Name, RootChainName, script, Timestamp.Now + TimeSpan.FromDays(300));

            tx.Sign(owner);
            return(tx);
        }
Exemplo n.º 23
0
        public void TestGetBotWrestler()
        {
            var owner     = PhantasmaKeys.Generate();
            var simulator = new NexusSimulator(owner, 1234);
            var nexus     = simulator.Nexus;
            var api       = new NexusAPI(nexus);


            var callScript    = ScriptUtils.BeginScript().CallContract("nacho", "GetWrestler", new object[] { -1 }).EndScript();
            var apiResult     = (ScriptResult)api.InvokeRawScript("main", Base16.Encode(callScript));
            var bytes         = Base16.Decode(apiResult.results[0]);
            var objResult     = Serialization.Unserialize <VMObject>(bytes);
            var nachoWrestler = objResult.ToStruct <NachoWrestler>();
        }
Exemplo n.º 24
0
        public void TimeSkip(double days)
        {
            CurrentTime = CurrentTime.AddDays(days);

            BeginBlock();
            var tx = GenerateCustomTransaction(_owner, () =>
                                               ScriptUtils.BeginScript().AllowGas(_owner.Address, Address.Null, 1, 9999)
                                               .CallContract("energy", "GetUnclaimed", _owner.Address).
                                               SpendGas(_owner.Address).EndScript());

            EndBlock();

            var txCost = Nexus.RootChain.GetTransactionFee(tx);
        }
Exemplo n.º 25
0
        private Transaction ConsensusStakeCreateTx(Chain chain, KeyPair owner)
        {
            var script = ScriptUtils.
                         BeginScript().
                         AllowGas(owner.Address, Address.Null, 1, 9999).
                         CallContract("consensus", "Stake", owner.Address).
                         SpendGas(owner.Address).
                         EndScript();

            var tx = new Transaction(Name, chain.Name, script, Timestamp.Now + TimeSpan.FromDays(300));

            tx.Sign(owner);
            return(tx);
        }
Exemplo n.º 26
0
        public Transaction InfuseNonFungibleToken(PhantasmaKeys owner, string tokenSymbol, BigInteger tokenID, string infuseSymbol, BigInteger value)
        {
            var chain  = Nexus.RootChain;
            var script = ScriptUtils.
                         BeginScript().
                         AllowGas(owner.Address, Address.Null, MinimumFee, 9999).
                         CallInterop("Runtime.InfuseToken", owner.Address, tokenSymbol, tokenID, infuseSymbol, value).
                         SpendGas(owner.Address).
                         EndScript();

            var tx = MakeTransaction(owner, ProofOfWork.None, chain, script);

            return(tx);
        }
Exemplo n.º 27
0
        private static async Task SameChainTransfer(string addressTo, string amount, string tokenSymbol, string chain)
        {
            var destinationAddress = Address.FromText(addressTo);
            int decimals           = Helper.GetTokenDecimals(tokenSymbol, _tokens);
            var bigIntAmount       = UnitConversion.ToBigInteger(decimal.Parse(amount), decimals);

            var script = ScriptUtils.BeginScript()
                         .AllowGas(_key.Address, Address.Null, 1, 9999)
                         .TransferTokens(tokenSymbol, _key.Address, destinationAddress, bigIntAmount)
                         .SpendGas(_key.Address)
                         .EndScript();

            await SignAndSendTx(script, chain);
        }
Exemplo n.º 28
0
        public void TimeSkipHours(int hours)
        {
            CurrentTime = CurrentTime.AddHours(hours);
            DateTime.SpecifyKind(CurrentTime, DateTimeKind.Utc);

            BeginBlock();
            var tx = GenerateCustomTransaction(_owner, ProofOfWork.None, () =>
                                               ScriptUtils.BeginScript().AllowGas(_owner.Address, Address.Null, MinimumFee, 9999)
                                               .CallContract(NativeContractKind.Stake, nameof(StakeContract.GetUnclaimed), _owner.Address).
                                               SpendGas(_owner.Address).EndScript());

            EndBlock();

            var txCost = Nexus.RootChain.GetTransactionFee(tx);
        }
Exemplo n.º 29
0
        private static async Task <string> SettleBlock(string sourceChainAddress, string blockHash, string destinationChainAddress)
        {
            var sourceChain          = Address.FromText(sourceChainAddress);
            var destinationChainName = _chains.SingleOrDefault(c => c.Address == destinationChainAddress).Name;

            var block = Hash.Parse(blockHash);

            var settleTxScript = ScriptUtils.BeginScript()
                                 .CallContract("token", "SettleBlock", sourceChain, block)
                                 .AllowGas(_key.Address, Address.Null, 1, 9999)
                                 .SpendGas(_key.Address)
                                 .EndScript();

            return(await SignAndSendTx(settleTxScript, destinationChainName));
        }
Exemplo n.º 30
0
        public Transaction MintTokens(PhantasmaKeys owner, Address destination, string symbol, BigInteger amount)
        {
            var chain = Nexus.RootChain;

            var script = ScriptUtils.
                         BeginScript().
                         AllowGas(owner.Address, Address.Null, MinimumFee, 9999).
                         MintTokens(symbol, owner.Address, destination, amount).
                         SpendGas(owner.Address).
                         EndScript();

            var tx = MakeTransaction(owner, ProofOfWork.None, chain, script);

            return(tx);
        }