Example #1
0
        public async Task <object> RegisterName(PhantasmaKeys keyPair, string name)
        {
            try
            {
                var script = ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .CallContract("account", "RegisterName", keyPair.Address, name)
                             .SpendGas(keyPair.Address)
                             .EndScript();

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, "main", script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                tx.Sign(keyPair);

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

                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #2
0
        public async Task <string> TransferMultipleNFT(PhantasmaKeys keyPair, string addressTo, string chainName, string symbol, string idArr, bool isName)
        {
            try
            {
                JArray jsonparam = JsonConvert.DeserializeObject <JArray>(idArr);
                byte[] bscript;

                if (isName)
                {
                    var script = ScriptUtils.BeginScript()
                                 .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800 * (jsonparam.Count));

                    foreach (var id in jsonparam)
                    {
                        var bigIntAmount = BigInteger.Parse((string)id["id"]);
                        script.TransferNFT(symbol, keyPair.Address, addressTo, bigIntAmount);
                    }

                    script.SpendGas(keyPair.Address);
                    bscript = script.EndScript();
                }
                else
                {
                    var destinationAddress = Address.FromText(addressTo);
                    var script             = ScriptUtils.BeginScript()
                                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800 * (jsonparam.Count));

                    foreach (var id in jsonparam)
                    {
                        var bigIntAmount = BigInteger.Parse((string)id["id"]);
                        script.TransferNFT(symbol, keyPair.Address, destinationAddress, bigIntAmount);
                    }

                    script.SpendGas(keyPair.Address);
                    bscript = script.EndScript();
                }


                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, chainName, bscript,
                                                                     DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");
                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}");
                return("");
            }
        }
Example #3
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("");
            }
        }
