Skip to content

Commit

Permalink
Merge pull request #13 from ArtBlocks/flex-support
Browse files Browse the repository at this point in the history
Flex support
  • Loading branch information
jakerockland authored Nov 1, 2022
2 parents 336a522 + 6a769d9 commit ba6e1bb
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 60 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ npm i artblocks
import ArtBlocks from 'artblocks'

// Ethereum mainnet
let artblocks = new ArtBlocks("thegraph", "mainnet");
let artblocks = new ArtBlocks("thegraph", "mainnet")

// Artist staging projects
// Please refrain from sharing these projects publicly
let artblocks = new ArtBlocks("thegraph", "ropsten");
let artblocks = new ArtBlocks("thegraph", "goerli")

// Flex contracts
let artblocks = new ArtBlocks("thegraph", "goerli", contracts=["<contract_address>"] flex=true)
```

## Methods
Expand Down
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.2",
"version": "0.1.4",
"description": "A package for fetching on-chain ArtBlocks data.",
"main": "src/index.js",
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions src/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import templates from './templates.js'

function template(dependency, dependency_url, project_script, token_data) {
token_data = `let tokenData = ${JSON.stringify(token_data)}`
switch(dependency) {
case "js":
return templates.js(token_data, project_script)
Expand Down
22 changes: 0 additions & 22 deletions src/hashes.js

This file was deleted.

16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ArtBlocks {
constructor(api="thegraph",
network="mainnet",
contracts=[],
pbab=false,
flex=false,
subgraph="") {

// TODO
Expand Down Expand Up @@ -53,7 +53,7 @@ class ArtBlocks {
this.api = api
this.network = network
this.contracts = contracts.map(x => x.toLowerCase())
this.pbab = pbab
this.flex = flex
}

// Query anything at all
Expand All @@ -63,32 +63,32 @@ class ArtBlocks {

// Query all available projects
projects() {
return query.projects(this.subgraph, this.contracts, this.pbab)
return query.projects(this.subgraph, this.contracts)
}

// Query project metadata
project_metadata(id) {
return query.project_metadata(id, this.subgraph, this.contracts, this.pbab)
return query.project_metadata(id, this.subgraph, this.contracts)
}

// Query project raw script
project_script(id) {
return query.project_script(id, this.subgraph, this.contracts, this.pbab)
return query.project_script(id, this.subgraph, this.contracts)
}

// Query token metadata
token_metadata(id) {
return query.token_metadata(id, this.subgraph, this.contracts, this.pbab)
return query.token_metadata(id, this.subgraph, this.contracts)
}

// Query token raw script with hash and dependency tags
token_script(id) {
return query.token_script(id, this.subgraph, this.contracts, this.pbab)
return query.token_script(id, this.subgraph, this.contracts, this.flex)
}

// Query token generator html file
token_generator(id) {
return query.token_generator(id, this.subgraph, this.contracts, this.pbab)
return query.token_generator(id, this.subgraph, this.contracts, this.flex)
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
There is a discrepancy between the way hashes are passed to projects
Contracts
V0 - { hashes: [hash_1, hash_2], ...}
V1 - { hash: hash_1, ...}
The first version of the contract anticipated the use of an array of multiple token
hashes for each token id to increase the available entropy for a script to utilize.
The latest version of the contract issues a single hash per token id.
This may change again with future contract versions
*/
function token_data(contract, hash, token_id, flex=false, preferred_ipfs_gateway=null, preferred_arweave_gateway=null, external_asset_dependencies=null) {
if (flex) {
return {
hash: hash,
tokenId: token_id,
preferredIPFSGateway: preferred_ipfs_gateway === null ? "https://ipfs.io/ipfs/" : preferred_ipfs_gateway,
preferredArweaveGateway: preferred_arweave_gateway=== null ? "https://arweave.net/" : preferred_arweave_gateway,
externalAssetDependencies: external_asset_dependencies
}
}
else if (contract == "0x059edd72cd353df5106d2b9cc5ab83a52287ac3a") {
return {
hashes: [hash],
tokenId: token_id
}
}
else {
return {
hash: hash,
tokenId: token_id
}
}
}

export default { token_data }
52 changes: 40 additions & 12 deletions src/query.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import utils from './utils.js'
import graph from './graph.js'
import build from './build.js'
import hashes from './hashes.js'
import metadata from './metadata.js'
import script from './script.js'

// Query anything at all
Expand All @@ -10,7 +10,7 @@ async function custom(x, subgraph) {
}

// Query all available projects
async function projects(subgraph, contracts, pbab) {
async function projects(subgraph, contracts) {
let { projects } = await graph.artblocks_subgraph(
`{
projects(first: 1000, orderBy: projectId, where: {contract_in: ${utils.arr_to_str(contracts)}} ) {
Expand All @@ -37,7 +37,7 @@ async function projects(subgraph, contracts, pbab) {
}

// Query project metadata
async function project_metadata(id, subgraph, contracts, pbab) {
async function project_metadata(id, subgraph, contracts) {
let { projects } = await graph.artblocks_subgraph(
`{
projects(where: {projectId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) {
Expand Down Expand Up @@ -83,7 +83,7 @@ async function project_metadata(id, subgraph, contracts, pbab) {
}

// Query project raw script
async function project_script(id, subgraph, contracts, pbab) {
async function project_script(id, subgraph, contracts) {
let { projects } = await graph.artblocks_subgraph(
`{
projects(where: {projectId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) {
Expand All @@ -101,7 +101,7 @@ async function project_script(id, subgraph, contracts, pbab) {

let data = {}
let project = projects[0]
let script_json = script.parse_json(project.scriptJSON, pbab)
let script_json = script.parse_json(project.scriptJSON)
data.id = parseInt(project.projectId, 10)
data.name = project.name
data.last_updated = project.scriptUpdatedAt
Expand All @@ -111,7 +111,7 @@ async function project_script(id, subgraph, contracts, pbab) {
}

// Query token metadata
async function token_metadata(id, subgraph, contracts, pbab) {
async function token_metadata(id, subgraph, contracts) {
let { tokens } = await graph.artblocks_subgraph(
`{
tokens(where: {tokenId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) {
Expand Down Expand Up @@ -140,7 +140,7 @@ async function token_metadata(id, subgraph, contracts, pbab) {
}

// Query token raw script with hash and dependency tags
async function token_script(id, subgraph, contracts, pbab) {
async function token_script(id, subgraph, contracts, flex) {
let { tokens } = await graph.artblocks_subgraph(
`{
tokens(where: {tokenId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) {
Expand All @@ -150,6 +150,12 @@ async function token_script(id, subgraph, contracts, pbab) {
script
contract {
id
preferredIPFSGateway
preferredArweaveGateway
}
externalAssetDependencies {
cid
dependencyType
}
}
tokenId
Expand All @@ -161,21 +167,29 @@ async function token_script(id, subgraph, contracts, pbab) {

let data = {}
let token = tokens[0]
let script_json = script.parse_json(token.project.scriptJSON, pbab)
let script_json = script.parse_json(token.project.scriptJSON)
data.token_id = parseInt(token.tokenId, 10)
data.token_invocation = parseInt(token.invocation, 10)
data.token_dependencies = {
dependency : script_json.dependency,
dependency_version : script_json.dependency_version,
dependency_url : script_json.dependency_url
}
data.token_data = hashes.hash(token.project.contract.id, token.tokenId, token.hash)
data.token_data = metadata.token_data(
token.project.contract.id,
token.hash,
token.tokenId,
flex,
token.project.contract.preferredIPFSGateway,
token.project.contract.preferredArweaveGateway,
token.project.externalAssetDependencies
)
data.token_script = token.project.script
return data
}

// Query token generator html file
async function token_generator(id, subgraph, contracts, pbab) {
async function token_generator(id, subgraph, contracts, flex) {
let { tokens } = await graph.artblocks_subgraph(
`{
tokens(where: {tokenId: "${id}", contract_in: ${utils.arr_to_str(contracts)}}) {
Expand All @@ -184,6 +198,12 @@ async function token_generator(id, subgraph, contracts, pbab) {
script
contract {
id
preferredIPFSGateway
preferredArweaveGateway
}
externalAssetDependencies {
cid
dependencyType
}
}
tokenId
Expand All @@ -193,8 +213,16 @@ async function token_generator(id, subgraph, contracts, pbab) {
`, subgraph)

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 token_data = metadata.token_data(
token.project.contract.id,
token.hash,
token.tokenId,
flex,
token.project.contract.preferredIPFSGateway,
token.project.contract.preferredArweaveGateway,
token.project.externalAssetDependencies
)
let script_json = script.parse_json(token.project.scriptJSON, flex)
let dependency = script_json.dependency
let dependency_url = script_json.dependency_url
let project_script = token.project.script
Expand Down
2 changes: 1 addition & 1 deletion src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function dependency_url(dependency) {
}

// Script json can vary between contract types
function parse_json(json, pbab=false) {
function parse_json(json) {
let x = JSON.parse(json)
return {
dependency : x.type,
Expand Down
Loading

0 comments on commit ba6e1bb

Please sign in to comment.