Skip to content

Commit

Permalink
fix MakeTransaction (neo-project#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Nov 11, 2018
1 parent bbb0c72 commit cc0f315
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Security.Cryptography.X509Certificates;
using System.Text;
using ECPoint = Neo.Cryptography.ECC.ECPoint;
using VMArray = Neo.VM.Types.Array;

namespace Neo.Wallets
{
Expand Down Expand Up @@ -116,7 +115,7 @@ public BigDecimal GetAvailable(UIntBase asset_id)
if (engine.State.HasFlag(VMState.FAULT))
return new BigDecimal(0, 0);
byte decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
BigInteger amount = ((VMArray)engine.ResultStack.Pop()).Aggregate(BigInteger.Zero, (x, y) => x + y.GetBigInteger());
BigInteger amount = engine.ResultStack.Pop().GetBigInteger();
return new BigDecimal(amount, decimals);
}
else
Expand Down Expand Up @@ -306,41 +305,36 @@ public virtual WalletAccount Import(string nep2, string passphrase)
{
foreach (var output in cOutputs)
{
byte[] script;
using (ScriptBuilder sb2 = new ScriptBuilder())
var balances = new List<(UInt160 Account, BigInteger Value)>();
foreach (UInt160 account in accounts)
{
sb2.EmitPush(0);
foreach (UInt160 account in accounts)
byte[] script;
using (ScriptBuilder sb2 = new ScriptBuilder())
{
sb2.EmitAppCall(output.AssetId, "balanceOf", account);
sb2.Emit(OpCode.ADD);
script = sb2.ToArray();
}
script = sb2.ToArray();
ApplicationEngine engine = ApplicationEngine.Run(script);
if (engine.State.HasFlag(VMState.FAULT)) return null;
balances.Add((account, engine.ResultStack.Pop().GetBigInteger()));
}
ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: Fixed8.FromDecimal(0.2m) * accounts.Length);
if (engine.State.HasFlag(VMState.FAULT)) return null;
var balances = ((IEnumerable<StackItem>)(VMArray)engine.ResultStack.Pop()).Reverse().Zip(accounts, (i, a) => new
{
Account = a,
Value = i.GetBigInteger()
}).ToArray();
BigInteger sum = balances.Aggregate(BigInteger.Zero, (x, y) => x + y.Value);
if (sum < output.Value) return null;
if (sum != output.Value)
{
balances = balances.OrderByDescending(p => p.Value).ToArray();
balances = balances.OrderByDescending(p => p.Value).ToList();
BigInteger amount = output.Value;
int i = 0;
while (balances[i].Value <= amount)
amount -= balances[i++].Value;
if (amount == BigInteger.Zero)
balances = balances.Take(i).ToArray();
balances = balances.Take(i).ToList();
else
balances = balances.Take(i).Concat(new[] { balances.Last(p => p.Value >= amount) }).ToArray();
balances = balances.Take(i).Concat(new[] { balances.Last(p => p.Value >= amount) }).ToList();
sum = balances.Aggregate(BigInteger.Zero, (x, y) => x + y.Value);
}
sAttributes.UnionWith(balances.Select(p => p.Account));
for (int i = 0; i < balances.Length; i++)
for (int i = 0; i < balances.Count; i++)
{
BigInteger value = balances[i].Value;
if (i == 0)
Expand Down

0 comments on commit cc0f315

Please sign in to comment.