Example #1
0
        public static void Migrate(string[] args, SpookSettings settings, NexusAPI api, BigInteger minFee)
        {
            if (args.Length != 1)
            {
                throw new CommandException("Invalid number of arguments, expected new wif");
            }

            DoChecks(api);

            var newWIF  = args[0];
            var newKeys = PhantasmaKeys.FromWIF(newWIF);

            var expectedLimit = 800;

            var sb = new ScriptBuilder();

            sb.AllowGas(Keys.Address, Address.Null, minFee, expectedLimit);
            sb.CallContract("validator", "Migrate", Keys.Address, newKeys.Address);
            sb.SpendGas(Keys.Address);
            var script = sb.EndScript();

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

            if (hash != Hash.Null)
            {
                logger.Message($"Migrated to " + newKeys.Address);
                Keys = newKeys;
            }
        }
Example #2
0
        static byte[] GenWhitelist()
        {
            Console.Write("Sale hash? ");
            Hash hash;

            if (!Hash.TryParse(Console.ReadLine(), out hash))
            {
                Console.Write("Invalid sale hash");
                return(null);
            }

            Console.Write("Addresses (separated by comma or filename): ");
            var str = Console.ReadLine();

            if (str.Contains("."))
            {
                if (File.Exists(str))
                {
                    str = File.ReadAllText(str);
                }
                else
                {
                    Console.Write("File not found");
                    return(null);
                }
            }


            var addresses = str.Replace("\n", ",").Split(',').Select(x => x.Trim()).Select(x => Address.FromText(x)).ToArray();


            var sb = new ScriptBuilder().AllowGas(signerKeys.Address, Address.Null, 100000, 99999);

            foreach (var addr in addresses)
            {
                sb.CallContract(NativeContractKind.Sale, nameof(SaleContract.AddToWhitelist), hash, addr);
            }

            var script = sb.SpendGas(signerKeys.Address).
                         EndScript();

            return(script);
        }
        public static ScriptBuilder CallNFT(this ScriptBuilder sb, string symbol, BigInteger seriesID, string method, params object[] args)
        {
            var contractName = $"{symbol}#{seriesID}";

            return(sb.CallContract(contractName, method, args));
        }
 public static ScriptBuilder CallContract(this ScriptBuilder sb, NativeContractKind contractKind, string method, params object[] args)
 {
     return(sb.CallContract(contractKind.ToString().ToLower(), method, args));
 }
 public static ScriptBuilder SpendGas(this ScriptBuilder sb, Address address)
 {
     return(sb.CallContract(NativeContractKind.Gas, nameof(GasContract.SpendGas), address));
 }
 public static ScriptBuilder AllowGas(this ScriptBuilder sb, Address from, Address to, BigInteger gasPrice, BigInteger gasLimit)
 {
     return(sb.CallContract(NativeContractKind.Gas, nameof(GasContract.AllowGas), from, to, gasPrice, gasLimit));
 }
Example #7
0
 public static ScriptBuilder CrossTransferNFT(this ScriptBuilder sb, Address destinationChain, string tokenSymbol, Address from, Address to, BigInteger tokenId)
 {
     return(sb.CallContract(TokenContract, "SendToken", destinationChain, from, to, tokenSymbol, tokenId));
 }
Example #8
0
 public static ScriptBuilder TransferNFT(this ScriptBuilder sb, string tokenSymbol, Address from, Address to, BigInteger tokenId)//todo check if this is valid
 {
     return(sb.CallContract(TokenContract, "TransferToken", from, to, tokenSymbol, tokenId));
 }
Example #9
0
 public static ScriptBuilder TransferTokens(this ScriptBuilder sb, string tokenSymbol, Address from, Address to, BigInteger amount)
 {
     return(sb.CallContract(TokenContract, "TransferTokens", from, to, tokenSymbol, amount));
 }
Example #10
0
 public static ScriptBuilder MintTokens(this ScriptBuilder sb, string tokenSymbol, Address target, BigInteger amount)
 {
     return(sb.CallContract(TokenContract, "MintTokens", tokenSymbol, target, amount));
 }
Example #11
0
 public static ScriptBuilder AllowGas(this ScriptBuilder sb, Address from, Address to, BigInteger gasPrice, BigInteger gasLimit)
 {
     return(sb.CallContract(Nexus.GasContractName, "AllowGas", from, to, gasPrice, gasLimit));
 }
Example #12
0
 public static ScriptBuilder SpendGas(this ScriptBuilder sb, Address address)
 {
     return(sb.CallContract(Nexus.GasContractName, "SpendGas", address));
 }
 public static ScriptBuilder CrossTransferToken(this ScriptBuilder sb, Address destinationChain, string tokenSymbol, string from, string to, BigInteger amount)
 {
     return(sb.CallContract(TokenContract, "SendTokens", destinationChain, from, to, tokenSymbol, amount));
 }
