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

fix: reduce snapshot requests number #8057

Merged
merged 25 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6fca8d2
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
926f33f
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
a7b2654
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
8ea8869
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
41ba1d0
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
30b7cb9
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
25dc736
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
a12cff3
feat: free fees for thor holders before tip 014
NeOMakinG Oct 30, 2024
3969eb7
feat: proposal link
NeOMakinG Oct 30, 2024
68096ca
feat: todo
NeOMakinG Oct 30, 2024
3ae7cc6
feat: promise fullfilled
NeOMakinG Oct 31, 2024
3162c9d
fix: throttle snapshot requests
NeOMakinG Oct 31, 2024
abba2aa
fix: throttle snapshot requests
NeOMakinG Oct 31, 2024
7581eaa
fix: throttle snapshot requests
NeOMakinG Oct 31, 2024
bdeb6fd
fix: throttle snapshot requests
NeOMakinG Oct 31, 2024
cc14d9a
fix: throttle snapshot requests
NeOMakinG Oct 31, 2024
13d190e
fix: wouhouuuuu
NeOMakinG Nov 4, 2024
7f0a42d
fix: wouhouuuuu
NeOMakinG Nov 4, 2024
e400fe9
Merge branch 'develop' into throttle-snapshot
NeOMakinG Nov 4, 2024
f33a793
fix: wouhouuuuu
NeOMakinG Nov 4, 2024
77530ef
fix: wouhouuuuu
NeOMakinG Nov 4, 2024
11ddab3
fix: review feedbacks
NeOMakinG Nov 4, 2024
0275bec
fix: review feedbacks
NeOMakinG Nov 4, 2024
c3ab74e
fix: review feedbacks
NeOMakinG Nov 4, 2024
c94c0e5
Merge branch 'develop' into throttle-snapshot
NeOMakinG Nov 4, 2024
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
1 change: 1 addition & 0 deletions src/lib/fees/parameters/thorchainLp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FeeCurveParameters } from './types'

// THOR asset doesn't have any fee models for now as it's not using curves, we only zero out values to make TS happy with less risks
kaladinlight marked this conversation as resolved.
Show resolved Hide resolved
const FEE_CURVE_MAX_FEE_BPS = 50 // basis points
const FEE_CURVE_MIN_FEE_BPS = 20 // basis points
const FEE_CURVE_NO_FEE_THRESHOLD_USD = 2_000 // usd
Expand Down
8 changes: 4 additions & 4 deletions src/state/apis/snapshot/getVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axiosRetry from 'axios-retry'
import type { Strategy } from './validators'

