|
| 1 | +import { BigNumberish } from '@ethersproject/bignumber'; |
| 2 | +import { formatUnits } from '@ethersproject/units'; |
| 3 | +import { Multicaller } from '../../utils'; |
| 4 | + |
| 5 | +export const author = 'JD0x2e'; |
| 6 | +export const version = '0.1.2'; |
| 7 | + |
| 8 | +const mfdAbi = [ |
| 9 | + 'function getUserLocks(address) view returns (tuple(uint256 amount, uint256 unlockTime, uint256 multiplier, uint256 duration, uint256 lockCreationTime)[])' |
| 10 | +]; |
| 11 | + |
| 12 | +const erc20Abi = ['function balanceOf(address) view returns (uint256)']; |
| 13 | + |
| 14 | +const toJsNum = (bn: BigNumberish) => parseFloat(formatUnits(bn)); |
| 15 | + |
| 16 | +export async function strategy( |
| 17 | + space, |
| 18 | + network, |
| 19 | + provider, |
| 20 | + addresses, |
| 21 | + options, |
| 22 | + snapshot |
| 23 | +): Promise<Record<string, number>> { |
| 24 | + const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; |
| 25 | + |
| 26 | + const MFD_CONTRACT = options.lockingContract; |
| 27 | + const HOME_TOKEN = options.homeToken; |
| 28 | + |
| 29 | + const locksMulticaller = new Multicaller(network, provider, mfdAbi, { |
| 30 | + blockTag |
| 31 | + }); |
| 32 | + const homeMulticaller = new Multicaller(network, provider, erc20Abi, { |
| 33 | + blockTag |
| 34 | + }); |
| 35 | + |
| 36 | + addresses.forEach((address) => { |
| 37 | + locksMulticaller.call(address, MFD_CONTRACT, 'getUserLocks', [address]); |
| 38 | + homeMulticaller.call(address, HOME_TOKEN, 'balanceOf', [address]); |
| 39 | + }); |
| 40 | + |
| 41 | + const [lockResults, homeBalances] = await Promise.all([ |
| 42 | + locksMulticaller.execute(), |
| 43 | + homeMulticaller.execute() |
| 44 | + ]); |
| 45 | + |
| 46 | + return Object.fromEntries( |
| 47 | + addresses.map((address) => { |
| 48 | + const locks = lockResults[address] ?? []; |
| 49 | + const sHome = locks.reduce((acc, lock) => acc + toJsNum(lock.amount), 0); |
| 50 | + const home = toJsNum(homeBalances[address] ?? 0); |
| 51 | + |
| 52 | + const weightedPower = sHome * 4 + home; |
| 53 | + return [address, weightedPower]; |
| 54 | + }) |
| 55 | + ); |
| 56 | +} |
0 commit comments