Skip to content

Commit

Permalink
refactor using uxdclient, update contract, tx passing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjillen committed Dec 9, 2021
1 parent c73727b commit 40e4e66
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 144 deletions.
2 changes: 1 addition & 1 deletion components/instructions/programs/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const PROGRAM_NAMES = {
'675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8': 'Raydium AMM Program',
EhhTKczWMGQt46ynNeRX1WfeagwwJd7ufHvCDjRxjo5Q: 'Raydium Staking Program',

FXY9qRsCxg6ioEFLu9ECQ5v2YF2qjeE1Tcnin3DUBW76: 'Controller UXD',
'73yvDSPxhtXmi8Gaheobm32n3jMdbvQzCahXgqqE12My': 'UXD Protocol Program',

SysvarRent111111111111111111111111111111111: 'Sysvar: Rent',
SysvarC1ock11111111111111111111111111111111: 'Sysvar: Clock',
Expand Down
114 changes: 52 additions & 62 deletions components/instructions/programs/uxdProtocol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Connection } from '@solana/web3.js'
import { struct, u8, u48 } from 'buffer-layout'
import { AccountMetaData } from '../../../models/accounts'

const UXD_INIT_CONTROLLER_INSTRUCTION = {
FXY9qRsCxg6ioEFLu9ECQ5v2YF2qjeE1Tcnin3DUBW76: {
export const UXD_PROGRAM_INSTRUCTIONS = {
'73yvDSPxhtXmi8Gaheobm32n3jMdbvQzCahXgqqE12My': {
1: {
name: 'UXD - Initialize Controller',
accounts: [
'authority',
'payer',
'controller',
'redeemableMint',
'systemProgram',
Expand All @@ -34,69 +35,58 @@ const UXD_INIT_CONTROLLER_INSTRUCTION = {
)
},
},
},
}

const UXD_SET_REDEEMABLE_GLOBAL_SUPPLY_CAP_INSTRUCTION = {
1: {
name: 'UXD - Set Redeemable Global Supply Cap',
accounts: ['authority', 'controller'],
getDataUI: (
_connection: Connection,
data: Uint8Array,
_accounts: AccountMetaData[]
) => {
const dataLayout = struct([u48('redeemable_global_supply_cap')])
2: {
name: 'UXD - Set Redeemable Global Supply Cap',
accounts: ['authority', 'controller'],
getDataUI: (
_connection: Connection,
data: Uint8Array,
_accounts: AccountMetaData[]
) => {
const dataLayout = struct([u48('redeemable_global_supply_cap')])

const args = dataLayout.decode(Buffer.from(data)) as any
console.log('args', args)
return (
<>
<p>{args}</p>
</>
)
const args = dataLayout.decode(Buffer.from(data)) as any
console.log('args', args)
return (
<>
<p>{args}</p>
</>
)
},
},
},
}

const UXD_REGISTER_MANGO_DEPOSITORY_INSTRUCTION = {
1: {
name: 'UXD - Register Mango Depository',
accounts: [
'authority',
'controller',
'depository',
'collateralMint', // BTC/ WSOL.....
'insuranceMint', // USDC
'depositoryCollateralPassthroughAccount',
'depositoryInsurancePassthroughAccount',
'depositoryMangoAccount',
'mangoGroup',
'rent',
'systemProgram',
'tokenProgram',
'mangoProgram',
],
getDataUI: (
_connection: Connection,
data: Uint8Array,
_accounts: AccountMetaData[]
) => {
const dataLayout = struct([u48('redeemable_global_supply_cap')])
3: {
name: 'UXD - Register Mango Depository',
accounts: [
'authority',
'payer',
'controller',
'depository',
'collateralMint', // BTC/ WSOL.....
'insuranceMint', // USDC
'depositoryCollateralPassthroughAccount',
'depositoryInsurancePassthroughAccount',
'depositoryMangoAccount',
'mangoGroup',
'rent',
'systemProgram',
'tokenProgram',
'mangoProgram',
],
getDataUI: (
_connection: Connection,
data: Uint8Array,
_accounts: AccountMetaData[]
) => {
const dataLayout = struct([u48('redeemable_global_supply_cap')])

const args = dataLayout.decode(Buffer.from(data)) as any
console.log('args', args)
return (
<>
<p>{args}</p>
</>
)
const args = dataLayout.decode(Buffer.from(data)) as any
console.log('args', args)
return (
<>
<p>{args}</p>
</>
)
},
},
},
}

export const UXD_INSTRUCTIONS = {
...UXD_INIT_CONTROLLER_INSTRUCTION,
...UXD_SET_REDEEMABLE_GLOBAL_SUPPLY_CAP_INSTRUCTION,
...UXD_REGISTER_MANGO_DEPOSITORY_INSTRUCTION,
}
4 changes: 2 additions & 2 deletions components/instructions/tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MANGO_INSTRUCTIONS } from './programs/mango'
import { getProgramName, isGovernanceProgram } from './programs/names'
import { RAYDIUM_INSTRUCTIONS } from './programs/raydium'
import { SPL_TOKEN_INSTRUCTIONS } from './programs/splToken'
import { UXD_INSTRUCTIONS } from './programs/uxdProtocol'
import { UXD_PROGRAM_INSTRUCTIONS } from './programs/uxdProtocol'

// Well known account names displayed on the instruction card
export const ACCOUNT_NAMES = {
Expand Down Expand Up @@ -88,7 +88,7 @@ export const INSTRUCTION_DESCRIPTORS = {
...BPF_UPGRADEABLE_LOADER_INSTRUCTIONS,
...MANGO_INSTRUCTIONS,
...RAYDIUM_INSTRUCTIONS,
...UXD_INSTRUCTIONS,
...UXD_PROGRAM_INSTRUCTIONS,
}

export async function getInstructionDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ const InitializeController = ({
) {
const createIx = createInitializeControllerInstruction(
form.governedAccount?.governance.info.governedAccount,
form.mintSymbol || '',
form.mintDecimals || 9,
form.governedAccount?.governance.pubkey,
new PublicKey(wallet.publicKey.toBase58()),
connection.current
)
serializedInstruction = serializeInstructionToBase64(createIx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ const RegisterMangoDepository = ({
connection.current,
form.governedAccount?.governance.info.governedAccount,
form.governedAccount?.governance.pubkey,
new PublicKey(wallet.publicKey.toBase58()),
new PublicKey(form.collateralMint),
new PublicKey(form.insuranceMint)
)
console.log('createIx', createIx)
serializedInstruction = serializeInstructionToBase64(createIx)
}
const obj: UiInstruction = {
Expand Down
29 changes: 11 additions & 18 deletions tools/sdk/uxdProtocol/createInitializeControllerInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Program, BN, utils, Provider, Wallet } from '@project-serum/anchor'
import { Program, BN, Provider, Wallet } from '@project-serum/anchor'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import {
SystemProgram,
Expand All @@ -8,12 +8,15 @@ import {
Connection,
Keypair,
} from '@solana/web3.js'
import { Controller } from '@uxdprotocol/uxd-client'
import uxdIdl from './uxdIdl'

const createInitializeControllerInstruction = (
uxdProgramId: PublicKey,
mintSymbol: string,
mintDecimals: number,
authority: PublicKey,
payer: PublicKey,
connection: Connection
): TransactionInstruction => {
// generating a random wallet to be able to instantiate a dummy provider
Expand All @@ -23,28 +26,18 @@ const createInitializeControllerInstruction = (
Provider.defaultOptions()
)
const program = new Program(uxdIdl, uxdProgramId, provider)

const [pda, bump] = utils.publicKey.findProgramAddressSync(
[Buffer.from('CONTROLLER')],
uxdProgramId
)
const [
redeemableMintPda,
redeemableMintBump,
] = utils.publicKey.findProgramAddressSync(
[Buffer.from('REDEEMABLE')],
uxdProgramId
)
const controller = new Controller(mintSymbol, mintDecimals, uxdProgramId)

return program.instruction.initializeController(
bump,
redeemableMintBump,
controller.bump,
controller.redeemableMintBump,
new BN(mintDecimals),
{
accounts: {
authority: authority,
controller: pda,
redeemableMint: redeemableMintPda,
authority,
payer,
controller: controller.pda,
redeemableMint: controller.redeemableMintPda,
rent: SYSVAR_RENT_PUBKEY,
systemProgram: SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
Expand Down
84 changes: 30 additions & 54 deletions tools/sdk/uxdProtocol/createRegisterMangoDepositoryInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Program, utils, Provider, Wallet } from '@project-serum/anchor'
import { Program, Provider, Wallet } from '@project-serum/anchor'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import {
SystemProgram,
Expand All @@ -8,13 +8,18 @@ import {
Connection,
Keypair,
} from '@solana/web3.js'
import { createAndInitializeMango } from '@uxdprotocol/uxd-client'
import {
Controller,
createAndInitializeMango,
MangoDepository,
} from '@uxdprotocol/uxd-client'
import uxdIdl from './uxdIdl'

const createRegisterMangoDepositoryInstruction = async (
connection: Connection,
uxdProgramId: PublicKey,
authority: PublicKey,
payer: PublicKey,
collateralMint: PublicKey,
insuranceMint: PublicKey
): Promise<TransactionInstruction> => {
Expand All @@ -26,65 +31,36 @@ const createRegisterMangoDepositoryInstruction = async (
)
const mango = await createAndInitializeMango(provider, 'devnet')
const program = new Program(uxdIdl, uxdProgramId, provider)
const [controllerPda] = utils.publicKey.findProgramAddressSync(
[Buffer.from('CONTROLLER')],
uxdProgramId
)

console.log('uxdProgramId', uxdProgramId)
console.log('authority', authority)
console.log('collateralMint', collateralMint)
console.log('insuranceMint', insuranceMint)

const [
depositoryPda,
depositoryBump,
] = utils.publicKey.findProgramAddressSync(
[Buffer.from('MANGODEPOSITORY'), collateralMint.toBuffer()],
uxdProgramId
)

const [
collateralPassthroughPda,
collateralPassthroughBump,
] = utils.publicKey.findProgramAddressSync(
[Buffer.from('COLLATERALPASSTHROUGH'), collateralMint.toBuffer()],
uxdProgramId
)
const [
insurancePassthroughPda,
insurancePassthroughBump,
] = utils.publicKey.findProgramAddressSync(
[
Buffer.from('INSURANCEPASSTHROUGH'),
collateralMint.toBuffer(),
insuranceMint.toBuffer(),
],
uxdProgramId
)
const [
mangoAccountPda,
mangoAccountBump,
] = utils.publicKey.findProgramAddressSync(
[Buffer.from('MANGOACCOUNT'), collateralMint.toBuffer()],
const controller = new Controller('UXD', 6, uxdProgramId)
const depository = new MangoDepository(
collateralMint,
'collateralName',
6,
insuranceMint,
'USDC',
6,
uxdProgramId
)

return program.instruction.registerMangoDepository(
depositoryBump,
collateralPassthroughBump,
insurancePassthroughBump,
mangoAccountBump,
depository.bump,
depository.collateralPassthroughBump,
depository.insurancePassthroughBump,
depository.mangoAccountBump,
{
accounts: {
authority: authority,
controller: controllerPda,
depository: depositoryPda,
collateralMint, // BTC/ WSOL.....
insuranceMint, // USDC
depositoryCollateralPassthroughAccount: collateralPassthroughPda,
depositoryInsurancePassthroughAccount: insurancePassthroughPda,
depositoryMangoAccount: mangoAccountPda,
authority,
payer,
controller: controller.pda,
depository: depository.pda,
collateralMint: depository.collateralMint, // BTC/ WSOL.....
insuranceMint: depository.insuranceMint, // USDC
depositoryCollateralPassthroughAccount:
depository.collateralPassthroughPda,
depositoryInsurancePassthroughAccount:
depository.insurancePassthroughPda,
depositoryMangoAccount: depository.mangoAccountPda,
mangoGroup: mango.group.publicKey,
rent: SYSVAR_RENT_PUBKEY,
systemProgram: SystemProgram.programId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Program, BN, utils, Provider, Wallet } from '@project-serum/anchor'
import { Program, BN, Provider, Wallet } from '@project-serum/anchor'
import {
TransactionInstruction,
PublicKey,
Connection,
Keypair,
} from '@solana/web3.js'
import { Controller } from '@uxdprotocol/uxd-client'
import uxdIdl from './uxdIdl'

const redeemableDecimals = 6
Expand All @@ -23,19 +24,17 @@ const createSetRedeemableGlobalSupplyCapInstruction = (
)

const program = new Program(uxdIdl, uxdProgramId, provider)
const [pda] = utils.publicKey.findProgramAddressSync(
[Buffer.from('CONTROLLER')],
uxdProgramId
)
const controller = new Controller('UXD', redeemableDecimals, uxdProgramId)

const decimals = new BN(10 ** redeemableDecimals)
const supplyCapNativeAmount = new BN(supplyCapUiAmount).mul(decimals)

return program.instruction.setRedeemableGlobalSupplyCap(
supplyCapNativeAmount,
{
accounts: {
authority: authority,
controller: pda,
controller: controller.pda,
},
options: Provider.defaultOptions(),
}
Expand Down
Loading

0 comments on commit 40e4e66

Please sign in to comment.