Skip to content

Commit

Permalink
[cookie-staking] cookie staking strategy (#1611)
Browse files Browse the repository at this point in the history
* updates

* some test are passing

* test passing

* reverted unnecesary changes
  • Loading branch information
spaceh3ad authored Oct 15, 2024
1 parent ea7dcd6 commit a34aa66
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/strategies/cookie-staking/examples.json
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
}
]
78 changes: 78 additions & 0 deletions src/strategies/cookie-staking/index.ts
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;
}
23 changes: 23 additions & 0 deletions src/strategies/cookie-staking/schema.json
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
}
}
}
2 changes: 2 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ import * as rep3Badges from './rep3-badges';
import * as marsecosystem from './marsecosystem';
import * as ari10StakingLocked from './ari10-staking-locked';
import * as skaleDelegationWeighted from './skale-delegation-weighted';
import * as cookieStaking from './cookie-staking';
import * as reliquary from './reliquary';
import * as acrossStakedAcx from './across-staked-acx';
import * as lodestarVesting from './lodestar-vesting';
Expand Down Expand Up @@ -817,6 +818,7 @@ const strategies = {
'ari10-staking-locked': ari10StakingLocked,
'skale-delegation-weighted': skaleDelegationWeighted,
reliquary,
'cookie-staking': cookieStaking,
'jpegd-locked-jpeg-of': jpegdLockedJpegOf,
'lodestar-vesting': lodestarVesting,
'lodestar-staked-lp': lodestarStakedLp,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4568,4 +4568,4 @@ yargs@^17.7.2:
yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

0 comments on commit a34aa66

Please sign in to comment.