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

Add data parameter when deploy and update contract #837

Merged
merged 6 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 10 additions & 27 deletions neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2016-2021 The Neo Project.
//
// The neo-cli is free software distributed under the MIT software
//
// The neo-cli is free software distributed under the MIT software
// license, see the accompanying file LICENSE in the main directory of
// the project or http://www.opensource.org/licenses/mit-license.php
// the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand All @@ -27,21 +27,12 @@ partial class MainService
/// <param name="filePath">File path</param>
/// <param name="manifestPath">Manifest path</param>
[ConsoleCommand("deploy", Category = "Contract Commands")]
private void OnDeployCommand(string filePath, string manifestPath = null)
private void OnDeployCommand(string filePath, string manifestPath = null, JObject data = null)
{
if (NoWallet()) return;
byte[] script = LoadDeploymentScript(filePath, manifestPath, out var nef, out var manifest);
byte[] script = LoadDeploymentScript(filePath, manifestPath, data, out var nef, out var manifest);

Transaction tx;
try
{
tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script);
}
catch (InvalidOperationException e)
{
Console.WriteLine("Error: " + GetExceptionMessage(e));
return;
}
Transaction tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script);

UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name);

Expand All @@ -62,7 +53,7 @@ private void OnDeployCommand(string filePath, string manifestPath = null)
/// <param name="filePath">File path</param>
/// <param name="manifestPath">Manifest path</param>
[ConsoleCommand("update", Category = "Contract Commands")]
private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifestPath, UInt160 sender, UInt160[] signerAccounts = null)
private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifestPath, UInt160 sender, UInt160[] signerAccounts = null, JObject data = null)
{
Signer[] signers = Array.Empty<Signer>();

Expand Down Expand Up @@ -91,16 +82,8 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes
Witnesses = Array.Empty<Witness>()
};

try
{
byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, out var nef, out var manifest);
tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers);
}
catch (InvalidOperationException e)
{
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}
byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest);
tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers);

ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
if (contract == null)
Expand Down
30 changes: 22 additions & 8 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2016-2021 The Neo Project.
//
// The neo-cli is free software distributed under the MIT software
//
// The neo-cli is free software distributed under the MIT software
// license, see the accompanying file LICENSE in the main directory of
// the project or http://www.opensource.org/licenses/mit-license.php
// the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -229,7 +229,7 @@ private bool NoWallet()
return true;
}

private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, out NefFile nef, out ContractManifest manifest)
private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, JObject data, out NefFile nef, out ContractManifest manifest)
{
if (string.IsNullOrEmpty(manifestFilePath))
{
Expand Down Expand Up @@ -259,6 +259,10 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath,
nef = stream.ReadSerializable<NefFile>();
}

ContractParameter dataParameter = null;
if (data is not null)
dataParameter = ContractParameter.FromJson(data);

// Basic script checks

Script script = new Script(nef.Script);
Expand All @@ -278,12 +282,15 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath,

using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString());
if (dataParameter is not null)
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString(), dataParameter);
else
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString());
return sb.ToArray();
}
}

private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string manifestFilePath, out NefFile nef, out ContractManifest manifest)
private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string manifestFilePath, JObject data, out NefFile nef, out ContractManifest manifest)
{
if (string.IsNullOrEmpty(manifestFilePath))
{
Expand Down Expand Up @@ -313,6 +320,10 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m
nef = stream.ReadSerializable<NefFile>();
}

ContractParameter dataParameter = null;
if (data is not null)
dataParameter = ContractParameter.FromJson(data);

// Basic script checks

Script script = new Script(nef.Script);
Expand All @@ -332,7 +343,10 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m

using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitDynamicCall(scriptHash, "update", nef.ToArray(), manifest.ToJson().ToString());
if (dataParameter is null)
sb.EmitDynamicCall(scriptHash, "update", nef.ToArray(), manifest.ToJson().ToString());
else
sb.EmitDynamicCall(scriptHash, "update", nef.ToArray(), manifest.ToJson().ToString(), dataParameter);
return sb.ToArray();
}
}
Expand Down