Skip to content

Commit

Permalink
[moonbase] Moonbase Strategy (snapshot-labs#1278)
Browse files Browse the repository at this point in the history
* 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
vespermystcl and ChaituVR authored Sep 7, 2023
1 parent 48fe81a commit e978c32
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ import * as gelatoStaking from './gelato-staking';
import * as erc4626AssetsOf from './erc4626-assets-of';
import * as sdVoteBoostTWAVPV2 from './sd-vote-boost-twavp-v2';
import * as friendTech from './friend-tech';
import * as moonbase from './moonbase';

const strategies = {
'cap-voting-power': capVotingPower,
Expand Down Expand Up @@ -928,7 +929,8 @@ const strategies = {
'gelato-staking': gelatoStaking,
'erc4626-assets-of': erc4626AssetsOf,
'friend-tech': friendTech,
'sd-vote-boost-twavp-v2': sdVoteBoostTWAVPV2
'sd-vote-boost-twavp-v2': sdVoteBoostTWAVPV2,
'moonbase': moonbase
};

Object.keys(strategies).forEach(function (strategyName) {
Expand Down
37 changes: 37 additions & 0 deletions src/strategies/moonbase/README.md
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.
27 changes: 27 additions & 0 deletions src/strategies/moonbase/examples.json
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
}
]
89 changes: 89 additions & 0 deletions src/strategies/moonbase/index.ts
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))
])
);
}

0 comments on commit e978c32

Please sign in to comment.