Example #14
0
        static void Main(string[] args)
        {
            //var orgAddress = Address.FromText("S3dGUjVwYa31AxdthdpsuyBKgX1N65FnoQhUkSgYbUEdRp4");
            var orgAddress    = Address.FromText("S3dDUXfgGosu3urCrNSUAZKx7xsLTrxDzcn4CDYQEogUbao");
            var signerAddress = Address.FromText("P2KFNXEbt65rQiWqogAzqkVGMqFirPmqPw8mQyxvRKsrXV8");

            Console.WriteLine($"Enter WIF for {signerAddress.Text}");
            var wif        = Console.ReadLine();
            var signerKeys = PhantasmaKeys.FromWIF(wif);

            if (signerKeys.Address.Text != signerAddress.Text)
            {
                Console.WriteLine("Invalid WIF");
                return;
            }

            var transfers = new Dictionary <string, BigInteger>();

            //transfers["P2KCmWd4iYXed7i9HmMbANeYNA8HFeSJ1aar5yiCjz96tjt"] = UnitConversion.ToBigInteger(35000, DomainSettings.StakingTokenDecimals);
            //transfers["P2K61GfcUbfWqCur644iLECZ62NAefuKgBkB6FrpMsqYHv6"] = UnitConversion.ToBigInteger(50000, DomainSettings.StakingTokenDecimals);
            transfers["P2KFNXEbt65rQiWqogAzqkVGMqFirPmqPw8mQyxvRKsrXV8"] = UnitConversion.ToBigInteger(128866, DomainSettings.StakingTokenDecimals);

            BigInteger gasPrice = 100000;

            var sb = new ScriptBuilder().AllowGas(signerAddress, Address.Null, 100000, 99999);

            foreach (var entry in transfers)
            {
                var targetAddress = Address.FromText(entry.Key);
                sb.CallContract(NativeContractKind.Stake, nameof(StakeContract.Unstake), orgAddress, entry.Value);
                sb.TransferTokens(DomainSettings.StakingTokenSymbol, orgAddress, targetAddress, entry.Value);
            }

            var script = sb.SpendGas(signerAddress).
                         EndScript();

            var expiration = new Timestamp(Timestamp.Now.Value + 1000);
            var tx         = new Transaction("mainnet", "main", script, expiration, "TXSNDER1.0");

            tx.Sign(signerKeys);

            var rawTx = tx.ToByteArray(true);

            var hexRawTx = Base16.Encode(rawTx);


            Console.Write("URL: ");
            var url = Console.ReadLine();

            if (!url.StartsWith("http"))
            {
                url = "http://" + url;
            }

            var baseUrl = url;

            url = baseUrl + "/api/sendRawTransaction/" + hexRawTx;

            if (url.Length > 2000)
            {
                Console.WriteLine("Script is too big");
                return;
            }

            Hash txHash;

            using (var wb = new WebClient())
            {
                var response = wb.DownloadString(url);

                Console.WriteLine(response);

                if (response.StartsWith("\""))
                {
                    response = response.Substring(1, response.Length - 2);
                }

                if (!Hash.TryParse(response, out txHash))
                {
                    Console.WriteLine("Failed...");
                    return;
                }
            }

            Console.WriteLine("Confirming...");
            Thread.Sleep(1000 * 20);

            url = baseUrl + "/api/getTransaction/" + txHash.ToString();
            using (var wb = new WebClient())
            {
                var response = wb.DownloadString(url);

                Console.WriteLine(response);
            }

            Console.WriteLine("Finished.");
            Console.ReadLine();
        }
Example #15
0
        static byte[] GenDAOTransfer()
        {
            Console.Write("Organization address? ");
            string orgStr     = Console.ReadLine();
            var    orgAddress = Address.FromText(orgStr);

            Console.Write("Target address? ");
            string targetStr     = Console.ReadLine();
            var    targetAddress = Address.FromText(targetStr);

            Console.Write("Token symbol? ");
            string symbol = Console.ReadLine();

            Console.Write($"Amount of {symbol} to transfer from {orgAddress} to {targetStr}?: ");

            decimal amount;

            if (!decimal.TryParse(Console.ReadLine(), out amount) || amount <= 0)
            {
                Console.Write("Invalid amount");
                return(null);
            }

            bool   needAction = false;
            string action     = null;
            int    decimals;

            switch (symbol)
            {
            case "SOUL": action = "unstaking"; decimals = DomainSettings.StakingTokenDecimals; break;

            case "KCAL": action = "claiming"; decimals = DomainSettings.FuelTokenDecimals; break;

            default:
                throw new Exception("Unsupported token: " + symbol);
            }

            if (action != null)
            {
                needAction = FetchAnswer($"Is {action} necessary for this operation");
            }

            //S3dGUjVwYa31AxdthdpsuyBKgX1N65FnoQhUkSgYbUEdRp4
            //S3dDUXfgGosu3urCrNSUAZKx7xsLTrxDzcn4CDYQEogUbao



            var transfers = new Dictionary <string, BigInteger>();

            //transfers["P2KCmWd4iYXed7i9HmMbANeYNA8HFeSJ1aar5yiCjz96tjt"] = UnitConversion.ToBigInteger(35000, DomainSettings.StakingTokenDecimals);
            //transfers["P2K61GfcUbfWqCur644iLECZ62NAefuKgBkB6FrpMsqYHv6"] = UnitConversion.ToBigInteger(50000, DomainSettings.StakingTokenDecimals);
            //transfers["P2KFNXEbt65rQiWqogAzqkVGMqFirPmqPw8mQyxvRKsrXV8"] = UnitConversion.ToBigInteger(128866, DomainSettings.StakingTokenDecimals);

            transfers[targetAddress.Text] = UnitConversion.ToBigInteger(amount, decimals);


            var sb = new ScriptBuilder().AllowGas(signerKeys.Address, Address.Null, 100000, 99999);

            foreach (var entry in transfers)
            {
                var target = Address.FromText(entry.Key);

                if (needAction)
                {
                    switch (symbol)
                    {
                    case "SOUL":
                        sb.CallContract(NativeContractKind.Stake, nameof(StakeContract.Unstake), orgAddress, entry.Value);
                        break;

                    case "KCAL":
                        sb.CallContract(NativeContractKind.Stake, nameof(StakeContract.Claim), targetAddress, orgAddress);
                        break;
                    }
                }

                if (target != orgAddress)
                {
                    sb.TransferTokens(symbol, orgAddress, target, entry.Value);
                }
            }

            var script = sb.SpendGas(signerKeys.Address).
                         EndScript();

            return(script);
        }
