Renames: propagate to idl and ts

This commit is contained in:
Christian Kamm 2022-06-22 11:21:02 +02:00
parent 648b462352
commit d8a6a29e70
5 changed files with 58 additions and 56 deletions

View File

@ -1,4 +1,4 @@
use super::{OracleConfig, TokenPosition, TokenIndex}; use super::{OracleConfig, TokenIndex, TokenPosition};
use crate::error::MangoError; use crate::error::MangoError;
use crate::util::checked_math as cm; use crate::util::checked_math as cm;
use anchor_lang::prelude::*; use anchor_lang::prelude::*;
@ -152,7 +152,8 @@ impl Bank {
} }
// pay back all borrows // pay back all borrows
self.indexed_total_borrows = cm!(self.indexed_total_borrows + position.indexed_position); // position.value is negative self.indexed_total_borrows =
cm!(self.indexed_total_borrows + position.indexed_position); // position.value is negative
position.indexed_position = I80F48::ZERO; position.indexed_position = I80F48::ZERO;
// deposit the rest // deposit the rest
native_amount = cm!(native_amount + native_position); native_amount = cm!(native_amount + native_position);
@ -225,7 +226,8 @@ impl Bank {
} }
// withdraw all deposits // withdraw all deposits
self.indexed_total_deposits = cm!(self.indexed_total_deposits - position.indexed_position); self.indexed_total_deposits =
cm!(self.indexed_total_deposits - position.indexed_position);
position.indexed_position = I80F48::ZERO; position.indexed_position = I80F48::ZERO;
// borrow the rest // borrow the rest
native_amount = -new_native_position; native_amount = -new_native_position;

View File

