Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoCatarinoDev committed Jun 26, 2019
2 parents 2b11679 + b87c1e2 commit 3299d33
Show file tree
Hide file tree
Showing 38 changed files with 7,673 additions and 1,518 deletions.
57 changes: 29 additions & 28 deletions C#/Samples/WalletSample/WalletSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class Program
private static AccountDto _account;
private static KeyPair _key;
private static List<ChainDto> _chains;
private static List<TokenDto> _tokens;
private static IList<TokenDto> _tokens;

private static void Main(string[] args)
{
Expand Down Expand Up @@ -116,8 +116,8 @@ private static async Task RunConsole()

private static async Task ListTransactions()
{
var txs = await _phantasmaApiService.GetAddressTxs.SendRequestAsync(_key.Address.ToString(), 10);
foreach (var tx in txs.Txs)
var txs = await _phantasmaApiService.GetAddressTxs.SendRequestAsync(_key.Address.ToString(), 0, 10);
foreach (var tx in txs.AccountTransactionsDto.Txs)
{
Console.WriteLine(Utils.Helper.GetTxDescription(tx, _chains, _tokens));
}
Expand All @@ -142,7 +142,7 @@ private static async Task ShowBalance()
Console.WriteLine("********************");
Console.WriteLine($"Token: {balanceSheetDto.Symbol}");
Console.WriteLine($"Chain: {balanceSheetDto.ChainName}");
Console.WriteLine($"Amount: {TokenUtils.ToDecimal(BigInteger.Parse(balanceSheetDto.Amount), Helper.GetTokenDecimals(balanceSheetDto.Symbol, _tokens))}");
Console.WriteLine($"Amount: {UnitConversion.ToDecimal(BigInteger.Parse(balanceSheetDto.Amount), Helper.GetTokenDecimals(balanceSheetDto.Symbol, _tokens))}");
Console.WriteLine();
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ private static async Task CrossChainTransfer()//todo
var destinationChain = _chains[selectedChainOption - 1];


Console.WriteLine($"Enter amount: (max {TokenUtils.ToDecimal(BigInteger.Parse(token.Amount), Helper.GetTokenDecimals(token.Symbol, _tokens))}");
Console.WriteLine($"Enter amount: (max {UnitConversion.ToDecimal(BigInteger.Parse(token.Amount), Helper.GetTokenDecimals(token.Symbol, _tokens))}");
var amount = Console.ReadLine();

Console.WriteLine("Enter destination address: ");
Expand All @@ -202,33 +202,34 @@ private static async Task CrossChainTransfer()//todo
}
else
{
var listSteps = Helper.GetShortestPath(token.ChainName, destinationChain.Name, _chains);
if (listSteps.Count >= 2)
{
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++;
}
}
Console.WriteLine("TODO");
//var listSteps = Helper.GetShortestPath(token.ChainName, destinationChain.Name, _chains);
//if (listSteps.Count >= 2)
//{
// 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.GetConfirmations.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++;
// }
//}
}
}

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

