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

feat: add governance apis #136

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
834 changes: 834 additions & 0 deletions src/abis/governance/election-governor-abi.ts

Large diffs are not rendered by default.

739 changes: 739 additions & 0 deletions src/abis/governance/vclob-abi.ts

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions src/apis/vclob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { PublicClient } from 'viem'

import { CHAIN_IDS } from '../constants/chain'
import { Subgraph } from '../constants/subgraph'
import { VCLOB as VCLOBModel } from '../model/vclob'
import { CONTRACT_ADDRESSES } from '../constants/addresses'
import { VCLOB_ABI } from '../abis/governance/vclob-abi'
import { VCLOB } from '../type'

const fetchVCLOBListFromSubgraph = async (
chainId: CHAIN_IDS,
userAddress: `0x${string}`,
) => {
const result = await Subgraph.get<{
data: {
vclobs: VCLOBModel[]
}
}>(
chainId,
'getVCLOBList',
'query getVCLOBList($owner: String!) { vclobs( where: { owner: $owner } ) { id owner amount lockedTimepoint } }',
{
owner: userAddress,
},
)
return result.data.vclobs
}

export const fetchVCLOBList = async (
publicClient: PublicClient,
chainId: CHAIN_IDS,
userAddress: `0x${string}`,
useSubgraph: boolean,
): Promise<VCLOB[]> => {
if (!useSubgraph) {
return []
}
const [depositDuration, withdrawDuration] = await publicClient.multicall({
contracts: [
{
address: CONTRACT_ADDRESSES[chainId]!.VoteLockedCloberToken,
abi: VCLOB_ABI,
functionName: 'depositDuration',
},
{
address: CONTRACT_ADDRESSES[chainId]!.VoteLockedCloberToken,
abi: VCLOB_ABI,
functionName: 'withdrawDuration',
},
],
allowFailure: false,
})
const currentTimepoint = BigInt(Math.floor(Date.now() / 1000))
const vclobs = await fetchVCLOBListFromSubgraph(chainId, userAddress)
return vclobs.map((vclob) => {
const phaseDuration = depositDuration + withdrawDuration
const phase = (currentTimepoint - vclob.lockedTimepoint) / phaseDuration
const withdrawalStartTimepoint =
vclob.lockedTimepoint + phase * phaseDuration + depositDuration
const withdrawalEndTimepoint = withdrawalStartTimepoint + withdrawDuration
return {
...vclob,
withdrawalStartTimepoint,
withdrawalEndTimepoint,
}
})
}
98 changes: 98 additions & 0 deletions src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { abs } from './utils/math'
import { toBytes32 } from './utils/pool-key'
import { OPERATOR_ABI } from './abis/rebalancer/operator-abi'
import { STRATEGY_ABI } from './abis/rebalancer/strategy-abi'
import { ELECTION_GOVERNOR_ABI } from './abis/governance/election-governor-abi'

