Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Modify claim operation to 50 claims per tx (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
txhsl authored and erikzhang committed Oct 11, 2018
1 parent 13773f7 commit 789d83d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
64 changes: 61 additions & 3 deletions neo-cli/Shell/Coins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Neo.SmartContract;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neo.Shell
Expand All @@ -14,6 +15,7 @@ public class Coins
{
private Wallet current_wallet;
private NeoSystem system;
public static int MAX_CLAIMS_AMOUNT = 50;

public Coins(Wallet wallet, NeoSystem system)
{
Expand Down Expand Up @@ -50,7 +52,6 @@ public Fixed8 AvailableBonus()
}
}


public ClaimTransaction Claim()
{

Expand All @@ -67,15 +68,15 @@ public ClaimTransaction Claim()
{
ClaimTransaction tx = new ClaimTransaction
{
Claims = claims,
Claims = claims.Take(MAX_CLAIMS_AMOUNT).ToArray(),
Attributes = new TransactionAttribute[0],
Inputs = new CoinReference[0],
Outputs = new[]
{
new TransactionOutput
{
AssetId = Blockchain.UtilityToken.Hash,
Value = snapshot.CalculateBonus(claims),
Value = snapshot.CalculateBonus(claims.Take(MAX_CLAIMS_AMOUNT)),
ScriptHash = current_wallet.GetChangeAddress()
}
}
Expand All @@ -87,6 +88,63 @@ public ClaimTransaction Claim()
}


public ClaimTransaction[] ClaimAll()
{

if (this.AvailableBonus() == Fixed8.Zero)
{
Console.WriteLine($"no gas to claim");
return null;
}

CoinReference[] claims = current_wallet.GetUnclaimedCoins().Select(p => p.Reference).ToArray();
if (claims.Length == 0) return null;

using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
{
int claim_count = (claims.Length - 1) / MAX_CLAIMS_AMOUNT + 1;
List<ClaimTransaction> txs = new List<ClaimTransaction>();
if (claim_count > 1)
{
Console.WriteLine($"total claims: {claims.Length}, processing(0/{claim_count})...");
}
for (int i = 0; i < claim_count; i++)
{
if (i > 0)
{
Console.WriteLine($"{i * MAX_CLAIMS_AMOUNT} claims processed({i}/{claim_count})...");
}
ClaimTransaction tx = new ClaimTransaction
{
Claims = claims.Skip(i * MAX_CLAIMS_AMOUNT).Take(MAX_CLAIMS_AMOUNT).ToArray(),
Attributes = new TransactionAttribute[0],
Inputs = new CoinReference[0],
Outputs = new[]
{
new TransactionOutput
{
AssetId = Blockchain.UtilityToken.Hash,
Value = snapshot.CalculateBonus(claims.Skip(i * MAX_CLAIMS_AMOUNT).Take(MAX_CLAIMS_AMOUNT)),
ScriptHash = current_wallet.GetChangeAddress()
}
}
};

if ((tx = (ClaimTransaction)SignTransaction(tx)) != null)
{
txs.Add(tx);
}
else
{
break;
}
}

return txs.ToArray();
}
}


private Transaction SignTransaction(Transaction tx)
{
if (tx == null)
Expand Down
31 changes: 26 additions & 5 deletions neo-cli/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ private bool OnHelpCommand(string[] args)
"\tlist key\n" +
"\tshow utxo [id|alias]\n" +
"\tshow gas\n" +
"\tclaim gas\n" +
"\tclaim gas [all]\n" +
"\tcreate address [n=1]\n" +
"\timport key <wif|path>\n" +
"\texport key [address] [path]\n" +
Expand Down Expand Up @@ -506,12 +506,33 @@ private bool OnClaimCommand(string[] args)
switch (args[1].ToLower())
{
case "gas":
ClaimTransaction tx = coins.Claim();
if (tx != null)
if (args.Length > 2)
{
Console.WriteLine($"Tranaction Suceeded: {tx.Hash}");
switch (args[2].ToLower())
{
case "all":
ClaimTransaction[] txs = coins.ClaimAll();
if (txs.Length > 0)
{
foreach (ClaimTransaction tx in txs)
{
Console.WriteLine($"Tranaction Suceeded: {tx.Hash}");
}
}
return true;
default:
return base.OnCommand(args);
}
}
else
{
ClaimTransaction tx = coins.Claim();
if (tx != null)
{
Console.WriteLine($"Tranaction Suceeded: {tx.Hash}");
}
return true;
}
return true;
default:
return base.OnCommand(args);
}
Expand Down

0 comments on commit 789d83d

Please sign in to comment.