Frontend Integration
Create an Index by retrieving all needed tokens starting from Ethereum
You can find all of the needed ABIS here.
var amms = [];
var ammAggregatorAddress = "0x81391d117a03A6368005e447197739D06550D4CD";
const ammAggregator = new web3.eth.Contract(abis.AMMAgregatorABI, ammAggregatorAddress);
var ammAddresses = await ammAggregator.methods.amms().call();
for (var address of ammAddresses) {
var contract = new web3.eth.Contract(abis.AMMABI, address);
var amm = {
address,
contract,
info: await contract.methods.info().call(),
data: await contract.methods.data().call()
}
amm.data[2] && amms.push(amm);
}
var amm = amms.filter(it => it.info[0] === 'UniswapV2')[0];
var ethereumAddress = amm.data[0];
var prestoAddress = "0x3660c73E030C497cccbE8aaabdc7431fAEB6E495";
var indexPrestoAddress = "0x7A219F277bF11E5D4c1152c0D73ADd561e4f7cB5";
var indexPresto = new web3.eth.Contract(abis.IndexPrestoABI, indexPrestoAddress);
var indexAddress = "0x3a6a5fcce56fa57eeb4b24aeb8d13a2e3197b333";
var index = new web3.eth.Contract(abis.IndexABI, indexAddress);
var objectId = "903718979238392026542673315938967756144559516106";
var indexAmount = "1";
indexAmount = web3.utils.toWei(indexAmount, 'ether');
var mintResult = await index.contract.methods.info(objectId, indexAmount).call();
async function calculateEthereumPrices(tokenAddress, tokenValue) {
var liquidityPoolAddress = (await amm.contract.methods.byTokens([ethereumAddress, tokenAddress]).call())[2];
var data = await amm.contract.methods.getSwapOutput(tokenAddress, tokenValue, [liquidityPoolAddress], [ethereumAddress]).call();
data = await amm.contract.methods.getSwapOutput(ethereumAddress, data[1], [liquidityPoolAddress], [tokenAddress]).call();
var ethereumValue;
var multiplier = parseInt(tokenValue) / parseInt(data[1]);
while (web3.utils.toBN(tokenValue).gt(web3.utils.toBN(data[1]))) {
var oldEthereumValue = ethereumValue;
ethereumValue = window.numberToString(parseInt(ethereumValue || data[0]) * multiplier).split('.')[0].split(',').join('');
if (multiplier === 1 || (oldEthereumValue && web3.utils.toBN(oldEthereumValue).gte(web3.utils.toBN(ethereumValue)))) {
ethereumValue = web3.utils.toBN(oldEthereumValue || ethereumValue).add(web3.utils.toBN(10000)).toString();
}
data = await amm.contract.methods.getSwapOutput(ethereumAddress, ethereumValue, [liquidityPoolAddress], [tokenAddress]).call();
multiplier = parseInt(tokenValue) / parseInt(data[1]);
console.log(ethereumValue, tokenValue, data[1]);
}
return { ethereumValue, liquidityPoolAddress, tokenAddress };
}
var swapForEthValues = [];
for (var i in mintResult[0]) {
swapForEthValues.push(await calculateEthereumPrices(mintResult[0][i], mintResult[1][i]));
}
var ethValue = "0";
var operations = [];
for (var i in swapForEthValues) {
var data = swapForEthValues[i];
operations.push({
inputTokenAddress: ethereumAddress,
inputTokenAmount: data.ethereumValue,
ammPlugin: amm.contract.options.address,
liquidityPoolAddresses: [data.liquidityPoolAddress],
swapPath: [data.tokenAddress],
enterInETH: true,
exitInETH: false,
receivers: [indexPresto.options.address],
receiversPercentages: []
});
ethValue = web3.utils.toBN(ethValue).add(web3.utils.toBN(data.ethereumValue)).toString();
}
await indexPresto.methods.mint(
prestoAddress,
operations,
index.options.address,
objectId,
indexAmount,
myAddress
).send({value : ethValue});
Last updated