diff --git a/ts/client/package.json b/package.json similarity index 75% rename from ts/client/package.json rename to package.json index 8886c53f1..e392ae5ba 100644 --- a/ts/client/package.json +++ b/package.json @@ -1,24 +1,28 @@ { - "name": "@blockworks-foundation/mango-client", - "version": "0.1.0", + "name": "@blockworks-foundation/mango-v4", + "version": "0.0.1", "description": "Typescript Client for mango-v4 program.", - "main": "lib/src/index.js", - "types": "lib/src/index.d.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", "repository": "https://github.com/blockworks-foundation/mango-v4", "scripts": { "build": "tsc", "clean": "rm -rf dist", - "example1-user": "ts-node src/scripts/example1-user.ts", - "example1-admin": "ts-node src/scripts/example1-admin.ts", + "example1-user": "ts-node ts/client/src/scripts/example1-user.ts", + "example1-admin": "ts-node ts/client/src/scripts/example1-admin.ts", "format": "prettier --check .", "lint": "eslint . --ext ts --ext tsx --ext js --quiet", - "type-check": "tsc --pretty --noEmit" + "type-check": "tsc --pretty --noEmit", + "prepare": "yarn build" }, "author": { "name": "Blockworks Foundation", "email": "hello@blockworks.foundation", "url": "https://blockworks.foundation" }, + "files": [ + "dist" + ], "devDependencies": { "@tsconfig/recommended": "^1.0.1", "@types/chai": "^4.3.0", @@ -36,6 +40,9 @@ "typedoc": "^0.22.5", "typescript": "^4.4.4" }, + "resolutions": { + "@types/bn.js": "^4.11.6" + }, "publishConfig": { "access": "public" }, diff --git a/ts/client/src/accounts/bank.ts b/ts/client/src/accounts/bank.ts index d33580fa8..537ed8efc 100644 --- a/ts/client/src/accounts/bank.ts +++ b/ts/client/src/accounts/bank.ts @@ -9,6 +9,7 @@ export class Bank { public name: string; public depositIndex: I80F48; public borrowIndex: I80F48; + public indexedTotalDeposits: I80F48; static from( publicKey: PublicKey, @@ -101,6 +102,7 @@ export class Bank { this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0]; this.depositIndex = I80F48.from(depositIndex); this.borrowIndex = I80F48.from(borrowIndex); + this.indexedTotalDeposits = I80F48.from(indexedTotalDeposits); } toString(): string { diff --git a/ts/client/src/accounts/mangoAccount.ts b/ts/client/src/accounts/mangoAccount.ts index 8d278fdd1..5e5c3ee48 100644 --- a/ts/client/src/accounts/mangoAccount.ts +++ b/ts/client/src/accounts/mangoAccount.ts @@ -3,7 +3,7 @@ import { utf8 } from '@project-serum/anchor/dist/cjs/utils/bytes'; import { PublicKey } from '@solana/web3.js'; import { MangoClient } from '../client'; import { Bank } from './bank'; -import { I80F48, I80F48Dto } from './I80F48'; +import { I80F48, I80F48Dto, ZERO_I80F48 } from './I80F48'; export class MangoAccount { public tokens: TokenAccount[]; public serum3: Serum3Account[]; @@ -79,12 +79,12 @@ export class MangoAccount { getNativeDeposit(bank: Bank): I80F48 { const ta = this.findToken(bank.tokenIndex); - return bank.depositIndex.mul(ta?.indexedValue!); + return ta ? bank.depositIndex.mul(ta?.indexedValue) : ZERO_I80F48; } getNativeBorrow(bank: Bank): I80F48 { const ta = this.findToken(bank.tokenIndex); - return bank.borrowIndex.mul(ta?.indexedValue!); + return ta ? bank.borrowIndex.mul(ta?.indexedValue) : ZERO_I80F48; } toString(): string { @@ -133,6 +133,10 @@ export class TokenAccount { public tokenIndex: number, public inUseCount: number, ) {} + + isActive(): boolean { + return this.tokenIndex !== 65535; + } } export class TokenAccountDto { diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 51ce4d02b..2ad96f044 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -49,10 +49,8 @@ export class MangoClient { } public async getGroup(groupPk: PublicKey): Promise { - const group = Group.from( - groupPk, - await this.program.account.group.fetch(groupPk), - ); + const groupAccount = await this.program.account.group.fetch(groupPk); + const group = Group.from(groupPk, groupAccount); await group.reload(this); return group; } @@ -807,10 +805,7 @@ export class MangoClient { /// static - static async connect( - provider: Provider, - devnet?: boolean, - ): Promise { + static connect(provider?: Provider, devnet?: boolean): MangoClient { // TODO: use IDL on chain or in repository? decide... // Alternatively we could fetch IDL from chain. // const idl = await Program.fetchIdl(MANGO_V4_ID, provider); diff --git a/ts/client/src/index.ts b/ts/client/src/index.ts index 762b29351..2d599b58d 100644 --- a/ts/client/src/index.ts +++ b/ts/client/src/index.ts @@ -8,3 +8,4 @@ export { export { StubOracle } from './accounts/oracle'; export { Serum3Market } from './accounts/serum3'; export * from './client'; +export * from './constants'; diff --git a/ts/client/src/mango_v4.ts b/ts/client/src/mango_v4.ts index 4029a4628..1a07a6150 100644 --- a/ts/client/src/mango_v4.ts +++ b/ts/client/src/mango_v4.ts @@ -514,13 +514,26 @@ export type MangoV4 = { "name": "owner", "isMut": false, "isSigner": true + }, + { + "name": "tokenProgram", + "isMut": false, + "isSigner": false } ], "args": [ { - "name": "banksLen", + "name": "numHealthAccounts", "type": "u64" }, + { + "name": "withdraws", + "type": { + "vec": { + "defined": "(u8,u64)" + } + } + }, { "name": "cpiData", "type": "bytes" @@ -1703,12 +1716,16 @@ export type MangoV4 = { "name": "tokenIndex", "type": "u16" }, + { + "name": "bump", + "type": "u8" + }, { "name": "reserved", "type": { "array": [ "u8", - 6 + 5 ] } } @@ -3354,13 +3371,26 @@ export const IDL: MangoV4 = { "name": "owner", "isMut": false, "isSigner": true + }, + { + "name": "tokenProgram", + "isMut": false, + "isSigner": false } ], "args": [ { - "name": "banksLen", + "name": "numHealthAccounts", "type": "u64" }, + { + "name": "withdraws", + "type": { + "vec": { + "defined": "(u8,u64)" + } + } + }, { "name": "cpiData", "type": "bytes" @@ -4543,12 +4573,16 @@ export const IDL: MangoV4 = { "name": "tokenIndex", "type": "u16" }, + { + "name": "bump", + "type": "u8" + }, { "name": "reserved", "type": { "array": [ "u8", - 6 + 5 ] } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..dd7d67348 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,18 @@ +{ + "extends": "@tsconfig/recommended/tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "target": "es6", + "lib": ["es2019"], + "allowJs": true, + "checkJs": true, + "declaration": true, + "declarationMap": true, + "noImplicitAny": false, + "resolveJsonModule": true, + "sourceMap": true, + "esModuleInterop": true + }, + "include": ["./ts/**/*"], + "exclude": ["./ts/**/*.test.js", "node_modules", "**/node_modules"] +} diff --git a/ts/client/yarn.lock b/yarn.lock similarity index 99% rename from ts/client/yarn.lock rename to yarn.lock index 14eb6680e..75a065228 100644 --- a/ts/client/yarn.lock +++ b/yarn.lock @@ -255,6 +255,13 @@ resolved "https://registry.yarnpkg.com/@tsconfig/recommended/-/recommended-1.0.1.tgz#7619bad397e06ead1c5182926c944e0ca6177f52" integrity sha512-2xN+iGTbPBEzGSnVp/Hd64vKJCJWxsi9gfs88x4PPMyEjHJoA3o5BY9r5OLPHIZU2pAQxkSAsJFqn6itClP8mQ== +"@types/bn.js@^4.11.6": + version "4.11.6" + resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-4.11.6.tgz#c306c70d9358aaea33cd4eda092a742b9505967c" + integrity sha512-pqr857jrp2kPuO9uRjZ3PwnJTjoQy+fcdxvBTvHm6dkmEL9q+hDD/2j/0ELOBPtPnS8LjCX0gI9nbl8lVkadpg== + dependencies: + "@types/node" "*" + "@types/bs58@^4.0.1": version "4.0.1" resolved "https://registry.yarnpkg.com/@types/bs58/-/bs58-4.0.1.tgz#3d51222aab067786d3bc3740a84a7f5a0effaa37"