-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move offchain space VP computation to sx.js (#115)
This is still a bit hacky as we have to work within constrains of some assumptions from SX (per-strategy validation, multiple proposal validation strategies), so I tried to make it "fit". As strategies are handled at Score API, sx.js's strategies are abstract wrappers to different types of validations, for example: 1. remote-vp: validates VP using get_vp call for all enabled strategies 2. remote-validate: validates proposal using validate call 3. only-members: not really abstract, performs validation offline
- Loading branch information
Showing
10 changed files
with
148 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@snapshot-labs/sx": patch | ||
--- | ||
|
||
add getOffchainStrategy function for computing voting power |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { getStrategy as getEvmStrategy } from './evm'; | ||
export { getStrategy as getStarknetStrategy } from './starknet'; | ||
export { getStrategy as getOffchainStrategy } from './offchain'; |
This file contains 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,14 @@ | ||
import createOnlyMembersStrategy from './only-members'; | ||
import createRemoteValidateStrategy from './remote-validate'; | ||
import createRemoteVpStrategy from './remote-vp'; | ||
import { Strategy } from '../../clients/offchain/types'; | ||
|
||
export function getStrategy(name: string): Strategy | null { | ||
if (name === 'only-members') return createOnlyMembersStrategy(); | ||
|
||
if (['any', 'basic', 'passport-gated', 'arbitrum', 'karma-eas-attestation'].includes(name)) { | ||
return createRemoteValidateStrategy(name); | ||
} | ||
|
||
return createRemoteVpStrategy(); | ||
} |
This file contains 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,14 @@ | ||
import { Strategy } from '../../clients/offchain/types'; | ||
|
||
export default function createOnlyMembersStrategy(): Strategy { | ||
return { | ||
type: 'only-members', | ||
async getVotingPower(voterAddress: string, params: any) { | ||
const isValid = params[0].addresses | ||
.map((address: string) => address.toLowerCase()) | ||
.includes(voterAddress.toLowerCase()); | ||
|
||
return [isValid ? 1n : 0n]; | ||
} | ||
}; | ||
} |
This file contains 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,20 @@ | ||
import { fetchScoreApi } from './utils'; | ||
import { Strategy, SnapshotInfo } from '../../clients/offchain/types'; | ||
|
||
export default function createRemoteValidateStrategy(type: string): Strategy { | ||
return { | ||
type, | ||
async getVotingPower(voterAddress: string, params: any, snapshotInfo: SnapshotInfo) { | ||
const isValid = await fetchScoreApi('validate', { | ||
validation: type, | ||
author: voterAddress, | ||
space: '', | ||
network: snapshotInfo.chainId, | ||
snapshot: snapshotInfo.at ?? 'latest', | ||
params: params[0] | ||
}); | ||
|
||
return [isValid ? 1n : 0n]; | ||
} | ||
}; | ||
} |
This file contains 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,24 @@ | ||
import { fetchScoreApi } from './utils'; | ||
import { Strategy, SnapshotInfo } from '../../clients/offchain/types'; | ||
|
||
export default function createRemoteVpStrategy(): Strategy { | ||
return { | ||
type: 'remote-vp', | ||
async getVotingPower(voterAddress: string, params: any, snapshotInfo: SnapshotInfo) { | ||
const result = await fetchScoreApi('get_vp', { | ||
address: voterAddress, | ||
space: '', | ||
strategies: params, | ||
network: snapshotInfo.chainId, | ||
snapshot: snapshotInfo.at ?? 'latest' | ||
}); | ||
|
||
return result.vp_by_strategy.map((vp: number, i: number) => { | ||
const strategy = params[i]; | ||
const decimals = parseInt(strategy.params.decimals || 0); | ||
|
||
return BigInt(vp * 10 ** decimals); | ||
}); | ||
} | ||
}; | ||
} |
This file contains 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,29 @@ | ||
const SCORE_URL = 'https://score.snapshot.org'; | ||
|
||
export async function fetchScoreApi( | ||
method: 'validate' | 'get_vp', | ||
params: Record<string, any> | ||
): Promise<any> { | ||
try { | ||
const response = await fetch(SCORE_URL, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
jsonrpc: '2.0', | ||
method, | ||
params | ||
}) | ||
}); | ||
|
||
const body = await response.json(); | ||
|
||
if (body.error) throw new Error(body.error.message); | ||
|
||
return body.result; | ||
} catch (e) { | ||
throw new Error('Failed to fetch score API'); | ||
} | ||
} |