Smart contract interaction
In the previous chapter we've already been introduced to the 2 main smart contracts Event
and Central Oracle
. In this chapter we're going to see how to interact with them and how they interact with each other. Have a quick glance at the diagram below and read on the learn more about the ins and outs!
The diagram displays 2 smart contracts. Event
on the left and Central Oracle
on the right. The available operations of the smart contracts are displayed in their respective bodies. Bold operations, as we will see next, are the main operations needed for running a speculation event. Operations in italic are helper functions and we could technically do without them.
Usage flow
- User registers an event by calling the register operation. This operation requires various arguments such as what NEP5 Token can be used for placing bets, what oracle to use and who the event owner is that can resolve the event among others. The contract generates a unique event_id that will be needed for all future interactions with this event.
- Once the
Event
contract did it's internal registration it notifies theCentral Oracle
that it wants to register an event that can only be solved via him. The Central Oracle receives an event_id, the event owner script hash and an end time that must expire before the event can be resolved. - After successful registration users can place as many predictions as they want for as long as the time window for placing prediction is open. The prediction window closes once the event starts. However, before they place a prediction they need to approve funds to be transferred from their account to the
Event
account. This is done to make sure that users can't withdraw their bets once the event is ongoing and they see the outcome not going their way. - Predictions can only be made following the possible outcomes as registered with the event. Predictions are also limited to NEP5Token indicated by during registration of the event.
- Once the initial parameter validation is done the
Event
contract will secure the funds by calling thetransferFrom
method on the NEP5 Token. Step 3-5 can be repeated as long as the betting window is open. - Only after
event end time
has expired can the event owner register the final outcome. TheCentral Oracle
validates that the caller is the correct owner registered to the event_id. - Finally, the
Central Oracle
contract calls theEvent
contract to let it know what the final outcome is. TheEvent
contract calculates the reward for each winning bet and does a final call to the NEP5 TOken contract totransfer
the winnings to each winning bettor. The results are then stored on the blockchain by both theEvent
andCentral Oracle
contracts to conclude.