BLOG — Developer Tutorials

500 days ago

Ensuring Data Continuity for Your Web3 App with API3 & Gelato

The classic oracle problem of blockchain revolves around how blockchain protocols can ingest data from third-party sources in a reliable, trusted way. Since web3 apps and smart contracts are highly networked and automatic, corrupt or invalid data ingestions can create huge consequences for users when they do occur.

API3 is a solution that promotes the development of 'decentralized APIs' (dAPIs), where third-party service providers operate their own oracle that can write data directly to a blockchain. These oracles are governed by the API3 DAO.

Self-Funded dAPIs

When it comes to Self-Funded dAPIs, as a developer you can use data feeds in a permissionless manner and with minimal up-front commitment.

You are required to fund the dAPI with your own funds, and the amount of gas you supply will determine how long your dAPI will be available.

These funds are used by the first-party oracle, that supports a particular data feed, to pay gas costs when placing the value of the data feed on-chain.

Gelato Automate for Business Continuity

Gelato Automate is a reliable, developer-friendly & decentralized smart contract automation that helps developers to set recurrent actions when predefined conditions are met.

When minimizing the up-front commitment to ensure maximal capital efficiency, to avoid running out of funds Gelato Automate provides the ability to auto-top up your Self-Funded dAPI.

A step-by-step implementation on how to implement Gelato Automate to automate Self-Funded dAPIs can be found in the API3 docs under 'Automate funding of multiple Self-Funded dAPIs with Gelato'.

Now let’s dive into the code (aka where the magic happens!)

 // @notice Check if sponsor wallet balance is less than minimum balance
    // @return canExec Boolean value to check if task can be executed
    // @return execPayload Payload to be executed
    function checkSponsorWallet() external view returns(bool canExec,  bytes memory execPayload) {
        uint unfunded = 0;
        canExec = false;
        for(uint i=0;i<sponsorWallets.length;i++){
            if(sponsorWallets[i].balance < minimumBalance) {
                unfunded = unfunded + 1;
            }
        }
        address[] memory unfundedSponsorWallets = new address[](unfunded);
        uint counter = 0;
        for(uint j=0; j<sponsorWallets.length;j++){
            if(sponsorWallets[j].balance < minimumBalance) {
                unfundedSponsorWallets[counter]=sponsorWallets[j];
                counter=counter+1;
            }
        }
        execPayload =  abi.encodeWithSignature("fundSponsorWallet(address[],uint256)",unfundedSponsorWallets,fundAmount);
        if(unfundedSponsorWallets.length > 0){
            canExec = true;
        }
    }

There are 3 steps involved in this function:

  1. Check the total number of sponsor wallets where the balance is less than a predefined minimumBalance
for(uint i=0;i<sponsorWallets.length;i++){
            if(sponsorWallets[i].balance < minimumBalance) {
                unfunded = unfunded + 1;
            }
        }
  1. Create an array with the sponsor wallets
address[] memory unfundedSponsorWallets = new address[](unfunded);
        uint counter = 0;
        for(uint j=0; j<sponsorWallets.length;j++){
            if(sponsorWallets[j].balance < minimumBalance) {
                unfundedSponsorWallets[counter]=sponsorWallets[j];
                counter=counter+1;
            }
        }
  1. Send the transaction on-chain to fund the required wallets
execPayload =  abi.encodeWithSignature("fundSponsorWallet(address[],uint256)",unfundedSponsorWallets,fundAmount);
        if(unfundedSponsorWallets.length > 0){
            canExec = true;
        }

With these simple lines of code you’re all set up to auto top up your Self-Funded dAPI ensuring business continuity and data quality using API3 and Gelato Automate.

Conclusion

By leveraging the power of API3 and Gelato Automate, developers can streamline the process of obtaining reliable data feeds, enhance the trustworthiness of their applications, and promote a more secure and efficient ecosystem within the blockchain industry.

Join our community and developer discussion on Discord.