forked from snapshot-labs/snapshot-strategies
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[moonbase] Moonbase Strategy (snapshot-labs#1278)
* init commit for snapshot analysis * Update yarn.lock * Update package.json * resolve final issues * Update src/strategies/moonbase/index.ts Co-authored-by: Chaitanya <yourchaitu@gmail.com> --------- Co-authored-by: Chaitanya <yourchaitu@gmail.com>
- Loading branch information
1 parent
48fe81a
commit e978c32
Showing
4 changed files
with
156 additions
and
1 deletion.
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,37 @@ | ||
# Moonbase | ||
|
||
This is the strategy, it returns the balances of the voters for MBG token balances | ||
in Moonbase project(pools, farms, vaults, token). | ||
|
||
Here is an example of parameters: | ||
|
||
```json | ||
[ | ||
{ | ||
"name": "Example query Moonbase", | ||
"strategy": { | ||
"name": "moonbase", | ||
"params": { | ||
"address": "0xc97c478Fc35d75b51549C39974053a679A5C67E1", | ||
"masterChef": "0x830304d6C669d33738c7E4c1F2310CC1E530Df63", | ||
"moonbaseLPs": [ | ||
{ | ||
"address": "0x230C64C42886A1F6b91eD8C11B59a2D45865d38F", | ||
"pid": 7 | ||
} | ||
], | ||
"symbol": "MBG", | ||
"decimals": 18 | ||
} | ||
}, | ||
"network": "84531", | ||
"addresses": [ | ||
"0xe32C26Be24232ba92cd89d116985F81f94Dd26a8", | ||
"0x7DC90A11489C384dc72234120B0f84C3932d94Ce", | ||
"0xf704872349a62ceBb40F841B635de268b2F7B9Fb" | ||
], | ||
"snapshot": 9182354 | ||
} | ||
] | ||
``` | ||
Note: A maximum of 1,000,000,000 moonbaseLPs are allowed in the strategy to avoid memory issues. |
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,27 @@ | ||
[ | ||
{ | ||
"name": "Example query Moonbase", | ||
"strategy": { | ||
"name": "moonbase", | ||
"params": { | ||
"address": "0xc97c478Fc35d75b51549C39974053a679A5C67E1", | ||
"masterChef": "0x830304d6C669d33738c7E4c1F2310CC1E530Df63", | ||
"moonbaseLPs": [ | ||
{ | ||
"address": "0x230C64C42886A1F6b91eD8C11B59a2D45865d38F", | ||
"pid": 7 | ||
} | ||
], | ||
"symbol": "MBG", | ||
"decimals": 18 | ||
} | ||
}, | ||
"network": "84531", | ||
"addresses": [ | ||
"0xe32C26Be24232ba92cd89d116985F81f94Dd26a8", | ||
"0x7DC90A11489C384dc72234120B0f84C3932d94Ce", | ||
"0xf704872349a62ceBb40F841B635de268b2F7B9Fb" | ||
], | ||
"snapshot": 9182354 | ||
} | ||
] |
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,89 @@ | ||
import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; | ||
import { formatUnits } from '@ethersproject/units'; | ||
import { Multicaller } from '../../utils'; | ||
import examplesFile from './examples.json'; | ||
|
||
export const author = 'MoonbaseMarkets'; | ||
export const version = '0.0.1'; | ||
export const examples = examplesFile; | ||
|
||
const abi = [ | ||
'function totalSupply() view returns (uint256)', | ||
'function balanceOf(address _owner) view returns (uint256 balance)', | ||
'function userInfo(uint256, address) view returns (uint256 amount, uint256 rewardDebt)' | ||
]; | ||
|
||
const bn = (num: any): BigNumber => { | ||
return BigNumber.from(num.toString()); | ||
}; | ||
|
||
const addUserBalance = (userBalances, user: string, balance) => { | ||
if (userBalances[user]) { | ||
return (userBalances[user] = userBalances[user].add(balance)); | ||
} else { | ||
return (userBalances[user] = balance); | ||
} | ||
}; | ||
|
||
const MAX_MOONBASE_LPS = 1000000000; | ||
|
||
export async function strategy( | ||
space, | ||
network, | ||
provider, | ||
addresses, | ||
options, | ||
snapshot | ||
): Promise<Record<string, number>> { | ||
|
||
if (options.moonbaseLPs.length > MAX_MOONBASE_LPS) { | ||
throw new Error(`Too many moonbaseLPs. Maximum allowed is ${MAX_MOONBASE_LPS}.`); | ||
} | ||
|
||
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; | ||
|
||
const multicall = new Multicaller(network, provider, abi, { blockTag }); | ||
addresses.forEach((address: any) => { | ||
multicall.call(`token.${address}`, options.address, 'balanceOf', [address]); | ||
}); | ||
options.moonbaseLPs.forEach((lp: { address: string; pid: number }) => { | ||
multicall.call(`lp.${lp.pid}.totalSupply`, lp.address, 'totalSupply'); | ||
multicall.call(`lp.${lp.pid}.balanceOf`, options.address, 'balanceOf', [ | ||
lp.address | ||
]); | ||
addresses.forEach((address: any) => { | ||
multicall.call( | ||
`lpUsers.${address}.${lp.pid}`, | ||
options.masterChef, | ||
'userInfo', | ||
[lp.pid, address] | ||
); | ||
}); | ||
}); | ||
const result = await multicall.execute(); | ||
|
||
const userBalances: any = []; | ||
for (let i = 0; i < addresses.length - 1; i++) { | ||
userBalances[addresses[i]] = bn(0); | ||
} | ||
|
||
addresses.forEach((address: any) => { | ||
addUserBalance(userBalances, address, result.token[address]); | ||
options.moonbaseLPs.forEach((lp: { address: string; pid: number }) => { | ||
addUserBalance( | ||
userBalances, | ||
address, | ||
result.lpUsers[address][lp.pid][0] | ||
.mul(result.lp[lp.pid].balanceOf) | ||
.div(result.lp[lp.pid].totalSupply) | ||
); | ||
}); | ||
}); | ||
|
||
return Object.fromEntries( | ||
Object.entries(userBalances).map(([address, balance]) => [ | ||
address, | ||
parseFloat(formatUnits(<BigNumberish>balance, options.decimals)) | ||
]) | ||
); | ||
} |