Frontend Integration

Add USDC/DAI Liquidity to WUSD on Uniswap starting from Ethereum in just one transaction

The WUSDPrestocontains all the business logic needed to add liquidity to WUSD by using Presto. It's a singleton contract that is reachable from this linked address in mainnet.

You can find all of the needed ABIS here.

var wusdExtensionControllerAddress = "0xc6749132243dA6B174BF502E7a85f5cEdD74A753";
var wusdExtensionController = new web3.eth.Contract(abis.WUSDExtensionControllerABI, wusdExtensionControllerAddress);
var allowedAMMS = await wusdExtensionController.methods.allowedAMMs().call();

var chosenAMMPosition = 0; // Uniswap
var choosenLiquidityPoolPosition = 0; //USDC/DAI LP on Uniswap

var chosenAMMAddress = allowedAMMS[chosenAMMPostion][0];
var chosenLiquidityPoolAddress = allowedAMMS[chosenAMMPostion][1][choosenLiquidityPoolPosition];

var amm = new web3.eth.Contract(abis.IAMMABI, chosenAMMAddress);
var usdcAddress = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
var daiAddress = "0x6b175474e89094c44da98b954eedeac495271d0f";

var prestoAddress = "0x3660c73E030C497cccbE8aaabdc7431fAEB6E495";
var wusdPrestoAddress = "0x1a0107aC208775820B14B998460F1333a145A854";
var wusdPresto = new web3.eth.Contract(abis.WUSDPrestoABI, wusdPrestoAddress);

var ethereumValue = "0.5";
ethereumValue = web3.utils.toWei(ethereumValue, "ether");

var halfValue = web3.utils.toBN(ethereumValue).div(web3.utils.toBN(2)).toString();
var ethereumAddress = (await amm.methods.data().call())[0];

function normalizeValue(amount, decimals) {
    return web3.utils.toBN(amount).mul(web3.utils.toBN(10 ** (18 - decimals))).toString();
}

async function calculateBestLP(firstToken, secondToken, firstDecimals, secondDecimals) {

    var liquidityPoolAddress = (await amm.methods.byTokens([ethereumAddress, firstToken]).call())[2];
    var firstTokenETHLiquidityPoolAddress = liquidityPoolAddress;

    var token0Value = (await amm.methods.getSwapOutput(ethereumAddress, halfValue, [liquidityPoolAddress], [firstToken]).call())[1];

    var token1Value = (await ammContract.methods.byTokenAmount(liquidityPool, firstToken, token0Value).call());
    var lpAmount = token1Value[0];
    token1Value = token1Value[1][token1Value[2].indexOf(secondToken)];

    const updatedFirstTokenAmount = parseInt(normalizeValue(token0Value, firstDecimals));
    const updatedSecondTokenAmount = parseInt(normalizeValue(token1Value, secondDecimals));

    liquidityPoolAddress = (await amm.methods.byTokens([ethereumAddress, secondToken]).call())[2];
    var secondTokenETHLiquidityPoolAddress = liquidityPoolAddress;
    var token1ValueETH = (await amm.methods.getSwapOutput(secondToken, token1Value, [liquidityPoolAddress], [ethereumAddress]).call())[1];

    return { lpAmount, updatedFirstTokenAmount, updatedSecondTokenAmount, token0Value, token1Value, token1ValueETH, firstTokenETHLiquidityPoolAddress, secondTokenETHLiquidityPoolAddress };
}

var bestLP = await calculateBestLP(usdcAddress, daiAddress, 6, 18);

var lpAmount = bestLP.lpAmount;
var firstTokenAmount = bestLP.token0Value;
var secondTokenAmount = bestLP.token1Value;
var firstTokenETH = halfValue;
var secondTokenETH = bestLP.token1ValueETH;
var firstTokenETHLiquidityPoolAddress = bestLP.firstTokenETHLiquidityPoolAddress;
var secondTokenETHLiquidityPoolAddress = bestLP.secondTokenETHLiquidityPoolAddress;

if (bestLP.updatedSecondTokenAmount > bestLP.updatedFirstTokenAmount) {
    bestLP = await calculateBestLP(daiAddress, usdcAddress, 18, 6);

    lpAmount = bestLP.lpAmount;
    firstTokenAmount = bestLP.token1Value;
    secondTokenAmount = bestLP.token0Value;
    firstTokenETH = bestLP.token1ValueETH;
    secondTokenETH = halfValue;
    firstTokenETHLiquidityPoolAddress = bestLP.secondTokenETHLiquidityPoolAddress;
    secondTokenETHLiquidityPoolAddress = bestLP.firstTokenETHLiquidityPoolAddress;
}

var operations = [{
    inputTokenAddress : ethereumAddress,
    inputTokenAmount : firstTokenETH,
    ammPlugin : amm.options.address,
    liquidityPoolAddresses : [firstTokenETHLiquidityPoolAddress],
    swapPath : [usdcAddress],
    enterInETH : true,
    exitInETH : false,
    receivers : [wusdPresto.options.address],
    receiversPercentages : []
}, {
    inputTokenAddress : ethereumAddress,
    inputTokenAmount : secondTokenETH,
    ammPlugin : amm.options.address,
    liquidityPoolAddresses : [secondTokenETHLiquidityPoolAddress],
    swapPath : [daiAddress],
    enterInETH : true,
    exitInETH : false,
    receivers : [wusdPresto.options.address],
    receiversPercentages : []
}];

value = web3.utils.toBN(firstTokenETH).add(web3.utils.toBN(secondTokenETH)).toString();

var chosenAMMPosition = 0; //Select the one corresponding to Uniswap from the allowedAMM method by the WUSDExtensionController
var choosenLiquidityPoolPosition = 0; //Select the one corresponding to the USDC/DAI LP on Uniswap from the allowedAMM method by the WUSDExtensionController

wusdPresto.methods.addLiquidity(
    prestoAddress,
    operations,
    wusdExtensionControllerAddress,
    chosenAMMPosition,
    choosenLiquidityPoolPosition
).send({
    value
});

Last updated