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 metadata information to generated .abi.json file (master-2.x) #148

Merged
merged 2 commits into from
Dec 6, 2019
Merged

Add metadata information to generated .abi.json file (master-2.x) #148

merged 2 commits into from
Dec 6, 2019

Conversation

devhawk
Copy link
Contributor

@devhawk devhawk commented Dec 5, 2019

Fixes #144

This PR adds a new "metadata" property to the .abi.json file generated by NEON (2.x branch). It contains the metadata needed by Neo.Contract.Create to deploy contracts that can't be obtained from the AVM or existing .abi.json file.

Having this information included in the abi.json file enables tools like neo-express and neo-visual-devtracker to deploy contracts w/o collecting this information from the developer at contract deployment time. Additionally, by specifying these values in source (as per below), we also reduce the potential for errors at contract deployment time.

The contract metadata can be included in the contract C# source code via a set of new custom attributes that this PR adds to the Neo.SmartContract.Framework package.

using Neo.SmartContract.Framework;

[assembly: ContractTitle("MyTestContract")]
[assembly: ContractDescription("This is the description of my contract")]
[assembly: ContractVersion("2.0")]
[assembly: ContractAuthor("DevHawk")]
[assembly: ContractEmail("devhawk@outlook.com")]
[assembly: ContractHasStorage]

// can also specify a contract can be dynamically invoked or is payable
// [assembly: ContractHasDynamicInvoke]
// [assembly: ContractIsPayable]

The resulting metadata node looks in the contract.abi.json file looks like this:

"metadata":
{
    "title":"MyTestContract",
    "description":"This is a description of my contract",
    "version":"2.0",
    "author":"DevHawk",
    "email":"devhawk@outlook.com",
    "has-storage":true,
    "has-dynamic-invoke":false,
    "is-payable":false
},

NEON uses the following defaults if metadata is not specified via custom attributes:

  • Contract title defaults to the AssemblyDef.Name.Name field defined in metadata.
  • Contract version defaults to the AssemblyDef.Name.Version field defined in metadata
  • Remaining string properties default to empty string
  • Boolean properties default to false

The .NET library already has AssemblyTitleAttribute and AssemblyDescriptionAttribute custom attributes. If these attributes are specified, NEON will use them for contract title and description respectively. However, if both standard and Neo contract attributes are used to specify the same metadata, NEON will use the value from the Neo contract attribute.

Copy link
Contributor

@lock9 lock9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are fine and you add UT.
Let's wait for the testing team before approving it.

@devhawk
Copy link
Contributor Author

devhawk commented Dec 5, 2019

@superboyiii Can you take a quick look at this PR from a testing perspective?

Copy link
Member

@lightszero lightszero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

@lightszero lightszero merged commit 6f8cef1 into neo-project:master-2.x Dec 6, 2019
@superboyiii
Copy link
Member

@superboyiii Can you take a quick look at this PR from a testing perspective?

Sure, @nicolegys will give a quick response of test result.

@nicolegys
Copy link

Test:

I added the attributes in my contract and the metadata infomation could be seen in the generated .abi.json file.

Contract code:
image

.abi.json file:
image

Now I could not make further test, because this part in neo-cli hasn't been changed.

In addtion, could you also add paramTypes and returnTypeHexString? Or they can be calculated automaticly from the smart contract code.

@devhawk
Copy link
Contributor Author

devhawk commented Dec 6, 2019

Thanks for verifying my PR @nicolegys!

I didn't include the hex string for parameter/return types because it's trivial to parse that information from existing data in the in the .abi.json file.

@devhawk devhawk deleted the devhawk/metadata branch December 6, 2019 06:23
@@ -80,6 +81,18 @@ public static MyJson.JsonNode_Object Export(NeoModule module, byte[] script)
}
outjson.SetDictValue("hash", sb.ToString());

//metadata
var metadataJson = new MyJson.JsonNode_Object();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happend if there the value it's null? maybe we should avoid the entry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metadata properties being inserted into .abi.json file here all have default values if not specified via custom attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants