Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Relfos committed Dec 29, 2018
2 parents 7ff2c28 + 88b3ead commit 2cd6849
Showing 1 changed file with 41 additions and 83 deletions.
124 changes: 41 additions & 83 deletions C#/WalletSample/WalletSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ private static async Task RunConsole()
Console.WriteLine("1 - show address");
Console.WriteLine("2 - show balance and account name");
Console.WriteLine("3 - register name");
Console.WriteLine("4 - send SOUL");
//Console.WriteLine("5 - send tokens cross chain");
Console.WriteLine("6 - list last 10 transactions");
Console.WriteLine("7 - logout");
Console.WriteLine("4 - send tokens");
Console.WriteLine("5 - list last 10 transactions");
Console.WriteLine("6 - logout");
var option = Console.ReadLine();
Console.WriteLine();
switch (option)
Expand All @@ -99,15 +98,12 @@ private static async Task RunConsole()
await RegisterName();
break;
case "4":
await SendTokens();
await CrossChainTransfer();
break;
case "5":
//await CrossChainTransfer();
break;
case "6":
await ListTransactions();
break;
case "7":
case "6":
logout = true;
break;
}
Expand Down Expand Up @@ -150,58 +146,15 @@ private static async Task ShowBalance()
}
}

private static async Task SendTokens()
{
string addressTo = null;
if (_account == null) _account = await _phantasmaApiService.GetAccount.SendRequestAsync(_key.Address.ToString());

if (!HaveSoulToTransfer())
{
Console.WriteLine("No SOUL to tranfer");
return;
}

Console.WriteLine($"Enter destination address: ");
addressTo = Console.ReadLine();
if (!Address.IsValidAddress(addressTo))
{
Console.WriteLine("Incorrect address");
}
else
{
var destinationAddress = Address.FromText(addressTo);
var token = "SOUL";
Console.WriteLine("Enter amount: ");

var amount = Console.ReadLine();
var amountDecimal = decimal.Parse(amount);
if (!HaveSoulToTransfer(amountDecimal))
{
Console.WriteLine("Insuficient funds");
return;
}

var bigIntAmount = TokenUtils.ToBigInteger(amountDecimal, 8);

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

await SignAndSendTx(script, "main");
}
}

private static bool HaveSoulToTransfer(decimal amount = 0)
private static bool HaveTokenBalanceToTransfer(decimal amount = 0)
{
var test = _account.Tokens.Where(p => decimal.Parse(p.Amount) > amount && p.Symbol == "SOUL");
var test = _account.Tokens.Where(p => decimal.Parse(p.Amount) > amount);
return test.Any();
}

private static async Task CrossChainTransfer()//todo
{
if (!HaveSoulToTransfer())
if (!HaveTokenBalanceToTransfer())
{
Console.WriteLine("No tokens to tranfer");
return;
Expand All @@ -225,7 +178,7 @@ private static async Task CrossChainTransfer()//todo
}

var selectedChainOption = int.Parse(Console.ReadLine());
var chain = _chains[selectedChainOption - 1];
var destinationChain = _chains[selectedChainOption - 1];


Console.WriteLine($"Enter amount: (max {TokenUtils.ToDecimal(BigInteger.Parse(token.Amount), Helper.GetTokenDecimals(token.Symbol, _tokens.Tokens))}");
Expand All @@ -241,39 +194,44 @@ private static async Task CrossChainTransfer()//todo
}

int cont = 1;
if (token.ChainName == chain.Name)
if (token.ChainName == destinationChain.Name)
{

await SameChainTransfer(destinationAddress, amount, token.Symbol, destinationChain.Name);
}

var listSteps = Helper.GetShortestPath(token.ChainName, chain.Name, _chains);
if (listSteps.Count > 2)
else
{
while (listSteps.Count > 2)
var listSteps = Helper.GetShortestPath(token.ChainName, destinationChain.Name, _chains);
if (listSteps.Count >= 2)
{
Console.WriteLine($"Sending {cont} transaction of {listSteps.Count / 2}");
var txHash = await CrossChainTransferToken(destinationAddress, listSteps[0].Name, listSteps[1].Name, token.Symbol,
amount);
var confirmationDto = await _phantasmaApiService.GetTxConfirmations.SendRequestAsync(txHash);
while (!confirmationDto.IsConfirmed) await Task.Delay(100);
Console.WriteLine($"Settling block...");
var settleTx = await SettleBlock(listSteps[0].Address, confirmationDto.Hash, listSteps[1].Address);
listSteps.RemoveAt(0);
cont++;
while (listSteps.Count >= 2)
{
Console.WriteLine($"Sending {cont} transaction of {listSteps.Count}");
var txHash = await CrossChainTransferToken(destinationAddress, listSteps[0].Name, listSteps[1].Name, token.Symbol,
amount);
var confirmationDto = await _phantasmaApiService.GetTxConfirmations.SendRequestAsync(txHash);
while (!confirmationDto.IsConfirmed) await Task.Delay(100);
Console.WriteLine($"Settling block...");
var settleTx = await SettleBlock(listSteps[0].Address, confirmationDto.Hash, listSteps[1].Address);
listSteps.RemoveAt(0);
cont++;
}
}
}
else
{
var amountDecimal = decimal.Parse(amount);
var bigIntAmount = TokenUtils.ToBigInteger(amountDecimal, 8);
var script = ScriptUtils.BeginScript()
.AllowGas(_key.Address, 1, 9999)
.TransferTokens(token.Symbol, _key.Address, Address.FromText(destinationAddress), bigIntAmount)
.SpendGas(_key.Address)
.EndScript();
}

await SignAndSendTx(script, chain.Name);
}
private static async Task SameChainTransfer(string addressTo, string amount, string tokenSymbol, string chain)
{
var destinationAddress = Address.FromText(addressTo);
int decimals = Helper.GetTokenDecimals(tokenSymbol, _tokens.Tokens);
var bigIntAmount = TokenUtils.ToBigInteger(decimal.Parse(amount), decimals);

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

await SignAndSendTx(script, chain);
}

private static async Task<string> SettleBlock(string sourceChainAddress, string blockHash, string destinationChainAddress)
Expand Down Expand Up @@ -314,7 +272,7 @@ private static async Task<string> CrossChainTransferToken(string addressTo, stri

private static async Task RegisterName()
{
if (!HaveSoulToTransfer())
if (!HaveTokenBalanceToTransfer())
{
Console.WriteLine("Insuficient funds");
return;
Expand Down

0 comments on commit 2cd6849

Please sign in to comment.