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 Cancel Tx RpcApi #837

Merged
merged 8 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,16 @@ public async Task<JObject> SendToAddressAsync(string assetId, string address, st
.ConfigureAwait(false);
}

/// <summary>
/// Cancel Tx.
/// </summary>
/// <returns>This function returns Signed Transaction JSON if successful, ContractParametersContext JSON if signing failed.</returns>
public async Task<JObject> CancelTransactionAsync(UInt256 txId, string[] signers, string extraFee)
{
JToken[] parameters = signers.Select(s => (JString)s.AsScriptHash()).ToArray();
return (JObject)await RpcSendAsync(GetRpcName(), txId.ToString(), new JArray(parameters), extraFee).ConfigureAwait(false);
}

#endregion Wallet

#region Plugins
Expand Down
51 changes: 51 additions & 0 deletions src/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,57 @@ protected virtual JToken SendToAddress(JArray _params)
return SignAndRelay(snapshot, tx);
}

[RpcMethod]
protected virtual JToken CancelTransaction(JArray _params)
{
CheckWallet();
var txid = UInt256.Parse(_params[0].AsString());
TransactionState state = NativeContract.Ledger.GetTransactionState(system.StoreView, txid);
if (state != null)
{
throw new RpcException(32700, "This tx is already confirmed, can't be cancelled.");
}

var conflict = new TransactionAttribute[] { new Conflicts() { Hash = txid } };
Signer[] signers = _params.Count >= 2 ? ((JArray)_params[1]).Select(j => new Signer() { Account = AddressToScriptHash(j.AsString(), system.Settings.AddressVersion), Scopes = WitnessScope.None }).ToArray() : Array.Empty<Signer>();
if (!signers.Any())
{
throw new RpcException(32701, "no signers");
}

Transaction tx = new Transaction
{
Signers = signers,
Attributes = conflict,
Witnesses = Array.Empty<Witness>(),
};

try
{
tx = wallet.MakeTransaction(system.StoreView, new[] { (byte)OpCode.RET }, signers[0].Account, signers, conflict);
}
catch (InvalidOperationException e)
{
throw new RpcException(-500, GetExceptionMessage(e));
}

if (system.MemPool.TryGetValue(txid, out Transaction conflictTx))
{
tx.NetworkFee = Math.Max(tx.NetworkFee, conflictTx.NetworkFee) + 1;
}
else if (_params.Count >= 3)
{
var extraFee = _params[2].AsString();
AssetDescriptor descriptor = new(system.StoreView, system.Settings, NativeContract.GAS.Hash);
if (!BigDecimal.TryParse(extraFee, descriptor.Decimals, out BigDecimal decimalExtraFee) || decimalExtraFee.Sign <= 0)
{
throw new RpcException(32702, "Incorrect Amount Format");
}
tx.NetworkFee += (long)decimalExtraFee.Value;
};
return SignAndRelay(system.StoreView, tx);
}

[RpcMethod]
protected virtual JToken InvokeContractVerify(JArray _params)
{
Expand Down