Skip to content

Commit

Permalink
Add Coordinape strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
bonustrack committed Sep 13, 2021
1 parent 29e27c8 commit d2e78d3
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 94 deletions.
9 changes: 7 additions & 2 deletions src/strategies/balancer-erc20-internal-balance-of/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { Multicaller } from '../../utils';
export const author = 'gerrrg';
export const version = '0.0.1';

const abi = ['function getInternalBalance(address user, address[] tokens) external view returns (uint256[] balances)'];
const abi = [
'function getInternalBalance(address user, address[] tokens) external view returns (uint256[] balances)'
];

export async function strategy(
space,
Expand All @@ -19,7 +21,10 @@ export async function strategy(

const multi = new Multicaller(network, provider, abi, { blockTag });
addresses.forEach((address) =>
multi.call(address, options.vault, 'getInternalBalance', [address, [options.token]])
multi.call(address, options.vault, 'getInternalBalance', [
address,
[options.token]
])
);
const result: Record<string, BigNumberish> = await multi.execute();

Expand Down
11 changes: 11 additions & 0 deletions src/strategies/coordinape/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Coordinape

Use Coordinape circle epoch tokens as voting power.

Here is an example of parameters:
```json
{
"symbol": "CIRCLE",
"circle": "92"
}
```
19 changes: 19 additions & 0 deletions src/strategies/coordinape/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"name": "Example query",
"strategy": {
"name": "coordinape",
"params": {
"symbol": "CIRCLE",
"circle": "92"
}
},
"network": "1",
"addresses": [
"0x823b92d6a4b2AED4b15675c7917c9f922ea8ADAD",
"0xd337fccaec6ea113baacca3a41eb8766706a0706",
"0xeF8305E140ac520225DAf050e2f71d5fBcC543e7"
],
"snapshot": 13219000
}
]
36 changes: 36 additions & 0 deletions src/strategies/coordinape/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fetch from 'cross-fetch';
import { getAddress } from '@ethersproject/address';

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

export async function strategy(
space,
network,
provider,
addresses,
options,
snapshot
) {
const url = `https://coordinape.me/api/${options.circle}/token-gifts?latest_epoch=1&snapshot=${snapshot}`;
const res = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const gifts = await res.json();
const scores = {};
gifts.forEach(gift => {
const address = getAddress(gift.recipient_address);
if (!scores[address]) scores[address] = 0;
scores[address] += gift.tokens;
});
return Object.fromEntries(
addresses.map(address => [
address,
scores[getAddress(address)] || 0
])
);
}
2 changes: 1 addition & 1 deletion src/strategies/crucible-erc20-balance-of/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function strategy(
walletIDToCrucibleAddresses
)) {
callCrucibleToLpBalance.call(walletID, options.erc20_address, 'balanceOf', [
hexZeroPad(crucibleAddress.toHexString(), 20)
hexZeroPad(crucibleAddress.toHexString(), 20)
]);
}
const walletIDToLpBalance: Record<
Expand Down
2 changes: 1 addition & 1 deletion src/strategies/delegation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function strategy(
addresses.map((address) => {
const addressScore = delegations[address]
? delegations[address].reduce(
(a, b) => a + scores.reduce((x, y) => y[b] ? (x + y[b]) : x, 0),
(a, b) => a + scores.reduce((x, y) => (y[b] ? x + y[b] : x), 0),
0
)
: 0;
Expand Down
88 changes: 50 additions & 38 deletions src/strategies/dfyn-staked-in-farms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ 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 DFYN_SUBGRAPH_URL =
'https://api.thegraph.com/subgraphs/name/ss-sonic/dfyn-v5';

const getAllLP = async (skip, stakedLP, snapshot) => {
let params =
{
const params = {
pairs: {
__args: {
where: {
Expand All @@ -25,45 +24,51 @@ const getAllLP = async (skip, stakedLP, snapshot) => {
totalSupply: true,
token0: {
id: true,
symbol: true,
symbol: true
},
token1: {
id: true,
symbol: true,
symbol: true
}
}
}
};
if (snapshot !== 'latest') {
// @ts-ignore
params.pairs.__args.block = { number: snapshot };
}
try {
const response = await subgraphRequest(DFYN_SUBGRAPH_URL, params)
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 = []
let lp = [];
let results = [];
do {
results = await getAllLP(lp.length, stakedLP, snapshot);
lp = lp.concat(results)
lp = lp.concat(results);
} while (results.length === 1000);
return lp
}
return lp;
};

function chunk(array, chunkSize) {
let tempArray: any[] = [];
const 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
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(
Expand All @@ -75,22 +80,28 @@ export async function strategy(
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 callData: [any, string, [any]][] = [];
const 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[] = []
let LP_balances: any[] = [];
for (let i = 0; i < callData.length; i++) {
let tempArray = await multicall(
const tempArray = await multicall(
network,
provider,
[options.methodABI_1],
callData[i],
{ blockTag }
);
LP_balances.push(...tempArray)
LP_balances.push(...tempArray);
}

let stakedLP = await multicall(
Expand All @@ -101,13 +112,12 @@ export async function strategy(
{ blockTag }
);

stakedLP = [].concat.apply([], stakedLP)
let dfyn_lp: any[] = await getLP(stakedLP, snapshot)
stakedLP = [].concat.apply([], stakedLP);
const 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 = {};
const itemPositions = {};
for (let i = 0; i < stakedLP.length; i++) {
itemPositions[stakedLP[i].toLowerCase()] = i;
}
Expand All @@ -118,24 +128,26 @@ export async function strategy(
for (let i = addresses.length; i > 0; i--) {
temp.push(LP_balances.splice(0, Math.ceil(LP_balances.length / i)));
}
}
else {
temp = [...LP_balances]
} else {
temp = [...LP_balances];
}

// finding users token balance from their lp balances
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),
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))
: parseFloat(dfyn_lp[i].reserve1)
);
sum = sum + element;
})
LP_balances.push(sum)
})
});
LP_balances.push(sum);
});

