This repository was archived by the owner on Aug 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 822
[erc721-contract-call] feat: erc721-contract-call #1703
Open
abysnart
wants to merge
1
commit into
snapshot-labs:master
Choose a base branch
from
abysnart:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
| # erc721-contract-call | ||
|
|
||
| This is a strategy that calls a function on your NFT contract to retrieve the voting power for each token ID owned by an address | ||
|
|
||
| Here is an example of parameters: | ||
|
|
||
| ```json | ||
| { | ||
| "address": "0x1234567890123456789012345678901234567890", // Your NFT contract address | ||
| "symbol": "xSGT", | ||
| "decimals": 0 | ||
| } | ||
| ``` |
This file contains hidden or 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,11 @@ | ||
| { | ||
| "name": "xSGT NFT Voting Power Example", | ||
| "strategy": { | ||
| "name": "xsgt-nft-voting-power", | ||
| "params": { | ||
| "address": "0x1234567890123456789012345678901234567890", | ||
| "symbol": "xSGT", | ||
| "decimals": 0 | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,105 @@ | ||
| import { formatUnits } from '@ethersproject/units'; | ||
|
Check failure on line 1 in src/strategies/erc721-contract-call/index.ts
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint / tests are failing in this fail, could take a look? |
||
| import { getAddress } from '@ethersproject/address'; | ||
| import { multicall } from '../../utils'; | ||
|
|
||
| export const author = 'abysnart'; | ||
| export const version = '1.0.0'; | ||
| export const name = 'xSGT NFT Voting Power'; | ||
| export const description = 'Strategy for xSGT NFT where each token ID has a specific voting power'; | ||
|
|
||
| // Define interfaces | ||
| interface StrategyOptions { | ||
| address: string; | ||
| symbol: string; | ||
| decimals: number; | ||
| } | ||
|
|
||
| type Scores = Record<string, number>; | ||
|
|
||
| // Define ABIs for the required calls | ||
| const abi = [ | ||
| // Get all tokens owned by an address | ||
| 'function tokensOfOwner(address _owner) external view returns (uint256[])', | ||
| // Get voting power for a specific token ID | ||
| 'function getVotingPower(uint256 _tokenId) external view returns (uint256)' | ||
| ]; | ||
|
|
||
| export async function strategy( | ||
| space: string, | ||
| network: string, | ||
| provider: any, | ||
| addresses: string[], | ||
| options: StrategyOptions, | ||
| snapshot: number | string | ||
| ): Promise<Scores> { | ||
| const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; | ||
| const contractAddress = options.address; // NFT contract address | ||
|
|
||
| const scores: Scores = {}; | ||
| const addressesLowercased = addresses.map(address => address.toLowerCase()); | ||
|
|
||
| // Initialize scores with 0 | ||
| addressesLowercased.forEach(address => { | ||
| scores[getAddress(address)] = 0; | ||
| }); | ||
|
|
||
| // First, get all token IDs owned by each address | ||
| const tokenOwnershipCalls = addresses.map(address => { | ||
| return { | ||
| target: contractAddress, | ||
| params: [address], | ||
| name: 'tokensOfOwner' | ||
| }; | ||
| }); | ||
|
|
||
| const multi = new multicall(provider, network); | ||
| const ownershipResponse: any[][] = await multi.call( | ||
| network, | ||
| provider, | ||
| abi, | ||
| tokenOwnershipCalls, | ||
| { blockTag } | ||
| ); | ||
|
|
||
| // Prepare calls to get voting power for each token ID | ||
| const votingPowerCalls: { target: string; params: string[]; name: string }[] = []; | ||
| const addressIndices: number[] = []; | ||
|
|
||
| addresses.forEach((address, addrIndex) => { | ||
| const tokenIds = ownershipResponse[addrIndex] || []; | ||
|
|
||
| // For each token ID owned by this address, prepare a call to get its voting power | ||
| tokenIds.forEach((tokenId: any) => { | ||
| votingPowerCalls.push({ | ||
| target: contractAddress, | ||
| params: [tokenId.toString()], | ||
| name: 'getVotingPower' | ||
| }); | ||
| addressIndices.push(addrIndex); | ||
| }); | ||
| }); | ||
|
|
||
| if (votingPowerCalls.length === 0) { | ||
| return scores; | ||
| } | ||
|
|
||
| // Make calls to get voting power for each token ID | ||
| const votingPowerResponse: any[][] = await multi.call( | ||
| abi, | ||
| votingPowerCalls, | ||
| { blockTag } | ||
| ); | ||
|
|
||
| // Sum up voting power for each address | ||
| votingPowerResponse.forEach((power, index) => { | ||
| const addressIndex = addressIndices[index]; | ||
| const address = getAddress(addresses[addressIndex]); | ||
|
|
||
| // Add this token's voting power to the address's total | ||
| if (power && power[0]) { | ||
| scores[address] += parseFloat(formatUnits(power[0], 0)); | ||
| } | ||
| }); | ||
|
|
||
| return scores; | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "$ref": "#/definitions/Strategy", | ||
| "definitions": { | ||
| "Strategy": { | ||
| "title": "xSGT NFT Voting Power", | ||
| "type": "object", | ||
| "properties": { | ||
| "address": { | ||
| "type": "string", | ||
| "title": "Contract Address", | ||
| "description": "The address of the xSGT NFT contract", | ||
| "format": "address", | ||
| "examples": ["0x1234567890123456789012345678901234567890"] | ||
| }, | ||
| "symbol": { | ||
| "type": "string", | ||
| "title": "Symbol", | ||
| "description": "The symbol of the NFT token", | ||
| "examples": ["xSGT"] | ||
| }, | ||
| "decimals": { | ||
| "type": "number", | ||
| "title": "Decimals", | ||
| "description": "The number of decimals for the voting power (typically 0 for NFTs)", | ||
| "default": 0 | ||
| } | ||
| }, | ||
| "required": ["address"], | ||
| "additionalProperties": false | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be same as strategy name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually the name
xsgt-nft-voting-powermakes more sense thanerc721-contract-call