Example #4
0
        public async Task <object> MarketBuyCustom(
            PhantasmaKeys keyPair, string chain, string contract, string method, object[] paramArray, string feeamount, string feesymbol)
        {
            try
            {
                var bigIntAmount = UnitConversion.ToBigInteger(decimal.Parse(feeamount), GetTokenDecimals(feesymbol));

                var destinationAddress = Address.FromText("P2KLzxq8cUi47URLZZYcLs54WPVYnknrHWVryUUUqhuhq5K");

                var script = (bigIntAmount == 0)
                    ? ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .CallContract(contract, method, paramArray)
                             .SpendGas(keyPair.Address)
                             .EndScript()

                    : ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .CallContract(contract, method, paramArray)
                             .TransferTokens(feesymbol, keyPair.Address, destinationAddress, bigIntAmount)
                             .SpendGas(keyPair.Address)
                             .EndScript();

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, chain, script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                tx.Sign(keyPair);

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

                Log.Information("txResult: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #5
0
        public async Task <string> CrossChainTransferToken(bool isFungible, PhantasmaKeys keyPair, string addressTo,
                                                           string chainName, string destinationChain, string symbol, string amountId)
        {
            try
            {
                var toChain            = PhantasmaChains.Find(p => p.Name == destinationChain);
                var destinationAddress = Address.FromText(addressTo);
                int decimals           = PhantasmaTokens.SingleOrDefault(t => t.Symbol == symbol).Decimals;
                var bigIntAmount       = UnitConversion.ToBigInteger(decimal.Parse(amountId), decimals);
                var fee = UnitConversion.ToBigInteger(0.0001m, 8);

                var script = isFungible
                    ? ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .CrossTransferToken(Address.FromText(toChain.Address), symbol, keyPair.Address,
                                                 keyPair.Address, fee)
                             .CrossTransferToken(Address.FromText(toChain.Address), symbol, keyPair.Address,
                                                 destinationAddress, bigIntAmount)
                             .SpendGas(keyPair.Address)
                             .EndScript()

                    : ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .CrossTransferNFT(Address.FromText(toChain.Address), 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), "PHT-0-8-7");
                tx.Sign(keyPair);

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

                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Debug.WriteLine($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(null);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Exception occurred: {ex.Message}");
                return(null);
            }
        }
Example #6
0
        public async Task <string> SendRawTx(Phantasma.Blockchain.Transaction tx)
        {
            try
            {
                var txResult = await _phantasmaRpcService.SendRawTx.SendRequestAsync(tx.ToByteArray(true).Encode());

                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Debug.WriteLine($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(null);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Exception occurred: {ex.Message}");
                return(null);
            }
        }
Example #7
0
        public async Task <object> CosmicCustom(
            PhantasmaKeys keyPair, string chain, string contract, string method, object[] paramArray)
        {
            try
            {
                var    bigIntAmount       = 10000000000;
                string symbol             = "KCAL";
                var    destinationAddress = Address.FromText("P2K61GfcUbfWqCur644iLECZ62NAefuKgBkB6FrpMsqYHv6");
                var    script             = ScriptUtils.BeginScript()
                                            .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                            .TransferTokens(symbol, keyPair.Address, destinationAddress, bigIntAmount)
                                            .CallContract(contract, method, paramArray)
                                            .SpendGas(keyPair.Address)
                                            .EndScript();

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, chain, script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                tx.Sign(keyPair);

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

                Log.Information("txResult: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #8
0
        public async Task <object> InvokeSettleTx(NeoKeys neoKeys, PhantasmaKeys phantasmaKeys, string txHash, string symbol)
        {
            try
            {
                Hash neoTxHash         = Hash.Parse(txHash);
                var  transcodedAddress = Address.FromKey(neoKeys);

                var script = ScriptUtils.BeginScript()
                             .CallContract("interop", "SettleTransaction", transcodedAddress, NeoWallet.NeoPlatform, NeoWallet.NeoPlatform, neoTxHash)
                             .CallContract("swap", "SwapFee", transcodedAddress, symbol, UnitConversion.ToBigInteger(0.1m, DomainSettings.FuelTokenDecimals))
                             .TransferBalance(symbol, transcodedAddress, phantasmaKeys.Address)
                             .AllowGas(transcodedAddress, Address.Null, MinimumFee, 800)
                             .SpendGas(transcodedAddress)
                             .EndScript();

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, "main", script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                tx.Sign(neoKeys);

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

                Log.Information("txResult: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #9
0
        public async Task <object> CreateStakeSoulTransactionWithClaim(
            PhantasmaKeys keyPair, string stakeAmount)
        {
            try
            {
                var bigIntAmount = UnitConversion.ToBigInteger(decimal.Parse(stakeAmount), 0);
                var script       = ScriptUtils.BeginScript()
                                   .CallContract("stake", "Stake", keyPair.Address, bigIntAmount)
                                   .CallContract("stake", "Claim", keyPair.Address, keyPair.Address)
                                   .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                   .SpendGas(keyPair.Address)
                                   .EndScript();

                var nexusName = WalletConfig.Network;
                var stakeTx   = new Phantasma.Blockchain.Transaction(nexusName, "main", script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                stakeTx.Sign(keyPair);

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

                Log.Information("txResult: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #10
0
        public async Task <string> SettleBlockTransfer(PhantasmaKeys keyPair, string sourceChainAddress, string blockHash,
                                                       string destinationChainAddress)
        {
            try
            {
                var sourceChain          = Address.FromText(sourceChainAddress);
                var destinationChainName =
                    PhantasmaChains.SingleOrDefault(c => c.Address == destinationChainAddress).Name;
                var nexusName = WalletConfig.Network;

                var block = Hash.Parse(blockHash);

                var settleTxScript = ScriptUtils.BeginScript()
                                     .CallContract("token", "SettleBlock", sourceChain, block)
                                     .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                     .SpendGas(keyPair.Address)
                                     .EndScript();

                var settleTx = new Phantasma.Blockchain.Transaction(nexusName, destinationChainName, settleTxScript, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");
                settleTx.Sign(keyPair);

                var settleResult =
                    await _phantasmaRpcService.SendRawTx.SendRequestAsync(settleTx.ToByteArray(true).Encode());

                return(settleResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Debug.WriteLine($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(null);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Exception occurred: {ex.Message}");
                return(null);
            }
        }
Example #11
0
        public async Task <object> CosmicFixed(PhantasmaKeys keyPair)
        {
            try
            {
                var bigIntAmount = 10000000;
                var script       = ScriptUtils.BeginScript()
                                   .CallContract("swap", "SwapTokens", keyPair.Address, DomainSettings.StakingTokenSymbol, DomainSettings.FuelTokenSymbol, bigIntAmount)
                                   .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                   .SpendGas(keyPair.Address)
                                   .EndScript();

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, "main", script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                tx.Sign(keyPair);

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

                Log.Information("txResult: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #12
0
        public async Task <object> InvokeContractTxGeneric(
            PhantasmaKeys keyPair, string chain, string contract, string method, object[] paramArray)
        {
            try
            {
                var script = ScriptUtils.BeginScript()
                             .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                             .CallContract(contract, method, paramArray)
                             .SpendGas(keyPair.Address)
                             .EndScript();

                var nexusName = WalletConfig.Network;
                var tx        = new Phantasma.Blockchain.Transaction(nexusName, chain, script, DateTime.UtcNow + TimeSpan.FromMinutes(30), "PHT-0-8-7");

                tx.Sign(keyPair);

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

                Log.Information("txResult: " + txResult);
                return(txResult);
            }
            catch (RpcResponseException rpcEx)
            {
                Log.Error($"RPC Exception occurred: {rpcEx.RpcError.Message}");
                return(new ErrorRes {
                    error = rpcEx.RpcError.Message
                });
            }
            catch (Exception ex)
            {
                Log.Error($"Exception occurred: {ex.Message}");
                return(new ErrorRes {
                    error = ex.Message
                });
            }
        }
Example #13
0
        public async Task <string> TransferTokens(bool isFungible, PhantasmaKeys keyPair, string addressTo, string chainName, string symbol, string amountId, bool isName, MultisigSettings settings = new MultisigSettings())
        {
            try
            {
                int    decimals = PhantasmaTokens.SingleOrDefault(t => t.Symbol == symbol).Decimals;
                byte[] script;
                if (isFungible)
                {
                    var bigIntAmount = UnitConversion.ToBigInteger(decimal.Parse(amountId), decimals);
                    if (NeoWallet.IsValidAddress(addressTo))
                    {
                        var addressNeo = NeoWallet.EncodeAddress(addressTo);
                        Log.Information("Transfer to " + addressNeo);
                        script = ScriptUtils.BeginScript()
                                 .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                 .TransferTokens(symbol, keyPair.Address, addressNeo, bigIntAmount)
                                 .SpendGas(keyPair.Address)
                                 .EndScript();
                    }
                    else
                    {
                        if (isName)
                        {
                            Log.Information("Transfer to " + addressTo);
                            script = ScriptUtils.BeginScript()
                                     .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                     .TransferTokens(symbol, keyPair.Address, addressTo, bigIntAmount)
                                     .SpendGas(keyPair.Address)
                                     .EndScript();
                        }
                        else
                        {
                            var destinationAddress = Address.FromText(addressTo);
                            Log.Information("Transfer to " + destinationAddress.Text);
                            script = ScriptUtils.BeginScript()
                                     .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                     .TransferTokens(symbol, keyPair.Address, destinationAddress, bigIntAmount)
                                     .SpendGas(keyPair.Address)
                                     .EndScript();
                        }
                    }
                }
                else
                {
                    var bigIntAmount = BigInteger.Parse(amountId);
                    if (isName)
                    {
                        Log.Information("Transfer to " + addressTo);
                        script = ScriptUtils.BeginScript()
                                 .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                 .TransferNFT(symbol, keyPair.Address, addressTo, bigIntAmount)
                                 .SpendGas(keyPair.Address)
                                 .EndScript();
                    }
                    else
                    {
                        var destinationAddress = Address.FromText(addressTo);
                        Log.Information("Transfer to " + destinationAddress.Text);
                        script = ScriptUtils.BeginScript()
                                 .AllowGas(keyPair.Address, Address.Null, MinimumFee, 800)
                                 .TransferNFT(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), "PHT-0-8-7");
                tx.Sign(keyPair);

                // from here on we need PhantasmaRelay to proceed with a multisig TX
                //
                //if (settings.addressCount != null)
                //{
                //

                //}
                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("");
            }
        }