Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Max_Fee as the optional for gas limit. #74

Merged
merged 7 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
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
7 changes: 7 additions & 0 deletions RpcWallet/RpcWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
<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" />
Expand Down
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);
}
}
}