AppLayerLabs / bdk-cpp

MIT License
7 stars 12 forks source link

#50 Implement robust return types for contract functions #51

Closed jcarraror closed 1 year ago

jcarraror commented 1 year ago

Tasks

Description:

This PR basically change these points:

  1. Now it's possible to implement functions that returns any ABI supported type;

  2. Calling view functions from another contracts it's more simple now:

  1. Now, the ABI JSON generator generates the ABI for ContractManager as well, based on the contracts that are loaded on it. Also, we can pass the ContractTypes tuple in the generator now:

int main() { return ContractReflectionInterface::writeContractsToJson<ERC20, ERC20Wrapper, NativeWrapper>(); }


- New way:

```c++
#include "utils/jsonabi.h"

int main() {
    return JsonAbi::writeContractsToJson<ContractTypes>(); //ContractTypes have all the developed dynamic contracts
}
  1. A dynamic contract can call non-view functions from another contract without encoding data.
  1. A dynamic contract can create another dynamic contract. You can have a function that creates another contract and retrieves it address:

    ...
    //just pass the ContractType that you want to create, the caller address, gas value, gas price value, caller value an the contract constructor parameters
    Address contract = this->callCreateContract<ERC20>(this->getContractAddress(), 0, 0, 0, "TestToken", "TST", 18, 1000000000000000000);
    ...
  2. If we want to use a proper C++ return in a dynamic contract view function, we can just return the type, without encoding:

std::string name() const;

std::string ERC20::name() const { return this->_name.get(); }
  1. If we want to encode manually the return data, we need to use BytesEncoded type. BytesEncoded it's a wrapper for Bytes, this way, we can return pure Bytes or encoded Bytes:
BytesEncoded ERC20::name() const {
    Bytes data = ABI::Encoder({this->_name.get()}).getData();
    return BytesEncoded{data};
}