-
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.
- Loading branch information
Showing
9 changed files
with
382 additions
and
9 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 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,37 @@ | ||
import { dotApi } from '@/clients' | ||
import { useLocks } from '@/contexts/LocksContext' | ||
import { useEffect, useState } from 'react' | ||
|
||
export const LocksCard = () => { | ||
const { currentLocks } = useLocks() | ||
const [currentBlock, setCurrentBlock] = useState(0) | ||
|
||
useEffect(() => { | ||
const sub = dotApi.query.System.Number.watchValue('best').subscribe( | ||
(value) => { | ||
setCurrentBlock(value) | ||
console.log('currentBlock', value) | ||
}, | ||
) | ||
|
||
return sub.unsubscribe() | ||
}, []) | ||
|
||
if (!currentLocks) return null | ||
|
||
return ( | ||
<div> | ||
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,68 @@ | ||
/* eslint-disable react-refresh/only-export-components */ | ||
import React, { createContext, useContext, useEffect, useState } from 'react' | ||
// import { dotApi, dotClient } from '../clients' | ||
import { | ||
Casting, | ||
Delegating, | ||
getVotingTrackInfo, | ||
} from '../lib/currentVotesAndDelegations' | ||
import { useAccounts } from './AccountsContext' | ||
import { getLocksInfo, Locks } from '@/lib/locks' | ||
|
||
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() | ||
|
||
useEffect(() => { | ||
if (!selectedAccount) { | ||
setCurrentVotes(undefined) | ||
return | ||
} | ||
|
||
getVotingTrackInfo(selectedAccount.address) | ||
.then((votes) => setCurrentVotes(votes)) | ||
.catch(console.error) | ||
}, [selectedAccount]) | ||
|
||
useEffect(() => { | ||
if (!selectedAccount) { | ||
setCurrentVotes(undefined) | ||
return | ||
} | ||
|
||
getLocksInfo(selectedAccount.address) | ||
.then((locks) => setCurrentLocks(locks)) | ||
.catch(console.error) | ||
}, [selectedAccount]) | ||
|
||
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
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,140 @@ | ||
import { SS58String } from 'polkadot-api' | ||
import { dotApi } from '../clients' | ||
import { MultiAddress, VotingConviction } from '@polkadot-api/descriptors' | ||
import { DEFAULT_TIME, ONE_DAY, THRESHOLD } from './constants' | ||
import { bnMin } from './bnMin' | ||
|
||
// 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 (): Promise<Record<number, string>> => | ||
Object.fromEntries( | ||
(await dotApi.constants.Referenda.Tracks()).map(([trackId, { name }]) => [ | ||
trackId, | ||
name | ||
.split('_') | ||
.map((part) => part[0].toUpperCase() + part.slice(1)) | ||
.join(' '), | ||
]), | ||
) | ||
|
||
export const getVotingTrackInfo = async ( | ||
address: SS58String, | ||
): Promise<Record<number, Casting | Delegating>> => { | ||
const convictionVoting = | ||
await dotApi.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>, | ||
) => { | ||
const tracksInfo = await getVotingTrackInfo(from) | ||
|
||
const txs: Array< | ||
| ReturnType<typeof dotApi.tx.ConvictionVoting.remove_vote> | ||
| ReturnType<typeof dotApi.tx.ConvictionVoting.undelegate> | ||
| ReturnType<typeof dotApi.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( | ||
dotApi.tx.ConvictionVoting.remove_vote({ | ||
class: trackId, | ||
index, | ||
}), | ||
) | ||
}) | ||
} else | ||
txs.push( | ||
dotApi.tx.ConvictionVoting.undelegate({ | ||
class: trackId, | ||
}), | ||
) | ||
} | ||
|
||
txs.push( | ||
dotApi.tx.ConvictionVoting.delegate({ | ||
class: trackId, | ||
conviction, | ||
to: MultiAddress.Id(target), | ||
balance: amount, | ||
}), | ||
) | ||
}) | ||
|
||
return dotApi.tx.Utility.batch_all({ | ||
calls: txs.map((tx) => tx.decodedCall), | ||
}) | ||
} | ||
|
||
export const getExpectedBlockTime = async (): Promise<bigint> => { | ||
const expectedBlockTime = await dotApi.constants.Babe.ExpectedBlockTime() | ||
if (expectedBlockTime) { | ||
return bnMin(ONE_DAY, expectedBlockTime) | ||
} | ||
|
||
const thresholdCheck = | ||
(await dotApi.constants.Timestamp.MinimumPeriod()) > THRESHOLD | ||
|
||
if (thresholdCheck) { | ||
return bnMin( | ||
ONE_DAY, | ||
(await dotApi.constants.Timestamp.MinimumPeriod()) * 2n, | ||
) | ||
} | ||
|
||
return bnMin(ONE_DAY, DEFAULT_TIME) | ||
} |
Oops, something went wrong.