Skip to content

Commit

Permalink
[mcn-farm] Update MCN strategy to handle different reward token posit…
Browse files Browse the repository at this point in the history
…ions (snapshot-labs#83)

* Add mcn-farm strategy

* Use string abi

Co-authored-by: Chaitanya <[email protected]>

* Allow address passing from params

* mcn-farm consider reward token index

Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
cinnaminty-d and ChaituVR authored Sep 28, 2021
1 parent cffde2f commit ce7ab67
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/strategies/mcn-farm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ const abi = [
'function totalSupply() view returns (uint256)',
'function balanceOf(address account) view returns (uint256)',
'function getPoolList() view returns (address[])',
'function getUser(address _lpToken, address _account) view returns (tuple(uint256 amount, uint256[] rewardsWriteoffs), uint256[])'
'function getPool(address _lpToken) view returns (tuple(tuple(address bonusTokenAddr, uint48 startTime, uint48 endTime, uint256 weeklyRewards, uint256 accRewardsPerToken, uint256 remBonus)[] bonuses, uint256 lastUpdatedAt, uint256 amount))',
'function getUser(address _lpToken, address _account) view returns (tuple(uint256 amount, uint256[] rewardsWriteoffs) user, uint256[] rewards)'
];

export async function strategy(
space,
network,
provider,
addresses,
addresses: string[],
options,
snapshot
) {
Expand All @@ -44,21 +45,40 @@ export async function strategy(
[
[lpAddress, 'totalSupply', []],
[tokenAddress, 'balanceOf', [lpAddress]]
].concat(params.map((p) => [farmAddress, 'getUser', p])),
]
.concat(pools.map((p) => [farmAddress, 'getPool', [p]]))
.concat(params.map((p) => [farmAddress, 'getUser', p])),
{ blockTag }
);
const [totalSupply] = res[0];
const [tokenBalanceInLP] = res[1];
const tokensPerLP = tokenBalanceInLP.div(totalSupply);
const response = res.slice(2);
const values = {};
const poolInfo = res.slice(2, 2 + pools.length);
// rewardToken_i maps pool index => pool bonus token index matching tokenAddress (if applicable)
let rewardToken_i = {};
for (let i = 0; i < pools.length; i++) {
let bonuses = poolInfo[i][0].bonuses;
if (bonuses === undefined) continue;
for (let j = 0; j < bonuses.length; j++) {
if (bonuses[j].bonusTokenAddr == tokenAddress) {
rewardToken_i[i] = j;
continue;
}
}
}
const response = res.slice(2 + pools.length);
let values = {};
Object.values(addresses).forEach(
(address: string) => (values[address] = BigNumber.from(0))
);
response.forEach(([userInfo, rewards], i) => {
const address_i = i % addresses.length;
const address = addresses[address_i];
const pool_i = Math.floor(i / addresses.length);
values[address] = values.hasOwnProperty(address)
? values[address].add(rewards[0])
: BigNumber.from(rewards[0]);
let address_i = i % addresses.length;
let address = addresses[address_i];
let pool_i = Math.floor(i / addresses.length);
let bonus_i = rewardToken_i[pool_i];
if (bonus_i && rewards.length > bonus_i) {
values[address].add(rewards[bonus_i]);
}
if (pool_i === 0) {
// this is the MCN staking pool
values[address] = values[address].add(userInfo.amount); // add staked amount
Expand All @@ -68,9 +88,9 @@ export async function strategy(
}
});

for (const address in values) {
for (let address in values) {
if (values.hasOwnProperty(address)) {
const value = parseFloat(formatUnits(values[address], 18));
let value = parseFloat(formatUnits(values[address], 18));
values[address] = value;
}
}
Expand Down

0 comments on commit ce7ab67

Please sign in to comment.