diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 08c5d36..09d597c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,7 +52,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: fmt - args: --all --manifest-path ./program/Cargo.toml -- --check + args: --all --manifest-path ./Cargo.toml -- --check soteria-scan: runs-on: ubuntu-latest @@ -116,8 +116,8 @@ jobs: - name: Setup run: mkdir -p target/deploy - run: cp -r tests/test-keypairs/* target/deploy - - run: find . -type f -name "*" -exec sed -i'' -e "s/pmvYY6Wgvpe3DEj3UX1FcRpMx43sMLYLJrFTVGcqpdn/$(solana-keygen pubkey tests/test-keypairs/cardinal_payment_manager-keypair.json)/g" {} - - run: find . -type f -name "Anchor.toml" -exec sed -i'' -e "s/tests\/\*.spec.ts/tests\/\*.spec.ts --reporter mocha-junit-reporter --reporter-options mochaFile=.\/tests\/out.xml/g" {} + - run: find . -type f -name "*" -exec sed -i'' -e "s/pmvYY6Wgvpe3DEj3UX1FcRpMx43sMLYLJrFTVGcqpdn/$(solana-keygen pubkey tests/test-keypairs/cardinal_payment_manager-keypair.json)/g" {} + + - run: find . -type f -name "Anchor.toml" -exec sed -i'' -e "s/yarn mocha tests\/\*.spec.ts/yarn mocha tests\/\*.spec.ts --reporter mocha-junit-reporter --reporter-options mochaFile=.\/tests\/out.xml/g" {} + - name: Run tests run: solana-test-validator --url https://api.devnet.solana.com --clone metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s --clone PwDiXFxQsGra4sFFTT8r1QWRMd4vfumiWC1jfWNfdYT --reset & echo $$! > validator.PID diff --git a/.gitignore b/.gitignore index 970d2c3..7d43da4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,6 @@ tests/out.xml # Ignore the build directory for Rust/Anchor target -# Ignore the build directory for Rust/Anchor -program/target # Anchor verified builds directory docker-target @@ -40,7 +38,7 @@ test-ledger/ # Generated IDL types artifacts/ dist/ -src/idls/ +sdk/idl/ keypairs site/ diff --git a/program/Cargo.lock b/Cargo.lock similarity index 100% rename from program/Cargo.lock rename to Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ad6f664 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[workspace] +members = ["programs/*"] + +[profile.release] +lto = "fat" +codegen-units = 1 + +[profile.release.build-override] +opt-level = 3 +incremental = false +codegen-units = 1 \ No newline at end of file diff --git a/Makefile b/Makefile index 9047146..d8b1515 100644 --- a/Makefile +++ b/Makefile @@ -8,19 +8,17 @@ install: yarn install test-keys: - mkdir -p target/deploy - cp -r tests/test-keypairs/* target/deploy - LC_ALL=C find program sdk -type f -exec sed -i '' -e "s/pmvYY6Wgvpe3DEj3UX1FcRpMx43sMLYLJrFTVGcqpdn/$$(solana-keygen pubkey tests/test-keypairs/cardinal_payment_manager-keypair.json)/g" {} + + LC_ALL=C find programs sdk -type f -exec sed -i '' -e "s/pmvYY6Wgvpe3DEj3UX1FcRpMx43sMLYLJrFTVGcqpdn/$$(solana-keygen pubkey ./target/deploy/cardinal_payment_manager-keypair.json)/g" {} + build: - cd program && anchor build + anchor build yarn idl:generate start: - solana-test-validator --url https://api.mainnet-beta.solana.com \ - --clone metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s --clone PwDiXFxQsGra4sFFTT8r1QWRMd4vfumiWC1jfWNfdYT \ - --bpf-program ./program/target/deploy/cardinal_payment_manager-keypair.json ./program/target/deploy/cardinal_payment_manager.so \ - --reset --quiet & echo $$! > validator.PID + solana-test-validator --url https://api.devnet.solana.com \ + --clone metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s --clone PwDiXFxQsGra4sFFTT8r1QWRMd4vfumiWC1jfWNfdYT \ + --bpf-program ./target/deploy/cardinal_payment_manager-keypair.json ./target/deploy/cardinal_payment_manager.so \ + --reset --quiet & echo $$! > validator.PID sleep 5 solana-keygen pubkey ./tests/test-key.json solana airdrop 1000 $(TEST_KEY) --url http://localhost:8899 @@ -29,7 +27,7 @@ test: anchor test --skip-local-validator --skip-build --skip-deploy --provider.cluster localnet clean-test-keys: - LC_ALL=C find program sdk -type f -exec sed -i '' -e "s/$$(solana-keygen pubkey tests/test-keypairs/cardinal_payment_manager-keypair.json)/pmvYY6Wgvpe3DEj3UX1FcRpMx43sMLYLJrFTVGcqpdn/g" {} + + LC_ALL=C find programs sdk -type f -exec sed -i '' -e "s/$$(solana-keygen pubkey ./target/deploy/cardinal_payment_manager-keypair.json)/pmvYY6Wgvpe3DEj3UX1FcRpMx43sMLYLJrFTVGcqpdn/g" {} + stop: pkill solana-test-validator \ No newline at end of file diff --git a/images/banner.png b/images/banner.png new file mode 100644 index 0000000..0c61c81 Binary files /dev/null and b/images/banner.png differ diff --git a/package.json b/package.json index 7682a22..a12ee17 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "idl:generate": "./scripts/generate-idls-types.sh", "typecheck": "tsc", "lint": "eslint . --cache", - "lint:ci": "eslint . --max-warnings=0" + "lint:ci": "eslint . --max-warnings=0", + "docs:generate": "typedoc --excludePrivate --includeVersion --out site/ sdk/index.ts" }, "devDependencies": { "@types/bn.js": "^5.1.1", @@ -35,6 +36,7 @@ "jsbi": "^4.3.0", "mocha": "^10.1.0", "ts-mocha": "^10.0.0", + "typedoc": "^0.23.17", "typescript": "^4.8.4" }, "peerDependencies": { @@ -55,7 +57,7 @@ }, "files": [ "dist/", - "src/" + "sdk/" ], "lint-staged": { "*.{ts,tsx}": "eslint --cache --fix", diff --git a/program/Cargo.toml b/programs/cardinal-payment-manager/Cargo.toml similarity index 100% rename from program/Cargo.toml rename to programs/cardinal-payment-manager/Cargo.toml diff --git a/program/Xargo.toml b/programs/cardinal-payment-manager/Xargo.toml similarity index 100% rename from program/Xargo.toml rename to programs/cardinal-payment-manager/Xargo.toml diff --git a/program/clippy.toml b/programs/cardinal-payment-manager/clippy.toml similarity index 100% rename from program/clippy.toml rename to programs/cardinal-payment-manager/clippy.toml diff --git a/program/src/errors.rs b/programs/cardinal-payment-manager/src/errors.rs similarity index 100% rename from program/src/errors.rs rename to programs/cardinal-payment-manager/src/errors.rs diff --git a/program/src/instructions/close.rs b/programs/cardinal-payment-manager/src/instructions/close.rs similarity index 100% rename from program/src/instructions/close.rs rename to programs/cardinal-payment-manager/src/instructions/close.rs diff --git a/program/src/instructions/handle_payment.rs b/programs/cardinal-payment-manager/src/instructions/handle_payment.rs similarity index 100% rename from program/src/instructions/handle_payment.rs rename to programs/cardinal-payment-manager/src/instructions/handle_payment.rs diff --git a/program/src/instructions/handle_payment_with_royalties.rs b/programs/cardinal-payment-manager/src/instructions/handle_payment_with_royalties.rs similarity index 100% rename from program/src/instructions/handle_payment_with_royalties.rs rename to programs/cardinal-payment-manager/src/instructions/handle_payment_with_royalties.rs diff --git a/program/src/instructions/init.rs b/programs/cardinal-payment-manager/src/instructions/init.rs similarity index 100% rename from program/src/instructions/init.rs rename to programs/cardinal-payment-manager/src/instructions/init.rs diff --git a/program/src/instructions/mod.rs b/programs/cardinal-payment-manager/src/instructions/mod.rs similarity index 100% rename from program/src/instructions/mod.rs rename to programs/cardinal-payment-manager/src/instructions/mod.rs diff --git a/program/src/instructions/update.rs b/programs/cardinal-payment-manager/src/instructions/update.rs similarity index 100% rename from program/src/instructions/update.rs rename to programs/cardinal-payment-manager/src/instructions/update.rs diff --git a/program/src/lib.rs b/programs/cardinal-payment-manager/src/lib.rs similarity index 100% rename from program/src/lib.rs rename to programs/cardinal-payment-manager/src/lib.rs diff --git a/program/src/state.rs b/programs/cardinal-payment-manager/src/state.rs similarity index 100% rename from program/src/state.rs rename to programs/cardinal-payment-manager/src/state.rs diff --git a/scripts/generate-idls-types.sh b/scripts/generate-idls-types.sh index 15da433..a2fc656 100755 --- a/scripts/generate-idls-types.sh +++ b/scripts/generate-idls-types.sh @@ -5,7 +5,7 @@ rm -rf sdk/idl/ mkdir -p sdk/idl/ -for PROGRAM in $(find program/ -maxdepth 3 -name lib.rs); do +for PROGRAM in $(find programs/ -maxdepth 3 -name lib.rs); do PROGRAM_NAME="cardinal_payment_manager" echo "Parsing IDL for $PROGRAM_NAME" anchor idl parse --file $PROGRAM --out-ts sdk/idl/$PROGRAM_NAME.ts || { diff --git a/sdk/idl/cardinal_payment_manager.ts b/sdk/idl/cardinal_payment_manager.ts index df43c8e..d8bff46 100644 --- a/sdk/idl/cardinal_payment_manager.ts +++ b/sdk/idl/cardinal_payment_manager.ts @@ -1,665 +1,665 @@ export type CardinalPaymentManager = { - "version": "1.7.5", - "name": "cardinal_payment_manager", - "instructions": [ + version: "1.7.5"; + name: "cardinal_payment_manager"; + instructions: [ { - "name": "init", - "accounts": [ + name: "init"; + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager"; + isMut: true; + isSigner: false; }, { - "name": "authority", - "isMut": true, - "isSigner": false + name: "authority"; + isMut: true; + isSigner: false; }, { - "name": "payer", - "isMut": true, - "isSigner": true + name: "payer"; + isMut: true; + isSigner: true; }, { - "name": "systemProgram", - "isMut": false, - "isSigner": false + name: "systemProgram"; + isMut: false; + isSigner: false; } - ], - "args": [ + ]; + args: [ { - "name": "ix", - "type": { - "defined": "InitIx" - } + name: "ix"; + type: { + defined: "InitIx"; + }; } - ] + ]; }, { - "name": "managePayment", - "accounts": [ + name: "managePayment"; + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager"; + isMut: true; + isSigner: false; }, { - "name": "payerTokenAccount", - "isMut": true, - "isSigner": false + name: "payerTokenAccount"; + isMut: true; + isSigner: false; }, { - "name": "feeCollectorTokenAccount", - "isMut": true, - "isSigner": false + name: "feeCollectorTokenAccount"; + isMut: true; + isSigner: false; }, { - "name": "paymentTokenAccount", - "isMut": true, - "isSigner": false + name: "paymentTokenAccount"; + isMut: true; + isSigner: false; }, { - "name": "payer", - "isMut": false, - "isSigner": true + name: "payer"; + isMut: false; + isSigner: true; }, { - "name": "tokenProgram", - "isMut": false, - "isSigner": false + name: "tokenProgram"; + isMut: false; + isSigner: false; } - ], - "args": [ + ]; + args: [ { - "name": "paymentAmount", - "type": "u64" + name: "paymentAmount"; + type: "u64"; } - ] + ]; }, { - "name": "handlePaymentWithRoyalties", - "accounts": [ + name: "handlePaymentWithRoyalties"; + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager"; + isMut: true; + isSigner: false; }, { - "name": "payerTokenAccount", - "isMut": true, - "isSigner": false + name: "payerTokenAccount"; + isMut: true; + isSigner: false; }, { - "name": "feeCollectorTokenAccount", - "isMut": true, - "isSigner": false + name: "feeCollectorTokenAccount"; + isMut: true; + isSigner: false; }, { - "name": "paymentTokenAccount", - "isMut": true, - "isSigner": false + name: "paymentTokenAccount"; + isMut: true; + isSigner: false; }, { - "name": "paymentMint", - "isMut": false, - "isSigner": false + name: "paymentMint"; + isMut: false; + isSigner: false; }, { - "name": "mint", - "isMut": false, - "isSigner": false + name: "mint"; + isMut: false; + isSigner: false; }, { - "name": "mintMetadata", - "isMut": false, - "isSigner": false + name: "mintMetadata"; + isMut: false; + isSigner: false; }, { - "name": "payer", - "isMut": false, - "isSigner": true + name: "payer"; + isMut: false; + isSigner: true; }, { - "name": "tokenProgram", - "isMut": false, - "isSigner": false + name: "tokenProgram"; + isMut: false; + isSigner: false; } - ], - "args": [ + ]; + args: [ { - "name": "paymentAmount", - "type": "u64" + name: "paymentAmount"; + type: "u64"; } - ] + ]; }, { - "name": "close", - "accounts": [ + name: "close"; + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager"; + isMut: true; + isSigner: false; }, { - "name": "collector", - "isMut": true, - "isSigner": false + name: "collector"; + isMut: true; + isSigner: false; }, { - "name": "closer", - "isMut": true, - "isSigner": true + name: "closer"; + isMut: true; + isSigner: true; } - ], - "args": [] + ]; + args: []; }, { - "name": "update", - "accounts": [ + name: "update"; + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager"; + isMut: true; + isSigner: false; }, { - "name": "payer", - "isMut": true, - "isSigner": true + name: "payer"; + isMut: true; + isSigner: true; }, { - "name": "systemProgram", - "isMut": false, - "isSigner": false + name: "systemProgram"; + isMut: false; + isSigner: false; } - ], - "args": [ + ]; + args: [ { - "name": "ix", - "type": { - "defined": "UpdateIx" - } + name: "ix"; + type: { + defined: "UpdateIx"; + }; } - ] + ]; } - ], - "accounts": [ + ]; + accounts: [ { - "name": "paymentManager", - "type": { - "kind": "struct", - "fields": [ + name: "paymentManager"; + type: { + kind: "struct"; + fields: [ { - "name": "bump", - "type": "u8" + name: "bump"; + type: "u8"; }, { - "name": "feeCollector", - "type": "publicKey" + name: "feeCollector"; + type: "publicKey"; }, { - "name": "authority", - "type": "publicKey" + name: "authority"; + type: "publicKey"; }, { - "name": "makerFeeBasisPoints", - "type": "u16" + name: "makerFeeBasisPoints"; + type: "u16"; }, { - "name": "takerFeeBasisPoints", - "type": "u16" + name: "takerFeeBasisPoints"; + type: "u16"; }, { - "name": "name", - "type": "string" + name: "name"; + type: "string"; }, { - "name": "includeSellerFeeBasisPoints", - "type": "bool" + name: "includeSellerFeeBasisPoints"; + type: "bool"; }, { - "name": "royaltyFeeShare", - "type": { - "option": "u64" - } + name: "royaltyFeeShare"; + type: { + option: "u64"; + }; } - ] - } + ]; + }; } - ], - "types": [ + ]; + types: [ { - "name": "InitIx", - "type": { - "kind": "struct", - "fields": [ + name: "InitIx"; + type: { + kind: "struct"; + fields: [ { - "name": "name", - "type": "string" + name: "name"; + type: "string"; }, { - "name": "feeCollector", - "type": "publicKey" + name: "feeCollector"; + type: "publicKey"; }, { - "name": "makerFeeBasisPoints", - "type": "u16" + name: "makerFeeBasisPoints"; + type: "u16"; }, { - "name": "takerFeeBasisPoints", - "type": "u16" + name: "takerFeeBasisPoints"; + type: "u16"; }, { - "name": "includeSellerFeeBasisPoints", - "type": "bool" + name: "includeSellerFeeBasisPoints"; + type: "bool"; }, { - "name": "royaltyFeeShare", - "type": { - "option": "u64" - } + name: "royaltyFeeShare"; + type: { + option: "u64"; + }; } - ] - } + ]; + }; }, { - "name": "UpdateIx", - "type": { - "kind": "struct", - "fields": [ + name: "UpdateIx"; + type: { + kind: "struct"; + fields: [ { - "name": "authority", - "type": "publicKey" + name: "authority"; + type: "publicKey"; }, { - "name": "feeCollector", - "type": "publicKey" + name: "feeCollector"; + type: "publicKey"; }, { - "name": "makerFeeBasisPoints", - "type": "u16" + name: "makerFeeBasisPoints"; + type: "u16"; }, { - "name": "takerFeeBasisPoints", - "type": "u16" + name: "takerFeeBasisPoints"; + type: "u16"; }, { - "name": "royaltyFeeShare", - "type": { - "option": "u64" - } + name: "royaltyFeeShare"; + type: { + option: "u64"; + }; } - ] - } + ]; + }; } - ], - "errors": [ + ]; + errors: [ { - "code": 6000, - "name": "InvalidFeeCollectorTokenAccount", - "msg": "Invalid fee collector token account" + code: 6000; + name: "InvalidFeeCollectorTokenAccount"; + msg: "Invalid fee collector token account"; }, { - "code": 6001, - "name": "InvalidAuthority", - "msg": "Invalid authority" + code: 6001; + name: "InvalidAuthority"; + msg: "Invalid authority"; }, { - "code": 6002, - "name": "InvalidMintMetadata", - "msg": "Invalid mint metadata" + code: 6002; + name: "InvalidMintMetadata"; + msg: "Invalid mint metadata"; }, { - "code": 6003, - "name": "InvalidCreatorAddress", - "msg": "Invalid creator address" + code: 6003; + name: "InvalidCreatorAddress"; + msg: "Invalid creator address"; }, { - "code": 6004, - "name": "InvalidTokenAccount", - "msg": "Invalid token account" + code: 6004; + name: "InvalidTokenAccount"; + msg: "Invalid token account"; }, { - "code": 6005, - "name": "InvalidPaymentManager", - "msg": "Invalid payment manager" + code: 6005; + name: "InvalidPaymentManager"; + msg: "Invalid payment manager"; }, { - "code": 6006, - "name": "InvalidMintMetadataOwner", - "msg": "Mint metadata is owned by the incorrect program" + code: 6006; + name: "InvalidMintMetadataOwner"; + msg: "Mint metadata is owned by the incorrect program"; } - ] + ]; }; export const IDL: CardinalPaymentManager = { - "version": "1.7.5", - "name": "cardinal_payment_manager", - "instructions": [ + version: "1.7.5", + name: "cardinal_payment_manager", + instructions: [ { - "name": "init", - "accounts": [ + name: "init", + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager", + isMut: true, + isSigner: false, }, { - "name": "authority", - "isMut": true, - "isSigner": false + name: "authority", + isMut: true, + isSigner: false, }, { - "name": "payer", - "isMut": true, - "isSigner": true + name: "payer", + isMut: true, + isSigner: true, }, { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } + name: "systemProgram", + isMut: false, + isSigner: false, + }, ], - "args": [ + args: [ { - "name": "ix", - "type": { - "defined": "InitIx" - } - } - ] + name: "ix", + type: { + defined: "InitIx", + }, + }, + ], }, { - "name": "managePayment", - "accounts": [ + name: "managePayment", + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager", + isMut: true, + isSigner: false, }, { - "name": "payerTokenAccount", - "isMut": true, - "isSigner": false + name: "payerTokenAccount", + isMut: true, + isSigner: false, }, { - "name": "feeCollectorTokenAccount", - "isMut": true, - "isSigner": false + name: "feeCollectorTokenAccount", + isMut: true, + isSigner: false, }, { - "name": "paymentTokenAccount", - "isMut": true, - "isSigner": false + name: "paymentTokenAccount", + isMut: true, + isSigner: false, }, { - "name": "payer", - "isMut": false, - "isSigner": true + name: "payer", + isMut: false, + isSigner: true, }, { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - } + name: "tokenProgram", + isMut: false, + isSigner: false, + }, ], - "args": [ + args: [ { - "name": "paymentAmount", - "type": "u64" - } - ] + name: "paymentAmount", + type: "u64", + }, + ], }, { - "name": "handlePaymentWithRoyalties", - "accounts": [ + name: "handlePaymentWithRoyalties", + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager", + isMut: true, + isSigner: false, }, { - "name": "payerTokenAccount", - "isMut": true, - "isSigner": false + name: "payerTokenAccount", + isMut: true, + isSigner: false, }, { - "name": "feeCollectorTokenAccount", - "isMut": true, - "isSigner": false + name: "feeCollectorTokenAccount", + isMut: true, + isSigner: false, }, { - "name": "paymentTokenAccount", - "isMut": true, - "isSigner": false + name: "paymentTokenAccount", + isMut: true, + isSigner: false, }, { - "name": "paymentMint", - "isMut": false, - "isSigner": false + name: "paymentMint", + isMut: false, + isSigner: false, }, { - "name": "mint", - "isMut": false, - "isSigner": false + name: "mint", + isMut: false, + isSigner: false, }, { - "name": "mintMetadata", - "isMut": false, - "isSigner": false + name: "mintMetadata", + isMut: false, + isSigner: false, }, { - "name": "payer", - "isMut": false, - "isSigner": true + name: "payer", + isMut: false, + isSigner: true, }, { - "name": "tokenProgram", - "isMut": false, - "isSigner": false - } + name: "tokenProgram", + isMut: false, + isSigner: false, + }, ], - "args": [ + args: [ { - "name": "paymentAmount", - "type": "u64" - } - ] + name: "paymentAmount", + type: "u64", + }, + ], }, { - "name": "close", - "accounts": [ + name: "close", + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager", + isMut: true, + isSigner: false, }, { - "name": "collector", - "isMut": true, - "isSigner": false + name: "collector", + isMut: true, + isSigner: false, }, { - "name": "closer", - "isMut": true, - "isSigner": true - } + name: "closer", + isMut: true, + isSigner: true, + }, ], - "args": [] + args: [], }, { - "name": "update", - "accounts": [ + name: "update", + accounts: [ { - "name": "paymentManager", - "isMut": true, - "isSigner": false + name: "paymentManager", + isMut: true, + isSigner: false, }, { - "name": "payer", - "isMut": true, - "isSigner": true + name: "payer", + isMut: true, + isSigner: true, }, { - "name": "systemProgram", - "isMut": false, - "isSigner": false - } + name: "systemProgram", + isMut: false, + isSigner: false, + }, ], - "args": [ + args: [ { - "name": "ix", - "type": { - "defined": "UpdateIx" - } - } - ] - } + name: "ix", + type: { + defined: "UpdateIx", + }, + }, + ], + }, ], - "accounts": [ + accounts: [ { - "name": "paymentManager", - "type": { - "kind": "struct", - "fields": [ + name: "paymentManager", + type: { + kind: "struct", + fields: [ { - "name": "bump", - "type": "u8" + name: "bump", + type: "u8", }, { - "name": "feeCollector", - "type": "publicKey" + name: "feeCollector", + type: "publicKey", }, { - "name": "authority", - "type": "publicKey" + name: "authority", + type: "publicKey", }, { - "name": "makerFeeBasisPoints", - "type": "u16" + name: "makerFeeBasisPoints", + type: "u16", }, { - "name": "takerFeeBasisPoints", - "type": "u16" + name: "takerFeeBasisPoints", + type: "u16", }, { - "name": "name", - "type": "string" + name: "name", + type: "string", }, { - "name": "includeSellerFeeBasisPoints", - "type": "bool" + name: "includeSellerFeeBasisPoints", + type: "bool", }, { - "name": "royaltyFeeShare", - "type": { - "option": "u64" - } - } - ] - } - } + name: "royaltyFeeShare", + type: { + option: "u64", + }, + }, + ], + }, + }, ], - "types": [ + types: [ { - "name": "InitIx", - "type": { - "kind": "struct", - "fields": [ + name: "InitIx", + type: { + kind: "struct", + fields: [ { - "name": "name", - "type": "string" + name: "name", + type: "string", }, { - "name": "feeCollector", - "type": "publicKey" + name: "feeCollector", + type: "publicKey", }, { - "name": "makerFeeBasisPoints", - "type": "u16" + name: "makerFeeBasisPoints", + type: "u16", }, { - "name": "takerFeeBasisPoints", - "type": "u16" + name: "takerFeeBasisPoints", + type: "u16", }, { - "name": "includeSellerFeeBasisPoints", - "type": "bool" + name: "includeSellerFeeBasisPoints", + type: "bool", }, { - "name": "royaltyFeeShare", - "type": { - "option": "u64" - } - } - ] - } + name: "royaltyFeeShare", + type: { + option: "u64", + }, + }, + ], + }, }, { - "name": "UpdateIx", - "type": { - "kind": "struct", - "fields": [ + name: "UpdateIx", + type: { + kind: "struct", + fields: [ { - "name": "authority", - "type": "publicKey" + name: "authority", + type: "publicKey", }, { - "name": "feeCollector", - "type": "publicKey" + name: "feeCollector", + type: "publicKey", }, { - "name": "makerFeeBasisPoints", - "type": "u16" + name: "makerFeeBasisPoints", + type: "u16", }, { - "name": "takerFeeBasisPoints", - "type": "u16" + name: "takerFeeBasisPoints", + type: "u16", }, { - "name": "royaltyFeeShare", - "type": { - "option": "u64" - } - } - ] - } - } + name: "royaltyFeeShare", + type: { + option: "u64", + }, + }, + ], + }, + }, ], - "errors": [ + errors: [ { - "code": 6000, - "name": "InvalidFeeCollectorTokenAccount", - "msg": "Invalid fee collector token account" + code: 6000, + name: "InvalidFeeCollectorTokenAccount", + msg: "Invalid fee collector token account", }, { - "code": 6001, - "name": "InvalidAuthority", - "msg": "Invalid authority" + code: 6001, + name: "InvalidAuthority", + msg: "Invalid authority", }, { - "code": 6002, - "name": "InvalidMintMetadata", - "msg": "Invalid mint metadata" + code: 6002, + name: "InvalidMintMetadata", + msg: "Invalid mint metadata", }, { - "code": 6003, - "name": "InvalidCreatorAddress", - "msg": "Invalid creator address" + code: 6003, + name: "InvalidCreatorAddress", + msg: "Invalid creator address", }, { - "code": 6004, - "name": "InvalidTokenAccount", - "msg": "Invalid token account" + code: 6004, + name: "InvalidTokenAccount", + msg: "Invalid token account", }, { - "code": 6005, - "name": "InvalidPaymentManager", - "msg": "Invalid payment manager" + code: 6005, + name: "InvalidPaymentManager", + msg: "Invalid payment manager", }, { - "code": 6006, - "name": "InvalidMintMetadataOwner", - "msg": "Mint metadata is owned by the incorrect program" - } - ] + code: 6006, + name: "InvalidMintMetadataOwner", + msg: "Mint metadata is owned by the incorrect program", + }, + ], }; diff --git a/sdk/index.ts b/sdk/index.ts index 10cf241..4f40bb1 100644 --- a/sdk/index.ts +++ b/sdk/index.ts @@ -2,3 +2,4 @@ export * as accounts from "./accounts"; export * from "./constants"; export * as instruction from "./instruction"; export * as pda from "./pda"; +export * as transaction from "./transaction"; diff --git a/sdk/instruction.ts b/sdk/instruction.ts index 12d59c4..03a7450 100644 --- a/sdk/instruction.ts +++ b/sdk/instruction.ts @@ -13,21 +13,22 @@ import { CRANK_KEY } from "."; import type { PAYMENT_MANAGER_PROGRAM } from "."; import { PAYMENT_MANAGER_ADDRESS, PAYMENT_MANAGER_IDL } from "."; -import { findPaymentManagerAddress } from "./pda"; -export const init = async ( +export const init = ( connection: Connection, wallet: Wallet, name: string, params: { + paymentManagerId: PublicKey; feeCollector: PublicKey; - authority?: PublicKey; + authority: PublicKey; makerFeeBasisPoints: number; takerFeeBasisPoints: number; includeSellerFeeBasisPoints: boolean; + payer: PublicKey; royaltyFeeShare?: BN; } -): Promise<[TransactionInstruction, PublicKey]> => { +): TransactionInstruction => { const provider = new AnchorProvider(connection, wallet, {}); const paymentManagerProgram = new Program( @@ -36,42 +37,37 @@ export const init = async ( provider ); - const [paymentManagerId] = await findPaymentManagerAddress(name); - - return [ - paymentManagerProgram.instruction.init( - { - name: name, - feeCollector: params.feeCollector, - makerFeeBasisPoints: params.makerFeeBasisPoints, - takerFeeBasisPoints: params.takerFeeBasisPoints, - includeSellerFeeBasisPoints: params.includeSellerFeeBasisPoints, - royaltyFeeShare: params.royaltyFeeShare ?? null, + return paymentManagerProgram.instruction.init( + { + name: name, + feeCollector: params.feeCollector, + makerFeeBasisPoints: params.makerFeeBasisPoints, + takerFeeBasisPoints: params.takerFeeBasisPoints, + includeSellerFeeBasisPoints: params.includeSellerFeeBasisPoints, + royaltyFeeShare: params.royaltyFeeShare ?? null, + }, + { + accounts: { + paymentManager: params.paymentManagerId, + authority: params.authority, + payer: params.payer, + systemProgram: SystemProgram.programId, }, - { - accounts: { - paymentManager: paymentManagerId, - authority: params.authority || wallet.publicKey, - payer: wallet.publicKey, - systemProgram: SystemProgram.programId, - }, - } - ), - paymentManagerId, - ]; + } + ); }; -export const managePayment = async ( +export const managePayment = ( connection: Connection, wallet: Wallet, - name: string, params: { + paymentManagerId: PublicKey; paymentAmount: BN; payerTokenAccount: PublicKey; feeCollectorTokenAccount: PublicKey; paymentTokenAccount: PublicKey; } -): Promise => { +): TransactionInstruction => { const provider = new AnchorProvider(connection, wallet, {}); const paymentManagerProgram = new Program( @@ -80,10 +76,9 @@ export const managePayment = async ( provider ); - const [paymentManagerId] = await findPaymentManagerAddress(name); return paymentManagerProgram.instruction.managePayment(params.paymentAmount, { accounts: { - paymentManager: paymentManagerId, + paymentManager: params.paymentManagerId, payerTokenAccount: params.payerTokenAccount, feeCollectorTokenAccount: params.feeCollectorTokenAccount, paymentTokenAccount: params.paymentTokenAccount, @@ -93,11 +88,11 @@ export const managePayment = async ( }); }; -export const handlePaymentWithRoyalties = async ( +export const handlePaymentWithRoyalties = ( connection: Connection, wallet: Wallet, - name: string, params: { + paymentManagerId: PublicKey; paymentAmount: BN; payerTokenAccount: PublicKey; feeCollectorTokenAccount: PublicKey; @@ -107,7 +102,7 @@ export const handlePaymentWithRoyalties = async ( mintMetadata: PublicKey; royaltiesRemainingAccounts: AccountMeta[]; } -): Promise => { +): TransactionInstruction => { const provider = new AnchorProvider(connection, wallet, {}); const paymentManagerProgram = new Program( @@ -116,12 +111,11 @@ export const handlePaymentWithRoyalties = async ( provider ); - const [paymentManagerId] = await findPaymentManagerAddress(name); return paymentManagerProgram.instruction.handlePaymentWithRoyalties( params.paymentAmount, { accounts: { - paymentManager: paymentManagerId, + paymentManager: params.paymentManagerId, payerTokenAccount: params.payerTokenAccount, feeCollectorTokenAccount: params.feeCollectorTokenAccount, paymentTokenAccount: params.paymentTokenAccount, @@ -136,12 +130,12 @@ export const handlePaymentWithRoyalties = async ( ); }; -export const close = async ( +export const close = ( connection: Connection, wallet: Wallet, - name: string, + paymentManagerId: PublicKey, collector?: PublicKey -): Promise => { +): TransactionInstruction => { const provider = new AnchorProvider(connection, wallet, {}); const paymentManagerProgram = new Program( @@ -150,7 +144,6 @@ export const close = async ( provider ); - const [paymentManagerId] = await findPaymentManagerAddress(name); return paymentManagerProgram.instruction.close({ accounts: { paymentManager: paymentManagerId, @@ -160,18 +153,18 @@ export const close = async ( }); }; -export const update = async ( +export const update = ( connection: Connection, wallet: Wallet, - name: string, params: { + paymentManagerId: PublicKey; authority: PublicKey; feeCollector: PublicKey; makerFeeBasisPoints: number; takerFeeBasisPoints: number; royaltyFeeShare?: BN; } -): Promise<[TransactionInstruction, PublicKey]> => { +): TransactionInstruction => { const provider = new AnchorProvider(connection, wallet, {}); const paymentManagerProgram = new Program( @@ -180,25 +173,20 @@ export const update = async ( provider ); - const [paymentManagerId] = await findPaymentManagerAddress(name); - - return [ - paymentManagerProgram.instruction.update( - { - authority: params.authority, - feeCollector: params.feeCollector, - makerFeeBasisPoints: params.makerFeeBasisPoints, - takerFeeBasisPoints: params.takerFeeBasisPoints, - royaltyFeeShare: params.royaltyFeeShare ?? null, + return paymentManagerProgram.instruction.update( + { + authority: params.authority, + feeCollector: params.feeCollector, + makerFeeBasisPoints: params.makerFeeBasisPoints, + takerFeeBasisPoints: params.takerFeeBasisPoints, + royaltyFeeShare: params.royaltyFeeShare ?? null, + }, + { + accounts: { + paymentManager: params.paymentManagerId, + payer: wallet.publicKey, + systemProgram: SystemProgram.programId, }, - { - accounts: { - paymentManager: paymentManagerId, - payer: wallet.publicKey, - systemProgram: SystemProgram.programId, - }, - } - ), - paymentManagerId, - ]; + } + ); }; diff --git a/sdk/transaction.ts b/sdk/transaction.ts index e69de29..eb6224c 100644 --- a/sdk/transaction.ts +++ b/sdk/transaction.ts @@ -0,0 +1,155 @@ +import { tryGetAccount } from "@cardinal/common"; +import { Wallet } from "@saberhq/solana-contrib"; +import { Connection, PublicKey, Transaction } from "@solana/web3.js"; +import BN from "bn.js"; +import { getPaymentManager } from "./accounts"; +import { + close, + handlePaymentWithRoyalties, + init, + managePayment, + update, +} from "./instruction"; +import { findPaymentManagerAddress } from "./pda"; +import { withRemainingAccountsForHandlePaymentWithRoyalties } from "./utils"; + +export const withInit = async ( + transaction: Transaction, + connection: Connection, + wallet: Wallet, + name: string, + feeCollectorId: PublicKey, + makerFeeBasisPoints: number, + takerFeeBasisPoints: number, + includeSellerFeeBasisPoints: boolean, + royaltyFeeShare: BN, + authority = wallet.publicKey +): Promise<[Transaction, PublicKey]> => { + const [paymentManagerId] = await findPaymentManagerAddress(name); + transaction.add( + init(connection, wallet, name, { + paymentManagerId: paymentManagerId, + feeCollector: feeCollectorId, + authority: wallet.publicKey, + makerFeeBasisPoints: makerFeeBasisPoints, + takerFeeBasisPoints: takerFeeBasisPoints, + includeSellerFeeBasisPoints: includeSellerFeeBasisPoints, + royaltyFeeShare: royaltyFeeShare, + payer: authority, + }) + ); + return [transaction, paymentManagerId]; +}; + +export const withManagePayment = async ( + transaction: Transaction, + connection: Connection, + wallet: Wallet, + name: string, + paymentAmount: BN, + payerTokenAccountId: PublicKey, + feeCollectorTokenAccountId: PublicKey, + paymentTokenAccountId: PublicKey +): Promise => { + const [paymentManagerId] = await findPaymentManagerAddress(name); + + transaction.add( + managePayment(connection, wallet, { + paymentManagerId: paymentManagerId, + paymentAmount: paymentAmount, + payerTokenAccount: payerTokenAccountId, + feeCollectorTokenAccount: feeCollectorTokenAccountId, + paymentTokenAccount: paymentTokenAccountId, + }) + ); + return transaction; +}; + +export const withHandlePaymentWithRoyalties = async ( + transaction: Transaction, + connection: Connection, + wallet: Wallet, + name: string, + paymentAmount: BN, + mintId: PublicKey, + mintMetadataId: PublicKey, + paymentMintId: PublicKey, + payerTokenAccountId: PublicKey, + feeCollectorTokenAccountId: PublicKey, + paymentTokenAccountId: PublicKey, + excludeCretors = [] +): Promise => { + const [paymentManagerId] = await findPaymentManagerAddress(name); + + const remainingAccounts = + await withRemainingAccountsForHandlePaymentWithRoyalties( + new Transaction(), + connection, + wallet, + mintId, + paymentMintId, + excludeCretors + ); + + transaction.add( + handlePaymentWithRoyalties(connection, wallet, { + paymentManagerId: paymentManagerId, + paymentAmount: paymentAmount, + payerTokenAccount: payerTokenAccountId, + feeCollectorTokenAccount: feeCollectorTokenAccountId, + paymentTokenAccount: paymentTokenAccountId, + paymentMint: paymentMintId, + mint: mintId, + mintMetadata: mintMetadataId, + royaltiesRemainingAccounts: remainingAccounts, + }) + ); + return transaction; +}; + +export const withClose = async ( + transaction: Transaction, + connection: Connection, + wallet: Wallet, + name: string, + collectorId = wallet.publicKey +): Promise => { + const [paymentManagerId] = await findPaymentManagerAddress(name); + + transaction.add(close(connection, wallet, paymentManagerId, collectorId)); + return transaction; +}; + +export const withUpdate = async ( + transaction: Transaction, + connection: Connection, + wallet: Wallet, + name: string, + feeCollectorId?: PublicKey, + makerFeeBasisPoints?: number, + takerFeeBasisPoints?: number, + royaltyFeeShare?: BN +): Promise => { + const [paymentManagerId] = await findPaymentManagerAddress(name); + const checkPaymentManager = await tryGetAccount(() => + getPaymentManager(connection, paymentManagerId) + ); + if (!checkPaymentManager) { + throw `No payment manager found with name ${name}`; + } + + transaction.add( + update(connection, wallet, { + paymentManagerId: paymentManagerId, + feeCollector: checkPaymentManager.parsed.feeCollector ?? feeCollectorId, + authority: checkPaymentManager.parsed.authority ?? wallet.publicKey, + makerFeeBasisPoints: + checkPaymentManager.parsed.makerFeeBasisPoints ?? makerFeeBasisPoints, + takerFeeBasisPoints: + checkPaymentManager.parsed.takerFeeBasisPoints ?? takerFeeBasisPoints, + royaltyFeeShare: + checkPaymentManager.parsed.royaltyFeeShare ?? royaltyFeeShare, + }) + ); + return transaction; +}; diff --git a/tests/handlePaymetWithRoyalties.spec.ts b/tests/handlePaymetWithRoyalties.spec.ts index daec824..219f93c 100644 --- a/tests/handlePaymetWithRoyalties.spec.ts +++ b/tests/handlePaymetWithRoyalties.spec.ts @@ -20,7 +20,6 @@ import type { AccountMeta } from "@solana/web3.js"; import { Keypair, LAMPORTS_PER_SOL, Transaction } from "@solana/web3.js"; import { expect } from "chai"; import { createMint, withRemainingAccountsForPayment } from "../sdk/utils"; -import { handlePaymentWithRoyalties, init } from "../sdk/instruction"; import { getProvider } from "./workspace"; import { findPaymentManagerAddress } from "../sdk/pda"; @@ -29,10 +28,12 @@ import { findAta, withFindOrInitAssociatedTokenAccount, } from "@cardinal/common"; +import { withHandlePaymentWithRoyalties, withInit } from "../sdk/transaction"; describe("Handle payment with royalties", () => { const MAKER_FEE = new BN(500); const TAKER_FEE = new BN(300); + const ROYALTEE_FEE_SHARE = new BN(5000); const BASIS_POINTS_DIVISOR = new BN(10000); const FEE_SPLIT = new BN(50); const paymentAmount = new BN(1000); @@ -148,19 +149,18 @@ describe("Handle payment with royalties", () => { const provider = getProvider(); const transaction = new web3.Transaction(); - const [ix] = await init( + await withInit( + transaction, provider.connection, provider.wallet, paymentManagerName, - { - feeCollector: feeCollector.publicKey, - makerFeeBasisPoints: MAKER_FEE.toNumber(), - takerFeeBasisPoints: TAKER_FEE.toNumber(), - includeSellerFeeBasisPoints: false, - } + feeCollector.publicKey, + MAKER_FEE.toNumber(), + TAKER_FEE.toNumber(), + false, + ROYALTEE_FEE_SHARE ); - transaction.add(ix); const txEnvelope = new TransactionEnvelope( SolanaProvider.init({ connection: provider.connection, @@ -304,22 +304,18 @@ describe("Handle payment with royalties", () => { ).to.be.rejectedWith(Error); }); - transaction.add( - await handlePaymentWithRoyalties( - provider.connection, - provider.wallet, - paymentManagerName, - { - paymentAmount: new BN(paymentAmount), - payerTokenAccount: payerTokenAccountId, - feeCollectorTokenAccount: feeCollectorTokenAccount, - paymentTokenAccount: paymentTokenAccountId, - paymentMint: paymentMint.publicKey, - mint: rentalMint.publicKey, - mintMetadata: metadataId, - royaltiesRemainingAccounts: royaltiesRemainingAccounts, - } - ) + await withHandlePaymentWithRoyalties( + transaction, + provider.connection, + provider.wallet, + paymentManagerName, + new BN(paymentAmount), + rentalMint.publicKey, + metadataId, + paymentMint.publicKey, + payerTokenAccountId, + feeCollectorTokenAccount, + paymentTokenAccountId ); const txEnvelope = new TransactionEnvelope( diff --git a/tests/handlePaymetWithRoyaltiesNoMetadata.spec.ts b/tests/handlePaymetWithRoyaltiesNoMetadata.spec.ts index 75899c9..91547b5 100644 --- a/tests/handlePaymetWithRoyaltiesNoMetadata.spec.ts +++ b/tests/handlePaymetWithRoyaltiesNoMetadata.spec.ts @@ -11,8 +11,8 @@ import * as splToken from "@solana/spl-token"; import { Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js"; import { expect } from "chai"; import { getPaymentManager } from "../sdk/accounts"; -import { handlePaymentWithRoyalties, init } from "../sdk/instruction"; import { findPaymentManagerAddress } from "../sdk/pda"; +import { withHandlePaymentWithRoyalties, withInit } from "../sdk/transaction"; import { createMint, withRemainingAccountsForPayment } from "../sdk/utils"; import { getProvider } from "./workspace"; @@ -20,6 +20,7 @@ import { getProvider } from "./workspace"; describe("Handle payment with royalties with no metadata", () => { const MAKER_FEE = new BN(500); const TAKER_FEE = new BN(300); + const ROYALTEE_FEE_SHARE = new BN(5000); const BASIS_POINTS_DIVISOR = new BN(10000); const paymentAmount = new BN(1000); const RECIPIENT_START_PAYMENT_AMOUNT = new BN(10000000000); @@ -61,19 +62,18 @@ describe("Handle payment with royalties with no metadata", () => { const provider = getProvider(); const transaction = new web3.Transaction(); - const [ix] = await init( + await withInit( + transaction, provider.connection, provider.wallet, paymentManagerName, - { - feeCollector: feeCollector.publicKey, - makerFeeBasisPoints: MAKER_FEE.toNumber(), - takerFeeBasisPoints: TAKER_FEE.toNumber(), - includeSellerFeeBasisPoints: false, - } + feeCollector.publicKey, + MAKER_FEE.toNumber(), + TAKER_FEE.toNumber(), + false, + ROYALTEE_FEE_SHARE ); - transaction.add(ix); const txEnvelope = new TransactionEnvelope( SolanaProvider.init({ connection: provider.connection, @@ -141,22 +141,19 @@ describe("Handle payment with royalties with no metadata", () => { null ); - transaction.add( - await handlePaymentWithRoyalties( - provider.connection, - provider.wallet, - paymentManagerName, - { - paymentAmount: new BN(paymentAmount), - payerTokenAccount: payerTokenAccountId, - feeCollectorTokenAccount: feeCollectorTokenAccountId, - paymentTokenAccount: paymentTokenAccountId, - paymentMint: paymentMint.publicKey, - mint: rentalMint.publicKey, - mintMetadata: metadataId, - royaltiesRemainingAccounts: [], - } - ) + await withHandlePaymentWithRoyalties( + transaction, + provider.connection, + provider.wallet, + paymentManagerName, + new BN(paymentAmount), + rentalMint.publicKey, + metadataId, + paymentMint.publicKey, + payerTokenAccountId, + feeCollectorTokenAccountId, + paymentTokenAccountId, + [] ); const txEnvelope = new TransactionEnvelope( diff --git a/tests/handlePaymetWithRoyaltiesWithSellerFee.spec.ts b/tests/handlePaymetWithRoyaltiesWithSellerFee.spec.ts index f5d9c11..1673633 100644 --- a/tests/handlePaymetWithRoyaltiesWithSellerFee.spec.ts +++ b/tests/handlePaymetWithRoyaltiesWithSellerFee.spec.ts @@ -19,16 +19,11 @@ import { } from "@saberhq/solana-contrib"; import type { Token } from "@solana/spl-token"; import * as splToken from "@solana/spl-token"; -import { - AccountMeta, - Keypair, - LAMPORTS_PER_SOL, - Transaction, -} from "@solana/web3.js"; +import { AccountMeta, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js"; import { expect } from "chai"; import { getPaymentManager } from "../sdk/accounts"; -import { handlePaymentWithRoyalties, init } from "../sdk/instruction"; import { findPaymentManagerAddress } from "../sdk/pda"; +import { withHandlePaymentWithRoyalties, withInit } from "../sdk/transaction"; import { createMint, withRemainingAccountsForPayment } from "../sdk/utils"; import { getProvider } from "./workspace"; @@ -36,9 +31,9 @@ import { getProvider } from "./workspace"; describe("Handle payment with royalties with seller fee", () => { const MAKER_FEE = new BN(500); const TAKER_FEE = new BN(300); + const ROYALTEE_FEE_SHARE = new BN(4500); const BASIS_POINTS_DIVISOR = new BN(10000); const paymentAmount = new BN(1000); - const royaltyFeeShare = new BN(4500); const sellerFeeBasisPoints = 100; const RECIPIENT_START_PAYMENT_AMOUNT = new BN(10000000000); const paymentManagerName = Math.random().toString(36).slice(2, 7); @@ -154,20 +149,18 @@ describe("Handle payment with royalties with seller fee", () => { const provider = getProvider(); const transaction = new web3.Transaction(); - const [ix] = await init( + await withInit( + transaction, provider.connection, provider.wallet, paymentManagerName, - { - feeCollector: feeCollector.publicKey, - makerFeeBasisPoints: MAKER_FEE.toNumber(), - takerFeeBasisPoints: TAKER_FEE.toNumber(), - includeSellerFeeBasisPoints: true, - royaltyFeeShare: royaltyFeeShare, - } + feeCollector.publicKey, + MAKER_FEE.toNumber(), + TAKER_FEE.toNumber(), + true, + ROYALTEE_FEE_SHARE ); - transaction.add(ix); const txEnvelope = new TransactionEnvelope( SolanaProvider.init({ connection: provider.connection, @@ -197,7 +190,7 @@ describe("Handle payment with royalties with seller fee", () => { ); expect(paymentManagerData.parsed.includeSellerFeeBasisPoints).to.be.true; expect(paymentManagerData.parsed.royaltyFeeShare?.toNumber()).to.eq( - royaltyFeeShare.toNumber() + ROYALTEE_FEE_SHARE.toNumber() ); }); @@ -223,12 +216,9 @@ describe("Handle payment with royalties with seller fee", () => { const royaltiesRemainingAccounts: AccountMeta[] = []; /// - const creator1MintTokenAccount = await withFindOrInitAssociatedTokenAccount( - new Transaction(), - provider.connection, + const creator1MintTokenAccount = await findAta( paymentMint.publicKey, creator1.publicKey, - provider.wallet.publicKey, true ); royaltiesRemainingAccounts.push({ @@ -237,12 +227,9 @@ describe("Handle payment with royalties with seller fee", () => { isWritable: true, }); - const creator2MintTokenAccount = await withFindOrInitAssociatedTokenAccount( - new Transaction(), - provider.connection, + const creator2MintTokenAccount = await findAta( paymentMint.publicKey, creator2.publicKey, - provider.wallet.publicKey, true ); royaltiesRemainingAccounts.push({ @@ -251,12 +238,9 @@ describe("Handle payment with royalties with seller fee", () => { isWritable: true, }); - const creator3MintTokenAccount = await withFindOrInitAssociatedTokenAccount( - new Transaction(), - provider.connection, + const creator3MintTokenAccount = await findAta( paymentMint.publicKey, creator3.publicKey, - provider.wallet.publicKey, true ); royaltiesRemainingAccounts.push({ @@ -332,22 +316,18 @@ describe("Handle payment with royalties with seller fee", () => { // pass } - transaction.add( - await handlePaymentWithRoyalties( - provider.connection, - provider.wallet, - paymentManagerName, - { - paymentAmount: new BN(paymentAmount), - payerTokenAccount: payerTokenAccountId, - feeCollectorTokenAccount: feeCollectorTokenAccount, - paymentTokenAccount: paymentTokenAccountId, - paymentMint: paymentMint.publicKey, - mint: rentalMint.publicKey, - mintMetadata: metadataId, - royaltiesRemainingAccounts: royaltiesRemainingAccounts, - } - ) + await withHandlePaymentWithRoyalties( + transaction, + provider.connection, + provider.wallet, + paymentManagerName, + new BN(paymentAmount), + rentalMint.publicKey, + metadataId, + paymentMint.publicKey, + payerTokenAccountId, + feeCollectorTokenAccount, + paymentTokenAccountId ); const txEnvelope = new TransactionEnvelope( @@ -374,7 +354,7 @@ describe("Handle payment with royalties with seller fee", () => { .mul(new BN(sellerFeeBasisPoints)) .div(BASIS_POINTS_DIVISOR); const totalCreatorsFee = totalFees - .mul(royaltyFeeShare) + .mul(ROYALTEE_FEE_SHARE) .div(BASIS_POINTS_DIVISOR) .add(sellerFee); totalFees = totalFees.add(sellerFee); diff --git a/tests/paymentManagerInitAgainClose.spec.ts b/tests/paymentManagerInitAgainClose.spec.ts index c59c3dd..4de5e2f 100644 --- a/tests/paymentManagerInitAgainClose.spec.ts +++ b/tests/paymentManagerInitAgainClose.spec.ts @@ -2,11 +2,12 @@ import { tryGetAccount } from "@cardinal/common"; import { web3 } from "@project-serum/anchor"; import { expectTXTable } from "@saberhq/chai-solana"; import { SolanaProvider, TransactionEnvelope } from "@saberhq/solana-contrib"; -import { Keypair } from "@solana/web3.js"; +import { Keypair, Transaction } from "@solana/web3.js"; +import { BN } from "bn.js"; import { expect } from "chai"; import { getPaymentManager } from "../sdk/accounts"; -import { close, init } from "../sdk/instruction"; import { findPaymentManagerAddress } from "../sdk/pda"; +import { withClose, withInit } from "../sdk/transaction"; import { getProvider } from "./workspace"; @@ -20,19 +21,18 @@ describe("Init again and close payment manager", () => { const provider = getProvider(); const transaction = new web3.Transaction(); - const [ix] = await init( + await withInit( + transaction, provider.connection, provider.wallet, paymentManagerName, - { - feeCollector: feeCollector.publicKey, - makerFeeBasisPoints: MAKER_FEE, - takerFeeBasisPoints: TAKER_FEE, - includeSellerFeeBasisPoints: false, - } + feeCollector.publicKey, + MAKER_FEE, + TAKER_FEE, + false, + new BN(0) ); - transaction.add(ix); const txEnvelope = new TransactionEnvelope( SolanaProvider.init({ connection: provider.connection, @@ -58,25 +58,27 @@ describe("Init again and close payment manager", () => { expect(paymentManagerData.parsed.takerFeeBasisPoints).to.eq(TAKER_FEE); }); - it("Init again fails", () => { + it("Init again fails", async () => { const provider = getProvider(); + + const transaction = new Transaction(); + await withInit( + transaction, + provider.connection, + provider.wallet, + paymentManagerName, + feeCollector.publicKey, + MAKER_FEE, + TAKER_FEE, + false, + new BN(0) + ); expect(async () => { await expectTXTable( - new TransactionEnvelope(SolanaProvider.init(provider), [ - ( - await init( - provider.connection, - provider.wallet, - paymentManagerName, - { - feeCollector: feeCollector.publicKey, - makerFeeBasisPoints: MAKER_FEE, - takerFeeBasisPoints: TAKER_FEE, - includeSellerFeeBasisPoints: false, - } - ) - )[0], - ]), + new TransactionEnvelope( + SolanaProvider.init(provider), + transaction.instructions + ), "Fail to init again", { verbosity: "error" } ).to.be.rejectedWith(Error); @@ -88,15 +90,20 @@ describe("Init again and close payment manager", () => { const balanceBefore = await provider.connection.getBalance( provider.wallet.publicKey ); + const transaction = new Transaction(); + await withClose( + transaction, + provider.connection, + provider.wallet, + paymentManagerName, + provider.wallet.publicKey + ); + await expectTXTable( - new TransactionEnvelope(SolanaProvider.init(provider), [ - await close( - provider.connection, - provider.wallet, - paymentManagerName, - provider.wallet.publicKey - ), - ]), + new TransactionEnvelope( + SolanaProvider.init(provider), + transaction.instructions + ), "Close payment manager", { verbosity: "error", diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 744d4d7..dfc56c9 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -4,5 +4,5 @@ "module": "CommonJS", "outDir": "dist/cjs/" }, - "include": ["src/"] + "include": ["sdk/"] } diff --git a/tsconfig.esm.json b/tsconfig.esm.json index 56932a9..9debc5d 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -6,5 +6,5 @@ "noEmit": false, "outDir": "dist/esm/" }, - "include": ["src/"] + "include": ["sdk/"] } diff --git a/tsconfig.json b/tsconfig.json index e23eb0c..425d95b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -35,5 +35,5 @@ "declarationMap": true, "inlineSources": true }, - "include": ["src/", "tests/"] + "include": ["sdk/", "tests/"] } diff --git a/yarn.lock b/yarn.lock index 341350c..ecfc9d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -872,6 +872,11 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" +jsonc-parser@^3.0.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.2.0.tgz#31ff3f4c2b9793f89c67212627c51c6394f88e76" + integrity sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w== + jsonparse@^1.2.0: version "1.3.1" resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280" @@ -914,11 +919,21 @@ lower-case@^2.0.2: dependencies: tslib "^2.0.3" +lunr@^2.3.9: + version "2.3.9" + resolved "https://registry.yarnpkg.com/lunr/-/lunr-2.3.9.tgz#18b123142832337dd6e964df1a5a7707b25d35e1" + integrity sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow== + make-error@^1.1.1: version "1.3.6" resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== +marked@^4.0.19: + version "4.1.1" + resolved "https://registry.yarnpkg.com/marked/-/marked-4.1.1.tgz#2f709a4462abf65a283f2453dc1c42ab177d302e" + integrity sha512-0cNMnTcUJPxbA6uWmCmjWz4NJRe/0Xfk2NhXCUHjew9qJzFN20krFnsUe7QynwqOwa5m1fZ4UDg0ycKFVC0ccw== + minimatch@5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.0.1.tgz#fb9022f7528125187c92bd9e9b6366be1cf3415b" @@ -933,6 +948,13 @@ minimatch@^3.0.4: dependencies: brace-expansion "^1.1.7" +minimatch@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.0.tgz#1717b464f4971b144f6aabe8f2d0b8e4511e09c7" + integrity sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg== + dependencies: + brace-expansion "^2.0.1" + minimist@^1.2.0, minimist@^1.2.6: version "1.2.7" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.7.tgz#daa1c4d91f507390437c6a8bc01078e7000c4d18" @@ -1132,6 +1154,15 @@ serialize-javascript@6.0.0: dependencies: randombytes "^2.1.0" +shiki@^0.11.1: + version "0.11.1" + resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.11.1.tgz#df0f719e7ab592c484d8b73ec10e215a503ab8cc" + integrity sha512-EugY9VASFuDqOexOgXR18ZV+TbFrQHeCpEYaXamO+SZlsnT/2LxuLBX25GGtIrwaEVFXUAbUQ601SWE2rMwWHA== + dependencies: + jsonc-parser "^3.0.0" + vscode-oniguruma "^1.6.1" + vscode-textmate "^6.0.0" + snake-case@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/snake-case/-/snake-case-3.0.4.tgz#4f2bbd568e9935abdfd593f34c691dadb49c452c" @@ -1278,6 +1309,16 @@ type-detect@^4.0.0, type-detect@^4.0.5: resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g== +typedoc@^0.23.17: + version "0.23.17" + resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.23.17.tgz#ca644e7ecac78be85cfd556f5436917a4ac76dda" + integrity sha512-3rtNubo1dK0pvs6ixpMAq4pESULd5/JNUqJbdyZoeilI14reb1RNVomN4fMgIadd0RMX1aenYjJSSMBOJ+/+0Q== + dependencies: + lunr "^2.3.9" + marked "^4.0.19" + minimatch "^5.1.0" + shiki "^0.11.1" + typescript@^4.8.4: version "4.8.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6" @@ -1295,6 +1336,16 @@ uuid@^8.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== +vscode-oniguruma@^1.6.1: + version "1.6.2" + resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.6.2.tgz#aeb9771a2f1dbfc9083c8a7fdd9cccaa3f386607" + integrity sha512-KH8+KKov5eS/9WhofZR8M8dMHWN2gTxjMsG4jd04YhpbPR91fUj7rYQ2/XjeHCJWbg7X++ApRIU9NUwM2vTvLA== + +vscode-textmate@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/vscode-textmate/-/vscode-textmate-6.0.0.tgz#a3777197235036814ac9a92451492f2748589210" + integrity sha512-gu73tuZfJgu+mvCSy4UZwd2JXykjK9zAZsfmDeut5dx/1a7FeTk0XwJsSuqQn+cuMCGVbIBfl+s53X4T19DnzQ== + webidl-conversions@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"