Skip to content

Commit

Permalink
Merge pull request #8 from ArtBlocks/robust-filtering
Browse files Browse the repository at this point in the history
Robust contract filtering and fixes
  • Loading branch information
jakerockland authored Feb 28, 2022
2 parents dfcfec6 + b725c3c commit 2623957
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 30 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ coverage
.DS_Store
.directory
.nfs*
.fuse_hidden*
.fuse_hidden*

# Custom
dev/
package-lock.json
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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'
},
...
]
}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 0 additions & 12 deletions src/filter.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 17 additions & 13 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -199,6 +202,7 @@ async function token_generator(id, subgraph, contracts, pbab) {
}

export default {
custom,
projects,
project_metadata,
project_script,
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -15,6 +19,7 @@ function eth_to_wei(x) {
}

export default {
arr_to_str,
mul_by_dec,
div_by_dec,
wei_to_eth,
Expand Down
2 changes: 0 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand All @@ -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")
})
Expand Down

0 comments on commit 2623957

Please sign in to comment.