Skip to content

Commit

Permalink
change json format to use arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenwang1996 committed Jul 10, 2019
1 parent 17951dc commit e994817
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions text/0000-contract-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ For developers, there will be two main changes:
- Instead of annotating view and change methods in the `initContract` function in `main.js`,
they will instead annotate the methods of a contract by decorators.
More specifically, every method is by default a change method, unless annotated by `@view_method`.
- Every contract will have a `metadata` method that returns a json that serializes the contract methods. For each method,
the json serialization is of the form `{"parameters": {<param1>: <type1>, .. }, "returnType": <return_type>}`.
The overall serialization is of the form `{"viewMethods": {<method_name>: <method_metadata>, .. }, "changeMethods": {<method_name>: <method_metadata>, .. }}`.
- Every contract will have a `metadata` method that returns a json that serializes the contract methods in an array.
For each method, the json serialization is of the form `{"name": <name>, "parameters": [{"name": <param_name1>, "type": <param_type1>, ..}, .. ], "returnType": <return_type>}`.

As an concrete example, suppose we have a contract that maintains a counter on chain:

Expand Down Expand Up @@ -57,31 +56,31 @@ export function getCounter(): i32 {
This contract has two change methods, `incrementCounter` and `decrementCounter`, as well as one view method, `getCounter`.
In this case, the metadata we want looks like
```json
{
"viewMethods":
[
{
"getCounter": {
"parameters": {"amount": "i32"},
"returnType": "i32"
}
"name": "getCounter",
"parameters": [{"name": "amount", "type": "i32"}],
"returnType": "i32",
"methodType": "view"
},
"changeMethods":
{
"incrementCounterBy": {
"parameters": {"amount": "i32"},
"returnType": "void"
},
"decrementCounterBy": {
"parameters": {},
"returnType": "void"
}
"name": "incrementCounterBy",
"parameters": [{"name": "amount", "type": "i32"}],
"returnType": "i32",
"methodType": "change"
},
{
"name": "decrementCounterBy",
"parameters": [{"name": "amount", "type": "i32"}],
"returnType": "void",
"methodType": "change"
}
}
]
```
and the generated `metadata` method looks like:
```typescript
export function metadata(): string {
return "{\"viewMethods\": {\"getCounter\": {\"parameters\": {}, \"returnType\": \"i32\"},\"changeMethods\": {\"incrementCounterBy\": {\"parameters\": {\"amount\": \"i32\"}, \"returnType\": \"void\"}}, {\"decrementCounterBy\": {\"parameters\": {\"amount\": \"i32\"}, \"returnType\": \"void\"}}}"
return '[{"name": "getCounter", "parameters": [{"name": "amount", "type": "i32"}], "returnType": "i32", "methodType": "view"}, {"name": "incrementCounterBy", "parameters": [{"name": "amount", "type": "i32"}], "returnType": "i32", "methodType": "change"}, {"name": "decrementCounterBy", "parameters": [{"name": "amount", "type": "i32"}], "returnType": "void", "returnType": "void"}]'
}
```

Expand Down

2 comments on commit e994817

@DanielRX
Copy link

Choose a reason for hiding this comment

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

If swapping to arrays was better than an object,
(I disagree with the methods being an array since metadata.viewMethods.getCounter looks a look better than metadata.filter((x) => x.methodType === 'view' && x.name === 'getCounter')[0])
Shouldn't you also swap the function data to an array of [name, methodType, params, returns]?
The other is see with this is now you have no grouping of view and none-view, instead you have to filter on each time (or the consumer has to filter into two arrays, which could have been done by the contract).

@bowenwang1996
Copy link
Collaborator Author

@bowenwang1996 bowenwang1996 commented on e994817 Jul 13, 2019

Choose a reason for hiding this comment

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

Using an array allows for more generality, especially for statically typed languages. Also another reason for this is that our json serializer current doesn't handle nested objects. For function data, since the format is fixed, it's fine to have an object. Filtering into two arrays is fine since we do not expect a contract to have a lot of methods.

Please sign in to comment.