@graphprotocol/graph-cli 0.23.0 → 0.23.1
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/README.md +83 -0
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/.travis.yml +4 -0
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/chain/ethereum.ts +10 -16
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/chain/near.ts +402 -0
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/collections.ts +125 -4
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/eager_offset.ts +2 -1
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/json.ts +14 -0
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/numbers.ts +58 -16
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/value.ts +2 -5
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/global/global.ts +108 -0
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/helper-functions.ts +5 -5
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/index.ts +6 -15
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/package.json +5 -2
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/test/bigInt.ts +138 -108
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/test/bytes.ts +31 -20
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/test/test.js +120 -88
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/tsconfig.json +2 -2
- package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/types/tsconfig.base.json +3 -0
- package/examples/basic-event-handlers/package.json +1 -1
- package/examples/basic-event-handlers/yarn.lock +4 -4
- package/examples/example-subgraph/package.json +1 -1
- package/examples/example-subgraph/yarn.lock +4 -4
- package/jest.config.js +2 -2
- package/package.json +2 -6
- package/src/commands/init.js +30 -3
- package/src/commands/test.js +1 -1
- package/src/scaffold.js +18 -2
- package/tests/cli/globalSetup.js +6 -0
- package/tests/cli/globalTeardown.js +6 -0
- package/tests/cli/init.test.js +1 -1
- package/tests/cli/util.js +17 -8
- package/tests/cli/init/from-contract/abis/Contract.json +0 -69
- package/tests/cli/init/from-contract/package.json +0 -16
- package/tests/cli/init/from-contract/schema.graphql +0 -6
- package/tests/cli/init/from-contract/src/mapping.ts +0 -49
- package/tests/cli/init/from-contract/subgraph.yaml +0 -26
package/src/commands/init.js
CHANGED
|
@@ -257,16 +257,24 @@ const loadAbiFromBlockScout = async (network, address) =>
|
|
|
257
257
|
},
|
|
258
258
|
)
|
|
259
259
|
|
|
260
|
+
const getEtherscanLikeAPIUrl = (network) => {
|
|
261
|
+
switch(network){
|
|
262
|
+
case "mainnet": return `https://api.etherscan.io/api`;
|
|
263
|
+
case "bsc": return `https://api.bscscan.com/api`;
|
|
264
|
+
case "matic": return `https://api.polygonscan.com/api`;
|
|
265
|
+
default: return `https://api-${network}.etherscan.io/api`;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
260
269
|
const loadAbiFromEtherscan = async (network, address) =>
|
|
261
270
|
await withSpinner(
|
|
262
271
|
`Fetching ABI from Etherscan`,
|
|
263
272
|
`Failed to fetch ABI from Etherscan`,
|
|
264
273
|
`Warnings while fetching ABI from Etherscan`,
|
|
265
274
|
async spinner => {
|
|
275
|
+
const scanApiUrl = getEtherscanLikeAPIUrl(network);
|
|
266
276
|
let result = await fetch(
|
|
267
|
-
|
|
268
|
-
network === 'mainnet' ? 'api' : `api-${network}`
|
|
269
|
-
}.etherscan.io/api?module=contract&action=getabi&address=${address}`,
|
|
277
|
+
`${scanApiUrl}?module=contract&action=getabi&address=${address}`,
|
|
270
278
|
)
|
|
271
279
|
let json = await result.json()
|
|
272
280
|
|
|
@@ -530,12 +538,26 @@ const initRepository = async (toolbox, directory) =>
|
|
|
530
538
|
},
|
|
531
539
|
)
|
|
532
540
|
|
|
541
|
+
// Only used for local testing / continuous integration.
|
|
542
|
+
//
|
|
543
|
+
// This requires that the command `npm link` is called
|
|
544
|
+
// on the root directory of this repository, as described here:
|
|
545
|
+
// https://docs.npmjs.com/cli/v7/commands/npm-link.
|
|
546
|
+
const npmLinkToLocalCli = async (toolbox, directory) => {
|
|
547
|
+
if (process.env.GRAPH_CLI_TESTS) {
|
|
548
|
+
await toolbox.system.run('npm link @graphprotocol/graph-cli', { cwd: directory })
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
533
552
|
const installDependencies = async (toolbox, directory, installCommand) =>
|
|
534
553
|
await withSpinner(
|
|
535
554
|
`Install dependencies with ${toolbox.print.colors.muted(installCommand)}`,
|
|
536
555
|
`Failed to install dependencies`,
|
|
537
556
|
`Warnings while installing dependencies`,
|
|
538
557
|
async spinner => {
|
|
558
|
+
// Links to local graph-cli if we're running the automated tests
|
|
559
|
+
await npmLinkToLocalCli(toolbox, directory)
|
|
560
|
+
|
|
539
561
|
await toolbox.system.run(installCommand, { cwd: directory })
|
|
540
562
|
return true
|
|
541
563
|
},
|
|
@@ -643,6 +665,11 @@ const initSubgraphFromExample = async (
|
|
|
643
665
|
delete pkgJson['license']
|
|
644
666
|
delete pkgJson['repository']
|
|
645
667
|
|
|
668
|
+
// Remove example's cli in favor of the local one (added via `npm link`)
|
|
669
|
+
if (process.env.GRAPH_CLI_TESTS) {
|
|
670
|
+
delete pkgJson['devDependencies']['@graphprotocol/graph-cli']
|
|
671
|
+
}
|
|
672
|
+
|
|
646
673
|
// Write package.json
|
|
647
674
|
await filesystem.write(pkgJsonFilename, pkgJson, { jsonIndent: 2 })
|
|
648
675
|
return true
|
package/src/commands/test.js
CHANGED
package/src/scaffold.js
CHANGED
|
@@ -18,6 +18,13 @@ const abiEvents = abi =>
|
|
|
18
18
|
setName: (event, name) => event.set('_alias', name),
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
+
const graphCliVersion = process.env.GRAPH_CLI_TESTS
|
|
22
|
+
// JSON.stringify should remove this key, we will install the local
|
|
23
|
+
// graph-cli for the tests using `npm link` instead of fetching from npm.
|
|
24
|
+
? undefined
|
|
25
|
+
// For scaffolding real subgraphs
|
|
26
|
+
: `${module.exports.version}`
|
|
27
|
+
|
|
21
28
|
// package.json
|
|
22
29
|
|
|
23
30
|
const generatePackageJson = ({ subgraphName, node }) =>
|
|
@@ -41,8 +48,8 @@ const generatePackageJson = ({ subgraphName, node }) =>
|
|
|
41
48
|
subgraphName,
|
|
42
49
|
},
|
|
43
50
|
dependencies: {
|
|
44
|
-
'@graphprotocol/graph-cli':
|
|
45
|
-
'@graphprotocol/graph-ts': `0.
|
|
51
|
+
'@graphprotocol/graph-cli': graphCliVersion,
|
|
52
|
+
'@graphprotocol/graph-ts': `0.23.1`,
|
|
46
53
|
},
|
|
47
54
|
}),
|
|
48
55
|
{ parser: 'json' },
|
|
@@ -145,6 +152,14 @@ const generateSchema = ({ abi, indexEvents }) => {
|
|
|
145
152
|
)
|
|
146
153
|
}
|
|
147
154
|
|
|
155
|
+
const tsConfig = prettier.format(
|
|
156
|
+
JSON.stringify({
|
|
157
|
+
extends: '@graphprotocol/graph-ts/types/tsconfig.base.json',
|
|
158
|
+
include: ['src'],
|
|
159
|
+
}),
|
|
160
|
+
{ parser: 'json' },
|
|
161
|
+
)
|
|
162
|
+
|
|
148
163
|
// Mapping
|
|
149
164
|
|
|
150
165
|
const generateTupleFieldAssignments = ({ keyPath, index, component }) => {
|
|
@@ -303,6 +318,7 @@ const generateScaffold = async (
|
|
|
303
318
|
'package.json': packageJson,
|
|
304
319
|
'subgraph.yaml': manifest,
|
|
305
320
|
'schema.graphql': schema,
|
|
321
|
+
'tsconfig.json': tsConfig,
|
|
306
322
|
src: { 'mapping.ts': mapping },
|
|
307
323
|
abis: {
|
|
308
324
|
[`${contractName}.json`]: prettier.format(JSON.stringify(abi.data), {
|
package/tests/cli/init.test.js
CHANGED
package/tests/cli/util.js
CHANGED
|
@@ -13,7 +13,7 @@ const cliTest = (title, args, testPath, options) => {
|
|
|
13
13
|
let cwd =
|
|
14
14
|
options !== undefined && options.cwd ? options.cwd : resolvePath(`./${testPath}`)
|
|
15
15
|
|
|
16
|
-
let [exitCode, stdout, stderr] = await
|
|
16
|
+
let [exitCode, stdout, stderr] = await runGraphCli(args, cwd)
|
|
17
17
|
|
|
18
18
|
let expectedExitCode = undefined
|
|
19
19
|
if (options !== undefined && options.exitCode !== undefined) {
|
|
@@ -55,18 +55,14 @@ const cliTest = (title, args, testPath, options) => {
|
|
|
55
55
|
)
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
const
|
|
59
|
-
// Resolve the path to graph.js
|
|
60
|
-
let graphCli = path.join(__dirname, '..', '..', 'bin', 'graph')
|
|
61
|
-
|
|
58
|
+
const runCommand = async (command, args = [], cwd = process.cwd()) => {
|
|
62
59
|
// Make sure to set an absolute working directory
|
|
63
60
|
cwd = cwd[0] !== '/' ? path.resolve(__dirname, cwd) : cwd
|
|
64
61
|
|
|
65
62
|
return new Promise((resolve, reject) => {
|
|
66
63
|
let stdout = ''
|
|
67
64
|
let stderr = ''
|
|
68
|
-
const
|
|
69
|
-
const child = spawn(command, { cwd })
|
|
65
|
+
const child = spawn(`${command} ${args.join(' ')}`, { cwd })
|
|
70
66
|
|
|
71
67
|
child.on('error', error => {
|
|
72
68
|
reject(error)
|
|
@@ -86,7 +82,20 @@ const runCli = async (args = [], cwd = process.cwd()) => {
|
|
|
86
82
|
})
|
|
87
83
|
}
|
|
88
84
|
|
|
85
|
+
const runGraphCli = async (args, cwd) => {
|
|
86
|
+
// Resolve the path to graph.js
|
|
87
|
+
let graphCli = path.join(__dirname, '..', '..', 'bin', 'graph')
|
|
88
|
+
|
|
89
|
+
return await runCommand(graphCli, args, cwd)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const npmLinkCli = () => runCommand('npm', ['link'])
|
|
93
|
+
|
|
94
|
+
const npmUnlinkCli = () => runCommand('npm', ['unlink'])
|
|
95
|
+
|
|
89
96
|
module.exports = {
|
|
90
97
|
cliTest,
|
|
91
|
-
|
|
98
|
+
npmLinkCli,
|
|
99
|
+
npmUnlinkCli,
|
|
100
|
+
runGraphCli,
|
|
92
101
|
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"constant": true,
|
|
4
|
-
"inputs": [],
|
|
5
|
-
"name": "proxyOwner",
|
|
6
|
-
"outputs": [{ "name": "", "type": "address" }],
|
|
7
|
-
"payable": false,
|
|
8
|
-
"stateMutability": "view",
|
|
9
|
-
"type": "function"
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
"constant": true,
|
|
13
|
-
"inputs": [],
|
|
14
|
-
"name": "currentContract",
|
|
15
|
-
"outputs": [{ "name": "", "type": "address" }],
|
|
16
|
-
"payable": false,
|
|
17
|
-
"stateMutability": "view",
|
|
18
|
-
"type": "function"
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
"constant": true,
|
|
22
|
-
"inputs": [],
|
|
23
|
-
"name": "owner",
|
|
24
|
-
"outputs": [{ "name": "", "type": "address" }],
|
|
25
|
-
"payable": false,
|
|
26
|
-
"stateMutability": "view",
|
|
27
|
-
"type": "function"
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
"constant": false,
|
|
31
|
-
"inputs": [
|
|
32
|
-
{ "name": "newContract", "type": "address" },
|
|
33
|
-
{ "name": "data", "type": "bytes" }
|
|
34
|
-
],
|
|
35
|
-
"name": "upgrade",
|
|
36
|
-
"outputs": [],
|
|
37
|
-
"payable": false,
|
|
38
|
-
"stateMutability": "nonpayable",
|
|
39
|
-
"type": "function"
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
"constant": false,
|
|
43
|
-
"inputs": [{ "name": "_newOwner", "type": "address" }],
|
|
44
|
-
"name": "transferOwnership",
|
|
45
|
-
"outputs": [],
|
|
46
|
-
"payable": false,
|
|
47
|
-
"stateMutability": "nonpayable",
|
|
48
|
-
"type": "function"
|
|
49
|
-
},
|
|
50
|
-
{ "payable": true, "stateMutability": "payable", "type": "fallback" },
|
|
51
|
-
{
|
|
52
|
-
"anonymous": false,
|
|
53
|
-
"inputs": [
|
|
54
|
-
{ "indexed": true, "name": "newContract", "type": "address" },
|
|
55
|
-
{ "indexed": false, "name": "initializedWith", "type": "bytes" }
|
|
56
|
-
],
|
|
57
|
-
"name": "Upgrade",
|
|
58
|
-
"type": "event"
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
"anonymous": false,
|
|
62
|
-
"inputs": [
|
|
63
|
-
{ "indexed": false, "name": "_prevOwner", "type": "address" },
|
|
64
|
-
{ "indexed": false, "name": "_newOwner", "type": "address" }
|
|
65
|
-
],
|
|
66
|
-
"name": "OwnerUpdate",
|
|
67
|
-
"type": "event"
|
|
68
|
-
}
|
|
69
|
-
]
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "subgraph-from-contract",
|
|
3
|
-
"license": "UNLICENSED",
|
|
4
|
-
"scripts": {
|
|
5
|
-
"codegen": "graph codegen",
|
|
6
|
-
"build": "graph build",
|
|
7
|
-
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ user/subgraph-from-contract",
|
|
8
|
-
"create-local": "graph create --node http://localhost:8020/ user/subgraph-from-contract",
|
|
9
|
-
"remove-local": "graph remove --node http://localhost:8020/ user/subgraph-from-contract",
|
|
10
|
-
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 user/subgraph-from-contract"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@graphprotocol/graph-cli": "file:../../../../",
|
|
14
|
-
"@graphprotocol/graph-ts": "0.22.1"
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { BigInt } from "@graphprotocol/graph-ts"
|
|
2
|
-
import { Contract, Upgrade, OwnerUpdate } from "../generated/Contract/Contract"
|
|
3
|
-
import { ExampleEntity } from "../generated/schema"
|
|
4
|
-
|
|
5
|
-
export function handleUpgrade(event: Upgrade): void {
|
|
6
|
-
// Entities can be loaded from the store using a string ID; this ID
|
|
7
|
-
// needs to be unique across all entities of the same type
|
|
8
|
-
let entity = ExampleEntity.load(event.transaction.from.toHex())
|
|
9
|
-
|
|
10
|
-
// Entities only exist after they have been saved to the store;
|
|
11
|
-
// `null` checks allow to create entities on demand
|
|
12
|
-
if (!entity) {
|
|
13
|
-
entity = new ExampleEntity(event.transaction.from.toHex())
|
|
14
|
-
|
|
15
|
-
// Entity fields can be set using simple assignments
|
|
16
|
-
entity.count = BigInt.fromI32(0)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// BigInt and BigDecimal math are supported
|
|
20
|
-
entity.count = entity.count + BigInt.fromI32(1)
|
|
21
|
-
|
|
22
|
-
// Entity fields can be set based on event parameters
|
|
23
|
-
entity.newContract = event.params.newContract
|
|
24
|
-
entity.initializedWith = event.params.initializedWith
|
|
25
|
-
|
|
26
|
-
// Entities can be written to the store with `.save()`
|
|
27
|
-
entity.save()
|
|
28
|
-
|
|
29
|
-
// Note: If a handler doesn't require existing field values, it is faster
|
|
30
|
-
// _not_ to load the entity from the store. Instead, create it fresh with
|
|
31
|
-
// `new Entity(...)`, set the fields that should be updated and save the
|
|
32
|
-
// entity back to the store. Fields that were not set or unset remain
|
|
33
|
-
// unchanged, allowing for partial updates to be applied.
|
|
34
|
-
|
|
35
|
-
// It is also possible to access smart contracts from mappings. For
|
|
36
|
-
// example, the contract that has emitted the event can be connected to
|
|
37
|
-
// with:
|
|
38
|
-
//
|
|
39
|
-
// let contract = Contract.bind(event.address)
|
|
40
|
-
//
|
|
41
|
-
// The following functions can then be called on this contract to access
|
|
42
|
-
// state variables and other data:
|
|
43
|
-
//
|
|
44
|
-
// - contract.proxyOwner(...)
|
|
45
|
-
// - contract.currentContract(...)
|
|
46
|
-
// - contract.owner(...)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function handleOwnerUpdate(event: OwnerUpdate): void {}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
specVersion: 0.0.1
|
|
2
|
-
schema:
|
|
3
|
-
file: ./schema.graphql
|
|
4
|
-
dataSources:
|
|
5
|
-
- kind: ethereum/contract
|
|
6
|
-
name: Contract
|
|
7
|
-
network: mainnet
|
|
8
|
-
source:
|
|
9
|
-
address: "0xF87E31492Faf9A91B02Ee0dEAAd50d51d56D5d4d"
|
|
10
|
-
abi: Contract
|
|
11
|
-
mapping:
|
|
12
|
-
kind: ethereum/events
|
|
13
|
-
apiVersion: 0.0.5
|
|
14
|
-
language: wasm/assemblyscript
|
|
15
|
-
entities:
|
|
16
|
-
- Upgrade
|
|
17
|
-
- OwnerUpdate
|
|
18
|
-
abis:
|
|
19
|
-
- name: Contract
|
|
20
|
-
file: ./abis/Contract.json
|
|
21
|
-
eventHandlers:
|
|
22
|
-
- event: Upgrade(indexed address,bytes)
|
|
23
|
-
handler: handleUpgrade
|
|
24
|
-
- event: OwnerUpdate(address,address)
|
|
25
|
-
handler: handleOwnerUpdate
|
|
26
|
-
file: ./src/mapping.ts
|