return Object.fromEntries(
LP_balances.map((value, i) => [
Expand Down
37 changes: 19 additions & 18 deletions src/strategies/dfyn-staked-in-vaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const author = 'vatsalgupta13';
export const version = '0.1.0';

function chunk(array, chunkSize) {
let tempArray: any[] = [];
const tempArray: any[] = [];
for (let i = 0, len = array.length; i < len; i += chunkSize)
tempArray.push(array.slice(i, i + chunkSize));
return tempArray;
Expand All @@ -20,41 +20,42 @@ export async function strategy(
snapshot
) {
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';
var callData: [any, string, [any]][] = []
addresses.map((userAddress: any) => { options.contractAddresses.map((vaultAddress: any) => { callData.push([vaultAddress, options.methodABI.name, [userAddress]]) }) })
callData = [...chunk(callData, 2000)] // chunking the callData into multiple arrays of 2000 requests
let response: any[] = []
let callData: [any, string, [any]][] = [];
addresses.map((userAddress: any) => {
options.contractAddresses.map((vaultAddress: any) => {
callData.push([vaultAddress, options.methodABI.name, [userAddress]]);
});
});
callData = [...chunk(callData, 2000)]; // chunking the callData into multiple arrays of 2000 requests
let response: any[] = [];
for (let i = 0; i < callData.length; i++) {
let tempArray = await multicall(
const tempArray = await multicall(
network,
provider,
[options.methodABI],
callData[i],
{ blockTag }
);
response.push(...tempArray)
response.push(...tempArray);
}
if (options.contractAddresses.length > 1) {
// grouping all balances of a particular address together
let result: any = [];
const result: any = [];
response = [].concat.apply([], response);
for (let i = addresses.length; i > 0; i--) {
result.push(response.splice(0, Math.ceil(response.length / i)));
}
// performing summation over all balances of the user
response = []
response = [];
result.map((item, index) => {
let sum = 0
let sum = 0;
result[index].map((element) => {
sum = sum + parseFloat(formatUnits(element.toString(), 18))
})
response.push(sum)
})
sum = sum + parseFloat(formatUnits(element.toString(), 18));
});
response.push(sum);
});
}
return Object.fromEntries(
response.map((value, i) => [
addresses[i],
options.scoreMultiplier * value
])
response.map((value, i) => [addresses[i], options.scoreMultiplier * value])
);
}
34 changes: 12 additions & 22 deletions src/strategies/has-rock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,24 @@ export async function strategy(
) {
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';

let calls = [] as any;
for (var i=0; i< 100; i++){
calls.push([
options.address,
'rocks',
[i]
])
const calls = [] as any;
for (let i = 0; i < 100; i++) {
calls.push([options.address, 'rocks', [i]]);
}

const response = await multicall(
network,
provider,
abi,
calls,
{ blockTag }
);
const response = await multicall(network, provider, abi, calls, { blockTag });

let result = {} as any;
const result = {} as any;

addresses.forEach((address, x)=> {
addresses.forEach((address, x) => {
let addressRocks = 0;
response.forEach((rockObject, i) => {
if (rockObject.owner == address){
addressRocks++;
}
})
result[address] = addressRocks
})
if (rockObject.owner == address) {
addressRocks++;
}
});
result[address] = addressRocks;
});

return result;
}
Loading

0 comments on commit d2e78d3

Please sign in to comment.