Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[multichain-supply-weighted] Multichain Supply Weighted Strategy (giveth) #1408

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import * as stablexswap from './stablexswap';
import * as stakedKeep from './staked-keep';
import * as stakedDaomaker from './staked-daomaker';
import * as typhoon from './typhoon';
import * as multichainSupplyWeighted from './multichain-supply-weighted';
import * as delegation from './delegation';
import * as delegationWithCap from './delegation-with-cap';
import * as delegationWithOverrides from './delegation-with-overrides';
Expand Down Expand Up @@ -414,6 +415,7 @@ import * as voltVotingPower from './volt-voting-power';
import * as xdaiStakersAndHolders from './xdai-stakers-and-holders';

const strategies = {
'multichain-supply-weighted': multichainSupplyWeighted,
'cap-voting-power': capVotingPower,
'izumi-veizi': izumiVeiZi,
'eco-voting-power': ecoVotingPower,
Expand Down
51 changes: 51 additions & 0 deletions src/strategies/math/examples copy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[
{
"name": "Example simple contract call plus / minus",
"strategy": {
"name": "math",
"params": {
"symbol": "MATH",
"operands": [
{
"type": "strategy",
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0x4f4F9b8D5B4d0Dc10506e5551B0513B61fD59e75",
"decimals": 18
}
},
{
"type": "strategy",
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0xfFBAbEb49be77E5254333d5fdfF72920B989425f",
"decimals": 18
}
}

],
"operator": "add"
}
},
"network": "1",
"addresses": [
"0x5D28FE1e9F895464aab52287d85Ebff32B351674",
"0x6D97d65aDfF6771b31671443a6b9512104312d3D",
"0x839395e20bbB182fa440d08F850E6c7A8f6F0780",
"0x701d0ECB3BA780De7b2b36789aEC4493A426010a",
"0x00d18ca9782bE1CaEF611017c2Fbc1a39779A57C",
"0x826976d7C600d45FB8287CA1d7c76FC8eb732030",
"0xC46c67Bb7E84490D7EbdD0b8ecDaca68Cf3823F4",
"0xed8DB37778804A913670d9367aAf4F043AAd938b",
"0xA4D506434445Bb7303eA34A07bc38484cdC64a95",
"0x10a84b835C5df26f2A380B3E00bCC84A66cD2d34",
"0x2Ea846Dc38C6b6451909F1E7ff2bF613a96DC1F3",
"0x8F48094a12c8F99d616AE8F3305D5eC73cBAA6b6"
],
"snapshot": 19023079
}
]


52 changes: 52 additions & 0 deletions src/strategies/multichain-erc20-weighted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# multichain

If you want to calculate the balance from various chains like Ethereum, Binance smart chain, polygon etc. and use them for voting using various strategies, you can do it by using a strategy called “multichain strategy”. This allows cross chain voting in which multiple chains can be used together to calculate the voting power.

In multichain strategy, the params should define sub strategies which would use different networks mentioned in the field to combine the voting power.

In order to provide multichain functionality, this strategy provides a way to calculate which block number should be used on additional chains: If a snapshot was created on block 125 on mainnet, it will find the timestamp for that block and go find which block number corresponds to that same timestamp on every other wanted chain. This way it can accurately represent an address' voting power at a given point in time.

Here is an example of parameters:

In the below example, the tokens on the three networks namely ethereum, polygon and bsc denotes combined voting power

```json
{
"symbol": "MULTI",
"strategies": [
{
"name": "erc20-balance-of",
"network": "1",
"params": {
"address": "0x579cea1889991f68acc35ff5c3dd0621ff29b0c9",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "137",
"params": {
"address": "0xB9638272aD6998708de56BBC0A290a1dE534a578",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "56",
"params": {
"address": "0x0e37d70b51ffa2b98b4d34a5712c5291115464e3",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": 137,
"params": {
"address": "0xfC0fA725E8fB4D87c38EcE56e8852258219C64Ee",
"decimals": 18
}
}
]
}

```
64 changes: 64 additions & 0 deletions src/strategies/multichain-erc20-weighted/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"name": "Multichain Voting",
"strategy": {
"name": "multichain",
"params": {
"symbol": "MULTI",
"api" : "https://circulating.giveth.io/token-supply",
"tokenPools": [
{
"weight": 0.5,
"strategies": [
{
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0x4f4F9b8D5B4d0Dc10506e5551B0513B61fD59e75",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0xfFBAbEb49be77E5254333d5fdfF72920B989425f",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "10",
"params": {
"address": "0x528CDc92eAB044E1E39FE43B9514bfdAB4412B98",
"decimals": 18
}
}
]
},
{
"weight": 0.5,
"strategies": [
{
"name": "erc20-balance-of",
"network": "10",
"params": {
"address": "0x16ef294ed9aeca7541183f19e4a5d01cebab88fb",
"decimals": 18
}
}
]
}
]

}
},
"network": "1",
"addresses": [
"0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5",
"0x9feab70f3c4a944b97b7565bac4991df5b7a69ff",
"0xaca39b187352d9805deced6e73a3d72abf86e7a0"
],
"snapshot": 13035566
}
]
65 changes: 65 additions & 0 deletions src/strategies/multichain-erc20-weighted/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getProvider, getSnapshots } from '../../utils';
import strategies from '..';

