From a80a9945c2c88aabf9213b879313afa93123c2e3 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 21 Dec 2023 00:32:10 +0800 Subject: [PATCH 1/7] feat: token-group interface js library --- .github/workflows/pull-request-js.yml | 16 +- .../workflows/pull-request-token-group.yml | 39 +++- ci/js-test-token-group.sh | 12 ++ package.json | 1 + pnpm-lock.yaml | 79 +++++++ pnpm-workspace.yaml | 1 + token-group/js/.eslintignore | 5 + token-group/js/.eslintrc | 34 +++ token-group/js/.gitignore | 13 ++ token-group/js/.mocharc.json | 5 + token-group/js/.nojekyll | 0 token-group/js/.prettierignore | 5 + token-group/js/.prettierrc | 7 + token-group/js/LICENSE | 202 ++++++++++++++++++ token-group/js/README.md | 61 ++++++ token-group/js/package.json | 78 +++++++ token-group/js/src/errors.ts | 35 +++ token-group/js/src/index.ts | 3 + token-group/js/src/instruction.ts | 134 ++++++++++++ token-group/js/src/state/index.ts | 2 + token-group/js/src/state/tokenGroup.ts | 72 +++++++ token-group/js/src/state/tokenGroupMember.ts | 37 ++++ token-group/js/test/instruction.test.ts | 123 +++++++++++ token-group/js/test/state.test.ts | 65 ++++++ token-group/js/tsconfig.all.json | 11 + token-group/js/tsconfig.base.json | 14 ++ token-group/js/tsconfig.cjs.json | 10 + token-group/js/tsconfig.esm.json | 13 ++ token-group/js/tsconfig.json | 8 + token-group/js/tsconfig.root.json | 6 + token-group/js/typedoc.json | 5 + 31 files changed, 1087 insertions(+), 9 deletions(-) create mode 100755 ci/js-test-token-group.sh create mode 100644 token-group/js/.eslintignore create mode 100644 token-group/js/.eslintrc create mode 100644 token-group/js/.gitignore create mode 100644 token-group/js/.mocharc.json create mode 100644 token-group/js/.nojekyll create mode 100644 token-group/js/.prettierignore create mode 100644 token-group/js/.prettierrc create mode 100644 token-group/js/LICENSE create mode 100644 token-group/js/README.md create mode 100644 token-group/js/package.json create mode 100644 token-group/js/src/errors.ts create mode 100644 token-group/js/src/index.ts create mode 100644 token-group/js/src/instruction.ts create mode 100644 token-group/js/src/state/index.ts create mode 100644 token-group/js/src/state/tokenGroup.ts create mode 100644 token-group/js/src/state/tokenGroupMember.ts create mode 100644 token-group/js/test/instruction.test.ts create mode 100644 token-group/js/test/state.test.ts create mode 100644 token-group/js/tsconfig.all.json create mode 100644 token-group/js/tsconfig.base.json create mode 100644 token-group/js/tsconfig.cjs.json create mode 100644 token-group/js/tsconfig.esm.json create mode 100644 token-group/js/tsconfig.json create mode 100644 token-group/js/tsconfig.root.json create mode 100644 token-group/js/typedoc.json diff --git a/.github/workflows/pull-request-js.yml b/.github/workflows/pull-request-js.yml index f38c156fb8e..a078a2155fb 100644 --- a/.github/workflows/pull-request-js.yml +++ b/.github/workflows/pull-request-js.yml @@ -10,6 +10,7 @@ on: - 'single-pool/js/**' - 'stake-pool/js/**' - 'token/js/**' + - 'token-group/js/**' - 'token-lending/js/**' - 'token-metadata/js/**' - 'token-swap/js/**' @@ -24,6 +25,7 @@ on: - 'single-pool/js/**' - 'stake-pool/js/**' - 'token/js/**' + - 'token-group/js/**' - 'token-lending/js/**' - 'token-metadata/js/**' - 'token-swap/js/**' @@ -34,7 +36,19 @@ jobs: js-test: strategy: matrix: - package: [account-compression, libraries, memo, name-service, single-pool, stake-pool, token, token-lending, token-metadata, token-swap] + package: + [ + account-compression, + libraries, + memo, + name-service, + single-pool, + stake-pool, + token, + token-lending, + token-metadata, + token-swap, + ] runs-on: ubuntu-latest env: NODE_VERSION: 20.5 diff --git a/.github/workflows/pull-request-token-group.yml b/.github/workflows/pull-request-token-group.yml index 990b93050de..bb61aab5083 100644 --- a/.github/workflows/pull-request-token-group.yml +++ b/.github/workflows/pull-request-token-group.yml @@ -3,17 +3,19 @@ name: Token-Group Pull Request on: pull_request: paths: - - 'token-group/**' - - 'token/program-2022/**' - - 'ci/*-version.sh' - - '.github/workflows/pull-request-token-group.yml' + - 'token-group/**' + - 'token/program-2022/**' + - 'ci/*-version.sh' + - '.github/workflows/pull-request-token-group.yml' + - '!token-group/js/**' push: branches: [master] paths: - - 'token-group/**' - - 'token/program-2022/**' - - 'ci/*-version.sh' - - '.github/workflows/pull-request-token-group.yml' + - 'token-group/**' + - 'token/program-2022/**' + - 'ci/*-version.sh' + - '.github/workflows/pull-request-token-group.yml' + - '!token-group/js/**' jobs: cargo-test-sbf: @@ -64,3 +66,24 @@ jobs: - name: Build and test example run: ./ci/cargo-test-sbf.sh token-group/example + + js-test: + runs-on: ubuntu-latest + env: + NODE_VERSION: 16.x + steps: + - uses: actions/checkout@v4 + - name: Use Node.js ${{ env.NODE_VERSION }} + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + - uses: pnpm/action-setup@v2 + with: + version: 8 + - uses: actions/cache@v3 + with: + path: ~/.npm + key: node-${{ hashFiles('pnpm-lock.yaml') }} + restore-keys: | + node- + - run: ./ci/js-test-token-group.sh diff --git a/ci/js-test-token-group.sh b/ci/js-test-token-group.sh new file mode 100755 index 00000000000..7c84fd16218 --- /dev/null +++ b/ci/js-test-token-group.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e +cd "$(dirname "$0")/.." + +set -x +pnpm install +pnpm build + +cd token-group/js +pnpm lint +pnpm test diff --git a/package.json b/package.json index f30b0cdb0b3..4d37df81934 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "single-pool/js", "stake-pool/js", "token/js", + "token-group/js", "token-lending/js", "token-metadata/js", "token-swap/js" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a0cce6f504e..84522b72cef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -495,6 +495,85 @@ importers: specifier: ^5.3.3 version: 5.3.3 + token-group/js: + dependencies: + '@solana/codecs-core': + specifier: 2.0.0-experimental.9741939 + version: 2.0.0-experimental.9741939 + '@solana/codecs-data-structures': + specifier: 2.0.0-experimental.9741939 + version: 2.0.0-experimental.9741939 + '@solana/codecs-numbers': + specifier: 2.0.0-experimental.9741939 + version: 2.0.0-experimental.9741939 + '@solana/codecs-strings': + specifier: 2.0.0-experimental.9741939 + version: 2.0.0-experimental.9741939(fastestsmallesttextencoderdecoder@1.0.22) + '@solana/options': + specifier: 2.0.0-experimental.9741939 + version: 2.0.0-experimental.9741939 + '@solana/spl-type-length-value': + specifier: 0.1.0 + version: link:../../libraries/type-length-value/js + devDependencies: + '@solana/web3.js': + specifier: ^1.87.6 + version: 1.87.6 + '@types/chai': + specifier: ^4.3.11 + version: 4.3.11 + '@types/mocha': + specifier: ^10.0.6 + version: 10.0.6 + '@types/node': + specifier: ^20.10.4 + version: 20.10.4 + '@typescript-eslint/eslint-plugin': + specifier: ^6.14.0 + version: 6.14.0(@typescript-eslint/parser@6.14.0)(eslint@8.55.0)(typescript@5.3.3) + '@typescript-eslint/parser': + specifier: ^6.14.0 + version: 6.14.0(eslint@8.55.0)(typescript@5.3.3) + chai: + specifier: ^4.3.6 + version: 4.3.10 + eslint: + specifier: ^8.55.0 + version: 8.55.0 + eslint-config-prettier: + specifier: ^9.1.0 + version: 9.1.0(eslint@8.55.0) + eslint-plugin-prettier: + specifier: ^5.0.1 + version: 5.0.1(@types/eslint@8.44.9)(eslint-config-prettier@9.1.0)(eslint@8.55.0)(prettier@3.1.1) + eslint-plugin-require-extensions: + specifier: ^0.1.1 + version: 0.1.3(eslint@8.55.0) + gh-pages: + specifier: ^6.1.0 + version: 6.1.0 + mocha: + specifier: ^10.1.0 + version: 10.2.0 + prettier: + specifier: ^3.1.1 + version: 3.1.1 + shx: + specifier: ^0.3.4 + version: 0.3.4 + ts-node: + specifier: ^10.9.2 + version: 10.9.2(@types/node@20.10.4)(typescript@5.3.3) + tslib: + specifier: ^2.3.1 + version: 2.6.2 + typedoc: + specifier: ^0.25.4 + version: 0.25.4(typescript@5.3.3) + typescript: + specifier: ^5.3.3 + version: 5.3.3 + token-lending/js: dependencies: '@solana/buffer-layout': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index fe000764a6d..e1424560139 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -6,6 +6,7 @@ packages: - "single-pool/js/packages/*" - "stake-pool/js" - "token/js" + - "token-group/js" - "token-lending/js" - "token-metadata/js" - "token-swap/js" diff --git a/token-group/js/.eslintignore b/token-group/js/.eslintignore new file mode 100644 index 00000000000..6da325effab --- /dev/null +++ b/token-group/js/.eslintignore @@ -0,0 +1,5 @@ +docs +lib +test-ledger + +package-lock.json diff --git a/token-group/js/.eslintrc b/token-group/js/.eslintrc new file mode 100644 index 00000000000..5aef10a4729 --- /dev/null +++ b/token-group/js/.eslintrc @@ -0,0 +1,34 @@ +{ + "root": true, + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "plugin:prettier/recommended", + "plugin:require-extensions/recommended" + ], + "parser": "@typescript-eslint/parser", + "plugins": [ + "@typescript-eslint", + "prettier", + "require-extensions" + ], + "rules": { + "@typescript-eslint/ban-ts-comment": "off", + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-unused-vars": "off", + "@typescript-eslint/no-empty-interface": "off", + "@typescript-eslint/consistent-type-imports": "error" + }, + "overrides": [ + { + "files": [ + "examples/**/*", + "test/**/*" + ], + "rules": { + "require-extensions/require-extensions": "off", + "require-extensions/require-index": "off" + } + } + ] +} diff --git a/token-group/js/.gitignore b/token-group/js/.gitignore new file mode 100644 index 00000000000..21f33db819c --- /dev/null +++ b/token-group/js/.gitignore @@ -0,0 +1,13 @@ +.idea +.vscode +.DS_Store + +node_modules + +pnpm-lock.yaml +yarn.lock + +docs +lib +test-ledger +*.tsbuildinfo diff --git a/token-group/js/.mocharc.json b/token-group/js/.mocharc.json new file mode 100644 index 00000000000..451c14c3016 --- /dev/null +++ b/token-group/js/.mocharc.json @@ -0,0 +1,5 @@ +{ + "extension": ["ts"], + "node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"], + "timeout": 5000 +} diff --git a/token-group/js/.nojekyll b/token-group/js/.nojekyll new file mode 100644 index 00000000000..e69de29bb2d diff --git a/token-group/js/.prettierignore b/token-group/js/.prettierignore new file mode 100644 index 00000000000..6da325effab --- /dev/null +++ b/token-group/js/.prettierignore @@ -0,0 +1,5 @@ +docs +lib +test-ledger + +package-lock.json diff --git a/token-group/js/.prettierrc b/token-group/js/.prettierrc new file mode 100644 index 00000000000..b9ce4c1923a --- /dev/null +++ b/token-group/js/.prettierrc @@ -0,0 +1,7 @@ +{ + "printWidth": 120, + "trailingComma": "es5", + "tabWidth": 4, + "semi": true, + "singleQuote": true +} \ No newline at end of file diff --git a/token-group/js/LICENSE b/token-group/js/LICENSE new file mode 100644 index 00000000000..d6456956733 --- /dev/null +++ b/token-group/js/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/token-group/js/README.md b/token-group/js/README.md new file mode 100644 index 00000000000..bb5bd337bc0 --- /dev/null +++ b/token-group/js/README.md @@ -0,0 +1,61 @@ +# `@solana/spl-token-group` + +A TypeScript interface describing the instructions required for a program to implement to be considered a "token-group" program for SPL token mints. The interface can be implemented by any program. + +## Links + +- [TypeScript Docs](https://solana-labs.github.io/solana-program-library/token-group/js/) +- [FAQs (Frequently Asked Questions)](#faqs) +- [Install](#install) +- [Build from Source](#build-from-source) + +## FAQs + +### How can I get support? + +Please ask questions in the Solana Stack Exchange: https://solana.stackexchange.com/ + +If you've found a bug or you'd like to request a feature, please +[open an issue](https://github.com/solana-labs/solana-program-library/issues/new). + +## Install + +```shell +npm install --save @solana/spl-token-group @solana/web3.js +``` +_OR_ +```shell +yarn add @solana/spl-token-group @solana/web3.js +``` + +## Build from Source + +0. Prerequisites + +* Node 16+ +* NPM 8+ + +1. Clone the project: +```shell +git clone https://github.com/solana-labs/solana-program-library.git +``` + +2. Navigate to the library: +```shell +cd solana-program-library/token-group/js +``` + +3. Install the dependencies: +```shell +npm install +``` + +4. Build the library: +```shell +npm run build +``` + +5. Build the on-chain programs: +```shell +npm run test:build-programs +``` diff --git a/token-group/js/package.json b/token-group/js/package.json new file mode 100644 index 00000000000..c54409d9106 --- /dev/null +++ b/token-group/js/package.json @@ -0,0 +1,78 @@ +{ + "name": "@solana/spl-token-group", + "description": "SPL Token Group Interface JS API", + "version": "0.0.1", + "author": "Solana Labs Maintainers ", + "repository": "https://github.com/solana-labs/solana-program-library", + "license": "Apache-2.0", + "type": "module", + "sideEffects": false, + "engines": { + "node": ">=16" + }, + "files": [ + "lib", + "src", + "LICENSE", + "README.md" + ], + "publishConfig": { + "access": "public" + }, + "main": "./lib/cjs/index.js", + "module": "./lib/esm/index.js", + "types": "./lib/types/index.d.ts", + "exports": { + "types": "./lib/types/index.d.ts", + "require": "./lib/cjs/index.js", + "import": "./lib/esm/index.js" + }, + "scripts": { + "build": "tsc --build --verbose tsconfig.all.json", + "clean": "shx rm -rf lib **/*.tsbuildinfo || true", + "deploy": "npm run deploy:docs", + "deploy:docs": "npm run docs && gh-pages --dest token-group/js --dist docs --dotfiles", + "docs": "shx rm -rf docs && typedoc && shx cp .nojekyll docs/", + "fmt": "prettier --write '{*,**/*}.{ts,tsx,js,jsx,json}'", + "lint": "prettier --check '{*,**/*}.{ts,tsx,js,jsx,json}' && eslint --max-warnings 0 .", + "lint:fix": "npm run fmt && eslint --fix .", + "nuke": "shx rm -rf node_modules package-lock.json || true", + "postbuild": "shx echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json", + "reinstall": "npm run nuke && npm install", + "release": "npm run clean && npm run build", + "test": "mocha test", + "watch": "tsc --build --verbose --watch tsconfig.all.json" + }, + "peerDependencies": { + "@solana/web3.js": "^1.87.6" + }, + "dependencies": { + "@solana/codecs-core": "2.0.0-experimental.9741939", + "@solana/codecs-data-structures": "2.0.0-experimental.9741939", + "@solana/codecs-numbers": "2.0.0-experimental.9741939", + "@solana/codecs-strings": "2.0.0-experimental.9741939", + "@solana/options": "2.0.0-experimental.9741939", + "@solana/spl-type-length-value": "0.1.0" + }, + "devDependencies": { + "@solana/web3.js": "^1.87.6", + "@types/chai": "^4.3.11", + "@types/mocha": "^10.0.6", + "@types/node": "^20.10.4", + "@typescript-eslint/eslint-plugin": "^6.14.0", + "@typescript-eslint/parser": "^6.14.0", + "chai": "^4.3.6", + "eslint": "^8.55.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-prettier": "^5.0.1", + "eslint-plugin-require-extensions": "^0.1.1", + "gh-pages": "^6.1.0", + "mocha": "^10.1.0", + "prettier": "^3.1.1", + "shx": "^0.3.4", + "ts-node": "^10.9.2", + "tslib": "^2.3.1", + "typedoc": "^0.25.4", + "typescript": "^5.3.3" + } +} diff --git a/token-group/js/src/errors.ts b/token-group/js/src/errors.ts new file mode 100644 index 00000000000..fc93a33960d --- /dev/null +++ b/token-group/js/src/errors.ts @@ -0,0 +1,35 @@ +export class TokenGroupError extends Error { + constructor(message?: string) { + super(message); + } +} + +/** Thrown if size is greater than proposed max size */ +export class SizeExceedsNewMaxSizeError extends TokenGroupError { + name = 'SizeExceedsNewMaxSizeError'; +} + +/** Thrown if size is greater than max size */ +export class SizeExceedsMaxSizeError extends TokenGroupError { + name = 'SizeExceedsMaxSizeError'; +} + +/** Thrown if group is immutable */ +export class ImmutableGroupError extends TokenGroupError { + name = 'ImmutableGroupError'; +} + +/** Thrown if incorrect mint authority has signed the instruction */ +export class IncorrectMintAuthorityError extends TokenGroupError { + name = 'IncorrectMintAuthorityError'; +} + +/** Thrown if incorrect update authority has signed the instruction */ +export class IncorrectUpdateAuthorityError extends TokenGroupError { + name = 'IncorrectUpdateAuthorityError'; +} + +/** Thrown if member account is the same as the group account */ +export class MemberAccountIsGroupAccountError extends TokenGroupError { + name = 'MemberAccountIsGroupAccountError'; +} diff --git a/token-group/js/src/index.ts b/token-group/js/src/index.ts new file mode 100644 index 00000000000..5bc9f496131 --- /dev/null +++ b/token-group/js/src/index.ts @@ -0,0 +1,3 @@ +export * from './errors.js'; +export * from './instruction.js'; +export * from './state/index.js'; diff --git a/token-group/js/src/instruction.ts b/token-group/js/src/instruction.ts new file mode 100644 index 00000000000..19e944a2d5f --- /dev/null +++ b/token-group/js/src/instruction.ts @@ -0,0 +1,134 @@ +import type { StructToEncoderTuple } from '@solana/codecs-data-structures'; +import type { PublicKey } from '@solana/web3.js'; +import { getBooleanEncoder, getBytesEncoder, getDataEnumCodec, getStructEncoder } from '@solana/codecs-data-structures'; +import { getU32Encoder, getU64Encoder } from '@solana/codecs-numbers'; +import { getStringEncoder } from '@solana/codecs-strings'; +import { getOptionEncoder } from '@solana/options'; +import { splDiscriminate } from '@solana/spl-type-length-value'; +import { TransactionInstruction } from '@solana/web3.js'; + +function packInstruction( + layout: StructToEncoderTuple, + discriminator: Uint8Array, + values: T +): Buffer { + const encoder = getStructEncoder(layout); + const data = encoder.encode(values); + return Buffer.concat([discriminator, data]); +} + +export interface InitializeGroupInstruction { + programId: PublicKey; + group: PublicKey; + mint: PublicKey; + mintAuthority: PublicKey; + updateAuthority: PublicKey | null; + maxSize: number; +} + +export function createInitializeGroupInstruction(args: InitializeGroupInstruction): TransactionInstruction { + const { programId, group, mint, mintAuthority, updateAuthority, maxSize } = args; + + const updateAuthorityBuffer = Buffer.alloc(32); + if (updateAuthority) { + updateAuthorityBuffer.set(updateAuthority.toBuffer()); + } else { + updateAuthorityBuffer.fill(0); + } + + return new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: false, isWritable: false, pubkey: mint }, + { isSigner: true, isWritable: false, pubkey: mintAuthority }, + ], + data: packInstruction( + [ + ['updateAuthority', getBytesEncoder({ size: 32 })], + ['maxSize', getU32Encoder()], + ], + splDiscriminate('spl_token_group_interface:initialize_token_group'), + { updateAuthority: updateAuthorityBuffer, maxSize } + ), + }); +} + +export interface UpdateGroupMaxSize { + programId: PublicKey; + group: PublicKey; + updateAuthority: PublicKey; + maxSize: number; +} + +export function createUpdateGroupMaxSizeInstruction(args: UpdateGroupMaxSize): TransactionInstruction { + const { programId, group, updateAuthority, maxSize } = args; + return new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: true, isWritable: false, pubkey: updateAuthority }, + ], + data: packInstruction( + [['maxSize', getU32Encoder()]], + splDiscriminate('spl_token_group_interface:update_group_max_size'), + { maxSize } + ), + }); +} + +export interface UpdateGroupAuthority { + programId: PublicKey; + group: PublicKey; + currentAuthority: PublicKey; + newAuthority: PublicKey | null; +} + +export function createUpdateGroupAuthorityInstruction(args: UpdateGroupAuthority): TransactionInstruction { + const { programId, group, currentAuthority, newAuthority } = args; + + const newAuthorityBuffer = Buffer.alloc(32); + if (newAuthority) { + newAuthorityBuffer.set(newAuthority.toBuffer()); + } else { + newAuthorityBuffer.fill(0); + } + + return new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: true, isWritable: false, pubkey: currentAuthority }, + ], + data: packInstruction( + [['newAuthority', getBytesEncoder({ size: 32 })]], + splDiscriminate('spl_token_group_interface:update_authority'), + { newAuthority: newAuthorityBuffer } + ), + }); +} + +export interface InitializeMember { + programId: PublicKey; + member: PublicKey; + memberMint: PublicKey; + memberMintAuthority: PublicKey; + group: PublicKey; + groupUpdateAuthority: PublicKey; +} + +export function createInitializeMemberInstruction(args: InitializeMember): TransactionInstruction { + const { programId, member, memberMint, memberMintAuthority, group, groupUpdateAuthority } = args; + + return new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: member }, + { isSigner: false, isWritable: false, pubkey: memberMint }, + { isSigner: true, isWritable: false, pubkey: memberMintAuthority }, + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: true, isWritable: false, pubkey: groupUpdateAuthority }, + ], + data: packInstruction([], splDiscriminate('spl_token_group_interface:initialize_member'), {}), + }); +} diff --git a/token-group/js/src/state/index.ts b/token-group/js/src/state/index.ts new file mode 100644 index 00000000000..e5db1ace95d --- /dev/null +++ b/token-group/js/src/state/index.ts @@ -0,0 +1,2 @@ +export * from './tokenGroup.js'; +export * from './tokenGroupMember.js'; diff --git a/token-group/js/src/state/tokenGroup.ts b/token-group/js/src/state/tokenGroup.ts new file mode 100644 index 00000000000..37b7053c566 --- /dev/null +++ b/token-group/js/src/state/tokenGroup.ts @@ -0,0 +1,72 @@ +import { PublicKey } from '@solana/web3.js'; +import { getBytesCodec, getStructCodec } from '@solana/codecs-data-structures'; + +const tokenGroupCodec = getStructCodec([ + ['updateAuthority', getBytesCodec({ size: 32 })], + ['mint', getBytesCodec({ size: 32 })], + ['size', getBytesCodec({ size: 4 })], + ['maxSize', getBytesCodec({ size: 4 })], +]); + +export class PodU32 { + #value: number; + constructor(value: number) { + this.#value = value; + } + toBuffer(): Buffer { + const buffer = Buffer.alloc(4); + buffer.writeUInt32LE(this.#value); + return buffer; + } +} + +export interface TokenGroup { + /** The authority that can sign to update the group */ + updateAuthority?: PublicKey; + /** The associated mint, used to counter spoofing to be sure that group belongs to a particular mint */ + mint: PublicKey; + /** The current number of group members */ + size: PodU32; + /** The maximum number of group members */ + maxSize: PodU32; +} + +// Checks if all elements in the array are 0 +function isNonePubkey(buffer: Uint8Array): boolean { + for (let i = 0; i < buffer.length; i++) { + if (buffer[i] !== 0) { + return false; + } + } + return true; +} + +// Pack TokenGroup into byte slab +export const packTokenGroup = (group: TokenGroup): Uint8Array => { + // If no updateAuthority given, set it to the None/Zero PublicKey for encoding + const updateAuthority = group.updateAuthority ?? PublicKey.default; + return tokenGroupCodec.encode({ + updateAuthority: updateAuthority.toBuffer(), + mint: group.mint.toBuffer(), + size: group.size.toBuffer(), + maxSize: group.maxSize.toBuffer(), + }); +}; + +// unpack byte slab into TokenGroup +export function unpackTokenGroup(buffer: Buffer | Uint8Array): TokenGroup { + const data = tokenGroupCodec.decode(buffer); + + return isNonePubkey(data.updateAuthority) + ? { + mint: new PublicKey(data.mint), + size: new PodU32(data.size), + maxSize: new PodU32(data.maxSize), + } + : { + updateAuthority: new PublicKey(data.updateAuthority), + mint: new PublicKey(data.mint), + size: new PodU32(data.size), + maxSize: new PodU32(data.maxSize), + }; +} diff --git a/token-group/js/src/state/tokenGroupMember.ts b/token-group/js/src/state/tokenGroupMember.ts new file mode 100644 index 00000000000..2c9d89b436e --- /dev/null +++ b/token-group/js/src/state/tokenGroupMember.ts @@ -0,0 +1,37 @@ +import { PublicKey } from '@solana/web3.js'; +import { getBytesCodec, getStructCodec } from '@solana/codecs-data-structures'; +import { PodU32 } from './tokenGroup.js'; + +const tokenGroupMemberCodec = getStructCodec([ + ['mint', getBytesCodec({ size: 32 })], + ['group', getBytesCodec({ size: 32 })], + ['memberNumber', getBytesCodec({ size: 4 })], +]); + +export interface TokenGroupMember { + /** The associated mint, used to counter spoofing to be sure that member belongs to a particular mint */ + mint: PublicKey; + /** The pubkey of the `TokenGroup` */ + group: PublicKey; + /** The member number */ + memberNumber: PodU32; +} + +// Pack TokenGroupMember into byte slab +export const packTokenGroupMember = (member: TokenGroupMember): Uint8Array => { + return tokenGroupMemberCodec.encode({ + mint: member.mint.toBuffer(), + group: member.group.toBuffer(), + memberNumber: member.memberNumber.toBuffer(), + }); +}; + +// unpack byte slab into TokenGroupMember +export function unpackTokenGroupMember(buffer: Buffer | Uint8Array): TokenGroupMember { + const data = tokenGroupMemberCodec.decode(buffer); + return { + mint: new PublicKey(data.mint), + group: new PublicKey(data.group), + memberNumber: new PodU32(data.memberNumber), + }; +} diff --git a/token-group/js/test/instruction.test.ts b/token-group/js/test/instruction.test.ts new file mode 100644 index 00000000000..5fca23382d2 --- /dev/null +++ b/token-group/js/test/instruction.test.ts @@ -0,0 +1,123 @@ +import { PublicKey, TransactionInstruction } from '@solana/web3.js'; +import { expect } from 'chai'; + +import { + createInitializeGroupInstruction, + createInitializeMemberInstruction, + createUpdateGroupMaxSizeInstruction, + createUpdateGroupAuthorityInstruction, +} from '../src'; + +describe('Token Group Instructions', () => { + const programId = new PublicKey('22222222222222222222222222222222222222222222'); + const group = new PublicKey('33333333333333333333333333333333333333333333'); + const updateAuthority = new PublicKey('44444444444444444444444444444444444444444444'); + const mint = new PublicKey('55555555555555555555555555555555555555555555'); + const mintAuthority = new PublicKey('66666666666666666666666666666666666666666666'); + const maxSize = 100; + + it('Can create InitializeGroup Instruction', () => { + const instruction = createInitializeGroupInstruction({ + programId, + group, + mint, + mintAuthority, + updateAuthority, + maxSize, + }); + + expect(instruction).to.deep.equal( + new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: false, isWritable: false, pubkey: mint }, + { isSigner: true, isWritable: false, pubkey: mintAuthority }, + ], + data: Buffer.from([ + // Output of rust implementation + 121, 113, 108, 39, 54, 51, 0, 4, 45, 91, 65, 60, 101, 64, 222, 21, 12, 147, 115, 20, 77, 81, 51, + 202, 76, 184, 48, 186, 15, 117, 103, 22, 172, 234, 14, 80, 215, 148, 53, 229, 100, 0, 0, 0, + ]), + }) + ); + }); + + it('Can create UpdateGroupMaxSize Instruction', () => { + const instruction = createUpdateGroupMaxSizeInstruction({ + programId, + group, + updateAuthority, + maxSize, + }); + + expect(instruction).to.deep.equal( + new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: true, isWritable: false, pubkey: updateAuthority }, + ], + data: Buffer.from([ + // Output of rust implementation + 108, 37, 171, 143, 248, 30, 18, 110, 100, 0, 0, 0, + ]), + }) + ); + }); + + it('Can create UpdateGroupAuthority Instruction', () => { + const instruction = createUpdateGroupAuthorityInstruction({ + programId, + group, + currentAuthority: updateAuthority, + newAuthority: PublicKey.default, + }); + + expect(instruction).to.deep.equal( + new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: true, isWritable: false, pubkey: updateAuthority }, + ], + data: Buffer.from([ + // Output of rust implementation + 161, 105, 88, 1, 237, 221, 216, 203, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]), + }) + ); + }); + + it('Can create InitializeMember Instruction', () => { + const member = new PublicKey('22222222222222222222222222222222222222222222'); + const memberMint = new PublicKey('33333333333333333333333333333333333333333333'); + const memberMintAuthority = new PublicKey('44444444444444444444444444444444444444444444'); + const group = new PublicKey('55555555555555555555555555555555555555555555'); + const groupUpdateAuthority = new PublicKey('66666666666666666666666666666666666666666666'); + + const instruction = createInitializeMemberInstruction({ + programId, + member, + memberMint, + memberMintAuthority, + group, + groupUpdateAuthority, + }); + + expect(instruction).to.deep.equal( + new TransactionInstruction({ + programId, + keys: [ + { isSigner: false, isWritable: true, pubkey: member }, + { isSigner: false, isWritable: false, pubkey: memberMint }, + { isSigner: true, isWritable: false, pubkey: memberMintAuthority }, + { isSigner: false, isWritable: true, pubkey: group }, + { isSigner: true, isWritable: false, pubkey: groupUpdateAuthority }, + ], + data: Buffer.from([152, 32, 222, 176, 223, 237, 116, 134]), + }) + ); + }); +}); diff --git a/token-group/js/test/state.test.ts b/token-group/js/test/state.test.ts new file mode 100644 index 00000000000..e029851fde1 --- /dev/null +++ b/token-group/js/test/state.test.ts @@ -0,0 +1,65 @@ +import { PublicKey } from '@solana/web3.js'; +import { expect } from 'chai'; + +import type { TokenGroup, TokenGroupMember } from '../src/state'; +import { unpackTokenGroupMember, packTokenGroupMember, unpackTokenGroup, packTokenGroup, PodU32 } from '../src'; + +describe('State', () => { + describe('Token Group', () => { + it('Can pack and unpack TokenGroup with updateAuthority as rust implementation', () => { + const tokenGroup: TokenGroup = { + mint: new PublicKey('44444444444444444444444444444444444444444444'), + updateAuthority: new PublicKey('55555555555555555555555555555555555555555555'), + size: new PodU32(10), + maxSize: new PodU32(20), + }; + + // From rust implementation + const bytes = Uint8Array.from([ + 60, 121, 172, 80, 135, 1, 40, 28, 16, 196, 153, 112, 103, 22, 239, 184, 102, 74, 235, 162, 191, 71, 52, + 30, 59, 226, 189, 193, 31, 112, 71, 220, 45, 91, 65, 60, 101, 64, 222, 21, 12, 147, 115, 20, 77, 81, 51, + 202, 76, 184, 48, 186, 15, 117, 103, 22, 172, 234, 14, 80, 215, 148, 53, 229, 10, 0, 0, 0, 20, 0, 0, 0, + ]); + + expect(packTokenGroup(tokenGroup)).to.deep.equal(bytes); + expect(unpackTokenGroup(bytes)).to.deep.equal(tokenGroup); + }); + + it('Can pack and unpack TokenGroup without updateAuthority as rust implementation', () => { + const tokenGroup: TokenGroup = { + mint: new PublicKey('44444444444444444444444444444444444444444444'), + size: new PodU32(10), + maxSize: new PodU32(20), + }; + + // From rust implementation + const bytes = Uint8Array.from([ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 91, + 65, 60, 101, 64, 222, 21, 12, 147, 115, 20, 77, 81, 51, 202, 76, 184, 48, 186, 15, 117, 103, 22, 172, + 234, 14, 80, 215, 148, 53, 229, 10, 0, 0, 0, 20, 0, 0, 0, + ]); + + expect(packTokenGroup(tokenGroup)).to.deep.equal(bytes); + expect(unpackTokenGroup(bytes)).to.deep.equal(tokenGroup); + }); + }); + + describe('Token Group Member', () => { + it('Can pack and unpack TokenGroupMember as rust implementation', () => { + const tokenGroupMember: TokenGroupMember = { + mint: new PublicKey('55555555555555555555555555555555555555555555'), + group: new PublicKey('66666666666666666666666666666666666666666666'), + memberNumber: new PodU32(8), + }; + // From rust implementation + const bytes = Uint8Array.from([ + 60, 121, 172, 80, 135, 1, 40, 28, 16, 196, 153, 112, 103, 22, 239, 184, 102, 74, 235, 162, 191, 71, 52, + 30, 59, 226, 189, 193, 31, 112, 71, 220, 75, 152, 23, 100, 168, 193, 114, 35, 20, 245, 191, 204, 128, + 220, 171, 166, 127, 221, 166, 139, 111, 25, 1, 37, 202, 219, 109, 49, 103, 76, 89, 211, 8, 0, 0, 0, + ]); + + expect(packTokenGroupMember(tokenGroupMember)).to.deep.equal(bytes); + expect(unpackTokenGroupMember(bytes)).to.deep.equal(tokenGroupMember); + }); + }); +}); diff --git a/token-group/js/tsconfig.all.json b/token-group/js/tsconfig.all.json new file mode 100644 index 00000000000..985513259e2 --- /dev/null +++ b/token-group/js/tsconfig.all.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.root.json", + "references": [ + { + "path": "./tsconfig.cjs.json" + }, + { + "path": "./tsconfig.esm.json" + } + ] +} diff --git a/token-group/js/tsconfig.base.json b/token-group/js/tsconfig.base.json new file mode 100644 index 00000000000..90620c4e485 --- /dev/null +++ b/token-group/js/tsconfig.base.json @@ -0,0 +1,14 @@ +{ + "include": [], + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "Node", + "esModuleInterop": true, + "isolatedModules": true, + "noEmitOnError": true, + "resolveJsonModule": true, + "strict": true, + "stripInternal": true + } +} diff --git a/token-group/js/tsconfig.cjs.json b/token-group/js/tsconfig.cjs.json new file mode 100644 index 00000000000..2db9b71569e --- /dev/null +++ b/token-group/js/tsconfig.cjs.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.base.json", + "include": ["src"], + "compilerOptions": { + "outDir": "lib/cjs", + "target": "ES2016", + "module": "CommonJS", + "sourceMap": true + } +} diff --git a/token-group/js/tsconfig.esm.json b/token-group/js/tsconfig.esm.json new file mode 100644 index 00000000000..25e7e25e751 --- /dev/null +++ b/token-group/js/tsconfig.esm.json @@ -0,0 +1,13 @@ +{ + "extends": "./tsconfig.base.json", + "include": ["src"], + "compilerOptions": { + "outDir": "lib/esm", + "declarationDir": "lib/types", + "target": "ES2020", + "module": "ES2020", + "sourceMap": true, + "declaration": true, + "declarationMap": true + } +} diff --git a/token-group/js/tsconfig.json b/token-group/js/tsconfig.json new file mode 100644 index 00000000000..2f9b239bfca --- /dev/null +++ b/token-group/js/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.all.json", + "include": ["src", "test"], + "compilerOptions": { + "noEmit": true, + "skipLibCheck": true + } +} diff --git a/token-group/js/tsconfig.root.json b/token-group/js/tsconfig.root.json new file mode 100644 index 00000000000..fadf294ab43 --- /dev/null +++ b/token-group/js/tsconfig.root.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "composite": true + } +} diff --git a/token-group/js/typedoc.json b/token-group/js/typedoc.json new file mode 100644 index 00000000000..c39fc53aee1 --- /dev/null +++ b/token-group/js/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": ["src/index.ts"], + "out": "docs", + "readme": "README.md" +} From f4e9f2557a871bec8c8c87e143760404b144c0d0 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 21 Dec 2023 19:43:16 +0800 Subject: [PATCH 2/7] remove Podu32 and use number --- .github/workflows/pull-request-js.yml | 1 + token-group/js/src/state/tokenGroup.ts | 34 ++++++++------------ token-group/js/src/state/tokenGroupMember.ts | 13 +++++--- token-group/js/test/state.test.ts | 18 +++++++---- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/.github/workflows/pull-request-js.yml b/.github/workflows/pull-request-js.yml index a078a2155fb..eda852e8d3e 100644 --- a/.github/workflows/pull-request-js.yml +++ b/.github/workflows/pull-request-js.yml @@ -45,6 +45,7 @@ jobs: single-pool, stake-pool, token, + token-group, token-lending, token-metadata, token-swap, diff --git a/token-group/js/src/state/tokenGroup.ts b/token-group/js/src/state/tokenGroup.ts index 37b7053c566..80ca87fd05d 100644 --- a/token-group/js/src/state/tokenGroup.ts +++ b/token-group/js/src/state/tokenGroup.ts @@ -8,27 +8,15 @@ const tokenGroupCodec = getStructCodec([ ['maxSize', getBytesCodec({ size: 4 })], ]); -export class PodU32 { - #value: number; - constructor(value: number) { - this.#value = value; - } - toBuffer(): Buffer { - const buffer = Buffer.alloc(4); - buffer.writeUInt32LE(this.#value); - return buffer; - } -} - export interface TokenGroup { /** The authority that can sign to update the group */ updateAuthority?: PublicKey; /** The associated mint, used to counter spoofing to be sure that group belongs to a particular mint */ mint: PublicKey; /** The current number of group members */ - size: PodU32; + size: number; /** The maximum number of group members */ - maxSize: PodU32; + maxSize: number; } // Checks if all elements in the array are 0 @@ -41,6 +29,12 @@ function isNonePubkey(buffer: Uint8Array): boolean { return true; } +export function numberToU32Buffer(num: number): Buffer { + const buffer = Buffer.alloc(4); + buffer.writeUInt32LE(num); + return buffer; +} + // Pack TokenGroup into byte slab export const packTokenGroup = (group: TokenGroup): Uint8Array => { // If no updateAuthority given, set it to the None/Zero PublicKey for encoding @@ -48,8 +42,8 @@ export const packTokenGroup = (group: TokenGroup): Uint8Array => { return tokenGroupCodec.encode({ updateAuthority: updateAuthority.toBuffer(), mint: group.mint.toBuffer(), - size: group.size.toBuffer(), - maxSize: group.maxSize.toBuffer(), + size: numberToU32Buffer(group.size), + maxSize: numberToU32Buffer(group.maxSize), }); }; @@ -60,13 +54,13 @@ export function unpackTokenGroup(buffer: Buffer | Uint8Array): TokenGroup { return isNonePubkey(data.updateAuthority) ? { mint: new PublicKey(data.mint), - size: new PodU32(data.size), - maxSize: new PodU32(data.maxSize), + size: Buffer.from(data.size).readUInt32LE(), + maxSize: Buffer.from(data.maxSize).readUInt32LE(), } : { updateAuthority: new PublicKey(data.updateAuthority), mint: new PublicKey(data.mint), - size: new PodU32(data.size), - maxSize: new PodU32(data.maxSize), + size: Buffer.from(data.size).readUInt32LE(), + maxSize: Buffer.from(data.maxSize).readUInt32LE(), }; } diff --git a/token-group/js/src/state/tokenGroupMember.ts b/token-group/js/src/state/tokenGroupMember.ts index 2c9d89b436e..dff62f29acf 100644 --- a/token-group/js/src/state/tokenGroupMember.ts +++ b/token-group/js/src/state/tokenGroupMember.ts @@ -1,6 +1,6 @@ import { PublicKey } from '@solana/web3.js'; import { getBytesCodec, getStructCodec } from '@solana/codecs-data-structures'; -import { PodU32 } from './tokenGroup.js'; +import { numberToU32Buffer } from './tokenGroup.js'; const tokenGroupMemberCodec = getStructCodec([ ['mint', getBytesCodec({ size: 32 })], @@ -14,7 +14,7 @@ export interface TokenGroupMember { /** The pubkey of the `TokenGroup` */ group: PublicKey; /** The member number */ - memberNumber: PodU32; + memberNumber: number; } // Pack TokenGroupMember into byte slab @@ -22,7 +22,7 @@ export const packTokenGroupMember = (member: TokenGroupMember): Uint8Array => { return tokenGroupMemberCodec.encode({ mint: member.mint.toBuffer(), group: member.group.toBuffer(), - memberNumber: member.memberNumber.toBuffer(), + memberNumber: numberToU32Buffer(member.memberNumber), }); }; @@ -32,6 +32,11 @@ export function unpackTokenGroupMember(buffer: Buffer | Uint8Array): TokenGroupM return { mint: new PublicKey(data.mint), group: new PublicKey(data.group), - memberNumber: new PodU32(data.memberNumber), + memberNumber: Buffer.from(data.memberNumber).readUInt32LE(), }; } + +// Uint8Array(4) to number +export function u32ToNumber(buffer: Buffer): number { + return buffer.readUInt32LE(); +} diff --git a/token-group/js/test/state.test.ts b/token-group/js/test/state.test.ts index e029851fde1..3882ffc4ee3 100644 --- a/token-group/js/test/state.test.ts +++ b/token-group/js/test/state.test.ts @@ -2,7 +2,13 @@ import { PublicKey } from '@solana/web3.js'; import { expect } from 'chai'; import type { TokenGroup, TokenGroupMember } from '../src/state'; -import { unpackTokenGroupMember, packTokenGroupMember, unpackTokenGroup, packTokenGroup, PodU32 } from '../src'; +import { + unpackTokenGroupMember, + packTokenGroupMember, + unpackTokenGroup, + packTokenGroup, + numberToU32Buffer, +} from '../src'; describe('State', () => { describe('Token Group', () => { @@ -10,8 +16,8 @@ describe('State', () => { const tokenGroup: TokenGroup = { mint: new PublicKey('44444444444444444444444444444444444444444444'), updateAuthority: new PublicKey('55555555555555555555555555555555555555555555'), - size: new PodU32(10), - maxSize: new PodU32(20), + size: 10, + maxSize: 20, }; // From rust implementation @@ -28,8 +34,8 @@ describe('State', () => { it('Can pack and unpack TokenGroup without updateAuthority as rust implementation', () => { const tokenGroup: TokenGroup = { mint: new PublicKey('44444444444444444444444444444444444444444444'), - size: new PodU32(10), - maxSize: new PodU32(20), + size: 10, + maxSize: 20, }; // From rust implementation @@ -49,7 +55,7 @@ describe('State', () => { const tokenGroupMember: TokenGroupMember = { mint: new PublicKey('55555555555555555555555555555555555555555555'), group: new PublicKey('66666666666666666666666666666666666666666666'), - memberNumber: new PodU32(8), + memberNumber: 8, }; // From rust implementation const bytes = Uint8Array.from([ From 9a153afb14abc195f0d3f2363ef88d7e1fa3fd44 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 21 Dec 2023 20:17:04 +0800 Subject: [PATCH 3/7] tests: remove hard code bytes --- token-group/js/test/instruction.test.ts | 140 ++++++++++-------------- token-group/js/test/state.test.ts | 66 ++++------- 2 files changed, 81 insertions(+), 125 deletions(-) diff --git a/token-group/js/test/instruction.test.ts b/token-group/js/test/instruction.test.ts index 5fca23382d2..f06a3528c37 100644 --- a/token-group/js/test/instruction.test.ts +++ b/token-group/js/test/instruction.test.ts @@ -1,5 +1,9 @@ -import { PublicKey, TransactionInstruction } from '@solana/web3.js'; import { expect } from 'chai'; +import type { StructToDecoderTuple } from '@solana/codecs-data-structures'; +import { getBytesDecoder, getStructDecoder } from '@solana/codecs-data-structures'; +import { splDiscriminate } from '@solana/spl-type-length-value'; +import { getU32Decoder } from '@solana/codecs-numbers'; +import { PublicKey, type TransactionInstruction } from '@solana/web3.js'; import { createInitializeGroupInstruction, @@ -8,6 +12,18 @@ import { createUpdateGroupAuthorityInstruction, } from '../src'; +function checkPackUnpack( + instruction: TransactionInstruction, + discriminator: Uint8Array, + layout: StructToDecoderTuple, + values: T +) { + expect(instruction.data.subarray(0, 8)).to.deep.equal(discriminator); + const decoder = getStructDecoder(layout); + const unpacked = decoder.decode(instruction.data.subarray(8)); + expect(unpacked).to.deep.equal(values); +} + describe('Token Group Instructions', () => { const programId = new PublicKey('22222222222222222222222222222222222222222222'); const group = new PublicKey('33333333333333333333333333333333333333333333'); @@ -17,76 +33,49 @@ describe('Token Group Instructions', () => { const maxSize = 100; it('Can create InitializeGroup Instruction', () => { - const instruction = createInitializeGroupInstruction({ - programId, - group, - mint, - mintAuthority, - updateAuthority, - maxSize, - }); - - expect(instruction).to.deep.equal( - new TransactionInstruction({ + checkPackUnpack( + createInitializeGroupInstruction({ programId, - keys: [ - { isSigner: false, isWritable: true, pubkey: group }, - { isSigner: false, isWritable: false, pubkey: mint }, - { isSigner: true, isWritable: false, pubkey: mintAuthority }, - ], - data: Buffer.from([ - // Output of rust implementation - 121, 113, 108, 39, 54, 51, 0, 4, 45, 91, 65, 60, 101, 64, 222, 21, 12, 147, 115, 20, 77, 81, 51, - 202, 76, 184, 48, 186, 15, 117, 103, 22, 172, 234, 14, 80, 215, 148, 53, 229, 100, 0, 0, 0, - ]), - }) + group, + mint, + mintAuthority, + updateAuthority, + maxSize, + }), + splDiscriminate('spl_token_group_interface:initialize_token_group'), + [ + ['updateAuthority', getBytesDecoder({ size: 32 })], + ['maxSize', getU32Decoder()], + ], + { updateAuthority: Uint8Array.from(updateAuthority.toBuffer()), maxSize } ); }); it('Can create UpdateGroupMaxSize Instruction', () => { - const instruction = createUpdateGroupMaxSizeInstruction({ - programId, - group, - updateAuthority, - maxSize, - }); - - expect(instruction).to.deep.equal( - new TransactionInstruction({ + checkPackUnpack( + createUpdateGroupMaxSizeInstruction({ programId, - keys: [ - { isSigner: false, isWritable: true, pubkey: group }, - { isSigner: true, isWritable: false, pubkey: updateAuthority }, - ], - data: Buffer.from([ - // Output of rust implementation - 108, 37, 171, 143, 248, 30, 18, 110, 100, 0, 0, 0, - ]), - }) + group, + updateAuthority, + maxSize, + }), + splDiscriminate('spl_token_group_interface:update_group_max_size'), + [['maxSize', getU32Decoder()]], + { maxSize } ); }); it('Can create UpdateGroupAuthority Instruction', () => { - const instruction = createUpdateGroupAuthorityInstruction({ - programId, - group, - currentAuthority: updateAuthority, - newAuthority: PublicKey.default, - }); - - expect(instruction).to.deep.equal( - new TransactionInstruction({ + checkPackUnpack( + createUpdateGroupAuthorityInstruction({ programId, - keys: [ - { isSigner: false, isWritable: true, pubkey: group }, - { isSigner: true, isWritable: false, pubkey: updateAuthority }, - ], - data: Buffer.from([ - // Output of rust implementation - 161, 105, 88, 1, 237, 221, 216, 203, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]), - }) + group, + currentAuthority: updateAuthority, + newAuthority: PublicKey.default, + }), + splDiscriminate('spl_token_group_interface:update_authority'), + [['newAuthority', getBytesDecoder({ size: 32 })]], + { newAuthority: Uint8Array.from(PublicKey.default.toBuffer()) } ); }); @@ -97,27 +86,18 @@ describe('Token Group Instructions', () => { const group = new PublicKey('55555555555555555555555555555555555555555555'); const groupUpdateAuthority = new PublicKey('66666666666666666666666666666666666666666666'); - const instruction = createInitializeMemberInstruction({ - programId, - member, - memberMint, - memberMintAuthority, - group, - groupUpdateAuthority, - }); - - expect(instruction).to.deep.equal( - new TransactionInstruction({ + checkPackUnpack( + createInitializeMemberInstruction({ programId, - keys: [ - { isSigner: false, isWritable: true, pubkey: member }, - { isSigner: false, isWritable: false, pubkey: memberMint }, - { isSigner: true, isWritable: false, pubkey: memberMintAuthority }, - { isSigner: false, isWritable: true, pubkey: group }, - { isSigner: true, isWritable: false, pubkey: groupUpdateAuthority }, - ], - data: Buffer.from([152, 32, 222, 176, 223, 237, 116, 134]), - }) + member, + memberMint, + memberMintAuthority, + group, + groupUpdateAuthority, + }), + splDiscriminate('spl_token_group_interface:initialize_member'), + [], + {} ); }); }); diff --git a/token-group/js/test/state.test.ts b/token-group/js/test/state.test.ts index 3882ffc4ee3..b0d5c6230cd 100644 --- a/token-group/js/test/state.test.ts +++ b/token-group/js/test/state.test.ts @@ -2,70 +2,46 @@ import { PublicKey } from '@solana/web3.js'; import { expect } from 'chai'; import type { TokenGroup, TokenGroupMember } from '../src/state'; -import { - unpackTokenGroupMember, - packTokenGroupMember, - unpackTokenGroup, - packTokenGroup, - numberToU32Buffer, -} from '../src'; +import { unpackTokenGroupMember, packTokenGroupMember, unpackTokenGroup, packTokenGroup } from '../src'; describe('State', () => { describe('Token Group', () => { - it('Can pack and unpack TokenGroup with updateAuthority as rust implementation', () => { - const tokenGroup: TokenGroup = { + function checkPackUnpack(tokenGroup: TokenGroup) { + const packed = packTokenGroup(tokenGroup); + const unpacked = unpackTokenGroup(packed); + expect(unpacked).to.deep.equal(tokenGroup); + } + + it('Can pack and unpack TokenGroup with updateAuthoritygroup', () => { + checkPackUnpack({ mint: new PublicKey('44444444444444444444444444444444444444444444'), updateAuthority: new PublicKey('55555555555555555555555555555555555555555555'), size: 10, maxSize: 20, - }; - - // From rust implementation - const bytes = Uint8Array.from([ - 60, 121, 172, 80, 135, 1, 40, 28, 16, 196, 153, 112, 103, 22, 239, 184, 102, 74, 235, 162, 191, 71, 52, - 30, 59, 226, 189, 193, 31, 112, 71, 220, 45, 91, 65, 60, 101, 64, 222, 21, 12, 147, 115, 20, 77, 81, 51, - 202, 76, 184, 48, 186, 15, 117, 103, 22, 172, 234, 14, 80, 215, 148, 53, 229, 10, 0, 0, 0, 20, 0, 0, 0, - ]); - - expect(packTokenGroup(tokenGroup)).to.deep.equal(bytes); - expect(unpackTokenGroup(bytes)).to.deep.equal(tokenGroup); + }); }); - it('Can pack and unpack TokenGroup without updateAuthority as rust implementation', () => { - const tokenGroup: TokenGroup = { + it('Can pack and unpack TokenGroup without updateAuthoritygroup', () => { + checkPackUnpack({ mint: new PublicKey('44444444444444444444444444444444444444444444'), size: 10, maxSize: 20, - }; - - // From rust implementation - const bytes = Uint8Array.from([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 91, - 65, 60, 101, 64, 222, 21, 12, 147, 115, 20, 77, 81, 51, 202, 76, 184, 48, 186, 15, 117, 103, 22, 172, - 234, 14, 80, 215, 148, 53, 229, 10, 0, 0, 0, 20, 0, 0, 0, - ]); - - expect(packTokenGroup(tokenGroup)).to.deep.equal(bytes); - expect(unpackTokenGroup(bytes)).to.deep.equal(tokenGroup); + }); }); }); describe('Token Group Member', () => { - it('Can pack and unpack TokenGroupMember as rust implementation', () => { - const tokenGroupMember: TokenGroupMember = { + function checkPackUnpack(tokenGroupMember: TokenGroupMember) { + const packed = packTokenGroupMember(tokenGroupMember); + const unpacked = unpackTokenGroupMember(packed); + expect(unpacked).to.deep.equal(tokenGroupMember); + } + it('Can pack and unpack TokenGroupMembergroup', () => { + checkPackUnpack({ mint: new PublicKey('55555555555555555555555555555555555555555555'), group: new PublicKey('66666666666666666666666666666666666666666666'), memberNumber: 8, - }; - // From rust implementation - const bytes = Uint8Array.from([ - 60, 121, 172, 80, 135, 1, 40, 28, 16, 196, 153, 112, 103, 22, 239, 184, 102, 74, 235, 162, 191, 71, 52, - 30, 59, 226, 189, 193, 31, 112, 71, 220, 75, 152, 23, 100, 168, 193, 114, 35, 20, 245, 191, 204, 128, - 220, 171, 166, 127, 221, 166, 139, 111, 25, 1, 37, 202, 219, 109, 49, 103, 76, 89, 211, 8, 0, 0, 0, - ]); - - expect(packTokenGroupMember(tokenGroupMember)).to.deep.equal(bytes); - expect(unpackTokenGroupMember(bytes)).to.deep.equal(tokenGroupMember); + }); }); }); }); From 2fbf5a7193425858e712fbab1bee5a18770a5214 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 21 Dec 2023 21:20:03 +0800 Subject: [PATCH 4/7] use getU32Codec --- token-group/js/src/state/tokenGroup.ts | 23 ++++++++------------ token-group/js/src/state/tokenGroupMember.ts | 13 ++++------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/token-group/js/src/state/tokenGroup.ts b/token-group/js/src/state/tokenGroup.ts index 80ca87fd05d..f5774ae0bba 100644 --- a/token-group/js/src/state/tokenGroup.ts +++ b/token-group/js/src/state/tokenGroup.ts @@ -1,11 +1,12 @@ import { PublicKey } from '@solana/web3.js'; import { getBytesCodec, getStructCodec } from '@solana/codecs-data-structures'; +import { getU32Codec } from '@solana/codecs-numbers'; const tokenGroupCodec = getStructCodec([ ['updateAuthority', getBytesCodec({ size: 32 })], ['mint', getBytesCodec({ size: 32 })], - ['size', getBytesCodec({ size: 4 })], - ['maxSize', getBytesCodec({ size: 4 })], + ['size', getU32Codec()], + ['maxSize', getU32Codec()], ]); export interface TokenGroup { @@ -29,12 +30,6 @@ function isNonePubkey(buffer: Uint8Array): boolean { return true; } -export function numberToU32Buffer(num: number): Buffer { - const buffer = Buffer.alloc(4); - buffer.writeUInt32LE(num); - return buffer; -} - // Pack TokenGroup into byte slab export const packTokenGroup = (group: TokenGroup): Uint8Array => { // If no updateAuthority given, set it to the None/Zero PublicKey for encoding @@ -42,8 +37,8 @@ export const packTokenGroup = (group: TokenGroup): Uint8Array => { return tokenGroupCodec.encode({ updateAuthority: updateAuthority.toBuffer(), mint: group.mint.toBuffer(), - size: numberToU32Buffer(group.size), - maxSize: numberToU32Buffer(group.maxSize), + size: group.size, + maxSize: group.maxSize, }); }; @@ -54,13 +49,13 @@ export function unpackTokenGroup(buffer: Buffer | Uint8Array): TokenGroup { return isNonePubkey(data.updateAuthority) ? { mint: new PublicKey(data.mint), - size: Buffer.from(data.size).readUInt32LE(), - maxSize: Buffer.from(data.maxSize).readUInt32LE(), + size: data.size, + maxSize: data.maxSize, } : { updateAuthority: new PublicKey(data.updateAuthority), mint: new PublicKey(data.mint), - size: Buffer.from(data.size).readUInt32LE(), - maxSize: Buffer.from(data.maxSize).readUInt32LE(), + size: data.size, + maxSize: data.maxSize, }; } diff --git a/token-group/js/src/state/tokenGroupMember.ts b/token-group/js/src/state/tokenGroupMember.ts index dff62f29acf..3fda58c9e4d 100644 --- a/token-group/js/src/state/tokenGroupMember.ts +++ b/token-group/js/src/state/tokenGroupMember.ts @@ -1,11 +1,11 @@ import { PublicKey } from '@solana/web3.js'; import { getBytesCodec, getStructCodec } from '@solana/codecs-data-structures'; -import { numberToU32Buffer } from './tokenGroup.js'; +import { getU32Codec } from '@solana/codecs-numbers'; const tokenGroupMemberCodec = getStructCodec([ ['mint', getBytesCodec({ size: 32 })], ['group', getBytesCodec({ size: 32 })], - ['memberNumber', getBytesCodec({ size: 4 })], + ['memberNumber', getU32Codec()], ]); export interface TokenGroupMember { @@ -22,7 +22,7 @@ export const packTokenGroupMember = (member: TokenGroupMember): Uint8Array => { return tokenGroupMemberCodec.encode({ mint: member.mint.toBuffer(), group: member.group.toBuffer(), - memberNumber: numberToU32Buffer(member.memberNumber), + memberNumber: member.memberNumber, }); }; @@ -32,11 +32,6 @@ export function unpackTokenGroupMember(buffer: Buffer | Uint8Array): TokenGroupM return { mint: new PublicKey(data.mint), group: new PublicKey(data.group), - memberNumber: Buffer.from(data.memberNumber).readUInt32LE(), + memberNumber: data.memberNumber, }; } - -// Uint8Array(4) to number -export function u32ToNumber(buffer: Buffer): number { - return buffer.readUInt32LE(); -} From 3a2cfb347702d7d6a150eb5f576aae73625cbfde Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Fri, 22 Dec 2023 09:25:18 +0800 Subject: [PATCH 5/7] remove unused deps --- token-group/js/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/token-group/js/package.json b/token-group/js/package.json index c54409d9106..45208020d65 100644 --- a/token-group/js/package.json +++ b/token-group/js/package.json @@ -47,7 +47,6 @@ "@solana/web3.js": "^1.87.6" }, "dependencies": { - "@solana/codecs-core": "2.0.0-experimental.9741939", "@solana/codecs-data-structures": "2.0.0-experimental.9741939", "@solana/codecs-numbers": "2.0.0-experimental.9741939", "@solana/codecs-strings": "2.0.0-experimental.9741939", From 6a8f38d931c3a6c7d8811dca269554b9f4c3f554 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 4 Jan 2024 10:10:13 +0800 Subject: [PATCH 6/7] remove unused import --- pnpm-lock.yaml | 11 ++++------- token-group/js/src/instruction.ts | 6 ++---- token-group/js/src/state/tokenGroup.ts | 4 ++-- token-group/js/test/state.test.ts | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fcb8111ae0f..1d2419fb68d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - importers: .: @@ -501,9 +497,6 @@ importers: token-group/js: dependencies: - '@solana/codecs-core': - specifier: 2.0.0-experimental.9741939 - version: 2.0.0-experimental.9741939 '@solana/codecs-data-structures': specifier: 2.0.0-experimental.9741939 version: 2.0.0-experimental.9741939 @@ -8096,3 +8089,7 @@ packages: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} dev: true + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false diff --git a/token-group/js/src/instruction.ts b/token-group/js/src/instruction.ts index 19e944a2d5f..048eb140863 100644 --- a/token-group/js/src/instruction.ts +++ b/token-group/js/src/instruction.ts @@ -1,9 +1,7 @@ import type { StructToEncoderTuple } from '@solana/codecs-data-structures'; import type { PublicKey } from '@solana/web3.js'; -import { getBooleanEncoder, getBytesEncoder, getDataEnumCodec, getStructEncoder } from '@solana/codecs-data-structures'; -import { getU32Encoder, getU64Encoder } from '@solana/codecs-numbers'; -import { getStringEncoder } from '@solana/codecs-strings'; -import { getOptionEncoder } from '@solana/options'; +import { getBytesEncoder, getStructEncoder } from '@solana/codecs-data-structures'; +import { getU32Encoder } from '@solana/codecs-numbers'; import { splDiscriminate } from '@solana/spl-type-length-value'; import { TransactionInstruction } from '@solana/web3.js'; diff --git a/token-group/js/src/state/tokenGroup.ts b/token-group/js/src/state/tokenGroup.ts index f5774ae0bba..ed45084314b 100644 --- a/token-group/js/src/state/tokenGroup.ts +++ b/token-group/js/src/state/tokenGroup.ts @@ -31,7 +31,7 @@ function isNonePubkey(buffer: Uint8Array): boolean { } // Pack TokenGroup into byte slab -export const packTokenGroup = (group: TokenGroup): Uint8Array => { +export function packTokenGroup(group: TokenGroup): Uint8Array { // If no updateAuthority given, set it to the None/Zero PublicKey for encoding const updateAuthority = group.updateAuthority ?? PublicKey.default; return tokenGroupCodec.encode({ @@ -40,7 +40,7 @@ export const packTokenGroup = (group: TokenGroup): Uint8Array => { size: group.size, maxSize: group.maxSize, }); -}; +} // unpack byte slab into TokenGroup export function unpackTokenGroup(buffer: Buffer | Uint8Array): TokenGroup { diff --git a/token-group/js/test/state.test.ts b/token-group/js/test/state.test.ts index b0d5c6230cd..36f874b5209 100644 --- a/token-group/js/test/state.test.ts +++ b/token-group/js/test/state.test.ts @@ -4,7 +4,7 @@ import { expect } from 'chai'; import type { TokenGroup, TokenGroupMember } from '../src/state'; import { unpackTokenGroupMember, packTokenGroupMember, unpackTokenGroup, packTokenGroup } from '../src'; -describe('State', () => { +describe('Token Group State', () => { describe('Token Group', () => { function checkPackUnpack(tokenGroup: TokenGroup) { const packed = packTokenGroup(tokenGroup); From 525807f3a7bf3bba5c0d0d814d715d336f137ffc Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Fri, 5 Jan 2024 10:26:00 +0800 Subject: [PATCH 7/7] fix: update lock file --- pnpm-lock.yaml | 65 +++++++++++++++++--- token-group/js/src/state/tokenGroupMember.ts | 4 +- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f40986c3a88..fc676e37c76 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -524,28 +524,28 @@ importers: version: 10.0.6 '@types/node': specifier: ^20.10.4 - version: 20.10.4 + version: 20.10.6 '@typescript-eslint/eslint-plugin': specifier: ^6.14.0 - version: 6.14.0(@typescript-eslint/parser@6.14.0)(eslint@8.55.0)(typescript@5.3.3) + version: 6.17.0(@typescript-eslint/parser@6.17.0)(eslint@8.56.0)(typescript@5.3.3) '@typescript-eslint/parser': specifier: ^6.14.0 - version: 6.14.0(eslint@8.55.0)(typescript@5.3.3) + version: 6.17.0(eslint@8.56.0)(typescript@5.3.3) chai: specifier: ^4.3.6 version: 4.3.10 eslint: specifier: ^8.55.0 - version: 8.55.0 + version: 8.56.0 eslint-config-prettier: specifier: ^9.1.0 - version: 9.1.0(eslint@8.55.0) + version: 9.1.0(eslint@8.56.0) eslint-plugin-prettier: specifier: ^5.0.1 - version: 5.0.1(@types/eslint@8.44.9)(eslint-config-prettier@9.1.0)(eslint@8.55.0)(prettier@3.1.1) + version: 5.0.1(eslint-config-prettier@9.1.0)(eslint@8.56.0)(prettier@3.1.1) eslint-plugin-require-extensions: specifier: ^0.1.1 - version: 0.1.3(eslint@8.55.0) + version: 0.1.3(eslint@8.56.0) gh-pages: specifier: ^6.1.0 version: 6.1.0 @@ -560,7 +560,7 @@ importers: version: 0.3.4 ts-node: specifier: ^10.9.2 - version: 10.9.2(@types/node@20.10.4)(typescript@5.3.3) + version: 10.9.2(@types/node@20.10.6)(typescript@5.3.3) tslib: specifier: ^2.3.1 version: 2.6.2 @@ -4369,6 +4369,27 @@ packages: rambda: 7.5.0 dev: true + /eslint-plugin-prettier@5.0.1(eslint-config-prettier@9.1.0)(eslint@8.56.0)(prettier@3.1.1): + resolution: {integrity: sha512-m3u5RnR56asrwV/lDC4GHorlW75DsFfmUcjfCYylTUs85dBRnB7VM6xG8eCMJdeDRnppzmxZVf1GEPJvl1JmNg==} + engines: {node: ^14.18.0 || >=16.0.0} + peerDependencies: + '@types/eslint': '>=8.0.0' + eslint: '>=8.0.0' + eslint-config-prettier: '*' + prettier: '>=3.0.0' + peerDependenciesMeta: + '@types/eslint': + optional: true + eslint-config-prettier: + optional: true + dependencies: + eslint: 8.56.0 + eslint-config-prettier: 9.1.0(eslint@8.56.0) + prettier: 3.1.1 + prettier-linter-helpers: 1.0.0 + synckit: 0.8.6 + dev: true + /eslint-plugin-prettier@5.1.2(@types/eslint@8.56.1)(eslint-config-prettier@9.1.0)(eslint@8.56.0)(prettier@3.1.1): resolution: {integrity: sha512-dhlpWc9vOwohcWmClFcA+HjlvUpuyynYs0Rf+L/P6/0iQE6vlHW9l5bkfzN62/Stm9fbq8ku46qzde76T1xlSg==} engines: {node: ^14.18.0 || >=16.0.0} @@ -4937,6 +4958,20 @@ packages: resolve-pkg-maps: 1.0.0 dev: true + /gh-pages@6.1.0: + resolution: {integrity: sha512-MdXigvqN3I66Y+tAZsQJMzpBWQOI1snD6BYuECmP+GEdryYMMOQvzn4AConk/+qNg/XIuQhB1xNGrl3Rmj1iow==} + engines: {node: '>=10'} + hasBin: true + dependencies: + async: 3.2.5 + commander: 11.1.0 + email-addresses: 5.0.0 + filenamify: 4.3.0 + find-cache-dir: 3.3.2 + fs-extra: 11.1.1 + globby: 6.1.0 + dev: true + /gh-pages@6.1.1: resolution: {integrity: sha512-upnohfjBwN5hBP9w2dPE7HO5JJTHzSGMV1JrLrHvNuqmjoYHg6TBrCcnEoorjG/e0ejbuvnwyKMdTyM40PEByw==} engines: {node: '>=10'} @@ -7808,6 +7843,20 @@ packages: is-typed-array: 1.1.12 dev: true + /typedoc@0.25.4(typescript@5.3.3): + resolution: {integrity: sha512-Du9ImmpBCw54bX275yJrxPVnjdIyJO/84co0/L9mwe0R3G4FSR6rQ09AlXVRvZEGMUg09+z/usc8mgygQ1aidA==} + engines: {node: '>= 16'} + hasBin: true + peerDependencies: + typescript: 4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x || 5.1.x || 5.2.x || 5.3.x + dependencies: + lunr: 2.3.9 + marked: 4.3.0 + minimatch: 9.0.3 + shiki: 0.14.7 + typescript: 5.3.3 + dev: true + /typedoc@0.25.6(typescript@5.3.3): resolution: {integrity: sha512-1rdionQMpOkpA58qfym1J+YD+ukyA1IEIa4VZahQI2ZORez7dhOvEyUotQL/8rSoMBopdzOS+vAIsORpQO4cTA==} engines: {node: '>= 16'} diff --git a/token-group/js/src/state/tokenGroupMember.ts b/token-group/js/src/state/tokenGroupMember.ts index 3fda58c9e4d..2bc4d3e2e11 100644 --- a/token-group/js/src/state/tokenGroupMember.ts +++ b/token-group/js/src/state/tokenGroupMember.ts @@ -18,13 +18,13 @@ export interface TokenGroupMember { } // Pack TokenGroupMember into byte slab -export const packTokenGroupMember = (member: TokenGroupMember): Uint8Array => { +export function packTokenGroupMember(member: TokenGroupMember): Uint8Array { return tokenGroupMemberCodec.encode({ mint: member.mint.toBuffer(), group: member.group.toBuffer(), memberNumber: member.memberNumber, }); -}; +} // unpack byte slab into TokenGroupMember export function unpackTokenGroupMember(buffer: Buffer | Uint8Array): TokenGroupMember {