-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* saga phone purchase * fix close aob account in mango withdraw + saga phone inst fixes * decode instruction + rebuy fixes * fixes * fix build * fix updateQuantity * transfer rent * fix rent * rent size fix * show usdc account instand of sol account
- Loading branch information
1 parent
0b72233
commit 5e77d7e
Showing
13 changed files
with
1,173 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
AnchorProvider, | ||
BorshInstructionCoder, | ||
Program, | ||
} from '@project-serum/anchor' | ||
import { Connection, Keypair } from '@solana/web3.js' | ||
import { MORTAR_PROGRAM_ID } from '@tools/sagaPhone/mortar' | ||
import { IDL, Mortar } from '@tools/sagaPhone/schema' | ||
|
||
export const SAGA_PHONE = { | ||
[MORTAR_PROGRAM_ID.toBase58()]: { | ||
194: { | ||
name: 'Purchase', | ||
accounts: [ | ||
{ name: 'Issuer' }, | ||
{ name: 'Purchaser' }, | ||
{ name: 'Payer' }, | ||
{ name: 'Receipt' }, | ||
{ name: 'Receipt Tokens' }, | ||
{ name: 'Purchaser Tokens' }, | ||
], | ||
getDataUI: async (connection: Connection, data: Uint8Array) => { | ||
try { | ||
return <div></div> | ||
} catch (e) { | ||
console.log(e) | ||
return <div>{JSON.stringify(data)}</div> | ||
} | ||
}, | ||
}, | ||
102: { | ||
name: 'Update Quantity', | ||
accounts: [ | ||
{ name: 'Issuer' }, | ||
{ name: 'Purchaser' }, | ||
{ name: 'Receipt' }, | ||
{ name: 'Receipt Tokens' }, | ||
{ name: 'Purchaser Tokens' }, | ||
], | ||
getDataUI: async (connection: Connection, data: Uint8Array) => { | ||
try { | ||
const program = new Program<Mortar>( | ||
IDL, | ||
MORTAR_PROGRAM_ID, | ||
new AnchorProvider(null as any, Keypair.generate() as any, {}) | ||
) | ||
const decodedInstructionData = new BorshInstructionCoder( | ||
program.idl | ||
).decode(Buffer.from(data))?.data as any | ||
return ( | ||
<div>quantity: {decodedInstructionData.newQuantity.toNumber()}</div> | ||
) | ||
} catch (e) { | ||
console.log(e) | ||
return <div>{JSON.stringify(data)}</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
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
204 changes: 204 additions & 0 deletions
204
pages/dao/[symbol]/proposal/components/instructions/Solana/SagaPhone/SagaPreOrder.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,204 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import React, { useContext, useEffect, useState } from 'react' | ||
import useRealm from '@hooks/useRealm' | ||
import { | ||
PublicKey, | ||
SystemProgram, | ||
TransactionInstruction, | ||
} from '@solana/web3.js' | ||
import * as yup from 'yup' | ||
import { isFormValid } from '@utils/formValidation' | ||
import { | ||
UiInstruction, | ||
SagaPhoneForm, | ||
} from '@utils/uiTypes/proposalCreationTypes' | ||
import { NewProposalContext } from '../../../../new' | ||
import useGovernanceAssets from '@hooks/useGovernanceAssets' | ||
import { Governance } from '@solana/spl-governance' | ||
import { ProgramAccount } from '@solana/spl-governance' | ||
import useWalletStore from 'stores/useWalletStore' | ||
import { serializeInstructionToBase64 } from '@solana/spl-governance' | ||
import { AccountType } from '@utils/uiTypes/assets' | ||
import InstructionForm, { | ||
InstructionInput, | ||
InstructionInputType, | ||
} from '../../FormCreator' | ||
import { BN } from '@project-serum/anchor' | ||
import { USDC_MINT } from 'Strategies/protocols/mango/tools' | ||
import { | ||
DEVNET_ISSUER, | ||
getPurchaseInstructions, | ||
MAINNET_ISSUER, | ||
USDC_DEVNET, | ||
} from '@tools/sagaPhone/mortar' | ||
import { abbreviateAddress } from '@utils/formatting' | ||
|
||
const SagaPreOrder = ({ | ||
index, | ||
governance, | ||
}: { | ||
index: number | ||
governance: ProgramAccount<Governance> | null | ||
}) => { | ||
const wallet = useWalletStore((s) => s.current) | ||
const { connection } = useWalletStore() | ||
const { realmInfo } = useRealm() | ||
const { assetAccounts } = useGovernanceAssets() | ||
const isDevnet = connection.cluster === 'devnet' | ||
const ISSUER = isDevnet ? DEVNET_ISSUER : MAINNET_ISSUER | ||
const SALE_MINT = isDevnet ? USDC_DEVNET : new PublicKey(USDC_MINT) | ||
const onePhoneDepositPrice = isDevnet ? 250 : 100 | ||
const governedSolAccounts = assetAccounts.filter( | ||
(solAcc) => | ||
solAcc.type === AccountType.SOL && | ||
assetAccounts.find( | ||
(tokenAcc) => | ||
tokenAcc.extensions.token?.account.owner.toBase58() === | ||
solAcc.extensions.transferAddress?.toBase58() && | ||
tokenAcc.extensions.mint?.publicKey.toBase58() === | ||
SALE_MINT.toBase58() | ||
) | ||
) | ||
const governedUSDCAccounts = assetAccounts.filter( | ||
(token) => | ||
token.isToken && | ||
token.extensions.mint?.publicKey.toBase58() === SALE_MINT.toBase58() && | ||
governedSolAccounts.find( | ||
(solAcc) => | ||
solAcc.extensions.transferAddress?.toBase58() === | ||
token.extensions.token?.account.owner.toBase58() | ||
) | ||
) | ||
const shouldBeGoverned = index !== 0 && governance | ||
const programId: PublicKey | undefined = realmInfo?.programId | ||
const [form, setForm] = useState<SagaPhoneForm>({ | ||
governedAccount: null, | ||
quantity: 1, | ||
}) | ||
const [formErrors, setFormErrors] = useState({}) | ||
const { handleSetInstructions } = useContext(NewProposalContext) | ||
const handleSetForm = ({ propertyName, value }) => { | ||
setFormErrors({}) | ||
setForm({ ...form, [propertyName]: value }) | ||
} | ||
const validateInstruction = async (): Promise<boolean> => { | ||
const { isValid, validationErrors } = await isFormValid(schema, form) | ||
setFormErrors(validationErrors) | ||
return isValid | ||
} | ||
async function getInstruction(): Promise<UiInstruction> { | ||
const isValid = await validateInstruction() | ||
let serializedInstructions: string[] = [] | ||
const prequisiteInstructions: TransactionInstruction[] = [] | ||
if ( | ||
isValid && | ||
programId && | ||
form.governedAccount?.governance?.account && | ||
wallet?.publicKey | ||
) { | ||
//size of ata | ||
const size = 165 | ||
const rent = await connection.current.getMinimumBalanceForRentExemption( | ||
size | ||
) | ||
const transferRentIx = SystemProgram.transfer({ | ||
fromPubkey: wallet.publicKey!, | ||
toPubkey: form.governedAccount.extensions.token!.account.owner!, | ||
lamports: rent, | ||
}) | ||
prequisiteInstructions.push(transferRentIx) | ||
//Mango instruction call and serialize | ||
const instructions = await getPurchaseInstructions( | ||
form.governedAccount.extensions.token!.account.owner!, | ||
ISSUER, | ||
form.governedAccount.extensions.token!.account.owner!, | ||
SALE_MINT, | ||
new BN(form.quantity), | ||
connection.current | ||
) | ||
|
||
serializedInstructions = instructions.map((x) => | ||
serializeInstructionToBase64(x) | ||
) | ||
} | ||
const obj: UiInstruction = { | ||
prerequisiteInstructions: prequisiteInstructions, | ||
additionalSerializedInstructions: serializedInstructions, | ||
serializedInstruction: '', | ||
isValid, | ||
governance: form.governedAccount?.governance, | ||
} | ||
return obj | ||
} | ||
useEffect(() => { | ||
handleSetForm({ | ||
propertyName: 'programId', | ||
value: programId?.toString(), | ||
}) | ||
}, [realmInfo?.programId]) | ||
useEffect(() => { | ||
handleSetInstructions( | ||
{ governedAccount: form.governedAccount?.governance, getInstruction }, | ||
index | ||
) | ||
}, [form]) | ||
const schema = yup.object().shape({ | ||
quantity: yup.number().min(1).required('Mango group is required'), | ||
governedAccount: yup | ||
.object() | ||
.nullable() | ||
.required('Program governed account is required'), | ||
}) | ||
const inputs: InstructionInput[] = [ | ||
{ | ||
label: 'USDC account owned by SOL account', | ||
initialValue: form.governedAccount, | ||
name: 'governedAccount', | ||
type: InstructionInputType.GOVERNED_ACCOUNT, | ||
shouldBeGoverned: shouldBeGoverned as any, | ||
governance: governance, | ||
options: governedUSDCAccounts, | ||
additionalComponent: form.governedAccount?.extensions.token ? ( | ||
<div> | ||
SOL account:{' '} | ||
<small> | ||
{abbreviateAddress( | ||
form.governedAccount?.extensions.token?.account.owner | ||
)} | ||
</small> | ||
</div> | ||
) : null, | ||
}, | ||
{ | ||
label: 'Quantity', | ||
initialValue: form.quantity, | ||
type: InstructionInputType.INPUT, | ||
validateMinMax: true, | ||
name: 'quantity', | ||
inputType: 'number', | ||
min: 1, | ||
additionalComponent: ( | ||
<div> | ||
To Deposit:{' '} | ||
{form.quantity ? Number(form.quantity) * onePhoneDepositPrice : 0} | ||
</div> | ||
), | ||
}, | ||
] | ||
|
||
return ( | ||
<> | ||
{form && ( | ||
<InstructionForm | ||
outerForm={form} | ||
setForm={setForm} | ||
inputs={inputs} | ||
setFormErrors={setFormErrors} | ||
formErrors={formErrors} | ||
></InstructionForm> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default SagaPreOrder |
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.
5e77d7e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
governance-ui – ./
governance-ui-git-main-blockworks-foundation.vercel.app
dao-beta.mango.markets
governance-ui-blockworks-foundation.vercel.app
dao.mango.markets