export const author = 'kesar';
export const version = '1.1.0';

export async function strategy(
space,
network,
provider,
addresses,
options,
snapshot
) {
const promises: any = [];
const blocks = await getSnapshots(
network,
snapshot,
provider,
options.strategies.map((s) => s.network || network)
);
for (const tokenPool of options.tokenPools) {
const tokenPromises: any = [];
for (const strategy of tokenPool.strategies) {
// If snapshot is taken before a network is activated then ignore its strategies
if (
options.startBlocks &&
blocks[strategy.network] < options.startBlocks[strategy.network]
) {
continue;
}

tokenPromises.push(
strategies[strategy.name].strategy(
space,
strategy.network,
getProvider(strategy.network),
addresses,
strategy.params,
blocks[strategy.network]
)
);
}
promises.push(tokenPromises);
}

const results = await Promise.all(promises);
const summedResults = results.map((result: any) =>
result.reduce((sum, value) => sum + value, 0)
);
const weightedResults = summedResults.map(
(result, index) => result * options.tokenPools[index].weight
);

console.log(weightedResults, 'weightedResults');
return weightedResults.reduce((finalResults: any, strategyResult: any) => {
for (const [address, value] of Object.entries(strategyResult)) {
if (!finalResults[address]) {
finalResults[address] = 0;
}
finalResults[address] += value;
}
return finalResults;
}, {});
}
56 changes: 56 additions & 0 deletions src/strategies/multichain-supply-weighted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# multichain-supply-weighted

This is a fork of the multichain strategy by kesar.

This strategy will calculate the balance of a multichain-token from various chains like Ethereum, Binance smart chain, polygon etc.

Exactly like the multichain strategy, the strategies array should define sub strategies which would use different networks mentioned in the field to combine the voting power.

In order to provide multichain functionality, this strategy provides a way to calculate which block number should be used on additional chains: If a snapshot was created on block 125 on mainnet, it will find the timestamp for that block and go find which block number corresponds to that same timestamp on every other wanted chain. This way it can accurately represent an address' voting power at a given point in time.

## Further more...

This strategy calls an external API to get the tokens total circulating/active supply. It also allows a weight to be defined as well. The supplyApi is where the GET request should be made to a REST API. you can use the supplyField to specify the field in the response data that contains the supply number you're looking for.

Using this information it will calculate the balance of a given token across all chains, divide it against the circulating supply to find the percentage of the circulating supply held by the address and then multiply it by the weight to get the final voting power.


Here is an example of parameters:

In the below example, the tokens on the three networks namely ethereum, polygon and bsc denotes combined voting power

```json
{
"symbol": " MULTI",
"supplyApi" : "https://circulating.giveth.io/token-supply",
"supplyField" : "circulating",
"weight" : 0.5,
"strategies": [
{
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0x4f4F9b8D5B4d0Dc10506e5551B0513B61fD59e75",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0xfFBAbEb49be77E5254333d5fdfF72920B989425f",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "10",
"params": {
"address": "0x528CDc92eAB044E1E39FE43B9514bfdAB4412B98",
"decimals": 18
}
}
]
}

```
56 changes: 56 additions & 0 deletions src/strategies/multichain-supply-weighted/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"name": "Multichain Supply Weighted Voting",
"strategy": {
"name": "multichain-supply-weighted",
"params": {
"symbol": "GIV",
"supplyApi" : "https://circulating.giveth.io/token-supply",
"supplyField" : "circulating",
"weight" : 0.5,
"strategies": [
{
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0x4f4F9b8D5B4d0Dc10506e5551B0513B61fD59e75",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "100",
"params": {
"address": "0xfFBAbEb49be77E5254333d5fdfF72920B989425f",
"decimals": 18
}
},
{
"name": "erc20-balance-of",
"network": "10",
"params": {
"address": "0x528CDc92eAB044E1E39FE43B9514bfdAB4412B98",
"decimals": 18
}
}
]
}
},
"network": "1",
"addresses": [
"0x5D28FE1e9F895464aab52287d85Ebff32B351674",
"0x6D97d65aDfF6771b31671443a6b9512104312d3D",
"0x839395e20bbB182fa440d08F850E6c7A8f6F0780",
"0x701d0ECB3BA780De7b2b36789aEC4493A426010a",
"0x00d18ca9782bE1CaEF611017c2Fbc1a39779A57C",
"0x826976d7C600d45FB8287CA1d7c76FC8eb732030",
"0xC46c67Bb7E84490D7EbdD0b8ecDaca68Cf3823F4",
"0xed8DB37778804A913670d9367aAf4F043AAd938b",
"0xA4D506434445Bb7303eA34A07bc38484cdC64a95",
"0x10a84b835C5df26f2A380B3E00bCC84A66cD2d34",
"0x2Ea846Dc38C6b6451909F1E7ff2bF613a96DC1F3",
"0x8F48094a12c8F99d616AE8F3305D5eC73cBAA6b6"
],
"snapshot": 19023079
}
]
Loading