Skip to content

Commit

Permalink
DEXTF: staked tokens (snapshot-labs#59)
Browse files Browse the repository at this point in the history
Strategy to calculate number of staked DEXTF

Co-authored-by: Fabrizio Audisio <[email protected]>
  • Loading branch information
dextfprotocol and faudisio authored Sep 16, 2021
1 parent 43a114e commit 0415b60
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/strategies/dextf-staked-in-vaults/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Multiple contract call strategy

This strategy allows users to call the 'balanceOf' function across multiple DEXTF contracts (vaults) and performs summation over the results. By calling 'balanceOf', DEXTF vaults return the amount of $DEXTF staked by the user in that vault. This strategy will make a single multicall which will retrieve all users' staked balances in all of DEXTF vaults.

## Example

The space config will look like this:

```JSON
{
"strategies": [
["dextf-staked-in-vaults", {
// vault contracts across which token balance needs to be calculated
"contractAddresses": [
"0x42a05787584ec09dDDe46f8CE6a715c93049ee88"
],
// 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"
}
}],
]
}
```
35 changes: 35 additions & 0 deletions src/strategies/dextf-staked-in-vaults/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"name": "Example query",
"strategy": {
"name": "dextf-staked-in-vaults",
"params": {
"name": "DEXTF Vaults",
"contractAddresses": ["0x42a05787584ec09dDDe46f8CE6a715c93049ee88"],
"scoreMultiplier": 1,
"methodABI": {
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
}
},
"network": "1",
"addresses": ["0x75527f00EC786dCCd6CEa82C9B90C81781C42E92"],
"snapshot": 13230652
}
]
61 changes: 61 additions & 0 deletions src/strategies/dextf-staked-in-vaults/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { formatUnits } from '@ethersproject/units';
import { multicall } from '../../utils';

export const author = 'dextf';
export const version = '1.0.0';

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

export async function strategy(
space,
network,
provider,
addresses,
options,
snapshot
) {
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';
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++) {
const tempArray = await multicall(
network,
provider,
[options.methodABI],
callData[i],
{ blockTag }
);
response.push(...tempArray);
}
if (options.contractAddresses.length > 1) {
// grouping all balances of a particular address together
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 = [];
result.map((item, index) => {
let sum = 0;
result[index].map((element) => {
sum = sum + parseFloat(formatUnits(element.toString(), 18));
});
response.push(sum);
});
}
return Object.fromEntries(
response.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 dextfVaults from './dextf-staked-in-vaults';
import * as dfynFarms from './dfyn-staked-in-farms';
import * as dfynVaults from './dfyn-staked-in-vaults';
import * as ensDomainsOwned from './ens-domains-owned';
Expand Down Expand Up @@ -152,6 +153,7 @@ const strategies = {
'balancer-erc20-internal-balance-of': balancerErc20InternalBalanceOf,
'erc20-received': erc20Received,
'contract-call': contractCall,
'dextf-staked-in-vaults': dextfVaults,
'dfyn-staked-in-farms': dfynFarms,
'dfyn-staked-in-vaults': dfynVaults,
'eth-received': ethReceived,
Expand Down

0 comments on commit 0415b60

Please sign in to comment.