Testing Transactions like swaps before execution

Just wondering if it is possible to test transactions via forking the chain or other methods to make sure that a swap will succeed.
Similar to what websites like https://rugdoc.io/honeypot/ do to tell you whether they were able to sell the coin or not. I know it is possible to fork the chain and tests on a certain contract but I cant find any resources on how to do this in code instead of terminal.
Any help would be much appreciated :slight_smile:

I think that it tests automatically when it tries to estimate the gas cost. And even if you test it at moment x, when the transaction will be processed the blockchain may have a different state from the other transactions that were processed meanwhile.

1 Like

I see. So do you think sites like that rugdoc just do small transactions to get their info?

I donโ€™t know exactly that they do, but you can run gas estimation for a transaction before sending it to the blockchain and you can find out if it will fail

1 Like

Oh damn it just clicked what you said. thats amazing. Iโ€™ve been doing small transactions this whole time. Thank you very much :slight_smile: I guess thats why when a transaction fails on ethers.js you mostly get โ€œcannot estimate gasโ€ errors

Oh wait but would I still have to buy the coin to do a gas estimation on selling it?

You can do that gas estimation with any parameters you want, you donโ€™t need to own the wallet

awesome. thank you very much

const provider = new ethers.providers.JsonRpcProvider("moralis https")

const wallet = ethers.Wallet.fromMnemonic(mnemonic);

const account = wallet.connect(provider);
const router = new ethers.Contract(
    'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)',
    'function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)',
    'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)',
tokencontract = new ethers.Contract(
const dec = await tokencontract.decimals();
const amounts = await router.getAmountsOut(ethers.utils.parseUnits("1", dec), [b_token, s_token]);
const amountOutMin = amounts[1].sub(amounts[1].div(div)); 
let tx = null;
tx = await router.estimateGas.swapExactTokensForTokens(
        [b_token, s_token],
        Math.floor(Date.now() / 1000) + 60 * 20

this is a shortened version of the code I use. I dont know if this is relevant to ask here so just let me know if not and I will remove.

When selling, I use this code but without the estimateGas modification (Also the amount used in GetAmountsOut is ofcourse calculated based off getBalance()). Where b_token is the token im selling and s_token is the token im receiving.

When doing the Estimate gas, how do I use a different wallet here? Iโ€™m not sure how to and I cant seem to find a way to do it without the wallet being for tests on a fork or testnet .

I usually send lower level raw data directly to node where I can change the address, I donโ€™t know exactly how you can change in that code

1 Like

Is it possible to do it through some moralis library functions? I will take a look around though