How to load abi inline in a generic way with abigen?

30 Views Asked by At

I want to create a util function to instantiate a contract with an ABI in a generic way rather than copy pasting code around. And I cannot find a way for the compiler to accept it.

fn instantiate_contract(contract_address_str:&str ,abi_file:&str,client:Provider<Http> ) -> eyre::Result<Value, Box<dyn std::error::Error>> {
    let contract_address : Address = contract_address_str.parse()?;
    abigen!(CONTRACT, format("./abis/{}",abi_file));
    let contract=CONTRACT::new(contract_address, Arc::new(client));
    Ok(contract)
}

I am getting a use of undeclared type CONTRACT`` error. I think this could be because abigen only supports full path as it needs to check it at compile time?

Now assuming I am passing a determined path:

fn instantiate_contract(contract_address_str:&str ,abi_file:&str,client:Provider<Http> ) -> eyre::Result<Value, Box<dyn std::error::Error>> {
    let contract_address : Address = contract_address_str.parse()?;
    abigen!(CONTRACT, "./abis/AAVE2.json",);
    let contract=CONTRACT::new(contract_address, Arc::new(client));
    Ok(contract)
}

I am getting an error on the returned type: expected Value, found CONTRACT<Provider>`` The issue is that I cannot use CONTRACT as return type as it is defined via the abigen within the function, how to solve this?

0

There are 0 best solutions below