Skip to content

Commit

Permalink
Automated lint (#1253)
Browse files Browse the repository at this point in the history
Co-authored-by: ChaituVR <[email protected]>
  • Loading branch information
github-actions[bot] and ChaituVR authored Aug 13, 2023
1 parent def02a8 commit 7063bba
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 145 deletions.
6 changes: 2 additions & 4 deletions src/strategies/sd-vote-boost-twavp-v2/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"decimals": 18,
"sampleSize": 10,
"sampleStep": 5,
"whiteListedAddress": [
"0x1c0D72a330F2768dAF718DEf8A19BAb019EEAd09"
]
"whiteListedAddress": ["0x1c0D72a330F2768dAF718DEf8A19BAb019EEAd09"]
}
},
"network": "1",
Expand All @@ -25,4 +23,4 @@
],
"snapshot": 17835000
}
]
]
265 changes: 143 additions & 122 deletions src/strategies/sd-vote-boost-twavp-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,134 +6,155 @@ export const version = '0.0.1';

// Used ABI
const abi = [
'function balanceOf(address account) external view returns (uint256)',
'function working_supply() external view returns (uint256)',
'function working_balances(address account) external view returns (uint256)'
'function balanceOf(address account) external view returns (uint256)',
'function working_supply() external view returns (uint256)',
'function working_balances(address account) external view returns (uint256)'
];

export async function strategy(
space,
network,
provider,
addresses,
options,
snapshot
space,
network,
provider,
addresses,
options,
snapshot
): Promise<Record<string, number>> {
// Maximum of 5 multicall!
if (options.sampleStep > 5) {
throw new Error('maximum of 5 call');
}

// Maximum of 20 whitelisted address
if (options.whiteListedAddress.length > 20) {
throw new Error('maximum of 20 whitelisted address');
}

// --- Create block number list for twavp
// Obtain last block number
let lastBlock = await provider.getBlockNumber();
// Create block tag
let blockTag = typeof snapshot === 'number' ? snapshot : lastBlock;
// Create block list
let blockList = getPreviousBlocks(blockTag, options.sampleStep, options.sampleSize);

// Query working balance of users
const workingBalanceQuery = addresses.map((address: any) => [
options.sdTokenGauge,
'working_balances',
[address]
]);

// Execute multicall `sampleStep` times
let response: number[] = [];
for (let i = 0; i < options.sampleStep; i++) {
// Use good block number
blockTag = blockList[i];

// Add mutlicall response to array
response.push(
await multicall(
network,
provider,
abi,
[
[options.sdTokenGauge, 'working_supply'],
[options.veToken, 'balanceOf', [options.liquidLocker]],
...workingBalanceQuery
],
{ blockTag }
)
)
};

// Get working supply
const workingSupply = response[response.length - 1][0][0]; // Last response, latest block
// Get voting power of liquid locker
const votingPowerLiquidLocker = response[response.length - 1][1][0]; // Last response, latest block

return Object.fromEntries(
Array(addresses.length)
.fill('x')
.map((_, i) => {
// Init array of working balances for user
let userWorkingBalances: BigNumber[] = [];

for (let j = 0; j < options.sampleStep; j++) {
// Add working balance to array.
userWorkingBalances.push(response[j][i + 2][0]);
}

// Get average working balance.
let averageWorkingBalance = average(userWorkingBalances, addresses[i], options.whiteListedAddress);

// Calculate voting power.
let votingPower =
workingSupply != 0
? (averageWorkingBalance).mul(votingPowerLiquidLocker).div(workingSupply.mul(BigNumber.from(10).pow(options.decimals)))
: 0;

// Return address and voting power
return [addresses[i], Number(votingPower)];
})
// Maximum of 5 multicall!
if (options.sampleStep > 5) {
throw new Error('maximum of 5 call');
}

// Maximum of 20 whitelisted address
if (options.whiteListedAddress.length > 20) {
throw new Error('maximum of 20 whitelisted address');
}

// --- Create block number list for twavp
// Obtain last block number
const lastBlock = await provider.getBlockNumber();
// Create block tag
let blockTag = typeof snapshot === 'number' ? snapshot : lastBlock;
// Create block list
const blockList = getPreviousBlocks(
blockTag,
options.sampleStep,
options.sampleSize
);

// Query working balance of users
const workingBalanceQuery = addresses.map((address: any) => [
options.sdTokenGauge,
'working_balances',
[address]
]);

// Execute multicall `sampleStep` times
const response: number[] = [];
for (let i = 0; i < options.sampleStep; i++) {
// Use good block number
blockTag = blockList[i];

// Add mutlicall response to array
response.push(
await multicall(
network,
provider,
abi,
[
[options.sdTokenGauge, 'working_supply'],
[options.veToken, 'balanceOf', [options.liquidLocker]],
...workingBalanceQuery
],
{ blockTag }
)
);
}

// Get working supply
const workingSupply = response[response.length - 1][0][0]; // Last response, latest block
// Get voting power of liquid locker
const votingPowerLiquidLocker = response[response.length - 1][1][0]; // Last response, latest block

return Object.fromEntries(
Array(addresses.length)
.fill('x')
.map((_, i) => {
// Init array of working balances for user
const userWorkingBalances: BigNumber[] = [];

for (let j = 0; j < options.sampleStep; j++) {
// Add working balance to array.
userWorkingBalances.push(response[j][i + 2][0]);
}

// Get average working balance.
const averageWorkingBalance = average(
userWorkingBalances,
addresses[i],
options.whiteListedAddress
);

// Calculate voting power.
const votingPower =
workingSupply != 0
? averageWorkingBalance
.mul(votingPowerLiquidLocker)
.div(
workingSupply.mul(BigNumber.from(10).pow(options.decimals))
)
: 0;

// Return address and voting power
return [addresses[i], Number(votingPower)];
})
);
}

function getPreviousBlocks(currentBlockNumber: number, numberOfBlocks: number, daysInterval: number): number[] {
// Estimate number of blocks per day
const blocksPerDay = 86400 / 12;
// Calculate total blocks interval
const totalBlocksInterval = blocksPerDay * daysInterval;
// Calculate block interval
const blockInterval = totalBlocksInterval / (numberOfBlocks - 1);

// Init array of block numbers
const blockNumbers: number[] = [];

for (let i = 0; i < numberOfBlocks; i++) {
// Calculate block number
let blockNumber = currentBlockNumber - totalBlocksInterval + (blockInterval * i);
// Add block number to array
blockNumbers.push(Math.round(blockNumber));
}

// Return array of block numbers
return blockNumbers;
function getPreviousBlocks(
currentBlockNumber: number,
numberOfBlocks: number,
daysInterval: number
): number[] {
// Estimate number of blocks per day
const blocksPerDay = 86400 / 12;
// Calculate total blocks interval
const totalBlocksInterval = blocksPerDay * daysInterval;
// Calculate block interval
const blockInterval = totalBlocksInterval / (numberOfBlocks - 1);

// Init array of block numbers
const blockNumbers: number[] = [];

for (let i = 0; i < numberOfBlocks; i++) {
// Calculate block number
const blockNumber =
currentBlockNumber - totalBlocksInterval + blockInterval * i;
// Add block number to array
blockNumbers.push(Math.round(blockNumber));
}

// Return array of block numbers
return blockNumbers;
}

function average(numbers: BigNumber[], address: string, whiteListedAddress: string[]): BigNumber {
// If no numbers, return 0 to avoid division by 0.
if (numbers.length === 0) return BigNumber.from(0);

// If address is whitelisted, return most recent working balance. i.e. no twavp applied.
if (whiteListedAddress.includes(address)) return numbers[numbers.length - 1];

// Init sum
let sum = BigNumber.from(0);
// Loop through all elements and add them to sum
for (let i = 0; i < numbers.length; i++) {
sum = sum.add(numbers[i]);
}

// Return sum divided by array length to get mean
return sum.div(numbers.length);
}
function average(
numbers: BigNumber[],
address: string,
whiteListedAddress: string[]
): BigNumber {
// If no numbers, return 0 to avoid division by 0.
if (numbers.length === 0) return BigNumber.from(0);

// If address is whitelisted, return most recent working balance. i.e. no twavp applied.
if (whiteListedAddress.includes(address)) return numbers[numbers.length - 1];

// Init sum
let sum = BigNumber.from(0);
// Loop through all elements and add them to sum
for (let i = 0; i < numbers.length; i++) {
sum = sum.add(numbers[i]);
}

// Return sum divided by array length to get mean
return sum.div(numbers.length);
}
40 changes: 21 additions & 19 deletions src/validations/karma-eas-attestation/schema.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Validation",
"definitions": {
"Validation": {
"title": "Eas attestation",
"type": "object",
"properties": {
"schemaId": {
"type": "string",
"title": "Schema Id",
"examples": ["e.g. 0xc0f979976278d9e1d4fa97b7270c0cc07835aa5f27dd897a871b3332ec6cff22"]
}
},
"required": [],
"additionalProperties": false
}
}
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Validation",
"definitions": {
"Validation": {
"title": "Eas attestation",
"type": "object",
"properties": {
"schemaId": {
"type": "string",
"title": "Schema Id",
"examples": [
"e.g. 0xc0f979976278d9e1d4fa97b7270c0cc07835aa5f27dd897a871b3332ec6cff22"
]
}
},
"required": [],
"additionalProperties": false
}
}
}

0 comments on commit 7063bba

Please sign in to comment.