Skip to content

Commit

Permalink
[dfyn-staked-in-farms] dfyn-staked-in-farms strategy added (snapshot-…
Browse files Browse the repository at this point in the history
…labs#48)

* dfyn-staked-in-farms strategy added

* Update author in src/strategies/dfyn-staked-in-farms/index.ts

Co-authored-by: Chaitanya <[email protected]>

* Update strategy to handle non latest snapshot

* Update author

Co-authored-by: Vatsal Gupta <[email protected]>
Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
3 people authored Aug 31, 2021
1 parent 8b7b59a commit a21c9a4
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/strategies/dfyn-staked-in-farms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Staked Dfyn in Farming Contracts

Allows fetching the amount of $DFYN staked in different farming contracts. By changing 'tokenAddress', 'decimals' and 'symbol' field, this strategy can also be used for other tokens. This strategy can be used for all contracts which return the amount of LP tokens in 'balanceOf' call. This strategy uses pair data from Dfyn's subgraph to calculate the amount of $DFYN present in users' LP token balance using the calcTokenBalance() function. A total of three calls have been made: 1) a subgraphRequest to fetch Dfyn's subgraph data 2) a multicall to fetch the staked LP token address corresponding to each farming contract and 3) a multicall to fetch all the users' staked LP token balance across all the farming contracts.

The strategy will also handle the case wherein only one contractAddress is to be used, however that address should also be put in an array.

## Example

The space config will look like this:

```JSON
{
"strategies": [
["dfyn-staked-in-farms", {
// farming contracts across which token balance needs to be calculated
"contractAddresses": [
"0xEdBB73C0ccD3F00cD75c2749b0df40A1BE394EE2",
"0x52b965ccd44A98A8aa064bC597C895adCD02e9BC",
"0x001A4e27CCDfe8ed6BBaFfEc9AE0985aB5542BEf",
"0xEAb0FD1FE0926E43b61612d65002Ba6320AA1080"
],
// token address
"tokenAddress": "0xc168e40227e4ebd8c1cae80f7a55a4f0e6d66c97",
// token decimals
"decimals": 18,
// token symbol
"symbol": "DFYN",
// scoreMultiplier can be used to increase users' scores by a certain magnitude
"scoreMultiplier": 1,
// ABI for balanceOf method
"methodABI_1": {
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
// ABI for stakingToken method
"methodABI_2": {
"inputs": [],
"name": "stakingToken",
"outputs": [
{
"internalType": "contract IERC20",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
}],
]
}
```
63 changes: 63 additions & 0 deletions src/strategies/dfyn-staked-in-farms/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[
{
"name": "Example query",
"strategy": {
"name": "dfyn-staked-in-farms",
"params": {
"name": "Dfyn Farms",
"contractAddresses": [
"0xEdBB73C0ccD3F00cD75c2749b0df40A1BE394EE2",
"0x52b965ccd44A98A8aa064bC597C895adCD02e9BC",
"0x001A4e27CCDfe8ed6BBaFfEc9AE0985aB5542BEf",
"0xEAb0FD1FE0926E43b61612d65002Ba6320AA1080"
],
"tokenAddress": "0xc168e40227e4ebd8c1cae80f7a55a4f0e6d66c97",
"decimal": 18,
"symbol": "DFYN",
"scoreMultiplier": 1,
"methodABI_1": {
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
"methodABI_2": {
"inputs": [],
"name": "stakingToken",
"outputs": [
{
"internalType": "contract IERC20",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
}
},
"network": "137",
"addresses": [
"0x41CdE29bF9aea095fDD204D150451678b2c6A736",
"0xD7B26f34775fa41D6dCE182851642573aBF9B532",
"0xd7539FCdC0aB79a7B688b04387cb128E75cb77Dc",
"0x6E33e22f7aC5A4b58A93C7f6D8Da8b46c50A3E20",
"0xC9dA7343583fA8Bb380A6F04A208C612F86C7701",
"0x2AC89522CB415AC333E64F52a1a5693218cEBD58"
],
"snapshot": 18342938
}
]
146 changes: 146 additions & 0 deletions src/strategies/dfyn-staked-in-farms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { formatUnits } from '@ethersproject/units';
import { multicall } from '../../utils';
import { subgraphRequest } from '../../utils';

export const author = 'vatsalgupta13';
export const version = '0.1.0';


const DFYN_SUBGRAPH_URL = "https://api.thegraph.com/subgraphs/name/ss-sonic/dfyn-v5"

const getAllLP = async (skip, stakedLP, snapshot) => {
let params =
{
pairs: {
__args: {
where: {
id_in: stakedLP.map((address) => address.toLowerCase())
},
skip: skip,
first: 1000
},
id: true,
reserve0: true,
reserve1: true,
totalSupply: true,
token0: {
id: true,
symbol: true,
},
token1: {
id: true,
symbol: true,
}
}
}
if (snapshot !== 'latest') {
// @ts-ignore
params.pairs.__args.block = { number: snapshot };
}
try {
const response = await subgraphRequest(DFYN_SUBGRAPH_URL, params)
return response.pairs;
} catch (error) {
console.error(error);
}
}

const getLP = async (stakedLP, snapshot) => {
let lp = []
let results = []
do {
results = await getAllLP(lp.length, stakedLP, snapshot);
lp = lp.concat(results)
} while (results.length === 1000);
return lp
}

function chunk(array, chunkSize) {
let tempArray: any[] = [];
for (let i = 0, len = array.length; i < len; i += chunkSize)
tempArray.push(array.slice(i, i + chunkSize));
return tempArray;
}

function calcTokenBalance(user_lp_balance, total_lp_supply, total_token_reserve) {
return total_lp_supply === 0 ? 0 : (total_token_reserve / total_lp_supply) * user_lp_balance
}

export async function strategy(
space,
network,
provider,
addresses,
options,
snapshot
) {
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';
var callData: [any, string, [any]][] = []
var callStakedAddress: [any, string][] = []
options.contractAddresses.map((contractAddress: any) => { callStakedAddress.push([contractAddress, options.methodABI_2.name]) })
addresses.map((userAddress: any) => { options.contractAddresses.map((contractAddress: any) => { callData.push([contractAddress, options.methodABI_1.name, [userAddress]]) }) })
callData = [...chunk(callData, 2000)] // chunking the callData into multiple arrays of 2000 requests

let LP_balances: any[] = []
for (let i = 0; i < callData.length; i++) {
let tempArray = await multicall(
network,
provider,
[options.methodABI_1],
callData[i],
{ blockTag }
);
LP_balances.push(...tempArray)
}

let stakedLP = await multicall(
network,
provider,
[options.methodABI_2],
callStakedAddress,
{ blockTag }
);

stakedLP = [].concat.apply([], stakedLP)
let dfyn_lp: any[] = await getLP(stakedLP, snapshot)
let temp: any = [];
if (options.contractAddresses.length > 1) {

// sorting dfyn_lp such that it aligns with users' lp balances
var itemPositions = {};
for (let i = 0; i < stakedLP.length; i++) {
itemPositions[stakedLP[i].toLowerCase()] = i;
}
dfyn_lp.sort((a, b) => itemPositions[a.id] - itemPositions[b.id]);

// grouping all balances of a particular address together
LP_balances = [].concat.apply([], LP_balances);
for (let i = addresses.length; i > 0; i--) {
temp.push(LP_balances.splice(0, Math.ceil(LP_balances.length / i)));
}
}
else {
temp = [...LP_balances]
}

// finding users token balance from their lp balances
LP_balances = []
temp.map((item, index) => {
let sum = 0;
temp[index].map((element, i) => {
element = calcTokenBalance(parseFloat(formatUnits(element.toString(), 18)), parseFloat(dfyn_lp[i].totalSupply),
options.tokenAddress.toLowerCase() === dfyn_lp[i].token0.id
? parseFloat(dfyn_lp[i].reserve0)
: parseFloat(dfyn_lp[i].reserve1))
sum = sum + element;
})
LP_balances.push(sum)
})

return Object.fromEntries(
LP_balances.map((value, i) => [
addresses[i],
options.scoreMultiplier * value
])
);
}
2 changes: 2 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as balancerErc20InternalBalanceOf from './balancer-erc20-internal-balan
import * as sunder from './sunder';
import * as balancerSmartPool from './balancer-smart-pool';
import * as contractCall from './contract-call';
import * as dfynFarms from './dfyn-staked-in-farms';
import * as dfynVaults from './dfyn-staked-in-vaults';
import * as ensDomainsOwned from './ens-domains-owned';
import * as ensReverseRecord from './ens-reverse-record';
Expand Down Expand Up @@ -148,6 +149,7 @@ const strategies = {
'balancer-erc20-internal-balance-of': balancerErc20InternalBalanceOf,
'erc20-received': erc20Received,
'contract-call': contractCall,
'dfyn-staked-in-farms': dfynFarms,
'dfyn-staked-in-vaults': dfynVaults,
'eth-received': ethReceived,
'eth-philanthropy': ethPhilanthropy,
Expand Down

0 comments on commit a21c9a4

Please sign in to comment.