Search code examples
oracle-databaseapisoliditychainlink

How to use arrays returned by oracle after making API calls inside the smart contract?


I am building a staking dapp where a user will get staking rewards after staking the custom ERC20 tokens for a specific period of time. In the contract I want to reward the users those who refers there friends into the staking website by providing a percentage of the amount staked by there referred person. The referral system will have five levels and rewards according to those levels.

The rewards are :-

Lvl 1 = 7% Referral deposit          
Lvl 2 = 3% Referral deposit          
Lvl 3 = 2% Referral deposit    
Lvl 4 = 1% Referral deposit          
Lvl 5 = 0.5% Referral deposit

For example - if someone joins through my referral then I become his/her Level 1 referrer and the person who referred me becomes the level 2 referrer and the person who referred the level 2 referrer becomes the level 3 referrer and this continues till level 5.

I am keeping a database and using an api in order to keep track of which address referred whom. This is the format to send data to the database, the referrer field is optional.

{
"type": "register",

"address":"0x78910",

"referrer":"0x12345",

"coin":20
}

In the frontend I am keeping an input field where the users will put there referrer's address and that would go to the database and then the smart contract will make a call to the database through oracles and get all the addresses and there levels and then the contract will pay them accordingly.

My question is how can I make this happen? I know using oracles is the way but I couldn't understand how to implement it in the code to make it work. Can anyone help me??


Solution

  • Chailink AnyAPI allows you to fetch any data from off-chain API and write the data back to on-chain smart contract.

    To use Chainlink AnyAPI, you should run a Chainlink node(you can find how to run a Chainlink node here) or rely on oracle nodes run by others(read more details here).

    With the Chainlink node, you are able to deploy an oracle contract and monitor requests from the smart contract(your dApp). Request from your smart contract can trigger the Chainlink node to finish its job and write back data to the smart contract. In your dApp, the smart contract can send a request to your database and get the referrer info with Chainlink Any API.

    A full tutorial can be found here.