Exemplo n.º 1
0
        public static void Stake(SpookSettings settings, NexusAPI api, BigInteger minFee, string[] args)
        {
            if (args.Length != 1)
            {
                throw new CommandException("Expected args: amount");
            }

            DoChecks(api);

            var tempAmount  = decimal.Parse(args[0]);
            var tokenSymbol = DomainSettings.StakingTokenSymbol;

            TokenResult tokenInfo;

            try
            {
                var result = api.GetToken(tokenSymbol);
                tokenInfo = (TokenResult)result;
            }
            catch (Exception e)
            {
                throw new CommandException(e.Message);
            }

            var amount = UnitConversion.ToBigInteger(tempAmount, tokenInfo.decimals);

            var script = ScriptUtils.BeginScript().
                         AllowGas(Keys.Address, Address.Null, minFee, 9999).
                         CallContract("stake", "Stake", Keys.Address, amount).
                         SpendGas(Keys.Address).
                         EndScript();

            var hash = ExecuteTransaction(settings, api, script, ProofOfWork.None, Keys);

            if (hash != Hash.Null)
            {
                var events = GetTransactionEvents(hash);

                if (events.Any(x => x.kind == EventKind.TokenStake.ToString()))
                {
                    logger.Message($"Staked succesfully {tempAmount} {tokenSymbol} at {Keys.Address.Text}");
                }
                else
                {
                    throw new CommandException("Transaction was confirmed but missing stake event?");
                }
            }
        }
Exemplo n.º 2
0
        public static void Transfer(NexusAPI api, BigInteger minimumFee, NeoAPI neoAPI, string[] args)
        {
            if (args.Length != 4)
            {
                throw new CommandException("Expected args: source_address target_address amount symbol");
            }

            DoChecks(api);

            var tempAmount  = decimal.Parse(args[2]);
            var tokenSymbol = args[3];

            TokenResult tokenInfo;

            try
            {
                var result = api.GetToken(tokenSymbol);
                tokenInfo = (TokenResult)result;
            }
            catch (Exception e)
            {
                throw new CommandException(e.Message);
            }

            if (!tokenInfo.flags.Contains("Fungible"))
            {
                throw new CommandException("Token must be fungible!");
            }

            var amount = UnitConversion.ToBigInteger(tempAmount, tokenInfo.decimals);

            var    sourceName = args[0];
            string sourcePlatform;

            if (Address.IsValidAddress(sourceName))
            {
                sourcePlatform = PhantasmaWallet.PhantasmaPlatform;
            }
            else
            if (NeoWallet.IsValidAddress(sourceName))
            {
                sourcePlatform = NeoWallet.NeoPlatform;
            }
            else
            {
                throw new CommandException("Invalid source address " + sourceName);
            }

            var    destName = args[1];
            string destPlatform;

            if (Address.IsValidAddress(destName))
            {
                destPlatform = PhantasmaWallet.PhantasmaPlatform;
            }
            else
            if (NeoWallet.IsValidAddress(destName))
            {
                destPlatform = NeoWallet.NeoPlatform;
            }
            else
            {
                throw new CommandException("Invalid destination address " + destName);
            }

            if (destName == sourceName)
            {
                throw new CommandException("Cannot transfer to same address");
            }

            if (sourcePlatform != PhantasmaWallet.PhantasmaPlatform)
            {
                if (destPlatform != PhantasmaWallet.PhantasmaPlatform)
                {
                    if (sourcePlatform != destPlatform)
                    {
                        throw new CommandException($"Cannot transfer directly from {sourcePlatform} to {destPlatform}");
                    }
                    else
                    {
                        switch (destPlatform)
                        {
                        case NeoWallet.NeoPlatform:
                        {
                            var neoKeys = new NeoKeys(Keys.PrivateKey);

                            if (sourceName != neoKeys.Address)
                            {
                                throw new CommandException("The current open wallet does not have keys that match address " + sourceName);
                            }

                            var neoHash = NeoTransfer(neoKeys, destName, tokenSymbol, tempAmount, neoAPI);
                            return;
                        }

                        default:
                            throw new CommandException($"Not implemented yet :(");
                        }
                    }
                }
                else
                {
                    logger.Message($"Source is {sourcePlatform} address, a swap will be performed using an interop address.");

                    IPlatform platformInfo = api.Nexus.GetPlatformInfo(api.Nexus.RootStorage, sourcePlatform);

                    Hash     extHash;
                    IKeyPair extKeys;

                    switch (sourcePlatform)
                    {
                    case NeoWallet.NeoPlatform:
                    {
                        try
                        {
                            var neoKeys = new NeoKeys(Keys.PrivateKey);

                            if (sourceName != neoKeys.Address)
                            {
                                throw new CommandException("The current open wallet does not have keys that match address " + sourceName);
                            }

                            extHash = NeoTransfer(neoKeys, platformInfo.InteropAddresses[0].ExternalAddress, tokenSymbol, tempAmount, neoAPI);

                            if (extHash == Hash.Null)
                            {
                                return;
                            }

                            extKeys = neoKeys;
                        }
                        catch (Exception e)
                        {
                            logger.Message($"{sourcePlatform} error: " + e.Message);
                            return;
                        }

                        break;
                    }

                    default:
                        logger.Message($"Transactions using platform {sourcePlatform} are not supported yet");
                        return;
                    }

                    var destAddress = Address.FromText(destName);
                    SettleSwap(api, minimumFee, sourcePlatform, tokenSymbol, extHash, extKeys, destAddress);
                }
                return;
            }
            else
            {
                Address destAddress;

                if (destPlatform != PhantasmaWallet.PhantasmaPlatform)
                {
                    switch (destPlatform)
                    {
                    case NeoWallet.NeoPlatform:
                        destAddress = NeoWallet.EncodeAddress(destName);
                        break;

                    default:
                        logger.Message($"Transactions to platform {destPlatform} are not supported yet");
                        return;
                    }

                    logger.Message($"Target is {destPlatform} address, a swap will be performed through interop address {destAddress}.");
                }
                else
                {
                    destAddress = Address.FromText(destName);
                }

                var script = ScriptUtils.BeginScript().
                             CallContract("swap", "SwapFee", Keys.Address, tokenSymbol, UnitConversion.ToBigInteger(0.01m, DomainSettings.FuelTokenDecimals)).
                             AllowGas(Keys.Address, Address.Null, minimumFee, 9999).
                             TransferTokens(tokenSymbol, Keys.Address, destAddress, amount).
                             SpendGas(Keys.Address).
                             EndScript();

                logger.Message($"Sending {tempAmount} {tokenSymbol} to {destAddress.Text}...");
                ExecuteTransaction(api, script, ProofOfWork.None, Keys);
            }
        }
