-5

I am making a dapp on MultiverseX blockchain. It has a function where user can buy my ESDT by paying with EGLD.

    #[endpoint]
    #[payable("EGLD")]  
    fn purchase(&self, amount: BigUint){
        require!(amount >= 1000, "Purchase minimum 10 ACCEL");
        require!(amount <= 10000, "Maximum purchase limit is 10000 ACCEL")
        let caller = self.blockchain().get_caller();
        self.send().direct_esdt(&caller, &ACCEL-0fe2ec, &amount);  
        // self.send().direct_egld(&owner, )
    }

I want to apply some logic by which i can set price for my ESDT. One way is hardcoding, but I want to make it realistic. I want to use coinmarket so that it can fetch the price of EGLD in dollar and in the purchase() I would calculate the price of my token based on the value provided by coinmarket. Can I fetch the value of coinmarket using Rust? If yes then how? If no then please provide me some reference related to it. If there method is available except coinmarket please mention. Thank you.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
Neon
  • 33
  • 2
  • *"Can I fetch the value of coinmarket using Rust?"* - Questions like these are usually synonym to "Is there an API?" - The question is yes, it is here: https://coinmarketcap.com/api/. Whether or not it would cost you money, is another question. – Finomnis May 12 '23 at 16:06

1 Answers1

1

You are basically trying to acquire information into chain operations. Therefore, the smart contract should interact with some on-chain source for that specific information. You can't access APIs from inside the smart contract as the blockchain is a contained environment.

Prices are usually fetched from other smart contracts that can expose the data you need, such as:

  1. a relevant pair (such as EGLD-USDC pair contract) invoking their current price views/endpoints
  2. smart contract oracles that are specialized on-chain monitors for different real world data
Brother Jder
  • 415
  • 2
  • 9