var script = ScriptUtils.BeginScript()
.AllowGas(_key.Address, 1, 9999)
.AllowGas(_key.Address, Address.Null, 1, 9999)
.TransferTokens(tokenSymbol, _key.Address, destinationAddress, bigIntAmount)
.SpendGas(_key.Address)
.EndScript();
Expand All @@ -245,7 +246,7 @@ private static async Task<string> SettleBlock(string sourceChainAddress, string

var settleTxScript = ScriptUtils.BeginScript()
.CallContract("token", "SettleBlock", sourceChain, block)
.AllowGas(_key.Address, 1, 9999)
.AllowGas(_key.Address, Address.Null, 1, 9999)
.SpendGas(_key.Address)
.EndScript();
return await SignAndSendTx(settleTxScript, destinationChainName);
Expand All @@ -257,11 +258,11 @@ private static async Task<string> CrossChainTransferToken(string addressTo, stri
var toChain = _chains.Find(p => p.Name == destinationChain);
var destinationAddress = Address.FromText(addressTo);
int decimals = Helper.GetTokenDecimals(symbol, _tokens);
var bigIntAmount = TokenUtils.ToBigInteger(decimal.Parse(amount), decimals);
var fee = TokenUtils.ToBigInteger(0.0001m, 8);
var bigIntAmount = UnitConversion.ToBigInteger(decimal.Parse(amount), decimals);
var fee = UnitConversion.ToBigInteger(0.0001m, 8);

var script = ScriptUtils.BeginScript()
.AllowGas(_key.Address, 1, 9999)
.AllowGas(_key.Address, Address.Null, 1, 9999)
.CrossTransferToken(Address.FromText(toChain.Address), symbol, _key.Address,
_key.Address, fee)
.CrossTransferToken(Address.FromText(toChain.Address), symbol, _key.Address,
Expand All @@ -282,7 +283,7 @@ private static async Task RegisterName()
Console.WriteLine("Enter name for address: ");
var name = Console.ReadLine();
var script = ScriptUtils.BeginScript()
.AllowGas(_key.Address, 1, 9999)
.AllowGas(_key.Address, Address.Null, 1, 9999)
.CallContract("account", "Register", _key.Address, name)
.SpendGas(_key.Address)
.EndScript();
Expand Down
28 changes: 14 additions & 14 deletions C#/Samples/WalletSample/WalletSample/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace WalletSample.Utils
{
internal static class Helper
{
public static int GetTokenDecimals(string tokenSymbol, List<TokenDto> tokens)
public static int GetTokenDecimals(string tokenSymbol, IList<TokenDto> tokens)
{
return tokens.SingleOrDefault(p => p.Symbol.Equals(tokenSymbol)).Decimals;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public static List<ChainDto> GetShortestPath(string from, string to, List<ChainD
return SelectShortestPath(from, to, allpaths, phantasmaChains);
}

public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasmaChains, List<TokenDto> phantasmaTokens)
public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasmaChains, IList<TokenDto> phantasmaTokens)
{
string description = null;

Expand All @@ -108,18 +108,18 @@ public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasm
Event nativeEvent;
if (evt.Data != null)
{
nativeEvent = new Event((EventKind)evt.EvtKind,
nativeEvent = new Event((Phantasma.Blockchain.Contracts.EventKind)evt.EventKind,
Address.FromText(evt.EventAddress), evt.Data.Decode());
}
else
{
nativeEvent =
new Event((EventKind)evt.EvtKind, Address.FromText(evt.EventAddress));
new Event((Phantasma.Blockchain.Contracts.EventKind)evt.EventKind, Address.FromText(evt.EventAddress));
}

switch (evt.EvtKind)
switch (evt.EventKind)
{
case EvtKind.TokenSend:
case Phantasma.RpcClient.DTOs.EventKind.TokenSend:
{
var data = nativeEvent.GetContent<TokenEventData>();
amount = data.value;
Expand All @@ -128,7 +128,7 @@ public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasm
}
break;

case EvtKind.TokenReceive:
case Phantasma.RpcClient.DTOs.EventKind.TokenReceive:
{
var data = nativeEvent.GetContent<TokenEventData>();
amount = data.value;
Expand All @@ -138,11 +138,11 @@ public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasm
}
break;

case EvtKind.TokenEscrow:
case Phantasma.RpcClient.DTOs.EventKind.TokenEscrow:
{
var data = nativeEvent.GetContent<TokenEventData>();
amount = data.value;
var amountDecimal = TokenUtils.ToDecimal(amount,
var amountDecimal = UnitConversion.ToDecimal(amount,
phantasmaTokens.SingleOrDefault(p => p.Symbol == data.symbol).Decimals);
receiverAddress = nativeEvent.Address;
receiverChain = data.chainAddress;
Expand All @@ -151,21 +151,21 @@ public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasm
$"{amountDecimal} {data.symbol} tokens escrowed for address {receiverAddress} in {chain}";
}
break;
case EvtKind.AddressRegister:
case Phantasma.RpcClient.DTOs.EventKind.AddressRegister:
{
var name = nativeEvent.GetContent<string>();
description = $"{nativeEvent.Address} registered the name '{name}'";
}
break;

case EvtKind.FriendAdd:
case Phantasma.RpcClient.DTOs.EventKind.AddFriend:
{
var address = nativeEvent.GetContent<Address>();
description = $"{nativeEvent.Address} added '{address} to friends.'";
}
break;

case EvtKind.FriendRemove:
case Phantasma.RpcClient.DTOs.EventKind.RemoveFriend:
{
var address = nativeEvent.GetContent<Address>();
description = $"{nativeEvent.Address} removed '{address} from friends.'";
Expand All @@ -179,14 +179,14 @@ public static string GetTxDescription(TransactionDto tx, List<ChainDto> phantasm
if (amount > 0 && senderAddress != Address.Null && receiverAddress != Address.Null &&
senderToken != null && senderToken == receiverToken)
{
var amountDecimal = TokenUtils.ToDecimal(amount,
var amountDecimal = UnitConversion.ToDecimal(amount,
phantasmaTokens.SingleOrDefault(p => p.Symbol == senderToken).Decimals);
description =
$"{amountDecimal} {senderToken} sent from {senderAddress.Text} to {receiverAddress.Text}";
}
else if (amount > 0 && receiverAddress != Address.Null && receiverToken != null)
{
var amountDecimal = TokenUtils.ToDecimal(amount,
var amountDecimal = UnitConversion.ToDecimal(amount,
phantasmaTokens.SingleOrDefault(p => p.Symbol == receiverToken).Decimals);
description = $"{amountDecimal} {receiverToken} received on {receiverAddress.Text} ";
}
Expand Down
Loading

0 comments on commit 3299d33

Please sign in to comment.