AppLayerLabs / bdk-cpp

MIT License
7 stars 12 forks source link

Test Suite Implementation #85

Closed itamarcps closed 8 months ago

itamarcps commented 9 months ago
SECTION("ERC20 Class Constructor") {
      Address erc20Address;
      {
        std::unique_ptr<Options> options;
        std::unique_ptr<DB> db;
        std::unique_ptr<ContractManager> contractManager;
        std::string dbName = testDumpPath + "/erc20ClassConstructor";
        std::string tokenName = "TestToken";
        std::string tokenSymbol = "TST";
        uint8_t tokenDecimals = 18;
        uint256_t tokenSupply = 1000000000000000000;
        initialize(options, db, contractManager, dbName, ownerPrivKey, tokenName, tokenSymbol, tokenDecimals, tokenSupply);

        erc20Address = contractManager->getContracts()[0].second;

        Bytes nameEncoder = Bytes(32, 0);
        Bytes symbolEncoder = Bytes(32, 0);
        Bytes decimalsEncoder = Bytes(32, 0);
        Bytes totalSupplyEncoder = Bytes(32, 0);

        Functor nameFunctor = ABI::FunctorEncoder::encode<void>("name");
        Functor symbolFunctor = ABI::FunctorEncoder::encode<void>("symbol");
        Functor decimalsFunctor = ABI::FunctorEncoder::encode<void>("decimals");
        Functor totalSupplyFunctor = ABI::FunctorEncoder::encode<void>("totalSupply");

        Bytes nameData = contractManager->callContract(buildCallInfo(erc20Address, nameFunctor, nameEncoder));

        auto nameDecoder = ABI::Decoder::decodeData<std::string>(nameData);
        REQUIRE(std::get<0>(nameDecoder) == tokenName);

        Bytes symbolData = contractManager->callContract(buildCallInfo(erc20Address, symbolFunctor, symbolEncoder));

        auto symbolDecoderResult = ABI::Decoder::decodeData<std::string>(symbolData);
        REQUIRE(std::get<0>(symbolDecoderResult) == tokenSymbol);

        Bytes decimalsData = contractManager->callContract(buildCallInfo(erc20Address, decimalsFunctor, decimalsEncoder));

        auto decimalsDecoder = ABI::Decoder::decodeData<uint256_t>(decimalsData);
        REQUIRE(std::get<0>(decimalsDecoder) == 18);

        Bytes totalSupplyData = contractManager->callContract(buildCallInfo(erc20Address, totalSupplyFunctor, totalSupplyEncoder));

        auto totalSupplyDecoder = ABI::Decoder::decodeData<uint256_t>(totalSupplyData);
        REQUIRE(std::get<0>(totalSupplyDecoder) == tokenSupply);

        Bytes balanceOfData = contractManager->callContract(buildCallInfo(erc20Address, totalSupplyFunctor, totalSupplyEncoder));

        auto balanceOfDecoder = ABI::Decoder::decodeData<uint256_t>(balanceOfData);
        REQUIRE(std::get<0>(balanceOfDecoder) == tokenSupply);
      }
  TEST_CASE("ERC2O Class", "[contract][erc20]") {
    SECTION("ERC20 Class Transfer") {
      Address erc20Address;
      {
        auto targetOfTxs = Address::randomAddress(); // randomAddress doesn't exist, please assume it does.
        auto TestEngine = std::make_unique<ContractTestSuite>("dbPath");

        erc20Address = TestEngine->createNewContract<ERC20>("TestToken","TSTT", 18, uint256_t("1000000000000000000");
        uint256_t erc20Balance = TestEngine->callViewFunction(erc20Address, &ERC20::balanceOf, TestEngine->chainOwnerAddress());

        /// When calling a function use the chain owner address as the transaction sender by default
        Hash sendToTargetOfTxs = TestEngine->callFunction(&ERC20::transfer, targetOfTxs, uint256_t("10000000"));

      }

Implementing "ContractTestSuite" will modernize and simplify the contract testing process in OrbiterSDK, aligning it with best practices in blockchain development and making it more accessible to a broader range of developers.