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.
1
var deployEvent = "FarmMainDeployed(address,address,bytes)";
2
3
var farmFactoryAddress = "0x6BC8530fecc0001b9FC0bf5DAA17873e847616ed";
4
5
var farmFactory = new web3.eth.Contract(abis.FarmFactoryABI, farmFactoryAddress);
6
7
var logs = await web3.eth.getPastLogs({
8
address : farmFactoryAddress,
9
topics : [
10
web3.util.sha3(deployEvent)
11
],
12
fromBlock : '12068380',
13
toBlock : 'latest'
14
});
15
16
var allFarmingContracts = logs.map(log => web3.eth.abi.decodeParameters("address", log.topics[1]));
Copied!

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

1
var buidlTokenAddress = "0x7b123f53421b1bf8533339bfbdc7c98aa94163db";
2
3
var event = "RewardToken(address)";
4
5
var logs = await web3.eth.getPastLogs({
6
address : allFarmingContracts,
7
topics : [
8
web3.util.sha3(event),
9
web3.eth.abi.encodeParameter("address", buidlTokenAddress)
10
],
11
fromBlock : '12068380',
12
toBlock : 'latest'
13
});
14
15
var specificContracts = logs.map(log => log.address);
Copied!

Find Farming Contracts using specific tokens in Setups

1
var tokens = [
2
"0x7b123f53421b1bf8533339bfbdc7c98aa94163db", //buidl
3
"0x6b175474e89094c44da98b954eedeac495271d0f" //DAI
4
];
5
6
var event = "SetupToken(address,address)";
7
8
var logs = await web3.eth.getPastLogs({
9
address : allFarmingContracts,
10
topics : [
11
web3.util.sha3(event),
12
tokens
13
],
14
fromBlock : '12068380',
15
toBlock : 'latest'
16
});
17
18
logs.push(... await web3.eth.getPastLogs({
19
address : allFarmingContracts,
20
topics : [
21
web3.util.sha3(event),
22
[],
23
tokens
24
],
25
fromBlock : '12068380',
26
toBlock : 'latest'
27
}));
28
29
//There can be duplicates (e.g. farming contracts with same address having two setups for buidl or DAI or maybe a buidl-DAI pair)
30
var specificContracts = {};
31
logs.forEach(log => specificContracts[log.address] = true);
32
var specificContracts = Object.keys(specificContracts);
Copied!

Create a simple farming contract

1
//Create setups, the code
2
var setups = [{
3
free : true, // if the setup is a free farming setup or a locked one.
4
blockDuration : 6400, // duration of setup
5
originalRewardPerBlock : 0.5 * 1e18,
6
minStakeable : 0, // minimum amount of staking tokens.
7
maxStakeable : 50 * 1e18, // maximum amount stakeable in the setup (used only if free is false).
8
renewTimes : 0, // if the setup is renewable or if it's one time.
9
ammPlugin : "0xFC1665BD717dB247CDFB3a08b1d496D1588a6340", // amm plugin address used for this setup (eg. uniswap amm plugin address).
10
liquidityPoolTokenAddress : "0xb0fB35Cc576034b01bED6f4D0333b1bd3859615C", // address of the liquidity pool token
11
mainTokenAddress : "0x7b123f53421b1bf8533339bfbdc7c98aa94163db", // eg. buidl address. The main token in the LP on which to base the calculations
12
ethereumAddress : "0x0000000000000000000000000000000000000000", //Used for internal calculations, must be always address(0) at initialization
13
involvingETH : true, // if the setup involves ETH or not.
14
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).
15
setupsCount : 0, // number of setups created by this info. Used for internal calculations. Must be always 0 at initialization
16
lastSetupIndex : 0 // index of last setup; Used for internal calculations. Must be always 0 at initialization
17
}];
18
19
var abiType = "tuple(bool,uint256,uint256,uint256,uint256,uint256,address,address,address,address,bool,uint256,uint256,uint256)[]";
20
21
//Abi-encoded setups
22
var ABIEncodedSetups = abi.encode([abiType], setups.map(setup => Object.values(setup)));
23
24
var transaction = await farmFactory.methods.cloneFarmDefaultExtension().send();
25
var receipt = await web3.eth.getTransactionReceipt(transaction.transactionHash);
26
var log = receipt.logs.filter(log => log.topics[0] === web3.utils.sha3("ExtensionCloned(address)"))[0];
27
var extensionAddress = web3.eth.abi.decodeParameter("address", log.topics[1]);
28
29
var defaultExtension = new web3.eth.Contract(abis.FarmExtensionABI, extensionAddress);
30
31
var extensionInitPayload = defaultExtension.methods.init(
32
false, //byMint
33
myWallet, //host
34
myWallet, //treasury
35
).encodeABI();
36
37
var farmMainTemplate = new web3.eth.Contract(abis.FarmMainABI);
38
39
var initPayload = farmMainTemplate.methods.init(
40
extensionAddress,
41
extensionInitPayload,
42
"0xf15aAAE073F578d3B7c086cA7Cb7901424120D51", //ethItem orchestrator address, useful to create Position Items
43
"0x7b123f53421b1bf8533339bfbdc7c98aa94163db", //reward Token,
44
ABIEncodedSetups
45
).encodeABI();
46
47
transaction = await farmFactory.methods.deploy(initPayload).send();
48
receipt = await web3.eth.getTransactionReceipt(transaction.transactionHash);
49
log = receipt.logs.filter(log => log.topics[0] === web3.utils.sha3("FarmMainDeployed(address,address,bytes)"))[0];
50
var farmMainAddress = web3.eth.abi.decodeParameter("address", log.topics[1]);
Copied!