Farming Multi-AMM Frontend Integration

Find All Farming Contracts

Farming contracts are factory-based, which means that they are created by sending directives to a factory smart contract and by having a fixed address like those found here in mainnet and Ropsten.

You can find all the needed ABIS here.

var deployEvent = "FarmMainDeployed(address,address,bytes)";

var farmFactoryAddress = "0x6BC8530fecc0001b9FC0bf5DAA17873e847616ed";

var farmFactory = new web3.eth.Contract(abis.FarmFactoryABI, farmFactoryAddress);

var logs = await web3.eth.getPastLogs({
    address : farmFactoryAddress,
    topics : [
        web3.util.sha3(deployEvent)
    ],
    fromBlock : '12068380',
    toBlock : 'latest'
});

var allFarmingContracts = logs.map(log => web3.eth.abi.decodeParameters("address", log.topics[1]));

Find Farming Contracts involving specific tokens

Farming Contracts involves many tokens, both for rewards and setups. The Main Farming Contract has several events that can be used to do specific searches for your purposes.

Find Farming Contracts having a specific reward token

var buidlTokenAddress = "0x7b123f53421b1bf8533339bfbdc7c98aa94163db";

var event = "RewardToken(address)";

var logs = await web3.eth.getPastLogs({
    address : allFarmingContracts,
    topics : [
        web3.util.sha3(event),
        web3.eth.abi.encodeParameter("address", buidlTokenAddress)
    ],
    fromBlock : '12068380',
    toBlock : 'latest'
});

var specificContracts = logs.map(log => log.address);

Find Farming Contracts using specific tokens in Setups

var tokens = [
    "0x7b123f53421b1bf8533339bfbdc7c98aa94163db", //buidl
    "0x6b175474e89094c44da98b954eedeac495271d0f" //DAI
];

var event = "SetupToken(address,address)";

var logs = await web3.eth.getPastLogs({
    address : allFarmingContracts,
    topics : [
        web3.util.sha3(event),
        tokens
    ],
    fromBlock : '12068380',
    toBlock : 'latest'
});

logs.push(... await web3.eth.getPastLogs({
    address : allFarmingContracts,
    topics : [
        web3.util.sha3(event),
        [],
        tokens
    ],
    fromBlock : '12068380',
    toBlock : 'latest'
}));

//There can be duplicates (e.g. farming contracts with same address having two setups for buidl or DAI or maybe a buidl-DAI pair)
var specificContracts = {};
logs.forEach(log => specificContracts[log.address] = true);
var specificContracts = Object.keys(specificContracts);

Create a simple farming contract

//Create setups, the code
var setups = [{
    free : true, // if the setup is a free farming setup or a locked one.
    blockDuration : 6400, // duration of setup
    originalRewardPerBlock : 0.5 * 1e18,
    minStakeable : 0, // minimum amount of staking tokens.
    maxStakeable : 50 * 1e18, // maximum amount stakeable in the setup (used only if free is false).
    renewTimes : 0, // if the setup is renewable or if it's one time.
    ammPlugin : "0xFC1665BD717dB247CDFB3a08b1d496D1588a6340", // amm plugin address used for this setup (eg. uniswap amm plugin address).
    liquidityPoolTokenAddress : "0xb0fB35Cc576034b01bED6f4D0333b1bd3859615C", // address of the liquidity pool token
    mainTokenAddress : "0x7b123f53421b1bf8533339bfbdc7c98aa94163db", // eg. buidl address. The main token in the LP on which to base the calculations
    ethereumAddress : "0x0000000000000000000000000000000000000000", //Used for internal calculations, must be always address(0) at initialization
    involvingETH : true, // if the setup involves ETH or not.
    penaltyFee : 0, // fee (expressed in a value from 0 to 1 * 1e18) paid when the user exits a still active locked farming setup (used only if free is false).
    setupsCount : 0, // number of setups created by this info. Used for internal calculations. Must be always 0 at initialization
    lastSetupIndex : 0 // index of last setup; Used for internal calculations. Must be always 0 at initialization
}];

var abiType = "tuple(bool,uint256,uint256,uint256,uint256,uint256,address,address,address,address,bool,uint256,uint256,uint256)[]";

//Abi-encoded setups
var ABIEncodedSetups = abi.encode([abiType], setups.map(setup => Object.values(setup)));

var transaction = await farmFactory.methods.cloneFarmDefaultExtension().send();
var receipt = await web3.eth.getTransactionReceipt(transaction.transactionHash);
var log = receipt.logs.filter(log => log.topics[0] === web3.utils.sha3("ExtensionCloned(address)"))[0];
var extensionAddress = web3.eth.abi.decodeParameter("address", log.topics[1]);

var defaultExtension = new web3.eth.Contract(abis.FarmExtensionABI, extensionAddress);

var extensionInitPayload = defaultExtension.methods.init(
    false, //byMint
    myWallet, //host
    myWallet, //treasury
).encodeABI();

var farmMainTemplate = new web3.eth.Contract(abis.FarmMainABI);

var initPayload = farmMainTemplate.methods.init(
    extensionAddress,
    extensionInitPayload,
    "0xf15aAAE073F578d3B7c086cA7Cb7901424120D51", //ethItem orchestrator address, useful to create Position Items
    "0x7b123f53421b1bf8533339bfbdc7c98aa94163db", //reward Token,
    ABIEncodedSetups
).encodeABI();

transaction = await farmFactory.methods.deploy(initPayload).send();
receipt = await web3.eth.getTransactionReceipt(transaction.transactionHash);
log = receipt.logs.filter(log => log.topics[0] === web3.utils.sha3("FarmMainDeployed(address,address,bytes)"))[0];
var farmMainAddress = web3.eth.abi.decodeParameter("address", log.topics[1]);

Last updated