@graphprotocol/graph-cli 0.37.2-alpha-20221219142030-0cf2a37 → 0.37.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -3
- package/package.json +2 -1
- package/src/codegen/schema.js +58 -49
- package/src/codegen/schema.test.js +27 -32
- package/src/codegen/template.js +2 -1
- package/src/codegen/types/conversions.js +14 -43
- package/src/codegen/types/index.js +23 -20
- package/src/codegen/typescript.js +3 -0
- package/src/command-helpers/abi.js +3 -2
- package/src/command-helpers/data-sources.js +4 -3
- package/src/command-helpers/scaffold.js +14 -18
- package/src/command-helpers/spinner.js +1 -0
- package/src/commands/add.js +25 -42
- package/src/compiler/index.js +13 -12
- package/src/protocols/arweave/subgraph.js +7 -2
- package/src/protocols/cosmos/subgraph.js +9 -2
- package/src/protocols/ethereum/abi.js +7 -6
- package/src/protocols/ethereum/codegen/abi.js +223 -217
- package/src/protocols/ethereum/codegen/abi.test.js +16 -15
- package/src/protocols/ethereum/subgraph.js +67 -55
- package/src/protocols/ethereum/type-generator.js +25 -16
- package/src/protocols/index.js +6 -5
- package/src/protocols/near/subgraph.js +5 -3
- package/src/protocols/substreams/subgraph.js +4 -2
- package/src/scaffold/cosmos.test.js +1 -0
- package/src/scaffold/ethereum.test.js +3 -2
- package/src/scaffold/index.js +1 -1
- package/src/scaffold/near.test.js +1 -0
- package/src/schema.js +2 -1
- package/src/subgraph.js +53 -44
- package/src/type-generator.js +7 -6
- package/src/validation/contract.js +11 -6
- package/src/validation/manifest.js +84 -77
- package/src/validation/schema.js +294 -284
package/CHANGELOG.md
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
# @graphprotocol/graph-cli
|
|
2
2
|
|
|
3
|
-
## 0.37.2
|
|
3
|
+
## 0.37.2
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- [#
|
|
8
|
-
- Removed dependency [`immutable@3.8.2` ↗︎](https://www.npmjs.com/package/immutable/v/3.8.2) (from `dependencies`)
|
|
7
|
+
- [#1023](https://github.com/graphprotocol/graph-cli/pull/1023) [`bf5ab6d`](https://github.com/graphprotocol/graph-cli/commit/bf5ab6dc0f19fcb5b8e599b7d9b1c2b6efcbe005) Thanks [@evaporei](https://github.com/evaporei)! - update graph-ts to 0.29.1
|
|
9
8
|
|
|
10
9
|
## 0.37.1
|
|
11
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphprotocol/graph-cli",
|
|
3
|
-
"version": "0.37.2
|
|
3
|
+
"version": "0.37.2",
|
|
4
4
|
"license": "(Apache-2.0 OR MIT)",
|
|
5
5
|
"description": "CLI for building for and deploying to The Graph",
|
|
6
6
|
"dependencies": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"glob": "7.1.6",
|
|
17
17
|
"gluegun": "https://github.com/edgeandnode/gluegun#v4.3.1-pin-colors-dep",
|
|
18
18
|
"graphql": "15.5.0",
|
|
19
|
+
"immutable": "3.8.2",
|
|
19
20
|
"ipfs-http-client": "34.0.0",
|
|
20
21
|
"jayson": "3.6.6",
|
|
21
22
|
"js-yaml": "3.13.1",
|
package/src/codegen/schema.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
+
const immutable = require('immutable')
|
|
2
|
+
|
|
1
3
|
const tsCodegen = require('./typescript')
|
|
2
4
|
const typesCodegen = require('./types')
|
|
3
5
|
|
|
6
|
+
const List = immutable.List
|
|
7
|
+
|
|
4
8
|
class IdField {
|
|
5
|
-
static BYTES = Symbol(
|
|
6
|
-
static STRING = Symbol(
|
|
9
|
+
static BYTES = Symbol("Bytes")
|
|
10
|
+
static STRING = Symbol("String")
|
|
7
11
|
|
|
8
12
|
constructor(idField) {
|
|
9
|
-
const typeName = idField.type
|
|
10
|
-
this.kind = typeName ===
|
|
13
|
+
const typeName = idField.getIn(['type', 'type', 'name', 'value'])
|
|
14
|
+
this.kind = typeName === "Bytes" ? IdField.BYTES : IdField.STRING
|
|
11
15
|
}
|
|
12
16
|
|
|
13
17
|
typeName() {
|
|
14
|
-
return this.kind === IdField.BYTES ?
|
|
18
|
+
return this.kind === IdField.BYTES ? "Bytes" : "string"
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
gqlTypeName() {
|
|
18
|
-
return this.kind === IdField.BYTES ?
|
|
22
|
+
return this.kind === IdField.BYTES ? "Bytes" : "String"
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
tsNamedType() {
|
|
@@ -23,28 +27,28 @@ class IdField {
|
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
tsValueFrom() {
|
|
26
|
-
return this.kind === IdField.BYTES ?
|
|
30
|
+
return this.kind === IdField.BYTES ? "Value.fromBytes(id)" : "Value.fromString(id)"
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
tsValueKind() {
|
|
30
|
-
return this.kind === IdField.BYTES ?
|
|
34
|
+
return this.kind === IdField.BYTES ? "ValueKind.BYTES" : "ValueKind.STRING"
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
tsValueToString() {
|
|
34
|
-
return this.kind == IdField.BYTES ?
|
|
38
|
+
return this.kind == IdField.BYTES ? "id.toBytes().toHexString()" : "id.toString()"
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
tsToString() {
|
|
38
|
-
return this.kind == IdField.BYTES ?
|
|
42
|
+
return this.kind == IdField.BYTES ? "id.toHexString()" : "id"
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
static fromFields(fields) {
|
|
42
|
-
const idField = fields.find(field => field.name
|
|
46
|
+
const idField = fields.find(field => field.getIn(['name', 'value']) === 'id')
|
|
43
47
|
return new IdField(idField)
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
static fromTypeDef(def) {
|
|
47
|
-
return IdField.fromFields(def.fields)
|
|
51
|
+
return IdField.fromFields(def.get("fields"))
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
54
|
|
|
@@ -77,26 +81,29 @@ module.exports = class SchemaCodeGenerator {
|
|
|
77
81
|
}
|
|
78
82
|
|
|
79
83
|
generateTypes() {
|
|
80
|
-
return this.schema.ast
|
|
84
|
+
return this.schema.ast
|
|
85
|
+
.get('definitions')
|
|
81
86
|
.filter(def => this._isEntityTypeDefinition(def))
|
|
82
87
|
.map(def => this._generateEntityType(def))
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
_isEntityTypeDefinition(def) {
|
|
86
91
|
return (
|
|
87
|
-
def.kind === 'ObjectTypeDefinition' &&
|
|
88
|
-
def
|
|
92
|
+
def.get('kind') === 'ObjectTypeDefinition' &&
|
|
93
|
+
def
|
|
94
|
+
.get('directives')
|
|
95
|
+
.find(directive => directive.getIn(['name', 'value']) === 'entity') !== undefined
|
|
89
96
|
)
|
|
90
97
|
}
|
|
91
98
|
|
|
92
99
|
_isInterfaceDefinition(def) {
|
|
93
|
-
return def.kind === 'InterfaceTypeDefinition'
|
|
100
|
+
return def.get('kind') === 'InterfaceTypeDefinition'
|
|
94
101
|
}
|
|
95
102
|
|
|
96
103
|
_generateEntityType(def) {
|
|
97
|
-
let name = def.name
|
|
104
|
+
let name = def.getIn(['name', 'value'])
|
|
98
105
|
let klass = tsCodegen.klass(name, { export: true, extends: 'Entity' })
|
|
99
|
-
const fields = def.fields
|
|
106
|
+
const fields = def.get('fields')
|
|
100
107
|
const idField = IdField.fromFields(fields)
|
|
101
108
|
|
|
102
109
|
// Generate and add a constructor
|
|
@@ -106,10 +113,11 @@ module.exports = class SchemaCodeGenerator {
|
|
|
106
113
|
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method))
|
|
107
114
|
|
|
108
115
|
// Generate and add entity field getters and setters
|
|
109
|
-
def
|
|
116
|
+
def
|
|
117
|
+
.get('fields')
|
|
110
118
|
.reduce(
|
|
111
119
|
(methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)),
|
|
112
|
-
|
|
120
|
+
List(),
|
|
113
121
|
)
|
|
114
122
|
.forEach(method => klass.addMethod(method))
|
|
115
123
|
|
|
@@ -130,7 +138,7 @@ module.exports = class SchemaCodeGenerator {
|
|
|
130
138
|
}
|
|
131
139
|
|
|
132
140
|
_generateStoreMethods(entityName, idField) {
|
|
133
|
-
return
|
|
141
|
+
return List.of(
|
|
134
142
|
tsCodegen.method(
|
|
135
143
|
'save',
|
|
136
144
|
[],
|
|
@@ -154,19 +162,19 @@ module.exports = class SchemaCodeGenerator {
|
|
|
154
162
|
return changetype<${entityName} | null>(store.get('${entityName}', ${idField.tsToString()}))
|
|
155
163
|
`,
|
|
156
164
|
),
|
|
157
|
-
|
|
165
|
+
)
|
|
158
166
|
}
|
|
159
167
|
|
|
160
168
|
_generateEntityFieldMethods(entityDef, fieldDef) {
|
|
161
|
-
return [
|
|
169
|
+
return List([
|
|
162
170
|
this._generateEntityFieldGetter(entityDef, fieldDef),
|
|
163
171
|
this._generateEntityFieldSetter(entityDef, fieldDef),
|
|
164
|
-
]
|
|
172
|
+
])
|
|
165
173
|
}
|
|
166
174
|
|
|
167
175
|
_generateEntityFieldGetter(entityDef, fieldDef) {
|
|
168
|
-
let name = fieldDef.name
|
|
169
|
-
let gqlType = fieldDef.type
|
|
176
|
+
let name = fieldDef.getIn(['name', 'value'])
|
|
177
|
+
let gqlType = fieldDef.get('type')
|
|
170
178
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
|
|
171
179
|
let returnType = this._typeFromGraphQl(gqlType)
|
|
172
180
|
let isNullable = returnType instanceof tsCodegen.NullableType
|
|
@@ -190,21 +198,25 @@ module.exports = class SchemaCodeGenerator {
|
|
|
190
198
|
}
|
|
191
199
|
|
|
192
200
|
_generateEntityFieldSetter(entityDef, fieldDef) {
|
|
193
|
-
let name = fieldDef.name
|
|
194
|
-
let gqlType = fieldDef.type
|
|
201
|
+
let name = fieldDef.getIn(['name', 'value'])
|
|
202
|
+
let gqlType = fieldDef.get('type')
|
|
195
203
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
|
|
196
204
|
let paramType = this._typeFromGraphQl(gqlType)
|
|
197
205
|
let isNullable = paramType instanceof tsCodegen.NullableType
|
|
198
206
|
let paramTypeString = isNullable ? paramType.inner.toString() : paramType.toString()
|
|
199
207
|
let isArray = paramType instanceof tsCodegen.ArrayType
|
|
200
208
|
|
|
201
|
-
if (
|
|
209
|
+
if (
|
|
210
|
+
isArray &&
|
|
211
|
+
paramType.inner instanceof tsCodegen.NullableType
|
|
212
|
+
) {
|
|
202
213
|
let baseType = this._baseType(gqlType)
|
|
203
214
|
|
|
204
215
|
throw new Error(`
|
|
205
216
|
GraphQL schema can't have List's with Nullable members.
|
|
206
217
|
Error in '${name}' field of type '[${baseType}]'.
|
|
207
|
-
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`
|
|
218
|
+
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`
|
|
219
|
+
)
|
|
208
220
|
}
|
|
209
221
|
|
|
210
222
|
let setNonNullable = `
|
|
@@ -230,15 +242,12 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
230
242
|
}
|
|
231
243
|
|
|
232
244
|
_resolveFieldType(gqlType) {
|
|
233
|
-
let typeName = gqlType.name
|
|
245
|
+
let typeName = gqlType.getIn(['name', 'value'])
|
|
234
246
|
|
|
235
247
|
// If this is a reference to another type, the field has the type of
|
|
236
248
|
// the referred type's id field
|
|
237
|
-
const typeDef = this.schema.ast.definitions.
|
|
238
|
-
def =>
|
|
239
|
-
(this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
240
|
-
def.name?.value === typeName,
|
|
241
|
-
)
|
|
249
|
+
const typeDef = this.schema.ast.get("definitions").
|
|
250
|
+
find(def => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) && def.getIn(["name", "value"]) === typeName)
|
|
242
251
|
if (typeDef) {
|
|
243
252
|
return IdField.fromTypeDef(typeDef).typeName()
|
|
244
253
|
} else {
|
|
@@ -251,10 +260,10 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
251
260
|
* other entity types, this is the same as the type of the id of the
|
|
252
261
|
* referred type, i.e., `string` or `Bytes`*/
|
|
253
262
|
_valueTypeFromGraphQl(gqlType) {
|
|
254
|
-
if (gqlType.kind === 'NonNullType') {
|
|
255
|
-
return this._valueTypeFromGraphQl(gqlType.type, false)
|
|
256
|
-
} else if (gqlType.kind === 'ListType') {
|
|
257
|
-
return '[' + this._valueTypeFromGraphQl(gqlType.type) + ']'
|
|
263
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
264
|
+
return this._valueTypeFromGraphQl(gqlType.get('type'), false)
|
|
265
|
+
} else if (gqlType.get('kind') === 'ListType') {
|
|
266
|
+
return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']'
|
|
258
267
|
} else {
|
|
259
268
|
return this._resolveFieldType(gqlType)
|
|
260
269
|
}
|
|
@@ -263,20 +272,20 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
263
272
|
/** Determine the base type of `gqlType` by removing any non-null
|
|
264
273
|
* constraints and using the type of elements of lists */
|
|
265
274
|
_baseType(gqlType) {
|
|
266
|
-
if (gqlType.kind === 'NonNullType') {
|
|
267
|
-
return this._baseType(gqlType.type)
|
|
268
|
-
} else if (gqlType.kind === 'ListType') {
|
|
269
|
-
return this._baseType(gqlType.type)
|
|
275
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
276
|
+
return this._baseType(gqlType.get('type'))
|
|
277
|
+
} else if (gqlType.get('kind') === 'ListType') {
|
|
278
|
+
return this._baseType(gqlType.get('type'))
|
|
270
279
|
} else {
|
|
271
|
-
return gqlType.name
|
|
280
|
+
return gqlType.getIn(['name', 'value'])
|
|
272
281
|
}
|
|
273
282
|
}
|
|
274
283
|
|
|
275
284
|
_typeFromGraphQl(gqlType, nullable = true) {
|
|
276
|
-
if (gqlType.kind === 'NonNullType') {
|
|
277
|
-
return this._typeFromGraphQl(gqlType.type, false)
|
|
278
|
-
} else if (gqlType.kind === 'ListType') {
|
|
279
|
-
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.type))
|
|
285
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
286
|
+
return this._typeFromGraphQl(gqlType.get('type'), false)
|
|
287
|
+
} else if (gqlType.get('kind') === 'ListType') {
|
|
288
|
+
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.get('type')))
|
|
280
289
|
return nullable ? tsCodegen.nullableType(type) : type
|
|
281
290
|
} else {
|
|
282
291
|
// NamedType
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const prettier = require('prettier')
|
|
2
2
|
const graphql = require('graphql/language')
|
|
3
|
+
const immutable = require('immutable')
|
|
3
4
|
const SchemaCodeGenerator = require('./schema')
|
|
4
5
|
const {
|
|
5
6
|
Class,
|
|
@@ -11,11 +12,15 @@ const {
|
|
|
11
12
|
ArrayType,
|
|
12
13
|
} = require('./typescript')
|
|
13
14
|
|
|
14
|
-
const formatTS = code =>
|
|
15
|
+
const formatTS = code =>
|
|
16
|
+
prettier.format(
|
|
17
|
+
code,
|
|
18
|
+
{ parser: 'typescript', semi: false }
|
|
19
|
+
)
|
|
15
20
|
|
|
16
21
|
const createSchemaCodeGen = schema =>
|
|
17
22
|
new SchemaCodeGenerator({
|
|
18
|
-
ast: graphql.parse(schema),
|
|
23
|
+
ast: immutable.fromJS(graphql.parse(schema)),
|
|
19
24
|
})
|
|
20
25
|
|
|
21
26
|
const testEntity = (generatedTypes, expectedEntity) => {
|
|
@@ -56,7 +61,7 @@ describe('Schema code generator', () => {
|
|
|
56
61
|
}
|
|
57
62
|
`)
|
|
58
63
|
|
|
59
|
-
expect(codegen.generateTypes().
|
|
64
|
+
expect(codegen.generateTypes().size).toBe(0)
|
|
60
65
|
})
|
|
61
66
|
|
|
62
67
|
describe('Should generate correct classes for each entity', () => {
|
|
@@ -94,7 +99,7 @@ describe('Schema code generator', () => {
|
|
|
94
99
|
const foo = generatedTypes.find(type => type.name === 'Foo')
|
|
95
100
|
expect(foo).toBe(undefined)
|
|
96
101
|
// Account and Wallet
|
|
97
|
-
expect(generatedTypes.
|
|
102
|
+
expect(generatedTypes.size).toBe(2)
|
|
98
103
|
})
|
|
99
104
|
|
|
100
105
|
test('Account is an entity with the correct methods', () => {
|
|
@@ -243,12 +248,7 @@ describe('Schema code generator', () => {
|
|
|
243
248
|
},
|
|
244
249
|
{
|
|
245
250
|
name: 'set wallets',
|
|
246
|
-
params: [
|
|
247
|
-
new Param(
|
|
248
|
-
'value',
|
|
249
|
-
new NullableType(new ArrayType(new NamedType('string'))),
|
|
250
|
-
),
|
|
251
|
-
],
|
|
251
|
+
params: [new Param('value', new NullableType(new ArrayType(new NamedType('string'))))],
|
|
252
252
|
returnType: undefined,
|
|
253
253
|
body: `
|
|
254
254
|
if (!value) {
|
|
@@ -377,21 +377,20 @@ describe('Schema code generator', () => {
|
|
|
377
377
|
|
|
378
378
|
const generatedTypes = codegen.generateTypes()
|
|
379
379
|
testEntity(generatedTypes, {
|
|
380
|
-
name:
|
|
380
|
+
name: "Task",
|
|
381
381
|
members: [],
|
|
382
382
|
methods: [
|
|
383
383
|
{
|
|
384
384
|
name: 'constructor',
|
|
385
385
|
params: [new Param('id', new NamedType('Bytes'))],
|
|
386
386
|
returnType: undefined,
|
|
387
|
-
body: "\n super()\n this.set('id', Value.fromBytes(id))\n "
|
|
387
|
+
body: "\n super()\n this.set('id', Value.fromBytes(id))\n "
|
|
388
388
|
},
|
|
389
389
|
{
|
|
390
390
|
name: 'save',
|
|
391
391
|
params: [],
|
|
392
392
|
returnType: new NamedType('void'),
|
|
393
|
-
body:
|
|
394
|
-
'\n' +
|
|
393
|
+
body: '\n' +
|
|
395
394
|
" let id = this.get('id')\n" +
|
|
396
395
|
' assert(id != null,\n' +
|
|
397
396
|
" 'Cannot save Task entity without an ID')\n" +
|
|
@@ -399,67 +398,63 @@ describe('Schema code generator', () => {
|
|
|
399
398
|
' assert(id.kind == ValueKind.BYTES,\n' +
|
|
400
399
|
" `Entities of type Task must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`)\n" +
|
|
401
400
|
" store.set('Task', id.toBytes().toHexString(), this)\n" +
|
|
402
|
-
' }'
|
|
401
|
+
' }'
|
|
403
402
|
},
|
|
404
403
|
{
|
|
405
404
|
name: 'load',
|
|
406
405
|
static: true,
|
|
407
406
|
params: [new Param('id', new NamedType('Bytes'))],
|
|
408
407
|
returnType: new NullableType(new NamedType('Task')),
|
|
409
|
-
body:
|
|
410
|
-
'\n' +
|
|
408
|
+
body: '\n' +
|
|
411
409
|
" return changetype<Task | null>(store.get('Task', id.toHexString()))\n" +
|
|
412
|
-
' '
|
|
410
|
+
' '
|
|
413
411
|
},
|
|
414
412
|
{
|
|
415
413
|
name: 'get id',
|
|
416
414
|
params: [],
|
|
417
415
|
returnType: new NamedType('Bytes'),
|
|
418
|
-
body:
|
|
419
|
-
'\n' +
|
|
416
|
+
body: '\n' +
|
|
420
417
|
" let value = this.get('id')\n" +
|
|
421
418
|
' return value!.toBytes()\n' +
|
|
422
|
-
' '
|
|
419
|
+
' '
|
|
423
420
|
},
|
|
424
421
|
{
|
|
425
422
|
name: 'set id',
|
|
426
423
|
params: [new Param('value', new NamedType('Bytes'))],
|
|
427
424
|
returnType: undefined,
|
|
428
|
-
body: "\n this.set('id', Value.fromBytes(value))\n "
|
|
425
|
+
body: "\n this.set('id', Value.fromBytes(value))\n "
|
|
429
426
|
},
|
|
430
427
|
{
|
|
431
428
|
name: 'get employee',
|
|
432
429
|
params: [],
|
|
433
430
|
returnType: new NamedType('Bytes'),
|
|
434
|
-
body:
|
|
435
|
-
'\n' +
|
|
431
|
+
body: '\n' +
|
|
436
432
|
" let value = this.get('employee')\n" +
|
|
437
433
|
' return value!.toBytes()\n' +
|
|
438
|
-
' '
|
|
434
|
+
' '
|
|
439
435
|
},
|
|
440
436
|
{
|
|
441
437
|
name: 'set employee',
|
|
442
438
|
params: [new Param('value', new NamedType('Bytes'))],
|
|
443
439
|
returnType: undefined,
|
|
444
|
-
body: "\n this.set('employee', Value.fromBytes(value))\n "
|
|
440
|
+
body: "\n this.set('employee', Value.fromBytes(value))\n "
|
|
445
441
|
},
|
|
446
442
|
{
|
|
447
443
|
name: 'get worker',
|
|
448
444
|
params: [],
|
|
449
445
|
returnType: new NamedType('Bytes'),
|
|
450
|
-
body:
|
|
451
|
-
'\n' +
|
|
446
|
+
body: '\n' +
|
|
452
447
|
" let value = this.get('worker')\n" +
|
|
453
448
|
' return value!.toBytes()\n' +
|
|
454
|
-
' '
|
|
449
|
+
' '
|
|
455
450
|
},
|
|
456
451
|
{
|
|
457
452
|
name: 'set worker',
|
|
458
453
|
params: [new Param('value', new NamedType('Bytes'))],
|
|
459
454
|
returnType: undefined,
|
|
460
|
-
body: "\n this.set('worker', Value.fromBytes(value))\n "
|
|
461
|
-
}
|
|
462
|
-
]
|
|
455
|
+
body: "\n this.set('worker', Value.fromBytes(value))\n "
|
|
456
|
+
}
|
|
457
|
+
]
|
|
463
458
|
})
|
|
464
459
|
})
|
|
465
460
|
})
|
package/src/codegen/template.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const immutable = require('immutable')
|
|
1
2
|
const IpfsFileTemplateCodeGen = require('../protocols/ipfs/codegen/file_template')
|
|
2
3
|
|
|
3
4
|
const tsCodegen = require('./typescript')
|
|
@@ -28,7 +29,7 @@ module.exports = class DataSourceTemplateCodeGenerator {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
generateTypes() {
|
|
31
|
-
return [this._generateTemplateType()]
|
|
32
|
+
return immutable.List([this._generateTemplateType()])
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
_generateTemplateType() {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const immutable = require('immutable')
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* ethereum.Value -> AssemblyScript conversions
|
|
3
5
|
*/
|
|
@@ -53,56 +55,29 @@ const ETHEREUM_VALUE_TO_ASSEMBLYSCRIPT = [
|
|
|
53
55
|
|
|
54
56
|
// Multi dimensional arrays
|
|
55
57
|
|
|
56
|
-
[
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
code => `${code}.toAddressMatrix()`,
|
|
60
|
-
],
|
|
61
|
-
[
|
|
62
|
-
/^bool\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
63
|
-
'Array<Array<boolean>>',
|
|
64
|
-
code => `${code}.toBooleanMatrix()`,
|
|
65
|
-
],
|
|
66
|
-
[
|
|
67
|
-
/^byte\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
68
|
-
'Array<Array<Bytes>>',
|
|
69
|
-
code => `${code}.toBytesMatrix()`,
|
|
70
|
-
],
|
|
58
|
+
[/^address\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<Address>>', code => `${code}.toAddressMatrix()`],
|
|
59
|
+
[/^bool\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<boolean>>', code => `${code}.toBooleanMatrix()`],
|
|
60
|
+
[/^byte\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<Bytes>>', code => `${code}.toBytesMatrix()`],
|
|
71
61
|
[
|
|
72
62
|
/^bytes(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32)?\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
73
63
|
'Array<Array<Bytes>>',
|
|
74
64
|
code => `${code}.toBytesMatrix()`,
|
|
75
65
|
],
|
|
76
|
-
[
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
code => `${code}.toI32Matrix()`,
|
|
80
|
-
],
|
|
81
|
-
[
|
|
82
|
-
/^uint(8|16|24)\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
83
|
-
'Array<Array<i32>>',
|
|
84
|
-
code => `${code}.toI32Matrix()`,
|
|
85
|
-
],
|
|
86
|
-
[
|
|
87
|
-
/^uint32\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
88
|
-
'Array<Array<BigInt>>',
|
|
89
|
-
code => `${code}.toBigIntMatrix()`,
|
|
90
|
-
],
|
|
66
|
+
[/^int(8|16|24|32)\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<i32>>', code => `${code}.toI32Matrix()`],
|
|
67
|
+
[/^uint(8|16|24)\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<i32>>', code => `${code}.toI32Matrix()`],
|
|
68
|
+
[/^uint32\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<BigInt>>', code => `${code}.toBigIntMatrix()`],
|
|
91
69
|
[
|
|
92
70
|
/^u?int(40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
93
71
|
'Array<Array<BigInt>>',
|
|
94
72
|
code => `${code}.toBigIntMatrix()`,
|
|
95
73
|
],
|
|
96
|
-
[
|
|
97
|
-
/^string\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
98
|
-
'Array<Array<string>>',
|
|
99
|
-
code => `${code}.toStringMatrix()`,
|
|
100
|
-
],
|
|
74
|
+
[/^string\[([0-9]+)?\]\[([0-9]+)?\]$/, 'Array<Array<string>>', code => `${code}.toStringMatrix()`],
|
|
101
75
|
[
|
|
102
76
|
/^tuple\[([0-9]+)?\]\[([0-9]+)?\]$/,
|
|
103
77
|
'Array<Array<ethereum.Tuple>>',
|
|
104
78
|
(code, type) => `${code}.toTupleMatrix<${type}>()`,
|
|
105
79
|
],
|
|
80
|
+
|
|
106
81
|
]
|
|
107
82
|
|
|
108
83
|
/**
|
|
@@ -203,7 +178,7 @@ const ASSEMBLYSCRIPT_TO_ETHEREUM_VALUE = [
|
|
|
203
178
|
/^tuple\[([0-9]+)?\]$/,
|
|
204
179
|
code => `ethereum.Value.fromTupleArray(${code})`,
|
|
205
180
|
],
|
|
206
|
-
|
|
181
|
+
|
|
207
182
|
// Multi dimentional arrays
|
|
208
183
|
|
|
209
184
|
[
|
|
@@ -307,13 +282,9 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
307
282
|
['Array<Array<BigInt>>', '[[BigInt]]', code => `Value.fromBigIntMatrix(${code})`],
|
|
308
283
|
['Array<Array<string>>', '[[String]]', code => `Value.fromStringMatrix(${code})`],
|
|
309
284
|
['Array<Array<string>>', '[[ID]]', code => `Value.fromStringMatrix(${code})`],
|
|
310
|
-
[
|
|
311
|
-
'Array<Array<BigDecimal>>',
|
|
312
|
-
'[[BigDecimal]]',
|
|
313
|
-
code => `Value.fromBigDecimalMatrix(${code})`,
|
|
314
|
-
],
|
|
285
|
+
['Array<Array<BigDecimal>>', '[[BigDecimal]]', code => `Value.fromBigDecimalMatrix(${code})`],
|
|
315
286
|
['Array<Array<string>>', /\[\[.*\]\]/, code => `Value.fromStringMatrix(${code})`],
|
|
316
|
-
['Array<Array<string | null>>', null, code => `Value.fromStringMatrix(${code})`]
|
|
287
|
+
['Array<Array<string | null>>', null, code => `Value.fromStringMatrix(${code})`],// is this overwriting the Array null below?
|
|
317
288
|
|
|
318
289
|
// Arrays
|
|
319
290
|
|
|
@@ -344,7 +315,7 @@ const ASSEMBLYSCRIPT_TO_VALUE = [
|
|
|
344
315
|
/**
|
|
345
316
|
* Type conversions
|
|
346
317
|
*/
|
|
347
|
-
module.exports =
|
|
318
|
+
module.exports = immutable.fromJS({
|
|
348
319
|
ethereum: {
|
|
349
320
|
AssemblyScript: ETHEREUM_VALUE_TO_ASSEMBLYSCRIPT,
|
|
350
321
|
},
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
const immutable = require('immutable')
|
|
2
|
+
|
|
1
3
|
const TYPE_CONVERSIONS = require('./conversions')
|
|
2
4
|
|
|
3
5
|
// Conversion utilities
|
|
4
6
|
|
|
5
7
|
const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
|
|
6
|
-
let conversions = TYPE_CONVERSIONS[fromTypeSystem
|
|
8
|
+
let conversions = TYPE_CONVERSIONS.getIn([fromTypeSystem, toTypeSystem])
|
|
7
9
|
if (conversions === undefined) {
|
|
8
10
|
throw new Error(
|
|
9
11
|
`Conversions from '${fromTypeSystem}' to '${toTypeSystem}' are not supported`,
|
|
@@ -13,16 +15,16 @@ const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
|
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
const objectifyConversion = (fromTypeSystem, toTypeSystem, conversion) => {
|
|
16
|
-
return
|
|
18
|
+
return immutable.fromJS({
|
|
17
19
|
from: {
|
|
18
20
|
typeSystem: fromTypeSystem,
|
|
19
|
-
type: conversion
|
|
21
|
+
type: conversion.get(0),
|
|
20
22
|
},
|
|
21
23
|
to: {
|
|
22
24
|
typeSystem: toTypeSystem,
|
|
23
|
-
type: conversion
|
|
25
|
+
type: conversion.get(1),
|
|
24
26
|
},
|
|
25
|
-
convert: conversion
|
|
27
|
+
convert: conversion.get(2),
|
|
26
28
|
})
|
|
27
29
|
}
|
|
28
30
|
|
|
@@ -30,9 +32,9 @@ const findConversionFromType = (fromTypeSystem, toTypeSystem, fromType) => {
|
|
|
30
32
|
let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
|
|
31
33
|
|
|
32
34
|
let conversion = conversions.find(conversion =>
|
|
33
|
-
typeof conversion
|
|
34
|
-
? conversion
|
|
35
|
-
: fromType.match(conversion
|
|
35
|
+
typeof conversion.get(0) === 'string'
|
|
36
|
+
? conversion.get(0) === fromType
|
|
37
|
+
: fromType.match(conversion.get(0)),
|
|
36
38
|
)
|
|
37
39
|
|
|
38
40
|
if (conversion === undefined) {
|
|
@@ -49,9 +51,9 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
|
|
|
49
51
|
let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
|
|
50
52
|
|
|
51
53
|
let conversion = conversions.find(conversion =>
|
|
52
|
-
typeof conversion
|
|
53
|
-
? conversion
|
|
54
|
-
: toType.match(conversion
|
|
54
|
+
typeof conversion.get(1) === 'string'
|
|
55
|
+
? conversion.get(1) === toType
|
|
56
|
+
: toType.match(conversion.get(1)),
|
|
55
57
|
)
|
|
56
58
|
|
|
57
59
|
if (conversion === undefined) {
|
|
@@ -67,34 +69,35 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
|
|
|
67
69
|
// High-level type system API
|
|
68
70
|
|
|
69
71
|
const ascTypeForProtocol = (protocol, protocolType) =>
|
|
70
|
-
findConversionFromType(protocol, 'AssemblyScript', protocolType).to
|
|
72
|
+
findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn(['to', 'type'])
|
|
71
73
|
|
|
72
74
|
// TODO: this can be removed/replaced by the function above
|
|
73
|
-
const ascTypeForEthereum = ethereumType =>
|
|
75
|
+
const ascTypeForEthereum = ethereumType =>
|
|
76
|
+
ascTypeForProtocol('ethereum', ethereumType)
|
|
74
77
|
|
|
75
78
|
const ethereumTypeForAsc = ascType =>
|
|
76
|
-
findConversionFromType('AssemblyScript', 'ethereum', ascType).to
|
|
79
|
+
findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn(['to', 'type'])
|
|
77
80
|
|
|
78
81
|
const ethereumToAsc = (code, ethereumType, internalType) =>
|
|
79
|
-
findConversionFromType('ethereum', 'AssemblyScript', ethereumType).convert(
|
|
82
|
+
findConversionFromType('ethereum', 'AssemblyScript', ethereumType).get('convert')(
|
|
80
83
|
code,
|
|
81
84
|
internalType,
|
|
82
85
|
)
|
|
83
86
|
|
|
84
87
|
const ethereumFromAsc = (code, ethereumType) =>
|
|
85
|
-
findConversionToType('AssemblyScript', 'ethereum', ethereumType).convert(code)
|
|
88
|
+
findConversionToType('AssemblyScript', 'ethereum', ethereumType).get('convert')(code)
|
|
86
89
|
|
|
87
90
|
const ascTypeForValue = valueType =>
|
|
88
|
-
findConversionFromType('Value', 'AssemblyScript', valueType).to
|
|
91
|
+
findConversionFromType('Value', 'AssemblyScript', valueType).getIn(['to', 'type'])
|
|
89
92
|
|
|
90
93
|
const valueTypeForAsc = ascType =>
|
|
91
|
-
findConversionFromType('AssemblyScript', 'Value', ascType).to
|
|
94
|
+
findConversionFromType('AssemblyScript', 'Value', ascType).getIn(['to', 'type'])
|
|
92
95
|
|
|
93
96
|
const valueToAsc = (code, valueType) =>
|
|
94
|
-
findConversionFromType('Value', 'AssemblyScript', valueType).convert(code)
|
|
97
|
+
findConversionFromType('Value', 'AssemblyScript', valueType).get('convert')(code)
|
|
95
98
|
|
|
96
99
|
const valueFromAsc = (code, valueType) =>
|
|
97
|
-
findConversionToType('AssemblyScript', 'Value', valueType).convert(code)
|
|
100
|
+
findConversionToType('AssemblyScript', 'Value', valueType).get('convert')(code)
|
|
98
101
|
|
|
99
102
|
module.exports = {
|
|
100
103
|
// protocol <-> AssemblyScript
|