Exemplo n.º 3
0
        public static void Stake(NexusAPI api, string[] args)
        {
            if (args.Length != 3)
            {
                throw new CommandException("Expected args: target_address amount");
            }

            // TODO more arg validation
            var dest = Address.FromText(args[0]);

            if (dest.Text == Keys.Address.Text)
            {
                throw new CommandException("Cannot transfer to same address");
            }

            var tempAmount  = decimal.Parse(args[1]);
            var tokenSymbol = DomainSettings.StakingTokenSymbol;

            TokenResult tokenInfo;

            try
            {
                var result = api.GetToken(tokenSymbol);
                tokenInfo = (TokenResult)result;
            }
            catch (Exception e)
            {
                throw new CommandException(e.Message);
            }

            var amount = UnitConversion.ToBigInteger(tempAmount, tokenInfo.decimals);

            var script = ScriptUtils.BeginScript().
                         AllowGas(Keys.Address, Address.Null, 1, 9999).
                         CallContract("energy", "Stake", Keys.Address, dest, amount).
                         SpendGas(Keys.Address).
                         EndScript();
            var tx = new Phantasma.Blockchain.Transaction(api.Nexus.Name, "main", script, Timestamp.Now + TimeSpan.FromMinutes(5));

            tx.Sign(Keys);
            var rawTx = tx.ToByteArray(true);

            logger.Message($"Staking {tempAmount} {tokenSymbol} with {dest.Text}...");
            try
            {
                api.SendRawTransaction(Base16.Encode(rawTx));
            }
            catch (Exception e)
            {
                throw new CommandException(e.Message);
            }

            Thread.Sleep(3000);
            var hash = tx.Hash.ToString();

            do
            {
                try
                {
                    var result = api.GetTransaction(hash);
                }
                catch (Exception e)
                {
                    throw new CommandException(e.Message);
                }

                /*if (result is ErrorResult)
                 * {
                 *  var temp = (ErrorResult)result;
                 *  if (temp.error.Contains("pending"))
                 *  {
                 *      Thread.Sleep(1000);
                 *  }
                 *  else
                 *  {
                 *      throw new CommandException(temp.error);
                 *  }
                 * }
                 * else*/
                {
                    break;
                }
            } while (true);
            logger.Success($"Sent transaction with hash {hash}!");
        }