Frontend Integration

Retrieve the Main Interesting Data

The most important contract of the WUSD ecosystem is the WUSDExtensionController, which contains all of the utility methods. 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 data = await wusdExtensionController.methods.wusdInfo().call();
var wusdCollection = new web3.eth.Contract(abis.EthItemNativeABI, data[0]);
var wusdObjectId = data[1];

data = await wusdExtensionController.methods.wusdNote2Info().call();
var wusdX2ObjectId = data[1];

data = await wusdExtensionController.methods.wusdNote5Info().call();
var wusdX5ObjectId = data[1];

//allowedAmms returns an array where each element is an array itself having length fixed to 2:
//first position is always the amm plugin address,
//second position is an array containing all liquidity pool addresses for that amm
var allowedAMMS = await wusdExtensionController.methods.allowedAMMs().call();

Add Liquidity

var chosenAMMPosition = 0;
var choosenLiquidityPoolPosition = 0;

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

var ammPlugin = new web3.eth.Contract(abis.IAMMABI, chosenAMMAddress);

data = await ammPlugin.methods.byLiquidityPool(chosenLiquidityPoolAddress).call();

var tokensAddresses = data[2];

var tokens = await Promise.all(tokenAddresses.map(async address => {
    var contract = new web3.eth.Contract(abis.IERC20ABI, address);
    return {
        address,
        contract,
        decimals : address === '0x0000000000000000000000000000000000000000' ? 18 : parseInt(await contract.methods.decimals().call())
    }
}));

//This means that will be minted (25 * tokens.length) WUSD
var firstAmount = 25 * (10 ** tokens[0].decimals);

data = await ammPlugin.byTokenAmount(chosenLiquidityPoolAddress, tokens[0].address, firstAmount).call();

var liquidityPoolAmount = data[0];
var tokenAmounts = data[1];

for(var token of tokens) {
    var amountToApprove = tokenAmounts[tokenAddresses.indexOf(token.address)];
    await token.contract.approve(wusdExtensionControllerAddress, amountToApprove).send();
}

await wusdExtensionController.methods.addLiquidity(
    chosenAMMPosition,
    choosenLiquidityPoolPosition,
    liquidityPoolAmount,
    false //byLiquidityPool === true means that extension will directly receive liquidityPool token amount (through transferFrom). false means that WUSDExtensionController will add liquidity to AMM before minting new WUSD tokens
).send();

Remove Liquidity

//Burn half of what minted before
liquidityPoolAmount = web3.utils.toBN(liquidityPoolAmount).div(web3.utils.toBN(2)).toString();

data = await ammPlugin.byLiquidityPoolAmount(chosenLiquidityPoolAddress, liquidityPoolAmount).call();

var amountToBurn = '0';
var tokenAmounts = data[0];

for(var token of tokens) {
    var normalizedTokenAmount = tokenAmounts[tokenAddresses.indexOf(token.address)];
    normalizedTokenAmount = web3.utils.toBN(normalizedTokenAmount).mul(web3.utils.toBN(10 ** (18 - token.decimals)));
    amountToBurn = web3.utils.toBN(amountToBurn).add(normalizedTokenAmount);
}

var types = ['uint256', 'uint256', 'uint256', 'bool'];                            //true means that you will receive liquidity pool token, false means you will receive tokens removed from AMM liqudity
var params = [chosenAMMPosition, choosenLiquidityPoolPosition, liquidityPoolAmount, false];
var removeLiquidityData = web3.eth.abi.encodeParameters(types, params);
                                                                 //0 means you're sending WUSD to remove liquidity, 1 means you're sending WUSD to rebalance by debt
var payload = web3.eth.abi.encodeParameters(["uint256", "bytes"], [0, removeLiquidityData]);

await wusdCollection.methods.safeTransferFrom(myAddress, wusdExtensionControllerAddress, amountToBurn, payload).send();

Rebalance By Debt

                                                                 //2 means you want to receive a x2 credit note, 5 means a x5 credit note
var rebalanceByDebtData = web3.eth.abi.encodeParameter("uint256", "2");
                                                             //0 means you're sending WUSD to remove liquidity, 1 means you're sending WUSD to rebalance by debt
payload = web3.eth.abi.encodeParameters(["uint256", "bytes"], [1, rebalanceByDebtData]);

amountToBurn = 10 * 1e18;

await wusdCollection.methods.safeTransferFrom(myAddress, wusdExtensionControllerAddress, amountToBurn, payload).send();

Last updated