Exceeded the prepaid gas error while doing cross contract calls

Viewed 431

This is my code for cross contract calls:

const NO_DEPOSIT: Balance = 0;
const GAS: Gas = 25_000_000_000_000; 

#[ext_contract(ext_erc20)]
pub trait ExtErc20 {
    fn get_total_supply(&self) -> U128;
}

Function call:

pub fn get_total_supply_erc20(&self, erc20_id: AccountId) -> Promise {
    ext_erc20::get_total_supply(&erc20_id, NO_DEPOSIT, GAS)
}

Javascript code:

const data = await nearvar.contract.get_total_supply_erc20({"erc20_id": "dev-1608541613194-2197959"})
console.log(data)

It gives "Exceeded the prepaid gas" error. I also tried with different gas values, but it didn't work.

Fungible token, get total supply:

pub fn get_total_supply(&self) -> U128 {
    self.total_supply.into()
}

Code files

1 Answers

Check this please: I would need to create a working example to be sure, but off the top of my head I think the problem is the following:

Be aware that you can get a "Exceeded the prepaid gas" error, with almost no execution. Let's say you make the call from the client to get_total_supply_erc20 with 50TGas limit, and in the called fn code you have:

const GAS = 51*TGAS;
pub fn get_total_supply_erc20(&self, erc20_id: AccountId) -> Promise {
    ext_erc20::get_total_supply(&erc20_id, NO_DEPOSIT, GAS)
}

You'll get an "Exceeded the prepaid gas" error, but not because consumption, it will be because the Promise engine can't reserve 51*Tgas for the promise call because the client-call has only 50Tgas limit. It does not matter if the real consumption is 2 or 3 TGas for both functions.

Related