Skip to content

Commit

Permalink
Automated lint (#1615)
Browse files Browse the repository at this point in the history
Co-authored-by: ChaituVR <[email protected]>
  • Loading branch information
github-actions[bot] and ChaituVR authored Oct 20, 2024
1 parent e0a2d52 commit 6be68d2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ import * as superboring from './superboring';
import * as erableGovernanceV1 from './erable-governance-v1';
import * as worldLibertyFinancial from './world-liberty-financial-erc20-balance-of-votes';
import * as snxMultichain from './snx-multichain';
import * as moxie from './moxie'
import * as moxie from './moxie';
import * as stakingAmountDurationLinear from './staking-amount-duration-linear';
import * as stakingAmountDurationExponential from './staking-amount-duration-exponential';

Expand Down Expand Up @@ -930,7 +930,7 @@ const strategies = {
'erable-governance-v1': erableGovernanceV1,
'world-liberty-financial-erc20-balance-of-votes': worldLibertyFinancial,
'snx-multichain': snxMultichain,
'moxie': moxie,
moxie: moxie,
'staking-amount-duration-linear': stakingAmountDurationLinear,
'staking-amount-duration-exponential': stakingAmountDurationExponential
};
Expand Down
23 changes: 10 additions & 13 deletions src/strategies/moxie/examples.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
[
{
"name": "Example query",
"strategy": {
"name": "moxie"
},
"network": "8453",
"addresses": [
"0xb59aa5bb9270d44be3fa9b6d67520a2d28cf80ab"
],
"snapshot": 20631331
}
]

{
"name": "Example query",
"strategy": {
"name": "moxie"
},
"network": "8453",
"addresses": ["0xb59aa5bb9270d44be3fa9b6d67520a2d28cf80ab"],
"snapshot": 20631331
}
]
106 changes: 70 additions & 36 deletions src/strategies/moxie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const QUERY_LIMIT = 1000;
const UNSTAKED_FAN_TOKEN_MULTIPLIER = 2;
const STAKED_FAN_TOKEN_MULTIPLIER = 3;
const MOXIE_LIQUIDITY_MULTIPLIER = 2;
const MOXIE_CONTRACT_ADDRESS = "0x8C9037D1Ef5c6D1f6816278C7AAF5491d24CD527";
const MOXIE_CONTRACT_ADDRESS = '0x8C9037D1Ef5c6D1f6816278C7AAF5491d24CD527';
const MOXIE_DECIMALS = 18;
const PORTFOLIO_SUBGRAPH_PAGE_LIMIT = 2;
const LIQUIDITY_POOL_SUBGRAPH_PAGE_LIMIT = 1;
Expand All @@ -29,25 +29,28 @@ export async function strategy(
snapshot
) {
//Check if the addresses array has length not equal to 1
if(addresses.length != 1) {
throw new Error("This strategy expects a single address");
};
const MOXIE_API_KEY = process.env.MOXIE_API_KEY || "";
const MOXIE_PROTOCOL_ID = "7zS29h4BDSujQq8R3TFF37JfpjtPQsRUpoC9p4vo4scx";
const MOXIE_VESTING_ID = "BuR6zAj2GSVZz6smGbJZkgQx8S6GUS881R493ZYZKSk3";
const MOXIE_LIQUIDITY_ID = "2rv5XN3LDQiuc9BXFzUri7ZLnS6K1ZqNJzp8Zj8TqMhy";
if (addresses.length != 1) {
throw new Error('This strategy expects a single address');
}
const MOXIE_API_KEY = process.env.MOXIE_API_KEY || '';
const MOXIE_PROTOCOL_ID = '7zS29h4BDSujQq8R3TFF37JfpjtPQsRUpoC9p4vo4scx';
const MOXIE_VESTING_ID = 'BuR6zAj2GSVZz6smGbJZkgQx8S6GUS881R493ZYZKSk3';
const MOXIE_LIQUIDITY_ID = '2rv5XN3LDQiuc9BXFzUri7ZLnS6K1ZqNJzp8Zj8TqMhy';

//SETTING DEFAULT SUBGRAPH URLS
let MOXIE_PROTOCOL_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/88457/moxie-protocol/version/latest";
let MOXIE_VESTING_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/88457/moxie-vesting/version/latest";
let MOXIE_LIQUIDITY_POOL_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/88457/moxie-liquidity/version/latest";

if (MOXIE_API_KEY !== "" ) {
let MOXIE_PROTOCOL_SUBGRAPH_URL =
'https://api.studio.thegraph.com/query/88457/moxie-protocol/version/latest';
let MOXIE_VESTING_SUBGRAPH_URL =
'https://api.studio.thegraph.com/query/88457/moxie-vesting/version/latest';
let MOXIE_LIQUIDITY_POOL_SUBGRAPH_URL =
'https://api.studio.thegraph.com/query/88457/moxie-liquidity/version/latest';

if (MOXIE_API_KEY !== '') {
MOXIE_PROTOCOL_SUBGRAPH_URL = `https://gateway.thegraph.com/api/${MOXIE_API_KEY}/subgraphs/id/${MOXIE_PROTOCOL_ID}`;
MOXIE_LIQUIDITY_POOL_SUBGRAPH_URL = `https://gateway.thegraph.com/api/${MOXIE_API_KEY}/subgraphs/id/${MOXIE_LIQUIDITY_ID}`;
MOXIE_VESTING_SUBGRAPH_URL = `https://gateway.thegraph.com/api/${MOXIE_API_KEY}/subgraphs/id/${MOXIE_VESTING_ID}`;
}

//Check if the snapshot is for a specific block number or it's latest
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';
const addressesMap = addresses.reduce((map, address) => {
Expand All @@ -68,22 +71,28 @@ export async function strategy(
}
},
id: true,
beneficiary: true,
beneficiary: true
}
};
//Adding block to the query if the snapshot is not latest
if (snapshot !== 'latest') {
vestingSubgraphQuery.tokenLockWallets.__args['block'] = { number: snapshot };
vestingSubgraphQuery.tokenLockWallets.__args['block'] = {
number: snapshot
};
}

//Query the vesting subgraph to get the vesting contract addresses
const vestingContractResponse = await subgraphRequest(MOXIE_VESTING_SUBGRAPH_URL, vestingSubgraphQuery);

const vestingContractResponse = await subgraphRequest(
MOXIE_VESTING_SUBGRAPH_URL,
vestingSubgraphQuery
);

// Generate a map of vesting contract addresses to beneficiaries
const addressToBeneficiaryMap = vestingContractResponse.tokenLockWallets.reduce((map, wallet) => {
map[wallet.id.toLowerCase()] = wallet.beneficiary.toLowerCase();
return map;
}, {});
const addressToBeneficiaryMap =
vestingContractResponse.tokenLockWallets.reduce((map, wallet) => {
map[wallet.id.toLowerCase()] = wallet.beneficiary.toLowerCase();
return map;
}, {});

// Add vesting contract addresses to the list of addresses to query
const allAddresses = [
Expand Down Expand Up @@ -122,15 +131,15 @@ export async function strategy(
userPools: {
__args: {
where: {
user_in: allAddresses,
user_in: allAddresses
},
first: QUERY_LIMIT,
skip: 0
},
totalLPAmount: true,
pool: {
totalSupply: true,
moxieReserve: true,
moxieReserve: true
},
user: {
id: true
Expand All @@ -144,13 +153,19 @@ export async function strategy(
liquidityPoolSubgraphQuery.userPools.__args['block'] = { number: snapshot };
}

const fetchSubgraphData = async (subgraphUrl, query, processFunction, key, pageLimit) => {
const fetchSubgraphData = async (
subgraphUrl,
query,
processFunction,
key,
pageLimit
) => {
let next_page = 0;
while (next_page < pageLimit) {
query[key].__args.skip = next_page * QUERY_LIMIT;
const response = await subgraphRequest(subgraphUrl, query);
const data = response[Object.keys(response)[0]];

processFunction(data);

if (data.length < QUERY_LIMIT) break;
Expand All @@ -161,20 +176,27 @@ export async function strategy(
const processProtocolData = (portfolios) => {
portfolios.forEach((portfolio) => {
const userAddress = getAddress(portfolio.user.id);
allAddressesScoreMap[userAddress] += parseFloat(formatUnits(portfolio.unstakedBalance, MOXIE_DECIMALS)) * UNSTAKED_FAN_TOKEN_MULTIPLIER * portfolio.subjectToken.currentPriceInMoxie +
parseFloat(formatUnits(portfolio.stakedBalance, MOXIE_DECIMALS)) * STAKED_FAN_TOKEN_MULTIPLIER * portfolio.subjectToken.currentPriceInMoxie;
allAddressesScoreMap[userAddress] +=
parseFloat(formatUnits(portfolio.unstakedBalance, MOXIE_DECIMALS)) *
UNSTAKED_FAN_TOKEN_MULTIPLIER *
portfolio.subjectToken.currentPriceInMoxie +
parseFloat(formatUnits(portfolio.stakedBalance, MOXIE_DECIMALS)) *
STAKED_FAN_TOKEN_MULTIPLIER *
portfolio.subjectToken.currentPriceInMoxie;
});
};

const processLiquidityPoolData = (userPools) => {
userPools.forEach((userPool) => {
const userAddress = getAddress(userPool.user.id);
allAddressesScoreMap[userAddress] += MOXIE_LIQUIDITY_MULTIPLIER * parseFloat(formatUnits(userPool.totalLPAmount, MOXIE_DECIMALS)) *
parseFloat(formatUnits(userPool.pool.moxieReserve, MOXIE_DECIMALS)) /
allAddressesScoreMap[userAddress] +=
(MOXIE_LIQUIDITY_MULTIPLIER *
parseFloat(formatUnits(userPool.totalLPAmount, MOXIE_DECIMALS)) *
parseFloat(formatUnits(userPool.pool.moxieReserve, MOXIE_DECIMALS))) /
parseFloat(formatUnits(userPool.pool.totalSupply, MOXIE_DECIMALS));
});
};

// RPC Call to get balance of Moxie at a block for users
const fetchBalances = async () => {
const multi = new Multicaller(network, provider, abi, { blockTag });
Expand All @@ -183,16 +205,28 @@ export async function strategy(
);
const result: Record<string, BigNumberish> = await multi.execute();
Object.entries(result).forEach(([address, balance]) => {
let formattedBalance = parseFloat(formatUnits(balance, MOXIE_DECIMALS));
const formattedBalance = parseFloat(formatUnits(balance, MOXIE_DECIMALS));
allAddressesScoreMap[getAddress(address)] += formattedBalance;
});
};

// Fetch data from both subgraphs in parallel
await Promise.all([
fetchSubgraphData(MOXIE_PROTOCOL_SUBGRAPH_URL, protocolSubgraphQuery, processProtocolData, "portfolios", PORTFOLIO_SUBGRAPH_PAGE_LIMIT),
fetchSubgraphData(MOXIE_LIQUIDITY_POOL_SUBGRAPH_URL, liquidityPoolSubgraphQuery, processLiquidityPoolData, "userPools", LIQUIDITY_POOL_SUBGRAPH_PAGE_LIMIT),
fetchBalances(),
fetchSubgraphData(
MOXIE_PROTOCOL_SUBGRAPH_URL,
protocolSubgraphQuery,
processProtocolData,
'portfolios',
PORTFOLIO_SUBGRAPH_PAGE_LIMIT
),
fetchSubgraphData(
MOXIE_LIQUIDITY_POOL_SUBGRAPH_URL,
liquidityPoolSubgraphQuery,
processLiquidityPoolData,
'userPools',
LIQUIDITY_POOL_SUBGRAPH_PAGE_LIMIT
),
fetchBalances()
]);

// Now we have the score for each address we need to ensure it is added to the beneficiary address if it exists
Expand All @@ -206,4 +240,4 @@ export async function strategy(
});

return addressesMap;
}
}

0 comments on commit 6be68d2

Please sign in to comment.