The backend-example package is a simple example for retrieve and store data from the blockchain using the media-sdk
git clone https://github.com/mediafoundation/backend-example.git
cp .env.example .env
#optional
cp networks.example.ts networks.ts
npm install# 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_DIALECTIf 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"],
}npm run initThis command retrieves all data from the blockchain and stores it into the configured database.
npm run startApiAfter running this command, you will see a message indicating that the API server has started successfully.
curl -G -i http://localhost:5000/dealscurl -G -i http://localhost:5000/deals/1/chainId/11155111Get 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}}}'curl -G -i 'http://localhost:5000/deals' --data-urlencode 'chainId=11155111' --data-urlencode 'filter={"nodeLocationFilter": {"location": {"eq": "BR"}}}' --data-urlencode 'pageSize=10'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.
{
"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}}}'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'For more information, refer to the API documentation
Find detailed instructions on customizing this example to suit your needs in the CUSTOMIZATION.md