Ethereum: How to process multiple stream data from binance websocket?
Road traffic data processing from Binance Websocket with Ethereum
As a cryptocurrency enthusiast, you probably experienced excitement when direct market prices fluctuate. In your case, you are using UNICORN_BINANCE_WEBSOCKT_API to pass 100 crypt prices and two different periods at a time. You will need to use a little more complicated to process this data and store these data and store the prices of various cryptocurrencies.
In this article, we will investigate how to achieve this using Ethereum Smart.
Conditions necessary
Before you dive into the code, make sure you have:
1
- UNICORN_BINANCE_WEBSOCKET_API : Install and configure the Unicorn Binance Websocket API to broadcast data about 100 crypts and two different periods.
Step 1: Define the data model
First, let’s define the main data model that will retain other prices of various cryptocurrencies. Create a new contract using Solid (Ethereum programming language).
`Solidarity
Pragma solidity ^0.8.0;
The proximity of the contract {
strucct cryptodata {
address token; // Cryptocurrency Access Key
UINT256 Time Tag; // time tag when data was obtained
Uint256 price; // Closed cryptocurrency price
UINT256 TIMestamp2; // Time Tag when second data (optional) was obtained
}
Map formation (address => cryptodata) public closenessprices;
}
`
Step 2: Initiate a contract
Create a new copy of the contract and start it with an empty map to save the closed prices.
`Solidarity
Pragma solidity ^0.8.0;
The proximity of the contract {
Closeprices Private _instance; // a copy of the contract
Function Init () publicly payable {
_instance = closeprices ();
}
Map formation (address => cryptodata) public closenessprices;
}
`
Step 3: Subscribe to Binance Websocket
Use the Unicorn Binance Websocket API to determine the price subscription. Make sure you handle errors and exceptions properly.
`Solidarity
Pragma solidity ^0.8.0;
The proximity of the contract {
Closeprices Private _instance; // a copy of the contract
Function Init () publicly payable {
_instance = closeprices ();
}
Map formation (address => cryptodata) public closenessprices;
Newprice event (address indexed token, Uint256 price);
Function Subscribe to () Public Due to {
// Subscribe to Binance Websocket to get price data
Try (new unicorns.binancewebsocketeapi (your_api_key, your_api_secret)) {
_instance.subscribe (
Address (this), // Contract that signed an event
Closeprices.newprice, // Event Name
Function (address token, Uint256 price) public {
Release Newprice (Access Key, Price);
}
);
} catch (error) {
return the mistake;
}
}
// Optional: Set the maximum number of data points to keep you
Functions setmaxdatapoints (Uint256 _maxdatapoints) public {
Closeprices [MSG.Sender] .Claprices = New Uint256 [_maxdatapoints];
}
}
`
Step 4: Process data
Create features in your contract to process the price data you receive. For example, if necessary, you can update maps or exhaust events.
`Solidarity
Pragma solidity ^0.8.