forked from solana-labs/governance-ui
-
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.
add friktion withdraw option (solana-labs#470)
* Friktion (#1) * remove unused signer * merge * merge * fix * bump version * begin adding withdraw * finish adding friktion withdraw instruction * pass in dao authority to cVoltSDK * bump * fix validation * fix validation * remove unused arg
- Loading branch information
1 parent
f2b1e35
commit 5bbf344
Showing
9 changed files
with
637 additions
and
10 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
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
193 changes: 193 additions & 0 deletions
193
pages/dao/[symbol]/proposal/components/instructions/Friktion/FriktionWithdraw.tsx
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,193 @@ | ||
import React, { useContext, useEffect, useState } from 'react' | ||
import Input from '@components/inputs/Input' | ||
import useRealm from '@hooks/useRealm' | ||
import { getMintMinAmountAsDecimal } from '@tools/sdk/units' | ||
import { PublicKey } from '@solana/web3.js' | ||
import { precision } from '@utils/formatting' | ||
import useWalletStore from 'stores/useWalletStore' | ||
import { GovernedMultiTypeAccount } from '@utils/tokens' | ||
import { | ||
FriktionWithdrawForm, | ||
UiInstruction, | ||
} from '@utils/uiTypes/proposalCreationTypes' | ||
import { NewProposalContext } from '../../../new' | ||
import { getFriktionWithdrawSchema } from '@utils/validations' | ||
import useGovernanceAssets from '@hooks/useGovernanceAssets' | ||
import { Governance } from '@solana/spl-governance' | ||
import { ProgramAccount } from '@solana/spl-governance' | ||
import GovernedAccountSelect from '../../GovernedAccountSelect' | ||
import { getFriktionWithdrawInstruction } from '@utils/instructions/Friktion' | ||
import Select from '@components/inputs/Select' | ||
import { FriktionSnapshot, VoltSnapshot } from '@friktion-labs/friktion-sdk' | ||
|
||
const FriktionWithdraw = ({ | ||
index, | ||
governance, | ||
}: { | ||
index: number | ||
governance: ProgramAccount<Governance> | null | ||
}) => { | ||
const connection = useWalletStore((s) => s.connection) | ||
const wallet = useWalletStore((s) => s.current) | ||
const { realmInfo } = useRealm() | ||
const { governedTokenAccountsWithoutNfts } = useGovernanceAssets() | ||
const shouldBeGoverned = index !== 0 && governance | ||
const programId: PublicKey | undefined = realmInfo?.programId | ||
const [form, setForm] = useState<FriktionWithdrawForm>({ | ||
amount: undefined, | ||
governedTokenAccount: undefined, | ||
voltVaultId: '', | ||
depositTokenMint: undefined, | ||
programId: programId?.toString(), | ||
mintInfo: undefined, | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const [friktionVolts, setFriktionVolts] = useState<VoltSnapshot[] | null>( | ||
null | ||
) | ||
const [governedAccount, setGovernedAccount] = useState< | ||
ProgramAccount<Governance> | undefined | ||
>(undefined) | ||
const [formErrors, setFormErrors] = useState({}) | ||
const mintMinAmount = form.mintInfo | ||
? getMintMinAmountAsDecimal(form.mintInfo) | ||
: 1 | ||
const currentPrecision = precision(mintMinAmount) | ||
const { handleSetInstructions } = useContext(NewProposalContext) | ||
const handleSetForm = ({ propertyName, value }) => { | ||
setFormErrors({}) | ||
setForm({ ...form, [propertyName]: value }) | ||
} | ||
const setMintInfo = (value) => { | ||
setForm({ ...form, mintInfo: value }) | ||
} | ||
const setAmount = (event) => { | ||
const value = event.target.value | ||
handleSetForm({ | ||
value: value, | ||
propertyName: 'amount', | ||
}) | ||
} | ||
const validateAmountOnBlur = () => { | ||
const value = form.amount | ||
|
||
handleSetForm({ | ||
value: parseFloat( | ||
Math.max( | ||
Number(mintMinAmount), | ||
Math.min(Number(Number.MAX_SAFE_INTEGER), Number(value)) | ||
).toFixed(currentPrecision) | ||
), | ||
propertyName: 'amount', | ||
}) | ||
} | ||
|
||
async function getInstruction(): Promise<UiInstruction> { | ||
return getFriktionWithdrawInstruction({ | ||
schema, | ||
form, | ||
amount: form.amount ?? 0, | ||
programId, | ||
connection, | ||
wallet, | ||
setFormErrors, | ||
}) | ||
} | ||
useEffect(() => { | ||
// call for the mainnet friktion volts | ||
const callfriktionRequest = async () => { | ||
const response = await fetch( | ||
'https://friktion-labs.github.io/mainnet-tvl-snapshots/friktionSnapshot.json' | ||
) | ||
const parsedResponse = (await response.json()) as FriktionSnapshot | ||
setFriktionVolts(parsedResponse.allMainnetVolts as VoltSnapshot[]) | ||
} | ||
|
||
callfriktionRequest() | ||
}, []) | ||
|
||
useEffect(() => { | ||
handleSetForm({ | ||
propertyName: 'programId', | ||
value: programId?.toString(), | ||
}) | ||
}, [realmInfo?.programId]) | ||
useEffect(() => { | ||
handleSetInstructions( | ||
{ governedAccount: governedAccount, getInstruction }, | ||
index | ||
) | ||
}, [form]) | ||
useEffect(() => { | ||
setGovernedAccount(form.governedTokenAccount?.governance) | ||
setMintInfo(form.governedTokenAccount?.mint?.account) | ||
}, [form.governedTokenAccount]) | ||
const schema = getFriktionWithdrawSchema() | ||
|
||
return ( | ||
<> | ||
<GovernedAccountSelect | ||
label="Source account" | ||
governedAccounts={ | ||
governedTokenAccountsWithoutNfts as GovernedMultiTypeAccount[] | ||
} | ||
onChange={(value) => { | ||
handleSetForm({ value, propertyName: 'governedTokenAccount' }) | ||
}} | ||
value={form.governedTokenAccount} | ||
error={formErrors['governedTokenAccount']} | ||
shouldBeGoverned={shouldBeGoverned} | ||
governance={governance} | ||
></GovernedAccountSelect> | ||
<Select | ||
label="Friktion Volt" | ||
value={form.voltVaultId} | ||
placeholder="Please select..." | ||
onChange={(value) => { | ||
const volt = friktionVolts?.find((x) => x.voltVaultId === value) | ||
setFormErrors({}) | ||
setForm({ | ||
...form, | ||
voltVaultId: value, | ||
depositTokenMint: volt?.depositTokenMint, | ||
}) | ||
}} | ||
error={formErrors['voltVaultId']} | ||
> | ||
{friktionVolts | ||
?.filter((x) => !x.isInCircuits) | ||
.map((value) => ( | ||
<Select.Option key={value.voltVaultId} value={value.voltVaultId}> | ||
<div className="break-all text-fgd-1 "> | ||
<div className="mb-2">{`Volt #${value.voltType} - ${ | ||
value.voltType === 1 | ||
? 'Generate Income' | ||
: value.voltType === 2 | ||
? 'Sustainable Stables' | ||
: '' | ||
} - ${value.underlyingTokenSymbol} - APY: ${value.apy}%`}</div> | ||
<div className="space-y-0.5 text-xs text-fgd-3"> | ||
<div className="flex items-center"> | ||
Withdraw Token: {value.depositTokenSymbol} | ||
</div> | ||
{/* <div>Capacity: {}</div> */} | ||
</div> | ||
</div> | ||
</Select.Option> | ||
))} | ||
</Select> | ||
<Input | ||
min={mintMinAmount} | ||
label="Amount" | ||
value={form.amount} | ||
type="number" | ||
onChange={setAmount} | ||
step={mintMinAmount} | ||
error={formErrors['amount']} | ||
onBlur={validateAmountOnBlur} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export default FriktionWithdraw |
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
Oops, something went wrong.