diff --git a/.github/workflows/pull-request-js.yml b/.github/workflows/pull-request-js.yml index f38c156fb8e..eda852e8d3e 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,20 @@ 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-group, + 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 0b271dc02a0..fc676e37c76 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - importers: .: @@ -499,6 +495,82 @@ importers: specifier: ^5.3.3 version: 5.3.3 + token-group/js: + dependencies: + '@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.6 + '@typescript-eslint/eslint-plugin': + specifier: ^6.14.0 + 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.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.56.0 + eslint-config-prettier: + specifier: ^9.1.0 + version: 9.1.0(eslint@8.56.0) + eslint-plugin-prettier: + specifier: ^5.0.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.56.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.6)(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': @@ -4297,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} @@ -4865,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'} @@ -7736,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'} @@ -8054,3 +8175,7 @@ packages: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} dev: true + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false 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..45208020d65 --- /dev/null +++ b/token-group/js/package.json @@ -0,0 +1,77 @@ +{ + "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-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..048eb140863 --- /dev/null +++ b/token-group/js/src/instruction.ts @@ -0,0 +1,132 @@ +import type { StructToEncoderTuple } from '@solana/codecs-data-structures'; +import type { PublicKey } from '@solana/web3.js'; +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'; + +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..ed45084314b --- /dev/null +++ b/token-group/js/src/state/tokenGroup.ts @@ -0,0 +1,61 @@ +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', getU32Codec()], + ['maxSize', getU32Codec()], +]); + +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: number; + /** The maximum number of group members */ + maxSize: number; +} + +// 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 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({ + updateAuthority: updateAuthority.toBuffer(), + mint: group.mint.toBuffer(), + size: group.size, + maxSize: group.maxSize, + }); +} + +// 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: data.size, + maxSize: data.maxSize, + } + : { + updateAuthority: new PublicKey(data.updateAuthority), + mint: new PublicKey(data.mint), + size: data.size, + maxSize: 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..2bc4d3e2e11 --- /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 { getU32Codec } from '@solana/codecs-numbers'; + +const tokenGroupMemberCodec = getStructCodec([ + ['mint', getBytesCodec({ size: 32 })], + ['group', getBytesCodec({ size: 32 })], + ['memberNumber', getU32Codec()], +]); + +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: number; +} + +// Pack TokenGroupMember into byte slab +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 { + const data = tokenGroupMemberCodec.decode(buffer); + return { + mint: new PublicKey(data.mint), + group: new PublicKey(data.group), + memberNumber: 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..f06a3528c37 --- /dev/null +++ b/token-group/js/test/instruction.test.ts @@ -0,0 +1,103 @@ +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, + createInitializeMemberInstruction, + createUpdateGroupMaxSizeInstruction, + 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'); + const updateAuthority = new PublicKey('44444444444444444444444444444444444444444444'); + const mint = new PublicKey('55555555555555555555555555555555555555555555'); + const mintAuthority = new PublicKey('66666666666666666666666666666666666666666666'); + const maxSize = 100; + + it('Can create InitializeGroup Instruction', () => { + checkPackUnpack( + createInitializeGroupInstruction({ + programId, + 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', () => { + checkPackUnpack( + createUpdateGroupMaxSizeInstruction({ + programId, + group, + updateAuthority, + maxSize, + }), + splDiscriminate('spl_token_group_interface:update_group_max_size'), + [['maxSize', getU32Decoder()]], + { maxSize } + ); + }); + + it('Can create UpdateGroupAuthority Instruction', () => { + checkPackUnpack( + createUpdateGroupAuthorityInstruction({ + programId, + group, + currentAuthority: updateAuthority, + newAuthority: PublicKey.default, + }), + splDiscriminate('spl_token_group_interface:update_authority'), + [['newAuthority', getBytesDecoder({ size: 32 })]], + { newAuthority: Uint8Array.from(PublicKey.default.toBuffer()) } + ); + }); + + 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'); + + checkPackUnpack( + createInitializeMemberInstruction({ + programId, + 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 new file mode 100644 index 00000000000..36f874b5209 --- /dev/null +++ b/token-group/js/test/state.test.ts @@ -0,0 +1,47 @@ +import { PublicKey } from '@solana/web3.js'; +import { expect } from 'chai'; + +import type { TokenGroup, TokenGroupMember } from '../src/state'; +import { unpackTokenGroupMember, packTokenGroupMember, unpackTokenGroup, packTokenGroup } from '../src'; + +describe('Token Group State', () => { + describe('Token Group', () => { + 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, + }); + }); + + it('Can pack and unpack TokenGroup without updateAuthoritygroup', () => { + checkPackUnpack({ + mint: new PublicKey('44444444444444444444444444444444444444444444'), + size: 10, + maxSize: 20, + }); + }); + }); + + describe('Token Group Member', () => { + 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, + }); + }); + }); +}); 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" +}