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

Support Oraichain custom staking module #1748

Merged
merged 4 commits into from
Mar 7, 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
565 changes: 565 additions & 0 deletions packages/state/contracts/OraichainCw20Staking.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/state/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export {
NeutronVotingRegistryClient,
NeutronVotingRegistryQueryClient,
} from './NeutronVotingRegistry'
export {
OraichainCw20StakingClient,
OraichainCw20StakingQueryClient,
} from './OraichainCw20Staking'
export {
NeutronCwdPreProposeSingleOverruleClient,
NeutronCwdPreProposeSingleOverruleQueryClient,
Expand Down
212 changes: 202 additions & 10 deletions packages/state/recoil/selectors/contracts/Cw20Stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { selectorFamily } from 'recoil'

import { WithChainId } from '@dao-dao/types'
import {
Claim,
ClaimsResponse,
GetConfigResponse,
GetHooksResponse,
Expand All @@ -11,18 +12,23 @@ import {
TotalStakedAtHeightResponse,
TotalValueResponse,
} from '@dao-dao/types/contracts/Cw20Stake'
import { ConfigResponse as OraichainCw20StakingProxySnapshotConfigResponse } from '@dao-dao/types/contracts/OraichainCw20StakingProxySnapshot'
import { ContractName } from '@dao-dao/utils'

import {
Cw20StakeClient,
Cw20StakeQueryClient,
} from '../../../contracts/Cw20Stake'
import {
refreshClaimsIdAtom,
refreshDaoVotingPowerAtom,
refreshWalletBalancesIdAtom,
signingCosmWasmClientAtom,
} from '../../atoms'
import { cosmWasmClientForChainSelector } from '../chain'
import { isContractSelector } from '../contract'
import { queryContractIndexerSelector } from '../indexer'
import { allLockInfosSelector } from './OraichainCw20Staking'

type QueryClientParams = WithChainId<{
contractAddress: string
Expand Down Expand Up @@ -72,12 +78,24 @@ export const stakedBalanceAtHeightSelector = selectorFamily<
async ({ get }) => {
const id = get(refreshWalletBalancesIdAtom(params[0].address))

// If Oraichain proxy, get staking token and pass to indexer query.
let oraichainStakingToken: string | undefined
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
oraichainStakingToken = get(
oraichainProxySnapshotConfigSelector(queryClientParams)
).asset_key
}

const balance = get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'cw20Stake/stakedBalance',
args: {
address: params[0].address,
oraichainStakingToken,
},
block: params[0].height ? { height: params[0].height } : undefined,
id,
Expand Down Expand Up @@ -107,12 +125,26 @@ export const totalStakedAtHeightSelector = selectorFamily<
async ({ get }) => {
const id = get(refreshWalletBalancesIdAtom(undefined))

// If Oraichain proxy, get staking token and pass to indexer query.
let oraichainStakingToken: string | undefined
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
oraichainStakingToken = get(
oraichainProxySnapshotConfigSelector(queryClientParams)
).asset_key
}

const total = get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'cw20Stake/totalStaked',
block: params[0].height ? { height: params[0].height } : undefined,
id,
args: {
oraichainStakingToken,
},
})
)
if (total && !isNaN(total)) {
Expand All @@ -139,6 +171,22 @@ export const stakedValueSelector = selectorFamily<
async ({ get }) => {
const id = get(refreshWalletBalancesIdAtom(params[0].address))

// Oraichain proxy handles passing the query through.
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
const { balance } = get(
stakedBalanceAtHeightSelector({
...queryClientParams,
params,
})
)
return {
value: balance,
}
}

const value = get(
queryContractIndexerSelector({
...queryClientParams,
Expand Down Expand Up @@ -168,6 +216,22 @@ export const totalValueSelector = selectorFamily<
async ({ get }) => {
const id = get(refreshWalletBalancesIdAtom(undefined))

// This query does not exist on Oraichain's proxy-snapshot.
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
const { total } = get(
totalStakedAtHeightSelector({
...queryClientParams,
params: [{}],
})
)
return {
total,
}
}

const total = get(
queryContractIndexerSelector({
...queryClientParams,
Expand All @@ -194,14 +258,22 @@ export const getConfigSelector = selectorFamily<
get:
({ params, ...queryClientParams }) =>
async ({ get }) => {
const config = get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'cw20Stake/config',
})
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (config) {
return config

// Oraichain proxy handles passing the query through.
if (!isOraichainProxy) {
const config = get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'cw20Stake/config',
})
)

if (config) {
return config
}
}

// If indexer query fails, fallback to contract query.
Expand All @@ -221,6 +293,36 @@ export const claimsSelector = selectorFamily<
async ({ get }) => {
const id = get(refreshClaimsIdAtom(params[0].address))

// Convert Oraichain lock infos to claims.
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
const { asset_key, staking_contract } = get(
oraichainProxySnapshotConfigSelector(queryClientParams)
)
const { lock_infos } = get(
allLockInfosSelector({
chainId: queryClientParams.chainId,
contractAddress: staking_contract,
stakerAddr: params[0].address,
stakingToken: asset_key,
})
)

return {
claims: lock_infos.map(
({ amount, unlock_time }): Claim => ({
amount,
release_at: {
// Convert seconds to nanoseconds.
at_time: (BigInt(unlock_time) * BigInt(1e9)).toString(),
},
})
),
}
}

const claims = get(
queryContractIndexerSelector({
...queryClientParams,
Expand Down Expand Up @@ -262,6 +364,14 @@ export const listStakersSelector = selectorFamily<
get:
({ params, ...queryClientParams }) =>
async ({ get }) => {
// Oraichain has their own interface.
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
return { stakers: [] }
}

const list = get(
queryContractIndexerSelector({
...queryClientParams,
Expand Down Expand Up @@ -292,14 +402,96 @@ export const topStakersSelector = selectorFamily<
key: 'cw20StakeTopStakers',
get:
({ limit, ...queryClientParams }) =>
({ get }) => {
const id =
get(refreshWalletBalancesIdAtom(undefined)) +
get(refreshDaoVotingPowerAtom(queryClientParams.contractAddress))

// If Oraichain proxy, get staking token and pass to indexer query.
let oraichainStakingToken: string | undefined
const isOraichainProxy = get(
isOraichainProxySnapshotContractSelector(queryClientParams)
)
if (isOraichainProxy) {
oraichainStakingToken = get(
oraichainProxySnapshotConfigSelector(queryClientParams)
).asset_key
}

return (
get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'cw20Stake/topStakers',
args: {
limit,
oraichainStakingToken,
},
id,
})
) ?? undefined
)
},
})

/**
* The Oraichain cw20-staking-proxy-snapshot contract is used as the staking
* contract for their custom staking solution. This selector returns whether or
* not this is a cw20-staking-proxy-snapshot contract.
*/
export const isOraichainProxySnapshotContractSelector = selectorFamily<
boolean,
QueryClientParams
>({
key: 'cw20StakeIsOraichainProxySnapshotContract',
get:
(queryClientParams) =>
({ get }) =>
get(
isContractSelector({
...queryClientParams,
name: ContractName.OraichainCw20StakingProxySnapshot,
})
),
})

/**
* Get config for Oraichain's cw20-staking-proxy-snapshot contract.
*/
export const oraichainProxySnapshotConfigSelector = selectorFamily<
OraichainCw20StakingProxySnapshotConfigResponse,
QueryClientParams
>({
key: 'cw20StakeOraichainProxySnapshotConfig',
get:
(queryClientParams) =>
async ({ get }) => {
if (!get(isOraichainProxySnapshotContractSelector(queryClientParams))) {
throw new Error(
'Contract is not an Oraichain cw20-staking proxy-snapshot contract'
)
}

let config = get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'cw20Stake/topStakers',
formula: 'item',
args: {
limit,
key: 'config',
},
})
) ?? undefined,
)
if (config) {
return config
}

// If indexer fails, fallback to querying chain.
const client = get(
cosmWasmClientForChainSelector(queryClientParams.chainId)
)
return await client.queryContractSmart(
queryClientParams.contractAddress,
{ config: {} }
)
},
})
28 changes: 18 additions & 10 deletions packages/state/recoil/selectors/contracts/DaoVotingCw20Staked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,22 @@ export const topStakersSelector = selectorFamily<
key: 'daoVotingCw20StakedTopStakers',
get:
({ limit, ...queryClientParams }) =>
({ get }) =>
get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'daoVotingCw20Staked/topStakers',
args: {
limit,
},
})
) ?? undefined,
({ get }) => {
const id =
get(refreshWalletBalancesIdAtom(undefined)) +
get(refreshDaoVotingPowerAtom(queryClientParams.contractAddress))

return (
get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'daoVotingCw20Staked/topStakers',
args: {
limit,
},
id,
})
) ?? undefined
)
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ export const topStakersSelector = selectorFamily<
get:
({ limit, ...queryClientParams }) =>
({ get }) => {
const id = get(refreshWalletBalancesIdAtom(undefined))
const id =
get(refreshWalletBalancesIdAtom(undefined)) +
get(refreshDaoVotingPowerAtom(queryClientParams.contractAddress))

return (
get(
Expand Down
22 changes: 15 additions & 7 deletions packages/state/recoil/selectors/contracts/DaoVotingNativeStaked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,19 @@ export const topStakersSelector = selectorFamily<
key: 'daoVotingNativeStakedTopStakers',
get:
(queryClientParams) =>
({ get }) =>
get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'daoVotingNativeStaked/topStakers',
})
) ?? undefined,
({ get }) => {
const id =
get(refreshWalletBalancesIdAtom(undefined)) +
get(refreshDaoVotingPowerAtom(queryClientParams.contractAddress))

return (
get(
queryContractIndexerSelector({
...queryClientParams,
formula: 'daoVotingNativeStaked/topStakers',
id,
})
) ?? undefined
)
},
})
Loading
Loading