/**
* Build a transaction to open a market.
Expand Down Expand Up @@ -1907,3 +1908,100 @@ export const resumePool = async ({
options?.gasPriceLimit,
)
}

export const vote = async ({
chainId,
userAddress,
candidateAddress,
inFavor,
options,
}: {
chainId: CHAIN_IDS
userAddress: `0x${string}`
candidateAddress: `0x${string}`
inFavor: boolean
options?: {
useSubgraph?: boolean
} & DefaultWriteContractOptions
}): Promise<Transaction> => {
const publicClient = createPublicClient({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
})

return buildTransaction(
publicClient,
{
chain: CHAIN_MAP[chainId],
account: userAddress,
address: CONTRACT_ADDRESSES[chainId]!.ElectionGovernor,
abi: ELECTION_GOVERNOR_ABI,
functionName: 'vote',
args: [candidateAddress, inFavor],
},
options?.gasLimit,
options?.gasPriceLimit,
)
}

export const register = async ({
chainId,
userAddress,
options,
}: {
chainId: CHAIN_IDS
userAddress: `0x${string}`
options?: {
useSubgraph?: boolean
} & DefaultWriteContractOptions
}): Promise<Transaction> => {
const publicClient = createPublicClient({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
})

return buildTransaction(
publicClient,
{
chain: CHAIN_MAP[chainId],
account: userAddress,
address: CONTRACT_ADDRESSES[chainId]!.ElectionGovernor,
abi: ELECTION_GOVERNOR_ABI,
functionName: 'register',
args: [],
},
options?.gasLimit,
options?.gasPriceLimit,
)
}

export const end = async ({
chainId,
userAddress,
options,
}: {
chainId: CHAIN_IDS
userAddress: `0x${string}`
options?: {
useSubgraph?: boolean
} & DefaultWriteContractOptions
}): Promise<Transaction> => {
const publicClient = createPublicClient({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
})

return buildTransaction(
publicClient,
{
chain: CHAIN_MAP[chainId],
account: userAddress,
address: CONTRACT_ADDRESSES[chainId]!.ElectionGovernor,
abi: ELECTION_GOVERNOR_ABI,
functionName: 'end',
args: [],
},
options?.gasLimit,
options?.gasPriceLimit,
)
}
20 changes: 20 additions & 0 deletions src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const CONTRACT_ADDRESSES: {
Strategy: `0x${string}`
Minter: `0x${string}`
Operator: `0x${string}`
VoteLockedCloberToken: `0x${string}`
ElectionGovernor: `0x${string}`
}
} = {
[CHAIN_IDS.CLOBER_TESTNET]: {
Expand All @@ -21,6 +23,10 @@ export const CONTRACT_ADDRESSES: {
Strategy: getAddress('0x8aDF62b0b6078EaE5a2D54e9e5DD2AA71F6748C4'),
Minter: getAddress('0xF2f51B00C2e9b77F23fD66649bbabf8a025c39eF'),
Operator: getAddress('0x33559576B062D08230b467ea7DC7Ce75aFcbdE92'),
VoteLockedCloberToken: getAddress(
'0xA8d4E6BC755b3ed8DCE2FaFE4104Bdad645763A5',
),
ElectionGovernor: getAddress('0xE002A871B314Cc253d4e25E43Afca0557df9577f'),
},
[CHAIN_IDS.CLOBER_TESTNET_2]: {
Controller: getAddress('0xE64aCE1bF550E57461cd4e24706633d7faC9D7b0'),
Expand All @@ -30,6 +36,10 @@ export const CONTRACT_ADDRESSES: {
Strategy: getAddress('0x540488b54c8DE6e44Db7553c3A2C4ABEb09Fc69C'),
Minter: getAddress('0x0b8361a2bbF853F5F6Aa0911a9d238d9CFDD9f1a'),
Operator: getAddress('0xFa47E8dD8F04BF23b238900e754041123a6bc6e2'),
VoteLockedCloberToken: getAddress(
'0xA8d4E6BC755b3ed8DCE2FaFE4104Bdad645763A5',
),
ElectionGovernor: getAddress('0xE002A871B314Cc253d4e25E43Afca0557df9577f'),
},
[CHAIN_IDS.ARBITRUM_SEPOLIA]: {
Controller: getAddress('0xE64aCE1bF550E57461cd4e24706633d7faC9D7b0'),
Expand All @@ -39,6 +49,10 @@ export const CONTRACT_ADDRESSES: {
Strategy: getAddress('0x540488b54c8DE6e44Db7553c3A2C4ABEb09Fc69C'),
Minter: getAddress('0x0b8361a2bbF853F5F6Aa0911a9d238d9CFDD9f1a'),
Operator: getAddress('0xFa47E8dD8F04BF23b238900e754041123a6bc6e2'),
VoteLockedCloberToken: getAddress(
'0xA8d4E6BC755b3ed8DCE2FaFE4104Bdad645763A5',
),
ElectionGovernor: getAddress('0xE002A871B314Cc253d4e25E43Afca0557df9577f'),
},
[CHAIN_IDS.BASE]: {
Controller: getAddress('0xe4AB03992e214acfdCD05ccFB5C5C16e3d0Ca371'),
Expand All @@ -48,6 +62,8 @@ export const CONTRACT_ADDRESSES: {
Strategy: getAddress('0xB203475338cfFF99357E7301617Ba5fC0f47329A'),
Minter: getAddress('0x3A46d45c36F5D3cEDf87B67E3A34F34aBe06AbA8'),
Operator: getAddress('0xBB854e8C0f04d919aD770b27015Ee90a9EF31Bf0'),
VoteLockedCloberToken: zeroAddress,
ElectionGovernor: zeroAddress,
},
[CHAIN_IDS.BERACHAIN_TESTNET]: {
Controller: getAddress('0xce3F3C90970C08Fe451998441b30879560AA6757'),
Expand All @@ -57,6 +73,8 @@ export const CONTRACT_ADDRESSES: {
Strategy: zeroAddress,
Minter: zeroAddress,
Operator: zeroAddress,
VoteLockedCloberToken: zeroAddress,
ElectionGovernor: zeroAddress,
},
[CHAIN_IDS.ZKSYNC]: {
Controller: getAddress('0x2Bd904F455928833F8E8C706d1cf01Eb5daaee7C'),
Expand All @@ -66,5 +84,7 @@ export const CONTRACT_ADDRESSES: {
Strategy: zeroAddress,
Minter: zeroAddress,
Operator: zeroAddress,
VoteLockedCloberToken: zeroAddress,
ElectionGovernor: zeroAddress,
},
}
6 changes: 6 additions & 0 deletions src/model/vclob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type VCLOB = {
id: string
owner: `0x${string}`
amount: bigint
lockedTimepoint: bigint
}
45 changes: 45 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,48 @@ export type PoolPerformanceData = {
poolSnapshots: PoolSnapshotDto[]
poolSpreadProfits: PoolSpreadProfitDto[]
}

export type VCLOB = {
id: string
owner: `0x${string}`
amount: bigint
lockedTimepoint: bigint
withdrawalStartTimepoint: bigint
withdrawalEndTimepoint: bigint
}

export type ElectionGovernorMetadata = {
minCandidateBalance: bigint
quota: number
}

export enum ElectionRoundStatus {
NotStarted,
Voting,
Registration,
Ended,
}

export type ElectionCandidate = {
address: `0x${string}`
vclobAmount: bigint
hasVotedTo: boolean
forVotes: bigint
againstVotes: bigint
}

export type ElectionRoundData = {
round: number
nextRoundStartTime: bigint
vclobAmount: bigint
status: ElectionRoundStatus
quota: number
finalistsThreshold: bigint
startTime: bigint
votingEndTime: bigint
registrationEndTime: bigint
candidatesLength: number
finalistsLength: number
candidates: ElectionCandidate[]
finalists: ElectionCandidate[]
}
Loading
Loading