-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
756 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Giveth Balances Supply Weighted | ||
|
||
This strategy is used to get the balance of tokens a user holds + the amount they have staked in GIVpower. It gets the sum of those balances, divides it by the circulating supply of the GIV token from an API endpoint then applies a weight to it. |
28 changes: 28 additions & 0 deletions
28
src/strategies/giveth-balances-supply-weighted/examples.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[ | ||
{ | ||
"name": "Example query", | ||
"strategy": { | ||
"name": "giveth-balances-supply-weighted", | ||
"params": { | ||
"tokenAddress": "0x528CDc92eAB044E1E39FE43B9514bfdAB4412B98", | ||
"stakedAddress": "0x301C739CF6bfb6B47A74878BdEB13f92F13Ae5E7", | ||
"supplyApi": "https://circulating.giveth.io/token-supply", | ||
"supplyField": "circulating", | ||
"weight": 0.5, | ||
"symbol": "GIV", | ||
"decimals": 18, | ||
"methodABI": { | ||
"method" : "function depositTokenBalance(address) public view returns (uint256)", | ||
"name": "depositTokenBalance" | ||
} | ||
} | ||
}, | ||
"network": "10", | ||
"addresses": [ | ||
"0x826976d7C600d45FB8287CA1d7c76FC8eb732030", | ||
"0x839395e20bbB182fa440d08F850E6c7A8f6F0780", | ||
"0x960A16c9070A9BbbB03e1bFd418982636D56D77d" | ||
], | ||
"snapshot": 117386958 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { formatUnits } from '@ethersproject/units'; | ||
import { Multicaller } from '../../utils'; | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
|
||
export const author = 'divine-comedian'; | ||
export const version = '0.1.0'; | ||
|
||
const abi = [ | ||
'function balanceOf(address account) external view returns (uint256)' | ||
]; | ||
|
||
export async function strategy( | ||
space, | ||
network, | ||
provider, | ||
addresses, | ||
options, | ||
snapshot | ||
): Promise<Record<string, number>> { | ||
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; | ||
|
||
const supply = fetch(options.supplyApi, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}).then((response) => response.json()); | ||
|
||
const tokenMulti = new Multicaller(network, provider, abi, { blockTag }); | ||
const stakedMulti = new Multicaller( | ||
network, | ||
provider, | ||
[options.methodABI.method] as string[], | ||
{ | ||
blockTag | ||
} | ||
); | ||
addresses.forEach((address) => { | ||
tokenMulti.call(address, options.tokenAddress, 'balanceOf', [address]), | ||
stakedMulti.call(address, options.stakedAddress, options.methodABI.name, [ | ||
address | ||
]); | ||
}); | ||
const tokenBalance = tokenMulti.execute(); | ||
const stakedBalance = stakedMulti.execute(); | ||
|
||
const [supplyResult, tokenResult, stakedResult]: [ | ||
any, | ||
Record<string, BigNumber>, | ||
Record<string, BigNumber> | ||
] = await Promise.all([supply, tokenBalance, stakedBalance]); | ||
|
||
const circulatingSupply = parseFloat( | ||
formatUnits(supplyResult[options.supplyField], options.decimals) | ||
); | ||
|
||
return Object.fromEntries( | ||
Object.entries(tokenResult).map(([address, tokenBalance]) => { | ||
const stakedBalance = stakedResult[address]; | ||
const totalBalance = tokenBalance.add(stakedBalance); | ||
return [ | ||
address, | ||
(parseFloat(formatUnits(totalBalance, options.decimals)) / | ||
circulatingSupply) * | ||
options.weight | ||
]; | ||
}) | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/strategies/giveth-gnosis-balance-supply-weighted-v3/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Giveth Gnosis balance | ||
|
||
This strategy sums up all the GIV on xDai, including the ones that are staked in Honeyswap pools, Sushiswap pools, single staked on GIV pool and GIVpower. This version divide the balance by the circulating supply of GIV and multiplies it by a weight. | ||
|
||
Here is an example of parameters: | ||
|
||
```json | ||
{ | ||
"symbol": "GIV", | ||
"decimals": 18 | ||
} | ||
``` |
20 changes: 20 additions & 0 deletions
20
src/strategies/giveth-gnosis-balance-supply-weighted-v3/examples.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"name": "Example query", | ||
"strategy": { | ||
"name": "giveth-gnosis-balance-supply-weighted-v3", | ||
"params": { | ||
"symbol": "GIV", | ||
"decimals": 18, | ||
"weight": 0.5 | ||
} | ||
}, | ||
"network": "100", | ||
"addresses": [ | ||
"0x839395e20bbb182fa440d08f850e6c7a8f6f0780", | ||
"0x960a16c9070a9bbbb03e1bfd418982636d56d77d", | ||
"0x826976d7C600d45FB8287CA1d7c76FC8eb732030" | ||
], | ||
"snapshot": 32900726 | ||
} | ||
] |
103 changes: 103 additions & 0 deletions
103
src/strategies/giveth-gnosis-balance-supply-weighted-v3/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { subgraphRequest } from '../../utils'; | ||
import { getAddress } from '@ethersproject/address'; | ||
import { formatUnits } from '@ethersproject/units'; | ||
|
||
export const author = 'divine-comedian'; | ||
export const version = '0.1.0'; | ||
|
||
const GIVETH_SUBGRAPH_API = | ||
'https://api.thegraph.com/subgraphs/name/giveth/giveth-economy-second-xdai'; | ||
const XDAI_BLOCKS_API = | ||
'https://api.thegraph.com/subgraphs/name/elkfinance/xdai-blocks'; | ||
|
||
const CIRUCLATING_SUPPLY_API = 'https://circulating.giveth.io/token-supply'; | ||
// "supplyField" : "circulating", | ||
|
||
const blockParams = { | ||
blocks: { | ||
__args: { | ||
first: 1, | ||
orderBy: 'timestamp', | ||
orderDirection: 'desc', | ||
where: { | ||
timestamp_lte: '' | ||
} | ||
}, | ||
number: true | ||
} | ||
}; | ||
|
||
const pairParams = { | ||
pair: { | ||
__args: { | ||
id: '' | ||
}, | ||
reserve0: true, | ||
totalSupply: true | ||
} | ||
}; | ||
|
||
const params = { | ||
tokenBalances: { | ||
__args: { | ||
orderBy: 'id', | ||
orderDirection: 'asc', | ||
where: { | ||
user_in: [] | ||
} | ||
}, | ||
id: true, | ||
balance: true, | ||
token: true | ||
} | ||
}; | ||
|
||
export async function strategy( | ||
space, | ||
network, | ||
provider, | ||
addresses, | ||
options, | ||
snapshot | ||
) { | ||
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; | ||
const block = await provider.getBlock(blockTag); | ||
blockParams.blocks.__args.where.timestamp_lte = block.timestamp; | ||
const xDaiBlock = await subgraphRequest(XDAI_BLOCKS_API, blockParams); | ||
const blockNumber = Number(xDaiBlock.blocks[0].number); | ||
// @ts-ignore | ||
params.tokenBalances.__args.block = { number: blockNumber }; | ||
if (snapshot !== 'latest') { | ||
// @ts-ignore | ||
pairParams.pair.__args.block = { number: blockNumber }; | ||
} | ||
|
||
params.tokenBalances.__args.where.user_in = addresses.map((address) => | ||
address.toLowerCase() | ||
); | ||
|
||
const data = subgraphRequest(GIVETH_SUBGRAPH_API, params); | ||
const supply = fetch(CIRUCLATING_SUPPLY_API, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
const [supplyData, balanceData] = await Promise.all([supply, data]); | ||
const supplyJSON = await supplyData.json(); | ||
const circulatingSupply = parseFloat( | ||
formatUnits(supplyJSON.circulating, options.decimals) | ||
); | ||
const score = {}; | ||
balanceData.tokenBalances.map((addressBalance) => { | ||
const id = addressBalance.id.split('-')[1]; | ||
const prevScore = score[getAddress(id)] ? score[getAddress(id)] : 0; | ||
score[getAddress(id)] = | ||
prevScore + | ||
(parseFloat(formatUnits(addressBalance.balance, options.decimals)) / | ||
circulatingSupply) * | ||
options.weight; | ||
}); | ||
return score; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.