Exemple #1
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("");
            }
        }
    /// <summary>
    /// Check the tokens deployed in Phantasma Blockchain.
    /// </summary>
    public void CheckTokens(Action callback = null)
    {
        IsTokenCreated = false;

        CanvasManager.Instance.ShowOperationPopup("Fetching Phantasma tokens...", false);

        PhantasmaTokens.Clear();

        StartCoroutine(PhantasmaApi.GetTokens(
                           (result) =>
        {
            foreach (var token in result)
            {
                PhantasmaTokens.Add(token.symbol, token);

                if (token.symbol.Equals(TOKEN_SYMBOL))
                {
                    IsTokenCreated = true;
                    break;
                }
            }

            CanvasManager.Instance.HideOperationPopup();

            if (callback != null)
            {
                callback();
            }
        },
                           (errorType, errorMessage) =>
        {
            CanvasManager.Instance.HideOperationPopup();

            if (CanvasManager.Instance.loginMenu.gameObject.activeInHierarchy)
            {
                CanvasManager.Instance.ShowResultPopup(EOPERATION_RESULT.FAIL, errorType + " - " + errorMessage);
            }
            else if (CanvasManager.Instance.adminMenu.gameObject.activeInHierarchy)
            {
                CanvasManager.Instance.ShowResultPopup(EOPERATION_RESULT.FAIL, errorType + " - " + errorMessage);
            }
            else
            {
                CanvasManager.Instance.SetErrorMessage(errorType + " - " + errorMessage);
            }
        }
                           ));
    }
Exemple #3
0
        public async Task <string> CrossChainTransferToken(bool isFungible, KeyPair 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, 1, 9999)
                             .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, 1, 9999)
                             .CrossTransferNFT(Address.FromText(toChain.Address), symbol, keyPair.Address,
                                               destinationAddress, bigIntAmount)
                             .SpendGas(keyPair.Address)
                             .EndScript();

                var nexusName = "simnet";

                var tx = new Blockchain.Transaction(nexusName, chainName, script, DateTime.UtcNow + TimeSpan.FromHours(1));
                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);
            }
        }
Exemple #4
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("");
            }
        }