-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cookie-staking] cookie staking strategy (#1611)
* updates * some test are passing * test passing * reverted unnecesary changes
- Loading branch information
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[ | ||
{ | ||
"name": "Example query", | ||
"strategy": { | ||
"name": "cookie-staking", | ||
"params": { | ||
"address": "0x036b6BA384656151471f348fccf5c2818b9223aE", | ||
"decimals": 18 | ||
} | ||
}, | ||
"network": "56", | ||
"addresses": [ | ||
"0xaE7EF51F517380Ca0211A60BeA83596080ddF478", | ||
"0xd0825555aA656c56e687A6412d40534ba7f0E096", | ||
"0x7018b9aB744cd438De14e9186De43698A29A7aAA" | ||
], | ||
"snapshot": 43137800 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Multicaller } from '../../utils'; | ||
import { formatUnits } from '@ethersproject/units'; | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
|
||
export const author = 'spaceh3ad'; | ||
export const version = '0.1.0'; | ||
|
||
const abi = [ | ||
'function users(uint256,address) view returns (uint256 shares, uint256 lastDepositedTime, uint256 totalInvested, uint256 totalClaimed)' | ||
]; | ||
|
||
interface UserData { | ||
shares: BigNumber; | ||
lastDepositedTime: BigNumber; | ||
totalInvested: BigNumber; | ||
totalClaimed: BigNumber; | ||
} | ||
|
||
export async function strategy( | ||
space, | ||
network, | ||
provider, | ||
addresses, | ||
options, | ||
snapshot | ||
) { | ||
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; | ||
|
||
const multi = new Multicaller(network, provider, abi, { blockTag }); | ||
|
||
const poolIds = 11; | ||
|
||
// Prepare multicall | ||
addresses.forEach((address) => { | ||
for (let poolId = 0; poolId < poolIds; poolId++) { | ||
multi.call(`${address}-${poolId}`, options.address, 'users', [ | ||
poolId, | ||
address | ||
]); | ||
} | ||
}); | ||
|
||
// Typecast result to Record<string, UserData> | ||
const result = (await multi.execute()) as Record<string, UserData>; | ||
|
||
// Initialize a mapping for user totals | ||
const userTotals: { [address: string]: BigNumber } = {}; | ||
|
||
// Process the result | ||
for (const [key, userData] of Object.entries(result)) { | ||
const address = key.split('-')[0]; | ||
|
||
const totalInvested = BigNumber.from(userData.totalInvested); | ||
const totalClaimed = BigNumber.from(userData.totalClaimed); | ||
|
||
if (!userTotals[address]) { | ||
userTotals[address] = BigNumber.from(0); | ||
} | ||
|
||
const amount = totalInvested.sub(totalClaimed); | ||
|
||
userTotals[address] = userTotals[address].add(amount); | ||
} | ||
|
||
const formattedResult = Object.fromEntries( | ||
addresses.map((address) => [ | ||
address, | ||
parseFloat( | ||
formatUnits( | ||
userTotals[address] || BigNumber.from(0), | ||
options.decimals || 18 | ||
) | ||
) | ||
]) | ||
); | ||
|
||
return formattedResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"type": "object", | ||
"definitions": { | ||
"Strategy": { | ||
"title": "Strategy", | ||
"type": "object", | ||
"properties": { | ||
"address": { | ||
"type": "string", | ||
"pattern": "^0x[a-fA-F0-9]{40}$", | ||
"minLength": 42, | ||
"maxLength": 42 | ||
}, | ||
"decimals": { | ||
"type": "integer", | ||
"minimum": 0 | ||
} | ||
}, | ||
"required": ["address"], | ||
"additionalProperties": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters