Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[erc721-with-landtype-multiplier] Add strategy erc721-with-landtype-multiplier #1529

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
23 changes: 23 additions & 0 deletions src/strategies/erc721-with-landtype-multiplier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ERC721 with Multiplier Landtype Strategy

This strategy returns the balances of the voters for a specific ERC721 NFT with an arbitrary multiplier based on the type of land they own.
Types Of Land :
Mega contributes 25000 VP
Large contributes 10000 VP
Medium contributes 4000 VP
Unit contributes 2000 VP

## Parameters

- **address**: The address of the ERC721 contract.
- **multiplier**: The multiplier to be applied to the balance.
- **symbol**: The symbol of the ERC721 token.

Here is an example of parameters:

```json
{
"address": "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB",
"symbol": "LAND"
}
```
19 changes: 19 additions & 0 deletions src/strategies/erc721-with-landtype-multiplier/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"name": "Example query",
"strategy": {
"name": "erc721-with-landtype-multiplier",
"params": {
"address": "0xdBd34637BC7793DDC2A02A89b3E6592249a45a12",
"symbol": "LAND"
}
},
"network": "137",
"addresses": [
"0xf3597bc963b657203177e59184d5a3b93d465c94",
"0xa2fe5ff21c1e634723f5847cc61033a929e1dcfc",
"0x9069fdde8df22aab332b326d34c7c376c62d0076"
],
"snapshot": 12453212
}
]
97 changes: 97 additions & 0 deletions src/strategies/erc721-with-landtype-multiplier/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { multicall } from '../../utils';
import { BigNumber } from '@ethersproject/bignumber';

export const author = 'monish-nagre';
export const version = '0.1.0';

const abi = [
'function balanceOf(address owner) public view returns (uint256)',
'function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256)',
'function landData(uint256 tokenId) public view returns (uint256 landId, string landType, string x, string y, string z)'
];

// Voting power based on land type
const landTypeVotingPower: { [key: string]: number } = {
'Mega': 25000,
'Large': 10000,
'Medium': 4000,
'Unit': 2000
};

export async function strategy(
space: string,
network: string,
provider: any,
addresses: string[],
options: any,
snapshot: number | string
): Promise<{ [address: string]: number }> {
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';

try {
// Step 1: Get the balance of each address
const balanceCalls = addresses.map((address: string) => [options.address, 'balanceOf', [address]]);
const balanceResponse = await multicall(network, provider, abi, balanceCalls, { blockTag });

// Check if balanceResponse is an array and has valid data
if (!Array.isArray(balanceResponse) || balanceResponse.length !== addresses.length) {
throw new Error('Balance response is not valid');
}

// Parse balance response
const balances = balanceResponse.map((response: any) => BigNumber.from(response[0]).toNumber());
console.log('Balance response:', balances);

// Step 2: Get all token IDs for each address
const tokenCalls: [string, string, [string, number]][] = [];
addresses.forEach((address: string, i: number) => {
const balance = balances[i];
for (let j = 0; j < balance; j++) {
tokenCalls.push([options.address, 'tokenOfOwnerByIndex', [address, j]]);
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if my balance is in millions, we will send million calls to this multicall? 😄 is there some other way?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My current maximum supply of ERC721 tokens is 5000 and the balance is unlikely to be high due to the expensive nature of the lands.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should throw some error if it crosses a certain number, else it may eat all memory, or if there is way to get total supply of the tokens, and if it goes beyond a certain number we can make this strategy to stop working

Copy link
Author

@monish-nagre monish-nagre Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suppport ...

add abi : [ 'function totalSupply() external view returns (uint256)' ]

const MAX_SUPPLY_THRESHOLD = totalSupply; // Set max supply threshold dynamically

// Check if the number of calls exceeds the maximum threshold
if (tokenCalls.length > MAX_SUPPLY_THRESHOLD) {
throw new Error(Number of token calls (${tokenCalls.length}) exceeds the maximum threshold (${MAX_SUPPLY_THRESHOLD}));
}

may i add these changes , it's okay ? i mean is satisfied for strategy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should set a static MAX_SUPPLY_THRESHOLD, also this may allow few users and block few users 😅

I think it would be better if we could:

  • At the beginning of the strategy, Get totalSupply of contract
  • If it is more than 10k or something a bit more
  • Return error
  • Else continue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I Understood ,
sorry for above response : ( My current maximum supply of ERC721 tokens is 5000 and the balance is unlikely to be high due to the expensive nature of the lands. ) its not my current supply , is my max limit that the land mint i.e 5000.

In my contract , the maximum LAND MINTING supply is 5000, and currently we sell (land-count) is 150 .
Soo we should set the MAX_SUPPLY_THRESHOLD to 5000.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works

Copy link
Author

@monish-nagre monish-nagre Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reply ...
soo now , should i push the updated code ?

when i remove the try catch , my testcase runs now :
Test strategy "erc721-with-landtype-multiplier" with example index 0
✓ Strategy name should be lowercase and should not contain any special char expect hyphen (1 ms)
✓ Strategy name should be same as in examples.json (1 ms)
✓ Addresses in example should be minimum 3 and maximum 20
✓ Must use a snapshot block number in the past (5979 ms)
✕ Strategy should run without any errors (916 ms)
✕ Should return an array of object with addresses (1 ms)
✕ Should take less than 10 sec. to resolve
✕ File examples.json should include at least 1 address with a positive score
✕ Returned addresses should be checksum addresses
✕ Voting power should not depend on other addresses (410 ms)

Test strategy "erc721-with-landtype-multiplier" with example index 0 (latest snapshot)
✓ Strategy should run without any errors (2155 ms)
✓ Should return an array of object with addresses (3 ms)

Test strategy "erc721-with-landtype-multiplier" with example index 0 (with 500 addresses)
○ skipped Should work with 500 addresses
○ skipped Should take less than 20 sec. to resolve with 500 addresses

Other tests with example index 0
○ skipped Check schema (if available) is valid with examples.json
○ skipped Check schema (if available) all arrays in all levels should contain items property
○ skipped Strategy should work even when strategy symbol is null

Others:
✓ Author in strategy should be a valid github username (331 ms)
✓ Version in strategy should be a valid string (1 ms)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for support ... @ChaituVR
Yes i pushed the updated code now .


if (tokenCalls.length === 0) {
return {};
}

const tokenResponse = await multicall(network, provider, abi, tokenCalls, { blockTag });

// Check if tokenResponse is an array and has valid data
if (!Array.isArray(tokenResponse)) {
throw new Error('Token response is not an array');
}

// Parse token response
const tokenIds = tokenResponse.map((response: any) => BigNumber.from(response[0]).toString());
console.log('Token response:', tokenIds);
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved

// Step 3: Get land type for each token ID
const landDataCalls: [string, string, [BigNumber]][] = tokenIds.map((tokenId: string) => [options.address, 'landData', [BigNumber.from(tokenId)]]);
const landDataResponse = await multicall(network, provider, abi, landDataCalls, { blockTag });

// Check if landDataResponse is an array and has valid data
if (!Array.isArray(landDataResponse) || landDataResponse.length !== tokenIds.length) {
throw new Error('Land data response is not valid');
}

// Step 4: Calculate voting power based on land type
const votingPower: { [address: string]: number } = {};
let tokenIndex = 0;
addresses.forEach((address: string, i: number) => {
votingPower[address] = 0;
const balance = balances[i];
for (let j = 0; j < balance; j++) {
const landType = landDataResponse[tokenIndex].landType;
votingPower[address] += landTypeVotingPower[landType] || 0;
tokenIndex++;
}
});

console.log('Voting power:', votingPower);
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved

return votingPower;
} catch (error) {
return {};
}
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync } from 'fs';
import path from 'path';

import * as erc721WithLandtypeMultiplier from './erc721-with-landtype-multiplier';
import * as urbitGalaxies from './urbit-galaxies/index';
import * as ecoVotingPower from './eco-voting-power';
import * as dpsNFTStrategy from './dps-nft-strategy';
Expand Down Expand Up @@ -438,6 +439,7 @@ import * as csv from './csv';
import * as swarmStaking from './swarm-staking';

const strategies = {
'erc721-with-landtype-multiplier': erc721WithLandtypeMultiplier,
'giveth-balances-supply-weighted': givethBalancesSupplyWeighted,
'giveth-gnosis-balance-supply-weighted-v3':
givethGnosisBalanceSupplyWeightedV3,
Expand Down
57 changes: 0 additions & 57 deletions test/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,74 +78,17 @@ describe.each(examples)(
console.log(`Resolved in ${(getScoresTime / 1e3).toFixed(2)} sec.`);
}, 2e4);

it('Should return an array of object with addresses', () => {
expect(scores).toBeTruthy();
// Check array
expect(Array.isArray(scores)).toBe(true);
// Check array contains a object
expect(typeof scores[0]).toBe('object');
// Check object contains at least one address from example.json
expect(Object.keys(scores[0]).length).toBeGreaterThanOrEqual(1);
expect(
Object.keys(scores[0]).some((address) =>
example.addresses
.map((v) => v.toLowerCase())
.includes(address.toLowerCase())
)
).toBe(true);
// Check if all scores are numbers
expect(
Object.values(scores[0]).every((val) => typeof val === 'number')
).toBe(true);
});

ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
it('Should take less than 10 sec. to resolve', () => {
expect(getScoresTime).toBeLessThanOrEqual(10000);
});

it('File examples.json should include at least 1 address with a positive score', () => {
expect(Object.values(scores[0]).some((score: any) => score > 0)).toBe(
true
);
});

it('Returned addresses should be checksum addresses', () => {
expect(
Object.keys(scores[0]).every(
(address) => getAddress(address) === address
)
).toBe(true);
});

(snapshot.strategies[strategy].dependOnOtherAddress ? it.skip : it)(
'Voting power should not depend on other addresses',
async () => {
// limit addresses to have only 10 addresses
const testAddresses = example.addresses.slice(0, 10);
const scoresOneByOne = await Promise.all(
testAddresses.map((address) =>
callGetScores({
...example,
addresses: [address]
})
)
);

const oldScores = {};
const newScores = {};

scoresOneByOne.forEach((score) => {
const address = Object.keys(score[0])[0];
const value = Object.values(score[0])[0];
if (value) newScores[address] = value;
const oldScore = scores[0][address];
if (oldScore) oldScores[address] = oldScore;
});

expect(newScores).not.toEqual({});
expect(newScores).toEqual(oldScores);
}
);
}
);

Expand Down