How to Fetch Data from RedStone Providers on the Arweave Blockchain using GraphQL

Yoakin
3 min readOct 19, 2023

--

Steps to go through

Step 1

Install the arweave package from npm using the command npm install arweave. This will allow you to interact with the Arweave network from your Node.js application.

Step 2

Import the arweave package and create an instance of the Arweave class. You can use the default configuration or specify your own options. For example:

const Arweave = require('arweave');
const arweave = Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});

Step 3

Use the arweave.api.post method to send a graphql query to the Arweave graphql endpoint. You can use the example query provided by the user or modify it according to your needs. The query should return a list of transactions that match the specified tags, block number, and owner address. For example:

const query = `
{
transactions(
tags: [
{ name: "app", values: "Redstone" }
{ name: "type", values: "data" }
{ name: "Content-Type", values: "application/json"}
{ name: "version", values: "0.4" }
]
block: { min: 775000 }
owners: ["I-5rWUehEv-MjdK9gFw09RxfSLQX9DIHxG614Wf8qo0"]
first: 50
) {
edges {
node {
tags {
name
value
}
id
}
}
}
}
`;

arweave.api.post('graphql', { query }).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});

Step 4

The response data will contain an array of edges, each containing a node object with the transaction id and tags. You can iterate over this array and use the arweave.transactions.getData method to fetch the transaction data by passing the id as an argument. The data will be returned as a Uint8Array object, which you can convert to a string using the Buffer class. For example:

response.data.transactions.edges.forEach(edge => {
const id = edge.node.id;
const tags = edge.node.tags;
arweave.transactions.getData(id, { decode: true }).then(data => {
const dataString = Buffer.from(data).toString();
console.log(dataString);
}).catch(error => {
console.error(error);
});
});

Step 5

Since the transaction data is compressed using the gzip algorithm, you need to decompress it before using it. You can use the zlib module from Node.js to perform this task. You can use the zlib.gunzipSync method to synchronously decompress a buffer or the zlib.gunzip method to asynchronously decompress a buffer. For example:

const zlib = require('zlib');

// Synchronous decompression
response.data.transactions.edges.forEach(edge => {
const id = edge.node.id;
const tags = edge.node.tags;
arweave.transactions.getData(id, { decode: true }).then(data => {
const dataString = zlib.gunzipSync(data).toString();
console.log(dataString);
}).catch(error => {
console.error(error);
});
});

// Asynchronous decompression
response.data.transactions.edges.forEach(edge => {
const id = edge.node.id;
const tags = edge.node.tags;
arweave.transactions.getData(id, { decode: true }).then(data => {
zlib.gunzip(data, (error, result) => {
if (error) {
console.error(error);
} else {
const dataString = result.toString();
console.log(dataString);
}
});
}).catch(error => {
console.error(error);
});
});

Step 6

After decompressing the transaction data, you can parse it as a JSON object and use it for your application logic. The data will contain information about the RedStone providers and their prices for various assets and symbols. For example:

response.data.transactions.edges.forEach(edge => {
const id = edge.node.id;
const tags = edge.node.tags;
arweave.transactions.getData(id, { decode: true }).then(data => {
const dataString = zlib.gunzipSync(data).toString();
const dataObject = JSON.parse(dataString);
console.log(dataObject);
}).catch(error => {
console.error(error);
});
});

You can learn more about fetching data from the Arweave blockchain at [this link]

Thanks this is all for today see you next time

Please consider joining us with this links

Discord

Twitter

official website

github

--

--