-
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: add basic voting for offchain spaces (#36)
* fix: show voting buttons for offchain proposals * refactor: move the network actions to actions.ts * feat: add support for basic voting for offchain proposals * fix: fix invalid import type * fix: use correct sequencer network depending on chainId * chore: add tests * Update packages/sx.js/src/clients/offchain/ethereum-sig/index.ts Co-authored-by: Wiktor Tkaczyński <wiktor.tkaczynski@gmail.com> * refactor: use same function order definition as other networks * fix: handle offchain receipt without tx_hash * fix: fix typing * fix: throw error on sequencer error response * chore: add unit test * chore: fix tests * fix: show voting form only for supported voting type * chore: remove cumbersome describe block * fix: switch to correct chain for offchain actions * fix: avoid changing type * chore: improve test * fix: revert network enforcer * fix: remove unused import * Update packages/sx.js/src/clients/starknet/starknet-tx/index.ts Co-authored-by: Wiktor Tkaczyński <wiktor.tkaczynski@gmail.com> * chore: update changeset --------- Co-authored-by: Wiktor Tkaczyński <wiktor.tkaczynski@gmail.com>
- Loading branch information
Showing
21 changed files
with
498 additions
and
102 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": minor | ||
--- | ||
|
||
add OffchainEthereumSig client with basic vote support |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { OffchainNetworkConfig, clients, offchainGoerli, offchainMainnet } from '@snapshot-labs/sx'; | ||
import { getSdkChoice } from './helpers'; | ||
import type { Web3Provider } from '@ethersproject/providers'; | ||
import type { StrategyParsedMetadata, Choice, Proposal } from '@/types'; | ||
import type { | ||
ReadOnlyNetworkActions, | ||
NetworkConstants, | ||
NetworkHelpers, | ||
SnapshotInfo, | ||
VotingPower, | ||
Connector | ||
} from '../types'; | ||
|
||
const SCORE_URL = 'https://score.snapshot.org'; | ||
const CONFIGS: Record<number, OffchainNetworkConfig> = { | ||
1: offchainMainnet, | ||
5: offchainGoerli | ||
}; | ||
|
||
export function createActions( | ||
constants: NetworkConstants, | ||
helpers: NetworkHelpers, | ||
chainId: number | ||
): ReadOnlyNetworkActions { | ||
const networkConfig = CONFIGS[chainId]; | ||
|
||
const client = new clients.OffchainEthereumSig({ | ||
networkConfig | ||
}); | ||
|
||
return { | ||
vote( | ||
web3: Web3Provider, | ||
connectorType: Connector, | ||
account: string, | ||
proposal: Proposal, | ||
choice: Choice | ||
): Promise<any> { | ||
const data = { | ||
space: proposal.space.id, | ||
proposal: proposal.proposal_id as string, | ||
choice: getSdkChoice(choice), | ||
authenticator: '', | ||
strategies: [], | ||
metadataUri: '' | ||
}; | ||
|
||
return client.vote({ | ||
signer: web3.getSigner(), | ||
data | ||
}); | ||
}, | ||
send: (envelope: any) => client.send(envelope), | ||
getVotingPower: async ( | ||
strategiesAddresses: string[], | ||
strategiesParams: any[], | ||
strategiesMetadata: StrategyParsedMetadata[], | ||
voterAddress: string, | ||
snapshotInfo: SnapshotInfo | ||
): Promise<VotingPower[]> => { | ||
const result = await fetch(SCORE_URL, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
method: 'get_vp', | ||
params: { | ||
address: voterAddress, | ||
space: '', | ||
strategies: strategiesParams, | ||
network: snapshotInfo.chainId ?? chainId, | ||
snapshot: snapshotInfo.at ?? 'latest' | ||
} | ||
}) | ||
}); | ||
const body = await result.json(); | ||
|
||
return body.result.vp_by_strategy.map((vp: number, index: number) => { | ||
const strategy = strategiesParams[index]; | ||
const decimals = parseInt(strategy.params.decimals || 0); | ||
|
||
return { | ||
address: strategy.name, | ||
value: BigInt(vp * 10 ** decimals), | ||
decimals, | ||
symbol: strategy.params.symbol, | ||
token: strategy.params.address, | ||
chainId: strategy.network ? parseInt(strategy.network) : undefined | ||
} as VotingPower; | ||
}); | ||
} | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Choice } from '@/types'; | ||
|
||
export function getSdkChoice(choice: Choice): number { | ||
if (choice === 'for') return 1; | ||
if (choice === 'against') return 2; | ||
return 3; | ||
} |
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
Oops, something went wrong.