ts client boilerplate

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-02-23 11:09:17 +01:00
parent a73ba559c2
commit 634d442469
8 changed files with 2753 additions and 0 deletions

2
.gitignore vendored
View File

@ -2,6 +2,8 @@
.DS_Store
target
**/*.rs.bk
dist
node_modules
.idea

42
package.json Normal file
View File

@ -0,0 +1,42 @@
{
"name": "@blockworks-foundation/mango-client",
"version": "0.1.0",
"description": "Typescript Client for mango-v4 program.",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
"repository": "https://github.com/blockworks-foundation/mango-v4",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"type-check": "tsc --pretty --noEmit",
"format": "prettier --check .",
"lint": "eslint . --ext ts --ext tsx --ext js --quiet"
},
"author": {
"name": "Blockworks Foundation",
"email": "hello@blockworks.foundation",
"url": "https://blockworks.foundation"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"eslint": "^7.28.0",
"eslint-config-prettier": "^7.2.0",
"prettier": "^2.0.5",
"ts-node": "^9.1.1",
"typedoc": "^0.22.5",
"typescript": "^4.1.3"
},
"publishConfig": {
"access": "public"
},
"prettier": {
"singleQuote": true,
"trailingComma": "all"
},
"dependencies": {
"@project-serum/anchor": "^0.18.0"
},
"license": "MIT"
}

31
release.sh Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ -z "${PROVIDER_WALLET}" ]]; then
echo "Please provide path to a provider wallet keypair."
exit -1
fi
if [[ -z "${VERSION_MANUALLY_BUMPED}" ]]; then
echo "Please bump versions in package.json and in cargo.toml."
exit -1
fi
# build program
anchor build
# update on chain program and IDL, atm used for testing/developing
anchor deploy --provider.cluster devnet --provider.wallet ${PROVIDER_WALLET}
anchor idl upgrade --provider.cluster devnet --provider.wallet ${PROVIDER_WALLET}\
--filepath target/idl/voter_stake_registry.json 4Q6WW2ouZ6V3iaNm56MTd5n2tnTm4C5fiH8miFHnAFHo
# update types in npm package and publish the npm package
cp cp ./target/types/mango_v4.ts ./src/mango_v4.ts
# yarn clean && yarn build && cp package.json ./dist/
# yarn publish dist
echo
echo Remember to commit and push the version update as well as the changes
echo to src/mango_v4.tx.
echo

32
src/client.ts Normal file
View File

@ -0,0 +1,32 @@
import { Program, Provider } from '@project-serum/anchor';
import { PublicKey } from '@solana/web3.js';
import { MangoV4, IDL } from './mango_v4';
export const MANGO_V4_ID = new PublicKey(
'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS',
);
export class MangoClient {
constructor(
public program: Program<MangoV4>,
public devnet?: boolean,
) {}
static async connect(
provider: Provider,
devnet?: boolean,
): Promise<MangoClient> {
// alternatively we could fetch from chain
// const idl = await Program.fetchIdl(MANGO_V$_ID, provider);
const idl = IDL;
return new MangoClient(
new Program<MangoV4>(
idl as MangoV4,
MANGO_V4_ID,
provider,
),
devnet,
);
}
}

1
src/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './client';

987
src/mango_v4.ts Normal file
View File

@ -0,0 +1,987 @@
export type MangoV4 = {
"version": "0.1.0",
"name": "mango_v4",
"instructions": [
{
"name": "createGroup",
"accounts": [
{
"name": "group",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "group"
},
{
"kind": "account",
"type": "publicKey",
"path": "admin"
}
]
}
},
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "registerToken",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "mint",
"isMut": false,
"isSigner": false
},
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenbank"
},
{
"kind": "account",
"type": "publicKey",
"account": "Mint",
"path": "mint"
}
]
}
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenvault"
},
{
"kind": "account",
"type": "publicKey",
"account": "Mint",
"path": "mint"
}
]
}
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "decimals",
"type": "u8"
}
]
},
{
"name": "createAccount",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "account"
},
{
"kind": "account",
"type": "publicKey",
"path": "owner"
},
{
"kind": "arg",
"type": "u8",
"path": "account_num"
}
]
}
},
{
"name": "owner",
"isMut": false,
"isSigner": true
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "accountNum",
"type": "u8"
}
]
},
{
"name": "deposit",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": false,
"isSigner": false
},
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenbank"
},
{
"kind": "account",
"type": "publicKey",
"account": "TokenAccount",
"path": "deposit_token.mint"
}
]
}
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenvault"
},
{
"kind": "account",
"type": "publicKey",
"account": "TokenAccount",
"path": "deposit_token.mint"
}
]
}
},
{
"name": "depositToken",
"isMut": true,
"isSigner": false
},
{
"name": "depositAuthority",
"isMut": false,
"isSigner": true
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
}
],
"accounts": [
{
"name": "mangoAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "group",
"type": "publicKey"
},
{
"name": "owner",
"type": "publicKey"
},
{
"name": "delegate",
"type": "publicKey"
},
{
"name": "indexedPositions",
"type": {
"defined": "IndexedPositions"
}
},
{
"name": "beingLiquidated",
"type": "bool"
},
{
"name": "isBankrupt",
"type": "bool"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
5
]
}
}
]
}
},
{
"name": "mangoGroup",
"type": {
"kind": "struct",
"fields": [
{
"name": "admin",
"type": "publicKey"
},
{
"name": "tokens",
"type": {
"defined": "Tokens"
}
}
]
}
},
{
"name": "tokenBank",
"type": {
"kind": "struct",
"fields": [
{
"name": "depositIndex",
"type": {
"defined": "I80F48"
}
},
{
"name": "borrowIndex",
"type": {
"defined": "I80F48"
}
},
{
"name": "indexedTotalDeposits",
"type": {
"defined": "I80F48"
}
},
{
"name": "indexedTotalBorrows",
"type": {
"defined": "I80F48"
}
}
]
}
}
],
"types": [
{
"name": "IndexedPosition",
"type": {
"kind": "struct",
"fields": [
{
"name": "indexedValue",
"type": {
"defined": "I80F48"
}
},
{
"name": "tokenIndex",
"type": {
"defined": "TokenIndex"
}
}
]
}
},
{
"name": "IndexedPositions",
"type": {
"kind": "struct",
"fields": [
{
"name": "values",
"type": {
"array": [
{
"defined": "IndexedPosition"
},
32
]
}
}
]
}
},
{
"name": "TokenInfo",
"type": {
"kind": "struct",
"fields": [
{
"name": "mint",
"type": "publicKey"
},
{
"name": "decimals",
"type": "u8"
},
{
"name": "bankBump",
"type": "u8"
},
{
"name": "vaultBump",
"type": "u8"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
30
]
}
}
]
}
},
{
"name": "Tokens",
"type": {
"kind": "struct",
"fields": [
{
"name": "infos",
"type": {
"array": [
{
"defined": "TokenInfo"
},
100
]
}
}
]
}
}
],
"errors": [
{
"code": 6000,
"name": "SomeError",
"msg": ""
}
]
};
export const IDL: MangoV4 = {
"version": "0.1.0",
"name": "mango_v4",
"instructions": [
{
"name": "createGroup",
"accounts": [
{
"name": "group",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "group"
},
{
"kind": "account",
"type": "publicKey",
"path": "admin"
}
]
}
},
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "registerToken",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "mint",
"isMut": false,
"isSigner": false
},
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenbank"
},
{
"kind": "account",
"type": "publicKey",
"account": "Mint",
"path": "mint"
}
]
}
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenvault"
},
{
"kind": "account",
"type": "publicKey",
"account": "Mint",
"path": "mint"
}
]
}
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "decimals",
"type": "u8"
}
]
},
{
"name": "createAccount",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "account"
},
{
"kind": "account",
"type": "publicKey",
"path": "owner"
},
{
"kind": "arg",
"type": "u8",
"path": "account_num"
}
]
}
},
{
"name": "owner",
"isMut": false,
"isSigner": true
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "accountNum",
"type": "u8"
}
]
},
{
"name": "deposit",
"accounts": [
{
"name": "group",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": false,
"isSigner": false
},
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenbank"
},
{
"kind": "account",
"type": "publicKey",
"account": "TokenAccount",
"path": "deposit_token.mint"
}
]
}
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "tokenvault"
},
{
"kind": "account",
"type": "publicKey",
"account": "TokenAccount",
"path": "deposit_token.mint"
}
]
}
},
{
"name": "depositToken",
"isMut": true,
"isSigner": false
},
{
"name": "depositAuthority",
"isMut": false,
"isSigner": true
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
}
],
"accounts": [
{
"name": "mangoAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "group",
"type": "publicKey"
},
{
"name": "owner",
"type": "publicKey"
},
{
"name": "delegate",
"type": "publicKey"
},
{
"name": "indexedPositions",
"type": {
"defined": "IndexedPositions"
}
},
{
"name": "beingLiquidated",
"type": "bool"
},
{
"name": "isBankrupt",
"type": "bool"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
5
]
}
}
]
}
},
{
"name": "mangoGroup",
"type": {
"kind": "struct",
"fields": [
{
"name": "admin",
"type": "publicKey"
},
{
"name": "tokens",
"type": {
"defined": "Tokens"
}
}
]
}
},
{
"name": "tokenBank",
"type": {
"kind": "struct",
"fields": [
{
"name": "depositIndex",
"type": {
"defined": "I80F48"
}
},
{
"name": "borrowIndex",
"type": {
"defined": "I80F48"
}
},
{
"name": "indexedTotalDeposits",
"type": {
"defined": "I80F48"
}
},
{
"name": "indexedTotalBorrows",
"type": {
"defined": "I80F48"
}
}
]
}
}
],
"types": [
{
"name": "IndexedPosition",
"type": {
"kind": "struct",
"fields": [
{
"name": "indexedValue",
"type": {
"defined": "I80F48"
}
},
{
"name": "tokenIndex",
"type": {
"defined": "TokenIndex"
}
}
]
}
},
{
"name": "IndexedPositions",
"type": {
"kind": "struct",
"fields": [
{
"name": "values",
"type": {
"array": [
{
"defined": "IndexedPosition"
},
32
]
}
}
]
}
},
{
"name": "TokenInfo",
"type": {
"kind": "struct",
"fields": [
{
"name": "mint",
"type": "publicKey"
},
{
"name": "decimals",
"type": "u8"
},
{
"name": "bankBump",
"type": "u8"
},
{
"name": "vaultBump",
"type": "u8"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
30
]
}
}
]
}
},
{
"name": "Tokens",
"type": {
"kind": "struct",
"fields": [
{
"name": "infos",
"type": {
"array": [
{
"defined": "TokenInfo"
},
100
]
}
}
]
}
}
],
"errors": [
{
"code": 6000,
"name": "SomeError",
"msg": ""
}
]
};

20
tsconfig.json Normal file
View File

@ -0,0 +1,20 @@
{
"extends": "@tsconfig/recommended/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"declaration": true,
"declarationDir": "dist",
"declarationMap": true,
"esModuleInterop": true,
"lib": ["es2019"],
"noImplicitAny": false,
"outDir": "dist",
"resolveJsonModule": true,
"sourceMap": true,
"target": "es6"
},
"include": ["./src/**/*"],
"exclude": ["./src/**/*.test.js", "node_modules", "**/node_modules"]
}

1638
yarn.lock Normal file

File diff suppressed because it is too large Load Diff