diff --git a/.gitignore b/.gitignore index 5a49c45..3bf7cd8 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,8 @@ coverage .DS_Store .directory .nfs* -.fuse_hidden* \ No newline at end of file +.fuse_hidden* + +# Custom +dev/ +package-lock.json diff --git a/README.md b/README.md index 94ea0c2..93421c3 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ ![npm](https://img.shields.io/npm/v/artblocks) [![CircleCI](https://circleci.com/gh/ArtBlocks/node-artblocks/tree/main.svg?style=svg)](https://circleci.com/gh/ArtBlocks/node-artblocks/tree/main) [![Node.js Package](https://github.com/ArtBlocks/node-artblocks/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/ArtBlocks/node-artblocks/actions/workflows/npm-publish.yml) +![GitHub package.json version](https://img.shields.io/github/package-json/v/artblocks/node-artblocks?color=blue&label=development) An open source [node package](https://www.npmjs.com/package/artblocks) for reading on-chain Art Blocks data and recreating generative art projects. By default the package reads data via the [ArtBlocks Subgraph](https://thegraph.com/explorer/subgraph/artblocks/art-blocks). For those using other languages, use this package as a how-to guide for working directly with ArtBlocks on-chain data. @@ -180,3 +181,48 @@ Promise { ... } ``` + +### Custom Queries + +```javascript +let x = ` +{ + projects( + first: 5, + orderBy: projectId, + orderDirection: desc, + where: {curationStatus: "curated"} + ) + { + projectId + name + artistName + curationStatus + } +} +` +``` + +```javascript +const response = artblocks.custom(x) +``` + +```javascript +Promise { + projects: [ + { + projectId: '225', + name: 'Vortex', + artistName: 'Jen Stark', + curationStatus: 'curated' + }, + { + projectId: '215', + name: 'Gazers', + artistName: 'Matt Kane', + curationStatus: 'curated' + }, + ... + ] +} +``` \ No newline at end of file diff --git a/package.json b/package.json index 0f3aba9..5cda254 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "artblocks", - "version": "0.1.0", + "version": "0.1.1", "description": "A package for fetching on-chain ArtBlocks data.", "main": "src/index.js", "type": "module", diff --git a/src/filter.js b/src/filter.js deleted file mode 100644 index 1cc7dec..0000000 --- a/src/filter.js +++ /dev/null @@ -1,12 +0,0 @@ -function projects_by_contracts(projects, contracts) { - return projects.filter(project => contracts.includes(project.contract.id)) -} - -function tokens_by_contracts(tokens, contracts) { - return tokens.filter(token => contracts.includes(token.project.contract.id)) -} - -export default { - projects_by_contracts, - tokens_by_contracts -} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 8586faf..d495539 100644 --- a/src/index.js +++ b/src/index.js @@ -52,10 +52,15 @@ class ArtBlocks { this.api = api this.network = network - this.contracts = contracts + this.contracts = contracts.map(x => x.toLowerCase()) this.pbab = pbab } + // Query anything at all + custom(x) { + return query.custom(x, this.subgraph) + } + // Query all available projects projects() { return query.projects(this.subgraph, this.contracts, this.pbab) diff --git a/src/query.js b/src/query.js index 339651d..5f35fda 100644 --- a/src/query.js +++ b/src/query.js @@ -2,14 +2,18 @@ import utils from './utils.js' import graph from './graph.js' import build from './build.js' import hashes from './hashes.js' -import filter from './filter.js' import script from './script.js' +// Query anything at all +async function custom(x, subgraph) { + return await graph.artblocks_subgraph(x, subgraph) +} + // Query all available projects async function projects(subgraph, contracts, pbab) { let { projects } = await graph.artblocks_subgraph( `{ - projects(first: 1000, orderBy: projectId) { + projects(first: 1000, orderBy: projectId, where: {contract_in: ${utils.arr_to_str(contracts)}} ) { projectId name artistName @@ -20,7 +24,6 @@ async function projects(subgraph, contracts, pbab) { } `, subgraph) - projects = filter.projects_by_contracts(projects, contracts) let data = [] for (let project of projects) { data.push({ @@ -37,7 +40,7 @@ async function projects(subgraph, contracts, pbab) { async function project_metadata(id, subgraph, contracts, pbab) { let { projects } = await graph.artblocks_subgraph( `{ - projects(where: { projectId: "${id}" }) { + projects(where: {projectId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) { projectId name artistName @@ -60,7 +63,7 @@ async function project_metadata(id, subgraph, contracts, pbab) { `, subgraph) let data = {} - let project = filter.projects_by_contracts(projects, contracts)[0] + let project = projects[0] data.id = parseInt(project.projectId, 10) data.name = project.name data.artist = project.artistName @@ -83,7 +86,7 @@ async function project_metadata(id, subgraph, contracts, pbab) { async function project_script(id, subgraph, contracts, pbab) { let { projects } = await graph.artblocks_subgraph( `{ - projects(where: { projectId: "${id}" }) { + projects(where: {projectId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) { projectId name scriptUpdatedAt @@ -97,7 +100,7 @@ async function project_script(id, subgraph, contracts, pbab) { `, subgraph) let data = {} - let project = filter.projects_by_contracts(projects, contracts)[0] + let project = projects[0] let script_json = script.parse_json(project.scriptJSON, pbab) data.id = parseInt(project.projectId, 10) data.name = project.name @@ -111,7 +114,7 @@ async function project_script(id, subgraph, contracts, pbab) { async function token_metadata(id, subgraph, contracts, pbab) { let { tokens } = await graph.artblocks_subgraph( `{ - tokens(where: { tokenId: "${id}" }) { + tokens(where: {tokenId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) { project { projectId name @@ -127,7 +130,7 @@ async function token_metadata(id, subgraph, contracts, pbab) { `, subgraph) let data = {} - let token = filter.tokens_by_contracts(tokens, contracts)[0] + let token = tokens[0] data.project_id = parseInt(token.project.projectId, 10) data.project_name = token.project.name data.token_id = parseInt(token.tokenId, 10) @@ -140,7 +143,7 @@ async function token_metadata(id, subgraph, contracts, pbab) { async function token_script(id, subgraph, contracts, pbab) { let { tokens } = await graph.artblocks_subgraph( `{ - tokens(where: { tokenId: "${id}"}) { + tokens(where: {tokenId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) { project { projectId scriptJSON @@ -157,7 +160,7 @@ async function token_script(id, subgraph, contracts, pbab) { `, subgraph) let data = {} - let token = filter.tokens_by_contracts(tokens, contracts)[0] + let token = tokens[0] let script_json = script.parse_json(token.project.scriptJSON, pbab) data.token_id = parseInt(token.tokenId, 10) data.token_invocation = parseInt(token.invocation, 10) @@ -175,7 +178,7 @@ async function token_script(id, subgraph, contracts, pbab) { async function token_generator(id, subgraph, contracts, pbab) { let { tokens } = await graph.artblocks_subgraph( `{ - tokens(where: { tokenId: "${id}"}) { + tokens(where: {tokenId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) { project { scriptJSON script @@ -189,7 +192,7 @@ async function token_generator(id, subgraph, contracts, pbab) { } `, subgraph) - let token = filter.tokens_by_contracts(tokens, contracts)[0] + let token = tokens[0] let token_data = hashes.hash(token.project.contract.id, token.tokenId, token.hash) let script_json = script.parse_json(token.project.scriptJSON, pbab) let dependency = script_json.dependency @@ -199,6 +202,7 @@ async function token_generator(id, subgraph, contracts, pbab) { } export default { + custom, projects, project_metadata, project_script, diff --git a/src/utils.js b/src/utils.js index 2815ac6..00283bf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,7 @@ +function arr_to_str(arr) { + return "["+arr.map(x => '"'+x+'"').join(",")+"]" +} + function mul_by_dec(x, decimals=18) { return x*Math.pow(10, decimals) } @@ -15,6 +19,7 @@ function eth_to_wei(x) { } export default { + arr_to_str, mul_by_dec, div_by_dec, wei_to_eth, diff --git a/test/index.js b/test/index.js index 8f2964d..c07d0a9 100644 --- a/test/index.js +++ b/test/index.js @@ -89,7 +89,6 @@ describe("Ropsten", function() { return artblocks.projects().then(function(x) { expect(x.length).to.be.above(1) expect(x[1].id).to.equal(4) - expect(x[1].name).to.equal("Reflection") expect(x[1].artist).to.equal("Jeff Davis") expect(x[1].contract).to.equal("0x1cd623a86751d4c4f20c96000fec763941f098a2") }) @@ -100,7 +99,6 @@ describe("Ropsten", function() { it("should return metadata for a given project", function() { return artblocks.project_metadata(4).then(function(x) { expect(x.id).to.equal(4) - expect(x.name).to.equal("Reflection") expect(x.artist).to.equal("Jeff Davis") expect(x.contract).to.equal("0x1cd623a86751d4c4f20c96000fec763941f098a2") })