Skip to content

mediafoundation/backend-example

Repository files navigation

The backend-example package is a simple example for retrieve and store data from the blockchain using the media-sdk

Installation

git clone https://github.com/mediafoundation/backend-example.git

cp .env.example .env

#optional
cp networks.example.ts networks.ts

npm install

Configuration

.env

# Replace all fields with your own values in .env file

dbName=DB_NAME
dbUser=DB_USER
dbPassword=DB_PASSWORD
dbHost=DB_HOST
dbPort=3306
dbDialect=DB_DIALECT

networks (optional)

If non provided, default rpc from media-sdk will be used

# Replace all fields with your own values in networks.ts file
export const wssNetworks: {[index: string] : string[]} = {
  "NETWORK_1_NAME": ["WSS_RPC_URL"],
  "NETWORK_2_NAME": ["WSS_RPC_URL"],
}

export const httpNetworks : {[index: string] : string[]}= {
  "NETWORK_1_NAME": ["WSS_RPC_URL"],
  "NETWORK_2_NAME": ["WSS_RPC_URL"],
}

Usage

Retrieve all data from the blockchain and store it into the database

npm run init

This command retrieves all data from the blockchain and stores it into the configured database.

Execute the api (npm run init must be executed before this command)

npm run startApi

After running this command, you will see a message indicating that the API server has started successfully.

Using the api

Deals

Get all deals

curl -G -i http://localhost:5000/deals

Get deal by id and chainId

curl -G -i http://localhost:5000/deals/1/chainId/11155111

Get deals with filtered by metadata, for more information about filters see api filters documentation

curl -G -i 'http://localhost:5000/deals' --data-urlencode 'chainId=11155111' --data-urlencode 'filter={"metadataFilter": {"burstSpeed": {"gt": 100}}}'

Get deals with filtered by nodeLocation and pagination

curl -G -i 'http://localhost:5000/deals' --data-urlencode 'chainId=11155111' --data-urlencode 'filter={"nodeLocationFilter": {"location": {"eq": "BR"}}}' --data-urlencode 'pageSize=10'

Filters

Schema and usages

There are four filters that can be used with deals and offers, for more information see models documentation:

  • metadataFilter
  • nodeLocationFilter
  • bandwidthFilter
  • genericFilter

They are meant to be used following the filter schema from sequelize, for more information about the filter schema see sequelize documentation

Also, every field from which the filter is going to be applied must be a field from the deal or offer model, and its associated tables. They are all defined in the models' folder.

The following example shows how to order the filters in the query:

{
  "metadataFilter": {
    "burstSpeed": {
      "gt": 100
    }
  },
  "nodeLocationFilter": {
    "location": {
      "eq": "BR"
    }
  },
  "bandwidthFilter": {
    "amount": {
      "gt": 100
    }
  },
  "genericFilter": {
    "maximumDeals": {
      "eq": 2000
    }
  }
}
curl -G -i 'http://localhost:5000/offers?' --data-urlencode 'chainId=11155111' --data-urlencode 'filters={"metadataFilter": {"burstSpeed": {"gt": 100}}, "nodeLocationFilter": {"location": {"eq": "BR"}}, "bandwidthFilter": {"amount": {"eq": 1}}, "genericFilter": {"maximumDeals": {"gt": 100}}}'

Take into account that the filters are optional and can be used together or separately.

Pagination

Pagination can be performed by passing page and pageSize params on the request. It can be used alongside the filters

curl -G -i 'http://localhost:5000/offers?' --data-urlencode 'chainId=11155111' --data-urlencode 'page=1' --data-urlencode 'pageSize=10'

Detailed information

For more information, refer to the API documentation

Customization

Find detailed instructions on customizing this example to suit your needs in the CUSTOMIZATION.md

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published