Skip to content

Commit

Permalink
Make calculation for extra gas on "send*" RPC (#70)
Browse files Browse the repository at this point in the history
* Make calculation for extra gas on "send*" RPC

* Fix for the calculation when little overstep
  • Loading branch information
superboyiii committed Mar 29, 2019
1 parent c611239 commit 0ce4311
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
46 changes: 45 additions & 1 deletion RpcWallet/RpcWallet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Akka.Actor;
using Akka.Actor;
using Microsoft.AspNetCore.Http;
using Neo.IO;
using Neo.IO.Json;
Expand All @@ -21,6 +21,7 @@ public class RpcWallet : Plugin, IRpcPlugin

public override void Configure()
{
Settings.Load(GetConfiguration());
}

public void PreProcess(HttpContext context, string method, JArray _params)
Expand Down Expand Up @@ -307,8 +308,25 @@ private JObject SendFrom(UIntBase assetId, UInt160 from, UInt160 to, string valu
ScriptHash = to
}
}, from: from, change_address: change_address, fee: fee);
if (tx.Size > 1024)
{
fee += Fixed8.FromDecimal(tx.Size * 0.00001m + 0.001m);
tx = Wallet.MakeTransaction(null, new[]
{
new TransferOutput
{
AssetId = assetId,
Value = amount,
ScriptHash = to
}
}, from: from, change_address: change_address, fee: fee);
}
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
if (fee > Settings.Default.MaxFee)
{
throw new RpcException(-100, "The necessary fee is more than the Max_fee, this transaction is failed. Please increase your Max_fee value.");
}
return SignAndRelay(tx);
}

Expand All @@ -334,8 +352,17 @@ private JObject SendMany(UInt160 from, JArray to, Fixed8 fee, UInt160 change_add
if (fee < Fixed8.Zero)
throw new RpcException(-32602, "Invalid params");
Transaction tx = Wallet.MakeTransaction(null, outputs, from: from, change_address: change_address, fee: fee);
if (tx.Size > 1024)
{
fee += Fixed8.FromDecimal(tx.Size * 0.00001m + 0.001m);
tx = Wallet.MakeTransaction(null, outputs, from: from, change_address: change_address, fee: fee);
}
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
if (fee > Settings.Default.MaxFee)
{
throw new RpcException(-100, "The necessary fee is more than the Max_fee, this transaction is failed. Please increase your Max_fee value.");
}
return SignAndRelay(tx);
}

Expand All @@ -357,8 +384,25 @@ private JObject SendToAddress(UIntBase assetId, UInt160 scriptHash, string value
ScriptHash = scriptHash
}
}, change_address: change_address, fee: fee);
if (tx.Size > 1024)
{
fee += Fixed8.FromDecimal(tx.Size * 0.00001m + 0.001m);
tx = Wallet.MakeTransaction(null, new[]
{
new TransferOutput
{
AssetId = assetId,
Value = amount,
ScriptHash = scriptHash
}
}, change_address: change_address, fee: fee);
}
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
if (fee > Settings.Default.MaxFee)
{
throw new RpcException(-100, "The necessary fee is more than the Max_fee, this transaction is failed. Please increase your Max_fee value.");
}
return SignAndRelay(tx);
}
}
Expand Down
11 changes: 11 additions & 0 deletions RpcWallet/RpcWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Neo.Plugins</RootNamespace>
</PropertyGroup>

<ItemGroup>
<None Update="RpcWallet\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="2.10.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\neo\neo\neo.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions RpcWallet/RpcWallet/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"PluginConfiguration": {
"MaxFee": 0.1
}
}
29 changes: 29 additions & 0 deletions RpcWallet/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;

namespace Neo.Plugins
{
internal class Settings
{
public Fixed8 MaxFee { get; }

public static Settings Default { get; private set; }

private Settings(IConfigurationSection section)
{
this.MaxFee = GetValueOrDefault(section.GetSection("MaxFee"), Fixed8.FromDecimal(0.1M), p => Fixed8.Parse(p));
}

public T GetValueOrDefault<T>(IConfigurationSection section, T defaultValue, Func<string, T> selector)
{
if (section.Value == null) return defaultValue;
return selector(section.Value);
}

public static void Load(IConfigurationSection section)
{
Default = new Settings(section);
}
}
}

0 comments on commit 0ce4311

Please sign in to comment.