Implementation
Last updated
Last updated
Pool managers can execute transactions on whitelisted DeFi projects by specifying the external contract, encoding the function signature as bytes data, and passing the data as a parameter to a pool’s executeTransaction() function. The data gets verified by a ‘verifier’ contract for the specified external contract. The ‘verifier’ contract checks the function signature against whitelisted signatures and checks for valid parameters. This prevents pool managers from withdrawing funds from Tradegen or interacting with unsupported protocols.
Tradegen collects price data from an oracle provider (either Chainlink or Band) for a list of supported assets. Oracle providers update the on-chain price of a supported asset in the Price Aggregator contract once every 30 seconds. The price data for each asset is represented as a ‘Candlestick’ struct, with high price, low price, open price, and close price (volume might be added if the data is provided) for each 5-minute interval. Whenever the Price Aggregator receives an update, it checks the latest price against the Candlestick data and updates the struct accordingly.
At the end of each 5-minute interval, the Candlestick struct is marked as complete. Trading bots would then have 30 seconds to respond to the price data to remain valid. If the bot does not respond in time, minting will be disabled for the bot until the next interval and the keeper in charge of managing the bot will be penalized.
Each strategy has an on-chain trading bot that responds to updates from the bots dedicated keeper once per interval (determined by the bot’s timeframe). When the bot receives an update, it decides whether to enter/exit/avoid a trade by checking the latest oracle price of the target asset against the strategy’s entry/exit criteria. If the entry/exit criteria are met, the bot’s artificial order history is updated to reflect a simulated trade.
The bot is implemented as a state machine, storing the state of each entry/exit rule and updating the state when the price feed updates. Entry/exit rules have two indicators and a comparator, each with their own state that gets updated whenever the price feed updates.
The price of a trading bot’s token is calculated using its artificial order history and the oracle price of the bot’s traded asset.