From d8a6a29e70094771afe291038e2ab332aa175ee4 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Wed, 22 Jun 2022 11:21:02 +0200 Subject: [PATCH] Renames: propagate to idl and ts --- programs/mango-v4/src/state/bank.rs | 8 +-- ts/client/src/accounts/mangoAccount.ts | 46 ++++++++-------- ts/client/src/index.ts | 4 +- ts/client/src/mango_v4.ts | 52 +++++++++---------- .../scripts/scratch/example1-user-account.ts | 4 +- 5 files changed, 58 insertions(+), 56 deletions(-) diff --git a/programs/mango-v4/src/state/bank.rs b/programs/mango-v4/src/state/bank.rs index 8cee22579..27395b46d 100644 --- a/programs/mango-v4/src/state/bank.rs +++ b/programs/mango-v4/src/state/bank.rs @@ -1,4 +1,4 @@ -use super::{OracleConfig, TokenPosition, TokenIndex}; +use super::{OracleConfig, TokenIndex, TokenPosition}; use crate::error::MangoError; use crate::util::checked_math as cm; use anchor_lang::prelude::*; @@ -152,7 +152,8 @@ impl Bank { } // 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; // deposit the rest native_amount = cm!(native_amount + native_position); @@ -225,7 +226,8 @@ impl Bank { } // 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; // borrow the rest native_amount = -new_native_position; diff --git a/ts/client/src/accounts/mangoAccount.ts b/ts/client/src/accounts/mangoAccount.ts index 8a9b89424..f83ba0140 100644 --- a/ts/client/src/accounts/mangoAccount.ts +++ b/ts/client/src/accounts/mangoAccount.ts @@ -7,9 +7,9 @@ import { Bank } from './bank'; import { Group } from './group'; import { I80F48, I80F48Dto, ZERO_I80F48 } from './I80F48'; export class MangoAccount { - public tokens: TokenAccount[]; - public serum3: Serum3Account[]; - public perps: PerpAccount[]; + public tokens: TokenPosition[]; + public serum3: Serum3Orders[]; + public perps: PerpPositions[]; public name: string; static from( @@ -62,20 +62,20 @@ export class MangoAccount { reserved: number[], ) { this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0]; - this.tokens = tokens.values.map((dto) => TokenAccount.from(dto)); - this.serum3 = serum3.values.map((dto) => Serum3Account.from(dto)); - this.perps = perps.accounts.map((dto) => PerpAccount.from(dto)); + this.tokens = tokens.values.map((dto) => TokenPosition.from(dto)); + this.serum3 = serum3.values.map((dto) => Serum3Orders.from(dto)); + this.perps = perps.accounts.map((dto) => PerpPositions.from(dto)); } async reload(client: MangoClient) { 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); } - findSerum3Account(marketIndex: number): Serum3Account | undefined { + findSerum3Account(marketIndex: number): Serum3Orders | undefined { return this.serum3.find((sa) => sa.marketIndex == marketIndex); } @@ -89,7 +89,7 @@ export class MangoAccount { return ta ? ta.ui(bank) : 0; } - tokens_active(): TokenAccount[] { + tokens_active(): TokenPosition[] { return this.tokens.filter((token) => token.isActive()); } @@ -98,7 +98,7 @@ export class MangoAccount { 'tokens:' + JSON.stringify( this.tokens - .filter((token) => token.tokenIndex != TokenAccount.TokenIndexUnset) + .filter((token) => token.tokenIndex != TokenPosition.TokenIndexUnset) .map((token) => token.toString(group)), null, 4, @@ -107,7 +107,7 @@ export class MangoAccount { JSON.stringify( this.serum3.filter( (serum3) => - serum3.marketIndex != Serum3Account.Serum3MarketIndexUnset, + serum3.marketIndex != Serum3Orders.Serum3MarketIndexUnset, ), null, 4, @@ -115,7 +115,7 @@ export class MangoAccount { '\nperps:' + JSON.stringify( this.perps.filter( - (perp) => perp.marketIndex != PerpAccount.PerpMarketIndexUnset, + (perp) => perp.marketIndex != PerpPositions.PerpMarketIndexUnset, ), null, 4, @@ -124,10 +124,10 @@ export class MangoAccount { } } -export class TokenAccount { +export class TokenPosition { static TokenIndexUnset: number = 65535; static from(dto: TokenAccountDto) { - return new TokenAccount( + return new TokenPosition( I80F48.from(dto.indexedValue), dto.tokenIndex, dto.inUseCount, @@ -135,7 +135,7 @@ export class TokenAccount { } constructor( - public indexedValue: I80F48, + public indexedPosition: I80F48, public tokenIndex: number, public inUseCount: number, ) {} @@ -145,10 +145,10 @@ export class TokenAccount { } public native(bank: Bank): I80F48 { - if (this.indexedValue.isPos()) { - return bank.depositIndex.mul(this.indexedValue); + if (this.indexedPosition.isPos()) { + return bank.depositIndex.mul(this.indexedPosition); } else { - return bank.borrowIndex.mul(this.indexedValue); + return bank.borrowIndex.mul(this.indexedPosition); } } @@ -174,7 +174,7 @@ export class TokenAccount { ', inUseCount: ' + this.inUseCount + ', indexedValue: ' + - this.indexedValue.toNumber() + + this.indexedPosition.toNumber() + extra ); } @@ -189,10 +189,10 @@ export class TokenAccountDto { ) {} } -export class Serum3Account { +export class Serum3Orders { static Serum3MarketIndexUnset = 65535; static from(dto: Serum3AccountDto) { - return new Serum3Account( + return new Serum3Orders( dto.openOrders, dto.marketIndex, dto.baseTokenIndex, @@ -218,10 +218,10 @@ export class Serum3AccountDto { ) {} } -export class PerpAccount { +export class PerpPositions { static PerpMarketIndexUnset = 65535; static from(dto: PerpAccountDto) { - return new PerpAccount( + return new PerpPositions( dto.marketIndex, dto.basePositionLots.toNumber(), dto.quotePositionNative.val.toNumber(), diff --git a/ts/client/src/index.ts b/ts/client/src/index.ts index 04ea210f0..5d3208976 100644 --- a/ts/client/src/index.ts +++ b/ts/client/src/index.ts @@ -1,7 +1,7 @@ import { Group } from './accounts/group'; import { MangoAccount, - TokenAccount, + TokenPosition, TokenAccountDto, } from './accounts/mangoAccount'; import { StubOracle } from './accounts/oracle'; @@ -21,7 +21,7 @@ export { Group, StubOracle, MangoAccount, - TokenAccount, + TokenPosition as TokenAccount, TokenAccountDto, MangoClient, MANGO_V4_ID, diff --git a/ts/client/src/mango_v4.ts b/ts/client/src/mango_v4.ts index dd811e769..88a3c4d4d 100644 --- a/ts/client/src/mango_v4.ts +++ b/ts/client/src/mango_v4.ts @@ -2151,19 +2151,19 @@ export type MangoV4 = { { "name": "tokens", "type": { - "defined": "MangoAccountTokens" + "defined": "MangoAccountTokenPositions" } }, { "name": "serum3", "type": { - "defined": "MangoAccountSerum3" + "defined": "MangoAccountSerum3Orders" } }, { "name": "perps", "type": { - "defined": "MangoAccountPerps" + "defined": "MangoAccountPerpPositions" } }, { @@ -2637,12 +2637,12 @@ export type MangoV4 = { } }, { - "name": "TokenAccount", + "name": "TokenPosition", "type": { "kind": "struct", "fields": [ { - "name": "indexedValue", + "name": "indexedPosition", "type": { "defined": "I80F48" } @@ -2668,7 +2668,7 @@ export type MangoV4 = { } }, { - "name": "MangoAccountTokens", + "name": "MangoAccountTokenPositions", "type": { "kind": "struct", "fields": [ @@ -2677,7 +2677,7 @@ export type MangoV4 = { "type": { "array": [ { - "defined": "TokenAccount" + "defined": "TokenPosition" }, 16 ] @@ -2687,7 +2687,7 @@ export type MangoV4 = { } }, { - "name": "Serum3Account", + "name": "Serum3Orders", "type": { "kind": "struct", "fields": [ @@ -2728,7 +2728,7 @@ export type MangoV4 = { } }, { - "name": "MangoAccountSerum3", + "name": "MangoAccountSerum3Orders", "type": { "kind": "struct", "fields": [ @@ -2737,7 +2737,7 @@ export type MangoV4 = { "type": { "array": [ { - "defined": "Serum3Account" + "defined": "Serum3Orders" }, 8 ] @@ -2747,7 +2747,7 @@ export type MangoV4 = { } }, { - "name": "PerpAccount", + "name": "PerpPositions", "type": { "kind": "struct", "fields": [ @@ -2806,7 +2806,7 @@ export type MangoV4 = { } }, { - "name": "MangoAccountPerps", + "name": "MangoAccountPerpPositions", "type": { "kind": "struct", "fields": [ @@ -2815,7 +2815,7 @@ export type MangoV4 = { "type": { "array": [ { - "defined": "PerpAccount" + "defined": "PerpPositions" }, 8 ] @@ -5443,19 +5443,19 @@ export const IDL: MangoV4 = { { "name": "tokens", "type": { - "defined": "MangoAccountTokens" + "defined": "MangoAccountTokenPositions" } }, { "name": "serum3", "type": { - "defined": "MangoAccountSerum3" + "defined": "MangoAccountSerum3Orders" } }, { "name": "perps", "type": { - "defined": "MangoAccountPerps" + "defined": "MangoAccountPerpPositions" } }, { @@ -5929,12 +5929,12 @@ export const IDL: MangoV4 = { } }, { - "name": "TokenAccount", + "name": "TokenPosition", "type": { "kind": "struct", "fields": [ { - "name": "indexedValue", + "name": "indexedPosition", "type": { "defined": "I80F48" } @@ -5960,7 +5960,7 @@ export const IDL: MangoV4 = { } }, { - "name": "MangoAccountTokens", + "name": "MangoAccountTokenPositions", "type": { "kind": "struct", "fields": [ @@ -5969,7 +5969,7 @@ export const IDL: MangoV4 = { "type": { "array": [ { - "defined": "TokenAccount" + "defined": "TokenPosition" }, 16 ] @@ -5979,7 +5979,7 @@ export const IDL: MangoV4 = { } }, { - "name": "Serum3Account", + "name": "Serum3Orders", "type": { "kind": "struct", "fields": [ @@ -6020,7 +6020,7 @@ export const IDL: MangoV4 = { } }, { - "name": "MangoAccountSerum3", + "name": "MangoAccountSerum3Orders", "type": { "kind": "struct", "fields": [ @@ -6029,7 +6029,7 @@ export const IDL: MangoV4 = { "type": { "array": [ { - "defined": "Serum3Account" + "defined": "Serum3Orders" }, 8 ] @@ -6039,7 +6039,7 @@ export const IDL: MangoV4 = { } }, { - "name": "PerpAccount", + "name": "PerpPositions", "type": { "kind": "struct", "fields": [ @@ -6098,7 +6098,7 @@ export const IDL: MangoV4 = { } }, { - "name": "MangoAccountPerps", + "name": "MangoAccountPerpPositions", "type": { "kind": "struct", "fields": [ @@ -6107,7 +6107,7 @@ export const IDL: MangoV4 = { "type": { "array": [ { - "defined": "PerpAccount" + "defined": "PerpPositions" }, 8 ] diff --git a/ts/client/src/scripts/scratch/example1-user-account.ts b/ts/client/src/scripts/scratch/example1-user-account.ts index 664fa78a7..4fd4950ca 100644 --- a/ts/client/src/scripts/scratch/example1-user-account.ts +++ b/ts/client/src/scripts/scratch/example1-user-account.ts @@ -1,7 +1,7 @@ import { AnchorProvider, Wallet } from '@project-serum/anchor'; import { Connection, Keypair } from '@solana/web3.js'; import fs from 'fs'; -import { TokenAccount } from '../../accounts/mangoAccount'; +import { TokenPosition } from '../../accounts/mangoAccount'; import { MangoClient } from '../../client'; import { MANGO_V4_ID } from '../../constants'; @@ -53,7 +53,7 @@ async function main() { // log users tokens for (const token of mangoAccount.tokens) { - if (token.tokenIndex == TokenAccount.TokenIndexUnset) continue; + if (token.tokenIndex == TokenPosition.TokenIndexUnset) continue; console.log(token.toString()); }