1

As a developer who wants to try out and possibly will want to adjust some functionality, how would I:

  • create a liquidity pool

  • create a farm pool

  • do a swap

  • add to / witdraw from liquity pool

...in Solidity itself. Not in JS or in Python as a user.

How to do that? Are there any comprehensive examples?

1

3 Answers 3

2

You'd need to do the following steps. You could also check out defi-minimal for more minimal examples of working with smart contracts.

We are going to use uniswap, but sushiswap has essentially the same functions.

1. Get the contract's interface

Get the Sushiswap/Uniswap ABI in your code by adding its interface including the functions you'd want to use. For swapping, it might look like this:

interface IUniswapV2Router {
    function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);
}

2. Get Contract addresses

Let's say you wait to work with Mainnet ETH with the Uniswap router.

// Uniswap Mainnet ETH Router: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D" 

Note: It's also good to know the WETH address since most tokens have a pool with WETH.

3. Build a swap function with input parameters

According to the uniswap abi / docs you need for input parameters:

uint256 amountIn, // amount of tokenA in
uint256 amountOutMin, // minimum amount of tokenB you're expecting back 
address[] calldata path, // the path of trading you expect. 
// Like what pools are you going to interact with? For example:
// USDC -> WETH -> SOME_OBSCURE_TOKEN
// or
// DAI -> WETH
// This requires an understand of how the pools work
address to, // who will recieve the tokens
uint256 deadline // how long is this trade good for

Then, you'd build an example func like so:

address private constant UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address private constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

function swap(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn,
        uint256 _amountOutMin,
        address _to
    ) external {
        IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn);
        IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn);

        address[] memory path;
        if (_tokenIn == WETH || _tokenOut == WETH) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
        } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = WETH;
            path[2] = _tokenOut;
        }

        IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactTokensForTokens(
            _amountIn,
            _amountOutMin,
            path,
            _to,
            block.timestamp
        );
    }

And your contract can now perform swaps.

1

You can interface with Smart Contracts by sending your request as JSON-RPC while connected to the Ethereum Network through any implementation of EVM.


Things can be further simplified by using development tools like Hardhat or Truffle.

In any case, here is a brief on how it happens:

  1. Instead of writing your own JSON-RPC request which can be daunting, you can connect to a provider through the use of: web3.js or ethers.js.
  2. Instead of connecting to a node directly or a wallet as a user, you can connect through a Node-as-a-Service provider. (like Alchemy and Infura).

For instance, using HardHat, install it via npm:

npm install --save-dev hardhat

Follow the instructions to install the dependency. Then run:

npx hardhat

Choose, "Create a sample project" then run:

npx hardhat run scripts/sample-script.js --network localhost

In the sample code (scripts/sample-script.js) you will find this two lines:

  const Greeter = await hre.ethers.getContractFactory("Greeter");
  const greeter = await Greeter.deploy("Hello, Hardhat!");

This is basically how you deploy a smart contract to the blockchain but since we are running it with --network localhost, we are deploying to a local virtual blockchain that was created by HardHat out of the box.

Which after deployed, you can execute a function of a smart contract via:

let txn = await greeter.functionInSmartContract(param1,param2);

This is if you have the Smart Contract source code, in which SushiSwap's contract code is available and they are also using HardHat. So you can either deploy it to a local blockchain and play around via the script or you can deploy it to a testnet (you will need an API URL from Alchemy or Infura and config in HardHat).

4
  • 1
    and how does that answer any of my questions?
    – arumichi
    Feb 22, 2022 at 17:33
  • Actually the first sentence already did, you can run a node on your own, figure out how to write and send request in json-rpc via something like PostMan. That way you can interface with the blockchain without using python or JavaScript. I'm presenting you an abstraction layer that simplifies things using JavaScript. You don't interact with the smart contract as a user via the method above.
    – 0xCCY
    Feb 22, 2022 at 23:53
  • Solidity is a Smart Contract programming language. Smart Contracts are program that execute on EVM. While there're ways to communicate between Smart Contracts, it usually start from a method invocation outside of Solidity. And the most convenient way is through the use of JavaScript npm packages or Python library. If you want to know how the Smart Contract called other functions within, you can then modified the Smart Contract code to emit events at various point.
    – 0xCCY
    Feb 23, 2022 at 0:10
  • Actually the first sentence already did, -- how do you know that I did? Maybe you've misunderstood me?
    – arumichi
    Feb 23, 2022 at 3:28
0

In order to perform your research, you may want to know that SushiSwap is actually a UniswapV2 fork. Therefore, any example you may find online applying to Uniswap V2 (and not V3, which is the latest version), including the well written Uniswap V2 doc you can find here. All the answers you are looking for are under the "Smart Contract Integration" section.

The only difference is that you will need to use SushiSwap deployment addresses instead of the Uniswap ones, that you can find in SushiSwap's docs.

Then it is up to you to use any development environment to deploy your smart contract and perform your transactions/calls (such as Remix IDE, Truffle or Hardhat as recommended by the other answer).

Finally, to give you an idea of how a swap looks like, here is a complete smart contract example of a swap on UniswapV2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.