export const getVotingPower = async (
address: string,
addresses: string[],
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved
network: string,
strategies: Strategy[],
snapshot: number | 'latest',
Expand All @@ -26,9 +26,9 @@ export const getVotingPower = async (
}
const body = {
jsonrpc: '2.0',
method: 'get_vp',
method: 'POST',
kaladinlight marked this conversation as resolved.
Show resolved Hide resolved
params: {
address,
addresses,
network,
strategies,
snapshot,
Expand All @@ -37,7 +37,7 @@ export const getVotingPower = async (
},
}

const { data } = await axiosWithRetry.post('https://score.snapshot.org', body, {
const { data } = await axiosWithRetry.post('https://score.snapshot.org/api/scores', body, {
headers,
})
return data.result
Expand Down
72 changes: 38 additions & 34 deletions src/state/apis/snapshot/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import { BigNumber, bn, bnOrZero } from 'lib/bignumber/bignumber'
import { FEE_CURVE_PARAMETERS } from 'lib/fees/parameters'
import type { ParameterModel } from 'lib/fees/parameters/types'
import { findClosestFoxDiscountDelayBlockNumber } from 'lib/fees/utils'
import { isFulfilled } from 'lib/utils'
import type { ReduxState } from 'state/reducer'

import { BASE_RTK_CREATE_API_CONFIG } from '../const'
import { getVotingPower } from './getVotingPower'
import type { Proposal, Strategy } from './validators'
import { ProposalSchema, SnapshotSchema, VotingPowerSchema } from './validators'
import { ProposalSchema, ScoresSchema, SnapshotSchema } from './validators'

type FoxVotingPowerCryptoBalance = string

Expand Down Expand Up @@ -165,23 +164,25 @@ export const snapshotApi = createApi({
FEE_CURVE_PARAMETERS[model].FEE_CURVE_FOX_DISCOUNT_DELAY_HOURS,
)
const delegation = false // don't let people delegate for discounts - ambiguous in spec
const votingPowerResults = await Promise.allSettled(
evmAddresses.map(async address => {
const votingPowerUnvalidated = await getVotingPower(
address,
'1',
strategies,
foxDiscountBlock,
SNAPSHOT_SPACE,
delegation,
)
// vp is FOX in crypto balance
return bnOrZero(VotingPowerSchema.parse(votingPowerUnvalidated).vp)
}),

const votingPowerResults = await getVotingPower(
evmAddresses,
'1',
strategies,
foxDiscountBlock,
SNAPSHOT_SPACE,
delegation,
)
const foxHeld = BigNumber.sum(
...votingPowerResults.filter(isFulfilled).map(r => r.value),
).toNumber()

const scores = ScoresSchema.parse(votingPowerResults).scores

const foxHeld = scores.reduce((acc: number, scoreByAddress: Record<string, number>) => {
const values = Object.values(scoreByAddress)

if (!values.length) return acc

return acc + BigNumber.sum(...values.map(bnOrZero)).toNumber()
}, 0)
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved
kaladinlight marked this conversation as resolved.
Show resolved Hide resolved

// Return an error tuple in case of an invalid foxHeld value so we don't cache an errored value
if (isNaN(foxHeld)) {
Expand All @@ -190,6 +191,7 @@ export const snapshotApi = createApi({
}

dispatch(snapshot.actions.setVotingPower({ foxHeld: foxHeld.toString(), model }))

return { data: foxHeld.toString() }
} catch (e) {
console.error(e)
Expand Down Expand Up @@ -224,23 +226,25 @@ export const snapshotApi = createApi({
}, new Set()),
)
const delegation = false // don't let people delegate for discounts - ambiguous in spec
const votingPowerResults = await Promise.allSettled(
evmAddresses.map(async address => {
const votingPowerUnvalidated = await getVotingPower(
address,
'1',
strategies,
THOR_TIP_014_BLOCK_NUMBER,
THORSWAP_SNAPSHOT_SPACE,
delegation,
)
// vp is THOR in crypto balance
return bnOrZero(VotingPowerSchema.parse(votingPowerUnvalidated).vp)
}),

const votingPowerResults = await getVotingPower(
evmAddresses,
'1',
strategies,
THOR_TIP_014_BLOCK_NUMBER,
THORSWAP_SNAPSHOT_SPACE,
delegation,
)
const thorHeld = BigNumber.sum(
...votingPowerResults.filter(isFulfilled).map(r => r.value),
).toNumber()

const scores = ScoresSchema.parse(votingPowerResults).scores
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved

const thorHeld = scores.reduce((acc: number, scoreByAddress: Record<string, number>) => {
const values = Object.values(scoreByAddress)

if (!values.length) return acc

return acc + BigNumber.sum(...values.map(bnOrZero)).toNumber()
}, 0)
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved

// Return an error tuple in case of an invalid thorHeld value so we don't cache an errored value
if (isNaN(thorHeld)) {
Expand Down
11 changes: 5 additions & 6 deletions src/state/apis/snapshot/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Infer } from 'myzod'
import { array, boolean, keySignature, number, object, string, unknown } from 'myzod'
import { array, boolean, keySignature, number, object, record, string, unknown } from 'myzod'

const MethodABI = object({
name: string(),
Expand Down Expand Up @@ -80,13 +80,12 @@ export type SnapshotStrategies = Infer<typeof SnapshotSchema>

// ###### voting power

export const VotingPowerSchema = object({
vp: number(),
vp_by_strategy: array(number()),
vp_state: string(),
export const ScoresSchema = object({
state: string(),
scores: array(record(number())),
})

export type VotingPower = Infer<typeof VotingPowerSchema>
export type VotingPower = Infer<typeof ScoresSchema>

// ###### proposals type

Expand Down
Loading