Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix(votium): Clean-up and ship
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich committed Jun 9, 2022
1 parent 7dab0ac commit 7a7eaca
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MerkleContractPositionHelper {
const tokens = [claimable(rewardToken)];
const dataProps = {};
const displayProps = {
label: getLabelFromToken(rewardToken),
label: `Claimable ${getLabelFromToken(rewardToken)}`,
secondaryLabel: buildDollarDisplayItem(rewardToken.price),
images: getImagesFromToken(rewardToken),
};
Expand Down
32 changes: 13 additions & 19 deletions src/apps/votium/ethereum/votium.balance-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
import { Inject } from '@nestjs/common';

import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface';
import { Register } from '~app-toolkit/decorators';
import { presentBalanceFetcherResponse } from '~app-toolkit/helpers/presentation/balance-fetcher-response.present';
import { BalanceFetcher } from '~balance/balance-fetcher.interface';
import { Network } from '~types/network.interface';
import { VotiumRewardsBalancesHelper } from '../helpers/votium.rewards.balance-helper';

import { VotiumClaimableBalancesHelper } from '../helpers/votium.rewards.balance-helper';
import { VOTIUM_DEFINITION } from '../votium.definition';

const network = Network.ETHEREUM_MAINNET;

@Register.BalanceFetcher(VOTIUM_DEFINITION.id, network)
export class EthereumVotiumBalanceFetcher implements BalanceFetcher {
constructor(
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit,
@Inject(VotiumRewardsBalancesHelper)
private readonly rewardsBalancesHelper: VotiumRewardsBalancesHelper,
constructor(
@Inject(VotiumClaimableBalancesHelper)
private readonly rewardsBalancesHelper: VotiumClaimableBalancesHelper,
) {}

private async getRewardsBalances(address: string) {
private async getRewardsBalances(address: string) {
return this.rewardsBalancesHelper.getBalances({ address });
}

async getBalances(address: string) {
const [rewardsBalances] = await Promise.all([
this.getRewardsBalances(address)
async getBalances(address: string) {
const [rewardsBalances] = await Promise.all([this.getRewardsBalances(address)]);

return presentBalanceFetcherResponse([
{
label: 'Claimable',
assets: rewardsBalances,
},
]);
const rewards = await Promise.all(
rewardsBalances.map(async (rewardData) => {
return rewardData
})
);
return presentBalanceFetcherResponse([{
label: 'Claimable',
assets: rewards,
}])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PositionFetcher } from '~position/position-fetcher.interface';
import { ContractPosition } from '~position/position.interface';
import { Network } from '~types/network.interface';

import { VotiumContractFactory } from '../contracts';
import { VotiumRewardsToken } from '../helpers/votium.rewards.balance-helper';
import { VOTIUM_DEFINITION } from '../votium.definition';

const appId = VOTIUM_DEFINITION.id;
Expand All @@ -16,27 +16,19 @@ const network = Network.ETHEREUM_MAINNET;

@Register.ContractPositionFetcher({ appId, groupId, network })
export class EthereumVotiumClaimableContractPositionFetcher implements PositionFetcher<ContractPosition> {
constructor(
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit,
@Inject(VotiumContractFactory) private readonly votiumContractFactory: VotiumContractFactory,
) {}
constructor(@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit) {}

async getPositions() {
const rewardTokenAddresses = await Axios.get<VotiumRewardsToken[]>(
'https://raw.githubusercontent.com/oo-00/Votium/main/merkle/activeTokens.json',
).then(v => v.data.map(v => v.value.toLowerCase()));

async getPositions() {
const tokens = await Axios.get<VotiumRewardTokens>(
'https://raw.githubusercontent.com/oo-00/Votium/main/merkle/activeTokens.json',
).then(v => v.data);
const rewards = await Promise.all(
tokens.map(async tok => {
return tok.value;
})
);
return this.appToolkit.helpers.merkleContractPositionHelper.getContractPositions({
address: '0x378Ba9B73309bE80BF4C2c027aAD799766a7ED5A',
address: '0x378ba9b73309be80bf4c2c027aad799766a7ed5a',
appId,
groupId,
network,
dependencies: [],
rewardTokenAddresses: rewards,
rewardTokenAddresses,
});
}
}
109 changes: 47 additions & 62 deletions src/apps/votium/helpers/votium.rewards.balance-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
import Axios from 'axios';
import BigNumber from 'bignumber.js';
import { getAddress } from 'ethers/lib/utils';
import { fromPairs } from 'lodash';

import { drillBalance } from '~app-toolkit';
import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
Expand All @@ -12,7 +13,7 @@ import { Network } from '~types/network.interface';
import { VotiumContractFactory } from '../contracts';
import { VOTIUM_DEFINITION } from '../votium.definition';

type VotiumRewardsBalancesParams = {
type VotiumClaiambleBalancesParams = {
address: string;
};

Expand All @@ -22,25 +23,25 @@ type VotiumRewardsClaim = {
proof: string[];
};

type VotiumRewardsTokens = {
value: string;
label: string;
symbol: string;
decimals: uint8;
};

type VotiumRewardsRewardsData = {
merkleRoot: string;
tokenTotal: string;
claims: Record<string, VotiumRewardsClaim>;
};

export type VotiumRewardsToken = {
value: string;
label: string;
symbol: string;
decimals: number;
};

const appId = VOTIUM_DEFINITION.id;
const groupId = VOTIUM_DEFINITION.groups.rewards;
const groupId = VOTIUM_DEFINITION.groups.claimable;
const network = Network.ETHEREUM_MAINNET;

@Injectable()
export class VotiumRewardsBalancesHelper {
export class VotiumClaimableBalancesHelper {
constructor(
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit,
@Inject(VotiumContractFactory) private readonly votiumContractFactory: VotiumContractFactory,
Expand All @@ -51,62 +52,46 @@ export class VotiumRewardsBalancesHelper {
timeout: 15 * 60 * 1000,
})
async getCachedRewardsData() {
try {
const tokens = await Axios.get<VotiumRewardTokens>(
'https://raw.githubusercontent.com/oo-00/Votium/main/merkle/activeTokens.json',
).then(v => v.data);
var rewards = {};
await Promise.all(
tokens.map(async tok => {
rewards[tok.value] = await Axios.get<VotiumRewardsRewardsData>(
'https://raw.githubusercontent.com/oo-00/Votium/main/merkle/'+tok.symbol+'/'+tok.symbol+'.json',
).then(v => v.data.claims);
})
)

return rewards;

} catch(e) {
console.log(e);
return [];
}
try {
const tokens = await Axios.get<VotiumRewardsToken[]>(
'https://raw.githubusercontent.com/oo-00/Votium/main/merkle/activeTokens.json',
).then(v => v.data);

const claims = await Promise.all(
tokens.map(async tok => {
const claims = await Axios.get<VotiumRewardsRewardsData>(
'https://raw.githubusercontent.com/oo-00/Votium/main/merkle/' + tok.symbol + '/' + tok.symbol + '.json',
).then(v => v.data.claims);
return [tok.value.toLowerCase(), claims];
}),
);

return fromPairs(claims);
} catch (e) {
return [];
}
}

async getBalances({ address }: VotiumRewardsBalancesParams) {
async getBalances({ address }: VotiumClaiambleBalancesParams) {
const rewardsData = await this.getCachedRewardsData();
const checksumAddress = getAddress(address);

const VLCVX_MERKLE_ADDRESS = "0x378Ba9B73309bE80BF4C2c027aAD799766a7ED5A"
const baseTokens = await this.appToolkit.getAppContractPositions({
return this.appToolkit.helpers.contractPositionBalanceHelper.getContractPositionBalances({
address,
appId: VOTIUM_DEFINITION.id,
groupIds: [VOTIUM_DEFINITION.groups.claimable.id],
network,
groupId: VOTIUM_DEFINITION.groups.claimable.id,
network: Network.ETHEREUM_MAINNET,
resolveBalances: async ({ contractPosition, multicall }) => {
const contract = this.votiumContractFactory.votiumMultiMerkle(contractPosition);
const rewardToken = contractPosition.tokens.find(isClaimable)!;
if (!rewardsData[rewardToken.address]?.[checksumAddress]?.index) return [drillBalance(rewardToken, '0')];

const { index, amount } = rewardsData[rewardToken.address][checksumAddress];
const isClaimed = await multicall.wrap(contract).isClaimed(rewardToken.address, index);

const balanceRaw = new BigNumber(isClaimed ? '0' : amount);
return [drillBalance(rewardToken, balanceRaw.toFixed(0))];
},
});
const rewardsData = await this.getCachedRewardsData();
const checksumAddress = getAddress(address);

return this.appToolkit.helpers.contractPositionBalanceHelper.getContractPositionBalances({
address,
appId: VOTIUM_DEFINITION.id,
groupId: VOTIUM_DEFINITION.groups.claimable.id,
network: Network.ETHEREUM_MAINNET,
resolveBalances: async ({ contractPosition, multicall }) => {
const contract = this.votiumContractFactory.votiumMultiMerkle(contractPosition);
const rewardToken = contractPosition.tokens.find(isClaimable);

if (!rewardsData[rewardToken.address]) {
return [drillBalance(rewardToken, '0')];
}
if (!rewardsData[rewardToken.address][checksumAddress]) {
return [drillBalance(rewardToken, '0')];
}
if (!rewardsData[rewardToken.address][checksumAddress].index) {
return [drillBalance(rewardToken, '0')];
}
const { index, amount } = rewardsData[rewardToken.address][checksumAddress];
const isClaimed = await multicall.wrap(contract).isClaimed(rewardToken.address, index);

const balanceRaw = new BigNumber(isClaimed ? '0' : amount);
return [drillBalance(rewardToken, balanceRaw.toFixed(0))];
},
})
}
}
5 changes: 2 additions & 3 deletions src/apps/votium/votium.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import { AbstractApp } from '~app/app.dynamic-module';
import { VotiumContractFactory } from './contracts';
import { EthereumVotiumBalanceFetcher } from './ethereum/votium.balance-fetcher';
import { EthereumVotiumClaimableContractPositionFetcher } from './ethereum/votium.claimable.contract-position-fetcher';
import { VotiumClaimableBalancesHelper } from './helpers/votium.rewards.balance-helper';
import { VotiumAppDefinition, VOTIUM_DEFINITION } from './votium.definition';
import { VotiumRewardsBalancesHelper } from './helpers/votium.rewards.balance-helper';


@Register.AppModule({
appId: VOTIUM_DEFINITION.id,
providers: [
EthereumVotiumBalanceFetcher,
EthereumVotiumClaimableContractPositionFetcher,
VotiumAppDefinition,
VotiumRewardsBalancesHelper,
VotiumClaimableBalancesHelper,
VotiumContractFactory,
],
})
Expand Down

0 comments on commit 7a7eaca

Please sign in to comment.