@ -7,9 +7,9 @@ import { Bank } from './bank';
import { Group } from './group'; import { Group } from './group';
import { I80F48, I80F48Dto, ZERO_I80F48 } from './I80F48'; import { I80F48, I80F48Dto, ZERO_I80F48 } from './I80F48';
export class MangoAccount { export class MangoAccount {
public tokens: TokenAccount[]; public tokens: TokenPosition[];
public serum3: Serum3Account[]; public serum3: Serum3Orders[];
public perps: PerpAccount[]; public perps: PerpPositions[];
public name: string; public name: string;
static from( static from(
@ -62,20 +62,20 @@ export class MangoAccount {
reserved: number[], reserved: number[],
) { ) {
this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0]; this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0];
this.tokens = tokens.values.map((dto) => TokenAccount.from(dto)); this.tokens = tokens.values.map((dto) => TokenPosition.from(dto));
this.serum3 = serum3.values.map((dto) => Serum3Account.from(dto)); this.serum3 = serum3.values.map((dto) => Serum3Orders.from(dto));
this.perps = perps.accounts.map((dto) => PerpAccount.from(dto)); this.perps = perps.accounts.map((dto) => PerpPositions.from(dto));
} }
async reload(client: MangoClient) { async reload(client: MangoClient) {
Object.assign(this, await client.getMangoAccount(this)); Object.assign(this, await client.getMangoAccount(this));
} }
findToken(tokenIndex: number): TokenAccount | undefined { findToken(tokenIndex: number): TokenPosition | undefined {
return this.tokens.find((ta) => ta.tokenIndex == tokenIndex); return this.tokens.find((ta) => ta.tokenIndex == tokenIndex);
} }
findSerum3Account(marketIndex: number): Serum3Account | undefined { findSerum3Account(marketIndex: number): Serum3Orders | undefined {
return this.serum3.find((sa) => sa.marketIndex == marketIndex); return this.serum3.find((sa) => sa.marketIndex == marketIndex);
} }
@ -89,7 +89,7 @@ export class MangoAccount {
return ta ? ta.ui(bank) : 0; return ta ? ta.ui(bank) : 0;
} }
tokens_active(): TokenAccount[] { tokens_active(): TokenPosition[] {
return this.tokens.filter((token) => token.isActive()); return this.tokens.filter((token) => token.isActive());
} }
@ -98,7 +98,7 @@ export class MangoAccount {
'tokens:' + 'tokens:' +
JSON.stringify( JSON.stringify(
this.tokens this.tokens
.filter((token) => token.tokenIndex != TokenAccount.TokenIndexUnset) .filter((token) => token.tokenIndex != TokenPosition.TokenIndexUnset)
.map((token) => token.toString(group)), .map((token) => token.toString(group)),
null, null,
4, 4,
@ -107,7 +107,7 @@ export class MangoAccount {
JSON.stringify( JSON.stringify(
this.serum3.filter( this.serum3.filter(
(serum3) => (serum3) =>
serum3.marketIndex != Serum3Account.Serum3MarketIndexUnset, serum3.marketIndex != Serum3Orders.Serum3MarketIndexUnset,
), ),
null, null,
4, 4,
@ -115,7 +115,7 @@ export class MangoAccount {
'\nperps:' + '\nperps:' +
JSON.stringify( JSON.stringify(
this.perps.filter( this.perps.filter(
(perp) => perp.marketIndex != PerpAccount.PerpMarketIndexUnset, (perp) => perp.marketIndex != PerpPositions.PerpMarketIndexUnset,
), ),
null, null,
4, 4,
@ -124,10 +124,10 @@ export class MangoAccount {
} }
} }
export class TokenAccount { export class TokenPosition {
static TokenIndexUnset: number = 65535; static TokenIndexUnset: number = 65535;
static from(dto: TokenAccountDto) { static from(dto: TokenAccountDto) {
return new TokenAccount( return new TokenPosition(
I80F48.from(dto.indexedValue), I80F48.from(dto.indexedValue),
dto.tokenIndex, dto.tokenIndex,
dto.inUseCount, dto.inUseCount,
@ -135,7 +135,7 @@ export class TokenAccount {
} }
constructor( constructor(
public indexedValue: I80F48, public indexedPosition: I80F48,
public tokenIndex: number, public tokenIndex: number,
public inUseCount: number, public inUseCount: number,
) {} ) {}
@ -145,10 +145,10 @@ export class TokenAccount {
} }
public native(bank: Bank): I80F48 { public native(bank: Bank): I80F48 {
if (this.indexedValue.isPos()) { if (this.indexedPosition.isPos()) {
return bank.depositIndex.mul(this.indexedValue); return bank.depositIndex.mul(this.indexedPosition);
} else { } else {
return bank.borrowIndex.mul(this.indexedValue); return bank.borrowIndex.mul(this.indexedPosition);
} }
} }
@ -174,7 +174,7 @@ export class TokenAccount {
', inUseCount: ' + ', inUseCount: ' +
this.inUseCount + this.inUseCount +
', indexedValue: ' + ', indexedValue: ' +
this.indexedValue.toNumber() + this.indexedPosition.toNumber() +
extra extra
); );
} }
@ -189,10 +189,10 @@ export class TokenAccountDto {
) {} ) {}
} }
export class Serum3Account { export class Serum3Orders {
static Serum3MarketIndexUnset = 65535; static Serum3MarketIndexUnset = 65535;
static from(dto: Serum3AccountDto) { static from(dto: Serum3AccountDto) {
return new Serum3Account( return new Serum3Orders(
dto.openOrders, dto.openOrders,
dto.marketIndex, dto.marketIndex,
dto.baseTokenIndex, dto.baseTokenIndex,
@ -218,10 +218,10 @@ export class Serum3AccountDto {
) {} ) {}
} }
export class PerpAccount { export class PerpPositions {
static PerpMarketIndexUnset = 65535; static PerpMarketIndexUnset = 65535;
static from(dto: PerpAccountDto) { static from(dto: PerpAccountDto) {
return new PerpAccount( return new PerpPositions(
dto.marketIndex, dto.marketIndex,
dto.basePositionLots.toNumber(), dto.basePositionLots.toNumber(),
dto.quotePositionNative.val.toNumber(), dto.quotePositionNative.val.toNumber(),

View File

@ -1,7 +1,7 @@
import { Group } from './accounts/group'; import { Group } from './accounts/group';
import { import {
MangoAccount, MangoAccount,
TokenAccount, TokenPosition,
TokenAccountDto, TokenAccountDto,
} from './accounts/mangoAccount'; } from './accounts/mangoAccount';
import { StubOracle } from './accounts/oracle'; import { StubOracle } from './accounts/oracle';
@ -21,7 +21,7 @@ export {
Group, Group,
StubOracle, StubOracle,
MangoAccount, MangoAccount,
TokenAccount, TokenPosition as TokenAccount,
TokenAccountDto, TokenAccountDto,
MangoClient, MangoClient,
MANGO_V4_ID, MANGO_V4_ID,

View File

@ -2151,19 +2151,19 @@ export type MangoV4 = {
{ {
"name": "tokens", "name": "tokens",
"type": { "type": {
"defined": "MangoAccountTokens" "defined": "MangoAccountTokenPositions"
} }
}, },
{ {
"name": "serum3", "name": "serum3",
"type": { "type": {
"defined": "MangoAccountSerum3" "defined": "MangoAccountSerum3Orders"
} }
}, },
{ {
"name": "perps", "name": "perps",
"type": { "type": {
"defined": "MangoAccountPerps" "defined": "MangoAccountPerpPositions"
} }
}, },
{ {
@ -2637,12 +2637,12 @@ export type MangoV4 = {
} }
}, },
{ {
"name": "TokenAccount", "name": "TokenPosition",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
{ {
"name": "indexedValue", "name": "indexedPosition",
"type": { "type": {
"defined": "I80F48" "defined": "I80F48"
} }
@ -2668,7 +2668,7 @@ export type MangoV4 = {
} }
}, },
{ {
"name": "MangoAccountTokens", "name": "MangoAccountTokenPositions",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -2677,7 +2677,7 @@ export type MangoV4 = {
"type": { "type": {
"array": [ "array": [
{ {
"defined": "TokenAccount" "defined": "TokenPosition"
}, },
16 16
] ]
@ -2687,7 +2687,7 @@ export type MangoV4 = {
} }
}, },
{ {
"name": "Serum3Account", "name": "Serum3Orders",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -2728,7 +2728,7 @@ export type MangoV4 = {
} }
}, },
{ {
"name": "MangoAccountSerum3", "name": "MangoAccountSerum3Orders",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -2737,7 +2737,7 @@ export type MangoV4 = {
"type": { "type": {
"array": [ "array": [
{ {
"defined": "Serum3Account" "defined": "Serum3Orders"
}, },
8 8
] ]
@ -2747,7 +2747,7 @@ export type MangoV4 = {
} }
}, },
{ {
"name": "PerpAccount", "name": "PerpPositions",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -2806,7 +2806,7 @@ export type MangoV4 = {
} }
}, },
{ {
"name": "MangoAccountPerps", "name": "MangoAccountPerpPositions",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -2815,7 +2815,7 @@ export type MangoV4 = {
"type": { "type": {
"array": [ "array": [
{ {
"defined": "PerpAccount" "defined": "PerpPositions"
}, },
8 8
] ]
@ -5443,19 +5443,19 @@ export const IDL: MangoV4 = {
{ {
"name": "tokens", "name": "tokens",
"type": { "type": {
"defined": "MangoAccountTokens" "defined": "MangoAccountTokenPositions"
} }
}, },
{ {
"name": "serum3", "name": "serum3",
"type": { "type": {
"defined": "MangoAccountSerum3" "defined": "MangoAccountSerum3Orders"
} }
}, },
{ {
"name": "perps", "name": "perps",
"type": { "type": {
"defined": "MangoAccountPerps" "defined": "MangoAccountPerpPositions"
} }
}, },
{ {
@ -5929,12 +5929,12 @@ export const IDL: MangoV4 = {
} }
}, },
{ {
"name": "TokenAccount", "name": "TokenPosition",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
{ {
"name": "indexedValue", "name": "indexedPosition",
"type": { "type": {
"defined": "I80F48" "defined": "I80F48"
} }
@ -5960,7 +5960,7 @@ export const IDL: MangoV4 = {
} }
}, },
{ {
"name": "MangoAccountTokens", "name": "MangoAccountTokenPositions",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -5969,7 +5969,7 @@ export const IDL: MangoV4 = {
"type": { "type": {
"array": [ "array": [
{ {
"defined": "TokenAccount" "defined": "TokenPosition"
}, },
16 16
] ]
@ -5979,7 +5979,7 @@ export const IDL: MangoV4 = {
} }
}, },
{ {
"name": "Serum3Account", "name": "Serum3Orders",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -6020,7 +6020,7 @@ export const IDL: MangoV4 = {
} }
}, },
{ {
"name": "MangoAccountSerum3", "name": "MangoAccountSerum3Orders",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -6029,7 +6029,7 @@ export const IDL: MangoV4 = {
"type": { "type": {
"array": [ "array": [
{ {
"defined": "Serum3Account" "defined": "Serum3Orders"
}, },
8 8
] ]
@ -6039,7 +6039,7 @@ export const IDL: MangoV4 = {
} }
}, },
{ {
"name": "PerpAccount", "name": "PerpPositions",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -6098,7 +6098,7 @@ export const IDL: MangoV4 = {
} }
}, },
{ {
"name": "MangoAccountPerps", "name": "MangoAccountPerpPositions",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -6107,7 +6107,7 @@ export const IDL: MangoV4 = {
"type": { "type": {
"array": [ "array": [
{ {
"defined": "PerpAccount" "defined": "PerpPositions"
}, },
8 8
] ]

View File

@ -1,7 +1,7 @@
import { AnchorProvider, Wallet } from '@project-serum/anchor'; import { AnchorProvider, Wallet } from '@project-serum/anchor';
import { Connection, Keypair } from '@solana/web3.js'; import { Connection, Keypair } from '@solana/web3.js';
import fs from 'fs'; import fs from 'fs';
import { TokenAccount } from '../../accounts/mangoAccount'; import { TokenPosition } from '../../accounts/mangoAccount';
import { MangoClient } from '../../client'; import { MangoClient } from '../../client';
import { MANGO_V4_ID } from '../../constants'; import { MANGO_V4_ID } from '../../constants';
@ -53,7 +53,7 @@ async function main() {
// log users tokens // log users tokens
for (const token of mangoAccount.tokens) { for (const token of mangoAccount.tokens) {
if (token.tokenIndex == TokenAccount.TokenIndexUnset) continue; if (token.tokenIndex == TokenPosition.TokenIndexUnset) continue;
console.log(token.toString()); console.log(token.toString());
} }