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

node: Attach during invoke #554

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Neo.ConsoleService/ConsoleServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ protected ConsoleServiceBase()
}
else
{
return CommandToken.ReadString(args, false).Split(',', ' ');
return CommandToken.ReadString(args, false).Split(',', ' ', StringSplitOptions.RemoveEmptyEntries);
}
});

Expand Down
84 changes: 83 additions & 1 deletion neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,47 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Neo.CLI
{
partial class MainService
{
private class AttachAsset
{
public AssetDescriptor Asset;
public BigDecimal Amount;

/// <summary>
/// Parse from string
/// Format: neo=1 gas=2.456 0x0..=123.5
/// </summary>
/// <param name="input">Input</param>
/// <returns></returns>
internal static AttachAsset Parse(string input)
{
var split = input.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (split.Length != 2) throw new FormatException("Format expected: neo=1");

var ret = new AttachAsset
{
Asset = (split[0].ToLowerInvariant()) switch
{
"neo" => new AssetDescriptor(NativeContract.NEO.Hash),
"gas" => new AssetDescriptor(NativeContract.GAS.Hash),
_ => new AssetDescriptor(UInt160.Parse(split[0])),
}
};

ret.Amount = BigDecimal.Parse(split[1], ret.Asset.Decimals);
return ret;
}
}

/// <summary>
/// Process "deploy" command
/// </summary>
Expand Down Expand Up @@ -48,8 +81,9 @@ private void OnDeployCommand(string filePath, string manifestPath = null)
/// <param name="operation">Operation</param>
/// <param name="contractParameters">Contract parameters</param>
/// <param name="witnessAddress">Witness address</param>
/// <param name="attach">Attach</param>
[ConsoleCommand("invoke", Category = "Contract Commands")]
private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contractParameters = null, UInt160[] witnessAddress = null)
private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contractParameters = null, UInt160[] witnessAddress = null, AttachAsset[] attach = null)
{
List<ContractParameter> parameters = new List<ContractParameter>();
List<Cosigner> signCollection = new List<Cosigner>();
Expand Down Expand Up @@ -95,6 +129,54 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra

using (ScriptBuilder scriptBuilder = new ScriptBuilder())
{
if (attach != null)
{
if (NoWallet()) return;

foreach (var entry in attach.GroupBy(u => u.Asset.AssetId))
{
// Get amount

var amount = BigInteger.Zero;
foreach (var am in entry) amount += am.Amount.Value;

// Find a valid sender

var tempTx = CurrentWallet.MakeTransaction(new[]
{
new TransferOutput
{
AssetId = entry.Key,
Value = new BigDecimal(amount, entry.First().Asset.Decimals),
ScriptHash = scriptHash
}
});

if (tempTx == null)
{
Console.WriteLine("Insufficient funds of: " + entry.First().Asset.AssetName);
return;
}

// Append at the begining the right script

scriptBuilder.EmitAppCall(entry.Key, "transfer", tempTx.Sender, scriptHash, amount);

// Compute new cosigners

signCollection.Add(new Cosigner()
{
Account = tempTx.Sender,
AllowedContracts = new UInt160[] { entry.Key },
Scopes = WitnessScope.CustomContracts
});
}

// Add new cosigners

tx.Cosigners = signCollection.ToArray();
}

scriptBuilder.EmitAppCall(scriptHash, operation, parameters.ToArray());
tx.Script = scriptBuilder.ToArray();
Console.WriteLine($"Invoking script with: '{tx.Script.ToHexString()}'");
Expand Down
1 change: 1 addition & 0 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public MainService() : base()
RegisterCommandHander<string[], ECPoint[]>((str) => str.Select(u => ECPoint.Parse(u.Trim(), ECCurve.Secp256r1)).ToArray());
RegisterCommandHander<string, JObject>((str) => JObject.Parse(str));
RegisterCommandHander<JObject, JArray>((obj) => (JArray)obj);
RegisterCommandHander<string[], AttachAsset[]>((str) => str.Select(u => AttachAsset.Parse(u)).ToArray());

RegisterCommand(this);

Expand Down