stub oracle

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-04-03 06:00:30 +02:00
parent 9debc63a6a
commit bf161ac3f6
4 changed files with 71 additions and 16 deletions

View File

@ -6,6 +6,7 @@ import { MangoClient } from './client';
import { import {
createGroup, createGroup,
createMangoAccount, createMangoAccount,
createStubOracle,
deposit, deposit,
getBank, getBank,
getBankForGroupAndMint, getBankForGroupAndMint,
@ -13,6 +14,7 @@ import {
getMangoAccount, getMangoAccount,
getMangoAccountsForGroupAndOwner, getMangoAccountsForGroupAndOwner,
getSerum3MarketForBaseAndQuote, getSerum3MarketForBaseAndQuote,
getStubOracleForGroupAndMint,
registerToken, registerToken,
serum3RegisterMarket, serum3RegisterMarket,
withdraw, withdraw,
@ -59,21 +61,18 @@ async function main() {
console.log(`Group ${group.publicKey}`); console.log(`Group ${group.publicKey}`);
// //
// Find existing or register new tokens // Find existing or register new oracles
// //
// TODO: replace with 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU,
// see https://developers.circle.com/docs/usdc-on-testnet#usdc-on-solana-testnet
const usdcDevnetMint = new PublicKey( const usdcDevnetMint = new PublicKey(
'8FRFC6MoGGkMFQwngccyu69VnYbzykGeez7ignHVAFSN', '8FRFC6MoGGkMFQwngccyu69VnYbzykGeez7ignHVAFSN',
); );
const usdcDevnetStubOracle = await findOrCreate( const usdcDevnetStubOracle = await findOrCreate(
'stubOracle', 'stubOracle',
getGroupForAdmin, getStubOracleForGroupAndMint,
[adminClient, admin.publicKey], [adminClient, group.publicKey, usdcDevnetMint],
createGroup, createStubOracle,
[adminClient, admin.publicKey, payer], [adminClient, group.publicKey, admin.publicKey, usdcDevnetMint, payer, 1],
); );
console.log(`Group ${group.publicKey}`);
const btcDevnetMint = new PublicKey( const btcDevnetMint = new PublicKey(
'3UNBZ6o52WTWwjac2kPUb4FyodhU1vFkRJheu1Sh2TvU', '3UNBZ6o52WTWwjac2kPUb4FyodhU1vFkRJheu1Sh2TvU',
); );
@ -81,6 +80,11 @@ async function main() {
'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J', 'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
); );
//
// Find existing or register new tokens
//
// TODO: replace with 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU,
// see https://developers.circle.com/docs/usdc-on-testnet#usdc-on-solana-testnet
const btcBank = await findOrCreate<Bank>( const btcBank = await findOrCreate<Bank>(
'bank', 'bank',
getBankForGroupAndMint, getBankForGroupAndMint,

View File

@ -8,7 +8,7 @@ import {
} from '@solana/web3.js'; } from '@solana/web3.js';
import * as bs58 from 'bs58'; import * as bs58 from 'bs58';
import { MangoClient } from './client'; import { MangoClient } from './client';
import { Bank, Group, MangoAccount, Serum3Market } from './types'; import { Bank, Group, MangoAccount, Serum3Market, StubOracle } from './types';
import { I80F48 } from './I80F48'; import { I80F48 } from './I80F48';
// //
@ -481,6 +481,8 @@ export async function getSerum3MarketForBaseAndQuote(
export async function createStubOracle( export async function createStubOracle(
client: MangoClient, client: MangoClient,
groupPk: PublicKey,
adminPk: PublicKey,
tokenMintPk: PublicKey, tokenMintPk: PublicKey,
payer: Keypair, payer: Keypair,
staticPrice: number, staticPrice: number,
@ -488,6 +490,8 @@ export async function createStubOracle(
return await client.program.methods return await client.program.methods
.createStubOracle({ val: I80F48.fromNumber(staticPrice).getData() }) .createStubOracle({ val: I80F48.fromNumber(staticPrice).getData() })
.accounts({ .accounts({
group: groupPk,
admin: adminPk,
tokenMint: tokenMintPk, tokenMint: tokenMintPk,
payer: payer.publicKey, payer: payer.publicKey,
}) })
@ -497,13 +501,17 @@ export async function createStubOracle(
export async function setStubOracle( export async function setStubOracle(
client: MangoClient, client: MangoClient,
groupPk: PublicKey,
adminPk: PublicKey,
tokenMintPk: PublicKey, tokenMintPk: PublicKey,
payer: Keypair, payer: Keypair,
staticPrice: number, staticPrice: number,
): Promise<void> { ): Promise<void> {
return await client.program.methods return await client.program.methods
.setStubOracle({ val: I80F48.fromNumber(staticPrice).getData() }) .setStubOracle({ val: new BN(staticPrice) })
.accounts({ .accounts({
group: groupPk,
admin: adminPk,
tokenMint: tokenMintPk, tokenMint: tokenMintPk,
payer: payer.publicKey, payer: payer.publicKey,
}) })
@ -511,24 +519,25 @@ export async function setStubOracle(
.rpc(); .rpc();
} }
export async function getStubOracleForMint( export async function getStubOracleForGroupAndMint(
client: MangoClient, client: MangoClient,
groupPk: PublicKey,
mintPk: PublicKey, mintPk: PublicKey,
): Promise<MangoAccount[]> { ): Promise<StubOracle[]> {
return ( return (
await client.program.account.mangoAccount.all([ await client.program.account.stubOracle.all([
{ {
memcmp: { memcmp: {
bytes: group.toBase58(), bytes: groupPk.toBase58(),
offset: 8, offset: 8,
}, },
}, },
{ {
memcmp: { memcmp: {
bytes: ownerPk.toBase58(), bytes: mintPk.toBase58(),
offset: 40, offset: 40,
}, },
}, },
]) ])
).map((pa) => MangoAccount.from(pa.publicKey, pa.account)); ).map((pa) => StubOracle.from(pa.publicKey, pa.account));
} }

View File

@ -1608,6 +1608,10 @@ export type MangoV4 = {
"name": "group", "name": "group",
"type": "publicKey" "type": "publicKey"
}, },
{
"name": "mint",
"type": "publicKey"
},
{ {
"name": "price", "name": "price",
"type": { "type": {
@ -3998,6 +4002,10 @@ export const IDL: MangoV4 = {
"name": "group", "name": "group",
"type": "publicKey" "type": "publicKey"
}, },
{
"name": "mint",
"type": "publicKey"
},
{ {
"name": "price", "name": "price",
"type": { "type": {

View File

@ -211,3 +211,37 @@ export class Serum3Market {
public quoteTokenIndex: number, public quoteTokenIndex: number,
) {} ) {}
} }
export class StubOracle {
public price: I80F48;
public lastUpdated: number;
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
mint: PublicKey;
price: I80F48Dto;
lastUpdated: BN;
reserved: unknown;
},
): StubOracle {
return new StubOracle(
publicKey,
obj.group,
obj.mint,
obj.price,
obj.lastUpdated,
);
}
constructor(
public publicKey: PublicKey,
public group: PublicKey,
public mint: PublicKey,
price: I80F48Dto,
lastUpdated: BN,
) {
this.price = I80F48.from(price);
this.lastUpdated = lastUpdated.toNumber();
}
}