1

My necessity is to write a test for the following smart contract method (DeFi) in Hardhat using chai. It searches for a profitable token swap and returns the tokens. I want to use the artifact file to the test, before deploying the smart contract to the mainnet or testnet. One main reason is I cannot use HH console.log for debugging any errors in the contract.

My question is how can I simulate a router when writing the test in HH. Is forking an option? Any reading material or examples. I am relatively new for programming and cannot find any material related to this issue.

Code is as follows:

function search(address _router, address _asset, 
        uint _amount) external view returns (uint,address,address,address) {
            uint amtBack;
            address token1;
            address token2;
            address token3;

            for(uint i1=0;i1<tokens.length;i1++) {
                for(uint i2=0;i2<stables.length;i2++) {
                    for(uint i3=0;i3<tokens.length;i3++) {
                        amtBack = amount(_router,_asset,tokens[i1],_amount);
                        amtBack = amount(_router,tokens[i1],stables[i2],amtBack);
                        amtBack = amount(_router,stables[i2], tokens[i3],amtBack);
                        amtBack = amount(_router,tokens[i3], _asset,amtBack);    
                       
                        if(amtBack>_amount){
                            token1 = tokens[i1];
                            token2 = tokens[i2];       /
                            token3 = tokens[i3];
                            break;
                        }
                    }
                }
            }
         
            return (amtBack,token1,token2,token3);
        }
DeFi
  • 77
  • 1
  • 8

0 Answers0