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

[cgpt-voting-power] Adding cgpt-voting-power strategy #1151

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/strategies/cgpt-voting-power/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# cgpt-voting-power: Voting Power by Contract for ChainGPT DAO

This strategy determines the voting power of DAO users based on the `getVotingPower` function of a specified contract.

## Configuration

The strategy requires the following parameters:

- `address`: The contract address where the `getVotingPower` function is defined.
- `decimals`: The number of decimals used by the token.

An optional parameter is:

- `symbol`: The token symbol (for informational purposes or UI display).

Here is an example of parameters:

```json
{
"address": "0xf97f22eddcaf2e891f0d528bda6113874f292b91",
"symbol": "CGPT",
"decimals": 18
}
36 changes: 36 additions & 0 deletions src/strategies/cgpt-voting-power/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "Example Query",
"strategy": {
"name": "cgpt-voting-power",
"params": {
"address": "0xf97f22eddcaf2e891f0d528bda6113874f292b91",
"decimals": 18
}
},
"network": "1",
"addresses": [
"0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"0x53d284357ec70cE289D6D64134DfAc8E511c8a3D"
],
"snapshot": 12419867,
"example": {
"input": {
"space": {},
"network": "1",
"provider": {},
"addresses": [
"0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"0x53d284357ec70cE289D6D64134DfAc8E511c8a3D"
],
"options": {
"address": "0xf97f22eddcaf2e891f0d528bda6113874f292b91",
"decimals": 18
},
"snapshot": 12419867
},
"output": {
"0x742d35Cc6634C0532925a3b844Bc454e4438f44e": 1000,
"0x53d284357ec70cE289D6D64134DfAc8E511c8a3D": 2000
}
}
}
40 changes: 40 additions & 0 deletions src/strategies/cgpt-voting-power/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BigNumberish } from '@ethersproject/bignumber';
import { formatUnits } from '@ethersproject/units';
import { Multicaller } from '../../utils';

export const author = 'ChainGPT';
export const version = '0.1.0';

const abi = [
{
"inputs": [{"internalType": "address", "name": "user", "type": "address"}],
"name": "getVotingPower",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function"
}
];

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

const multi = new Multicaller(network, provider, abi, { blockTag });
addresses.forEach((address) =>
multi.call(address, options.address, 'getVotingPower', [address])
);
const result: Record<string, BigNumberish> = await multi.execute();

return Object.fromEntries(
Object.entries(result).map(([address, votingPower]) => [
address,
parseFloat(formatUnits(votingPower, options.decimals))
])
Comment on lines +26 to +38
Copy link
Member

Choose a reason for hiding this comment

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

Hi @chaingptuser You can use the contract-call strategy for this, no need to create a separate strategy, can try it?

);
}
27 changes: 27 additions & 0 deletions src/strategies/cgpt-voting-power/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "cgpt-voting-power",
"type": "object",
"properties": {
"address": {
"type": "string",
"title": "Contract Address",
"pattern": "^0x[a-fA-F0-9]{40}$"
},
"symbol": {
"type": "string",
"title": "CGPT",
"minLength": 1,
"maxLength": 10
},
"decimals": {
"type": "integer",
"title": "Decimals",
"default": 18,
"minimum": 0,
"maximum": 77
}
},
"required": ["address", "decimals"],
"additionalProperties": false
}