Example #16
0
        static byte[] GenCreateSale()
        {
            Console.Write("Sale name? ");
            string name = Console.ReadLine();

            //Console.Write("Whitelist? ");
            SaleFlags flags = SaleFlags.Whitelist;

            Console.Write("Start date?: ");
            uint startDate;

            if (!uint.TryParse(Console.ReadLine(), out startDate))
            {
                Console.Write("Invalid start date");
                return(null);
            }
            var startTimeStamp = (Timestamp)startDate;

            Console.WriteLine($"Start time set to {startTimeStamp}");

            Console.Write("End date?: ");
            uint endDate;

            if (!uint.TryParse(Console.ReadLine(), out endDate) || endDate <= startDate)
            {
                Console.Write("Invalid end date");
                return(null);
            }
            var endTimeStamp = (Timestamp)endDate;

            Console.WriteLine($"End time set to {endTimeStamp}");

            string receiveSymbol = "SOUL";

            Console.Write("What token symbol will be sold?: ");
            string sellSymbol = Console.ReadLine();

            if (sellSymbol == DomainSettings.StakingTokenSymbol || receiveSymbol == DomainSettings.FuelTokenSymbol || !ValidationUtils.IsValidTicker(receiveSymbol))
            {
                Console.Write("Invalid token symbol");
                return(null);
            }

            int decimals;

            Console.Write($"How many decimals {sellSymbol} has?: ");
            if (!int.TryParse(Console.ReadLine(), out decimals) || decimals < 0 || decimals > 18)
            {
                Console.Write("Invalid decimals");
                return(null);
            }

            Console.Write($"Price: How many {sellSymbol} per 1 {receiveSymbol}? (must be integer number): ");
            int price;

            if (!int.TryParse(Console.ReadLine(), out price) || price <= 0)
            {
                Console.Write("Invalid decimals");
                return(null);
            }

            Console.Write($"Softcap: How many {sellSymbol} to sell minimum for sale to be succesful? (Or zero if no soft-cap): ");
            decimal globalSoftCap;

            if (!decimal.TryParse(Console.ReadLine(), out globalSoftCap) || globalSoftCap < 0)
            {
                Console.Write("Invalid softcap");
                return(null);
            }

            Console.Write($"Hardcap: How many {sellSymbol} maximum?: ");
            decimal globalHardCap;

            if (!decimal.TryParse(Console.ReadLine(), out globalHardCap) || globalHardCap <= 0)
            {
                Console.Write("Invalid hardcap");
                return(null);
            }

            Console.Write($"How many {sellSymbol} must a user buy minimum? (Or zero if no minimum): ");
            decimal userSoftCap;

            if (!decimal.TryParse(Console.ReadLine(), out userSoftCap) || userSoftCap < 0)
            {
                Console.Write("Invalid user minimum");
                return(null);
            }

            Console.Write($"What is the maximum {sellSymbol} a user can buy?: ");
            decimal userHardCap;

            if (!decimal.TryParse(Console.ReadLine(), out userHardCap) || userHardCap < userSoftCap)
            {
                Console.Write("Invalid hardcap");
                return(null);
            }

            var sb = new ScriptBuilder().AllowGas(signerKeys.Address, Address.Null, 100000, 99999);

            sb.CallContract(NativeContractKind.Sale, nameof(SaleContract.CreateSale), signerKeys.Address, name, flags, startTimeStamp, endTimeStamp, sellSymbol, receiveSymbol, price, UnitConversion.ToBigInteger(globalSoftCap, decimals), UnitConversion.ToBigInteger(globalHardCap, decimals), UnitConversion.ToBigInteger(userSoftCap, decimals), UnitConversion.ToBigInteger(userHardCap, decimals));

            var script = sb.SpendGas(signerKeys.Address).
                         EndScript();

            return(script);
        }