-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Nikos Kontakis <[email protected]>
- Loading branch information
Showing
11 changed files
with
390 additions
and
34 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
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
import { useLocks } from '@/contexts/LocksContext' | ||
import { useNetwork } from '@/contexts/NetworkContext' | ||
import { useEffect, useState } from 'react' | ||
|
||
export const LocksCard = () => { | ||
const { currentLocks } = useLocks() | ||
const [currentBlock, setCurrentBlock] = useState(0) | ||
const { api } = useNetwork() | ||
|
||
useEffect(() => { | ||
if (!api) return | ||
|
||
const sub = api.query.System.Number.watchValue('best').subscribe( | ||
(value) => { | ||
setCurrentBlock(value) | ||
}, | ||
) | ||
|
||
return () => sub.unsubscribe() | ||
}, [api]) | ||
|
||
if (!currentLocks) return null | ||
|
||
return ( | ||
<div> | ||
Current block: {currentBlock} | ||
<br /> | ||
Current locks: | ||
<br /> | ||
{Object.entries(currentLocks).map(([track, lockValue]) => ( | ||
<div key={track}> | ||
<ul> | ||
<li>track: {track}</li> | ||
<li>Amount: {lockValue.lock.amount.toString()}</li> | ||
<li>Release: {lockValue.lock.blockNumber}</li> | ||
</ul> | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} |
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,69 @@ | ||
/* eslint-disable react-refresh/only-export-components */ | ||
import React, { createContext, useContext, useEffect, useState } from 'react' | ||
import { | ||
Casting, | ||
Delegating, | ||
getVotingTrackInfo, | ||
} from '../lib/currentVotesAndDelegations' | ||
import { useAccounts } from './AccountsContext' | ||
import { getLocksInfo, Locks } from '@/lib/locks' | ||
import { useNetwork } from './NetworkContext' | ||
|
||
type LocksContextProps = { | ||
children: React.ReactNode | React.ReactNode[] | ||
} | ||
|
||
export interface ILocksContext { | ||
currentVotes: Record<number, Casting | Delegating> | undefined | ||
currentLocks: Locks | undefined | ||
} | ||
|
||
const LocksContext = createContext<ILocksContext | undefined>(undefined) | ||
|
||
const LocksContextProvider = ({ children }: LocksContextProps) => { | ||
const [currentVotes, setCurrentVotes] = useState< | ||
Record<number, Casting | Delegating> | undefined | ||
>() | ||
const [currentLocks, setCurrentLocks] = useState<Locks | undefined>() | ||
|
||
const { selectedAccount } = useAccounts() | ||
const { api } = useNetwork() | ||
|
||
useEffect(() => { | ||
if (!selectedAccount || !api) { | ||
setCurrentVotes(undefined) | ||
return | ||
} | ||
|
||
getVotingTrackInfo(selectedAccount.address, api) | ||
.then((votes) => setCurrentVotes(votes)) | ||
.catch(console.error) | ||
}, [selectedAccount, api]) | ||
|
||
useEffect(() => { | ||
if (!selectedAccount || !api) { | ||
setCurrentVotes(undefined) | ||
return | ||
} | ||
|
||
getLocksInfo(selectedAccount.address, api) | ||
.then((locks) => setCurrentLocks(locks)) | ||
.catch(console.error) | ||
}, [selectedAccount, api]) | ||
|
||
return ( | ||
<LocksContext.Provider value={{ currentVotes, currentLocks }}> | ||
{children} | ||
</LocksContext.Provider> | ||
) | ||
} | ||
|
||
const useLocks = () => { | ||
const context = useContext(LocksContext) | ||
if (context === undefined) { | ||
throw new Error('useLocks must be used within a LocksContextProvider') | ||
} | ||
return context | ||
} | ||
|
||
export { LocksContextProvider, useLocks } |
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 @@ | ||
export const bnMin = (n1: bigint, n2: bigint) => (n1 < n2 ? n1 : n2) |
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 @@ | ||
/* eslint-disable react-refresh/only-export-components */ | ||
export const THRESHOLD = BigInt(500) | ||
export const DEFAULT_TIME = BigInt(6000) | ||
export const ONE_DAY = BigInt(24 * 60 * 60 * 1000) | ||
|
||
export const lockPeriod: Record<string, number> = { | ||
None: 0, | ||
Locked1x: 1, | ||
Locked2x: 2, | ||
Locked3x: 4, | ||
Locked4x: 8, | ||
Locked5x: 16, | ||
Locked6x: 32, | ||
} |
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,142 @@ | ||
import { SS58String } from 'polkadot-api' | ||
import { MultiAddress, VotingConviction } from '@polkadot-api/descriptors' | ||
import { DEFAULT_TIME, ONE_DAY, THRESHOLD } from './constants' | ||
import { bnMin } from './bnMin' | ||
import { ApiType } from '@/contexts/NetworkContext' | ||
|
||
// export const getOptimalAmount = async ( | ||
// account: SS58String, | ||
// at: string = 'best', | ||
// ) => (await dotApi.query.Staking.Ledger.getValue(account, { at }))?.active | ||
|
||
export interface Casting { | ||
type: 'Casting' | ||
referendums: Array<number> | ||
} | ||
|
||
export interface Delegating { | ||
type: 'Delegating' | ||
target: SS58String | ||
amount: bigint | ||
conviction: VotingConviction | ||
} | ||
|
||
export const getTracks = async ( | ||
api: ApiType, | ||
): Promise<Record<number, string>> => | ||
Object.fromEntries( | ||
(await api.constants.Referenda.Tracks()).map(([trackId, { name }]) => [ | ||
trackId, | ||
name | ||
.split('_') | ||
.map((part) => part[0].toUpperCase() + part.slice(1)) | ||
.join(' '), | ||
]), | ||
) | ||
|
||
export const getVotingTrackInfo = async ( | ||
address: SS58String, | ||
api: ApiType, | ||
): Promise<Record<number, Casting | Delegating>> => { | ||
const convictionVoting = | ||
await api.query.ConvictionVoting.VotingFor.getEntries(address) | ||
|
||
return Object.fromEntries( | ||
convictionVoting | ||
.filter( | ||
({ value: convictionVote }) => | ||
convictionVote.type === 'Delegating' || | ||
convictionVote.value.votes.length > 0, | ||
) | ||
.map(({ keyArgs: [, votingClass], value: { type, value } }) => [ | ||
votingClass, | ||
type === 'Casting' | ||
? { | ||
type: 'Casting', | ||
referendums: value.votes.map(([refId]) => refId), | ||
} | ||
: { | ||
type: 'Delegating', | ||
target: value.target, | ||
amount: value.balance, | ||
conviction: value.conviction, | ||
}, | ||
]), | ||
) | ||
} | ||
|
||
export const getDelegateTx = async ( | ||
from: SS58String, | ||
target: SS58String, | ||
conviction: VotingConviction, | ||
amount: bigint, | ||
tracks: Array<number>, | ||
api: ApiType, | ||
) => { | ||
const tracksInfo = await getVotingTrackInfo(from, api) | ||
|
||
const txs: Array< | ||
| ReturnType<typeof api.tx.ConvictionVoting.remove_vote> | ||
| ReturnType<typeof api.tx.ConvictionVoting.undelegate> | ||
| ReturnType<typeof api.tx.ConvictionVoting.delegate> | ||
> = [] | ||
tracks.forEach((trackId) => { | ||
const trackInfo = tracksInfo[trackId] | ||
|
||
if (trackInfo) { | ||
if ( | ||
trackInfo.type === 'Delegating' && | ||
trackInfo.target === target && | ||
conviction.type === trackInfo.conviction.type && | ||
amount === trackInfo.amount | ||
) | ||
return | ||
|
||
if (trackInfo.type === 'Casting') { | ||
trackInfo.referendums.forEach((index) => { | ||
txs.push( | ||
api.tx.ConvictionVoting.remove_vote({ | ||
class: trackId, | ||
index, | ||
}), | ||
) | ||
}) | ||
} else | ||
txs.push( | ||
api.tx.ConvictionVoting.undelegate({ | ||
class: trackId, | ||
}), | ||
) | ||
} | ||
|
||
txs.push( | ||
api.tx.ConvictionVoting.delegate({ | ||
class: trackId, | ||
conviction, | ||
to: MultiAddress.Id(target), | ||
balance: amount, | ||
}), | ||
) | ||
}) | ||
|
||
// @ts-expect-error we need to fix this, it appeared once we added the dynamic api (either kusama or polkadot) | ||
return api.tx.Utility.batch_all({ | ||
calls: txs.map((tx) => tx.decodedCall), | ||
}) | ||
} | ||
|
||
export const getExpectedBlockTime = async (api: ApiType): Promise<bigint> => { | ||
const expectedBlockTime = await api.constants.Babe.ExpectedBlockTime() | ||
if (expectedBlockTime) { | ||
return bnMin(ONE_DAY, expectedBlockTime) | ||
} | ||
|
||
const thresholdCheck = | ||
(await api.constants.Timestamp.MinimumPeriod()) > THRESHOLD | ||
|
||
if (thresholdCheck) { | ||
return bnMin(ONE_DAY, (await api.constants.Timestamp.MinimumPeriod()) * 2n) | ||
} | ||
|
||
return bnMin(ONE_DAY, DEFAULT_TIME) | ||
} |
Oops, something went wrong.