From 00327ad056f0dac20dac146203cfc9cd1149c53d Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 23 Jan 2024 15:50:38 +0300 Subject: [PATCH 01/34] Bump api --- idea/frontend/package.json | 6 +- utils/gear-hooks/package.json | 14 +- yarn.lock | 244 ++++------------------------------ 3 files changed, 34 insertions(+), 230 deletions(-) diff --git a/idea/frontend/package.json b/idea/frontend/package.json index 071f90796c..23bb7b7157 100644 --- a/idea/frontend/package.json +++ b/idea/frontend/package.json @@ -9,15 +9,15 @@ "preview": "vite preview" }, "dependencies": { - "@gear-js/api": "0.35.2", + "@gear-js/api": "0.36.1", "@gear-js/react-hooks": "workspace:^", "@gear-js/ui": "workspace:^", "@hcaptcha/react-hcaptcha": "1.8.1", "@hookform/resolvers": "3.3.2", - "@polkadot/api": "10.10.1", + "@polkadot/api": "10.11.2", "@polkadot/extension-dapp": "0.46.5", "@polkadot/react-identicon": "3.6.3", - "@polkadot/types": "10.10.1", + "@polkadot/types": "10.11.2", "@polkadot/util": "12.5.1", "@react-aria/utils": "3.21.0", "bignumber.js": "9.1.2", diff --git a/utils/gear-hooks/package.json b/utils/gear-hooks/package.json index ec62559823..fce1e9b92c 100644 --- a/utils/gear-hooks/package.json +++ b/utils/gear-hooks/package.json @@ -1,6 +1,6 @@ { "name": "@gear-js/react-hooks", - "version": "0.9.7", + "version": "0.10.0", "description": "React hooks used across Gear applications", "author": "Gear Technologies", "license": "GPL-3.0", @@ -21,14 +21,14 @@ "prepare": "yarn build" }, "peerDependencies": { - "@gear-js/api": "0.35.2", - "@polkadot/api": "10.10.1", + "@gear-js/api": "0.36.1", + "@polkadot/api": "10.11.2", "@polkadot/extension-dapp": "0.46.5", "react": "18.2.0", "react-dom": "18.2.0" }, "dependencies": { - "@polkadot/api-derive": "10.10.1", + "@polkadot/api-derive": "10.11.2", "@polkadot/extension-inject": "0.46.5", "@polkadot/util": "12.5.1", "@substrate/connect": "0.7.33", @@ -37,10 +37,10 @@ "react-transition-group": "4.4.5" }, "devDependencies": { - "@gear-js/api": "0.35.2", - "@polkadot/api": "10.10.1", + "@gear-js/api": "0.36.1", + "@polkadot/api": "10.11.2", "@polkadot/extension-dapp": "0.46.5", - "@polkadot/types": "10.10.1", + "@polkadot/types": "10.11.2", "@rollup/plugin-commonjs": "25.0.5", "@rollup/plugin-node-resolve": "15.2.3", "@types/react": "18.2.28", diff --git a/yarn.lock b/yarn.lock index c7dd8533d5..f371bad611 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2313,25 +2313,25 @@ __metadata: languageName: unknown linkType: soft -"@gear-js/api@npm:0.35.2": - version: 0.35.2 - resolution: "@gear-js/api@npm:0.35.2" +"@gear-js/api@npm:0.36.0": + version: 0.36.0 + resolution: "@gear-js/api@npm:0.36.0" peerDependencies: - "@polkadot/api": 10.10.1 - "@polkadot/wasm-crypto": 7.2.2 + "@polkadot/api": 10.11.2 + "@polkadot/wasm-crypto": 7.3.2 rxjs: 7.8.1 - checksum: de59f1dbd4a4cdd8fc5adf28d21b7b21b7d6d0de74c4bfa18435516b38e137c46fc78fb4db3a394c3b45d68426383e5aee0c8e3bba4c15d690c87d0ff8013514 + checksum: 204f6314d995df88e5b964ecbd4410bec3773482d625a020d8ca2511867c856e0b7c2fa9b7664c6bcc98b39aa619caec3263c96414988b773150e52e11fe924a languageName: node linkType: hard -"@gear-js/api@npm:0.36.0": - version: 0.36.0 - resolution: "@gear-js/api@npm:0.36.0" +"@gear-js/api@npm:0.36.1": + version: 0.36.1 + resolution: "@gear-js/api@npm:0.36.1" peerDependencies: "@polkadot/api": 10.11.2 "@polkadot/wasm-crypto": 7.3.2 rxjs: 7.8.1 - checksum: 204f6314d995df88e5b964ecbd4410bec3773482d625a020d8ca2511867c856e0b7c2fa9b7664c6bcc98b39aa619caec3263c96414988b773150e52e11fe924a + checksum: c1a28dea074ae3883ba4e05a1130286e59cba6f1677a1f39a2b9d10fe86eee41ca339338f4ec772c7dcb21ad86cf9dfbc235bf8e04fb1f13cb72c9767f12b316 languageName: node linkType: hard @@ -2350,15 +2350,15 @@ __metadata: version: 0.0.0-use.local resolution: "@gear-js/frontend@workspace:idea/frontend" dependencies: - "@gear-js/api": 0.35.2 + "@gear-js/api": 0.36.1 "@gear-js/react-hooks": "workspace:^" "@gear-js/ui": "workspace:^" "@hcaptcha/react-hcaptcha": 1.8.1 "@hookform/resolvers": 3.3.2 - "@polkadot/api": 10.10.1 + "@polkadot/api": 10.11.2 "@polkadot/extension-dapp": 0.46.5 "@polkadot/react-identicon": 3.6.3 - "@polkadot/types": 10.10.1 + "@polkadot/types": 10.11.2 "@polkadot/util": 12.5.1 "@react-aria/utils": 3.21.0 "@types/lodash.isequal": 4.5.6 @@ -2451,12 +2451,12 @@ __metadata: version: 0.0.0-use.local resolution: "@gear-js/react-hooks@workspace:utils/gear-hooks" dependencies: - "@gear-js/api": 0.35.2 - "@polkadot/api": 10.10.1 - "@polkadot/api-derive": 10.10.1 + "@gear-js/api": 0.36.1 + "@polkadot/api": 10.11.2 + "@polkadot/api-derive": 10.11.2 "@polkadot/extension-dapp": 0.46.5 "@polkadot/extension-inject": 0.46.5 - "@polkadot/types": 10.10.1 + "@polkadot/types": 10.11.2 "@polkadot/util": 12.5.1 "@rollup/plugin-commonjs": 25.0.5 "@rollup/plugin-node-resolve": 15.2.3 @@ -2475,8 +2475,8 @@ __metadata: rollup-plugin-typescript2: 0.36.0 typescript: 5.2.2 peerDependencies: - "@gear-js/api": 0.35.2 - "@polkadot/api": 10.10.1 + "@gear-js/api": 0.36.1 + "@polkadot/api": 10.11.2 "@polkadot/extension-dapp": 0.46.5 react: 18.2.0 react-dom: 18.2.0 @@ -3101,21 +3101,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/api-augment@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/api-augment@npm:10.10.1" - dependencies: - "@polkadot/api-base": 10.10.1 - "@polkadot/rpc-augment": 10.10.1 - "@polkadot/types": 10.10.1 - "@polkadot/types-augment": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/util": ^12.5.1 - tslib: ^2.6.2 - checksum: 16ca2d71215019faba506b6dc455ef15ea1eec8b97bd146aef49a04ae15dc9246405540219bfbea36027ee50c5dbb15885296c30ee98908afdd7a56626efd06c - languageName: node - linkType: hard - "@polkadot/api-augment@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/api-augment@npm:10.11.2" @@ -3131,19 +3116,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/api-base@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/api-base@npm:10.10.1" - dependencies: - "@polkadot/rpc-core": 10.10.1 - "@polkadot/types": 10.10.1 - "@polkadot/util": ^12.5.1 - rxjs: ^7.8.1 - tslib: ^2.6.2 - checksum: 374a4378484817b29c52908a9dac649b4d4f231db21a0b0b3d3ec3331c7b9b9c374c103b5e64d028c212b8ab3eb98244cd760d20e2fe5f46a8fdc1d923555047 - languageName: node - linkType: hard - "@polkadot/api-base@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/api-base@npm:10.11.2" @@ -3157,24 +3129,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/api-derive@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/api-derive@npm:10.10.1" - dependencies: - "@polkadot/api": 10.10.1 - "@polkadot/api-augment": 10.10.1 - "@polkadot/api-base": 10.10.1 - "@polkadot/rpc-core": 10.10.1 - "@polkadot/types": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/util": ^12.5.1 - "@polkadot/util-crypto": ^12.5.1 - rxjs: ^7.8.1 - tslib: ^2.6.2 - checksum: ff0f016d39aa73f55a881927e6ae3dd75c2a81fe4095cdda5e558f420d815a12c204e6a2082acbef2c74867b810d41ef349de2b7924d46957be7260b71fb1512 - languageName: node - linkType: hard - "@polkadot/api-derive@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/api-derive@npm:10.11.2" @@ -3193,31 +3147,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/api@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/api@npm:10.10.1" - dependencies: - "@polkadot/api-augment": 10.10.1 - "@polkadot/api-base": 10.10.1 - "@polkadot/api-derive": 10.10.1 - "@polkadot/keyring": ^12.5.1 - "@polkadot/rpc-augment": 10.10.1 - "@polkadot/rpc-core": 10.10.1 - "@polkadot/rpc-provider": 10.10.1 - "@polkadot/types": 10.10.1 - "@polkadot/types-augment": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/types-create": 10.10.1 - "@polkadot/types-known": 10.10.1 - "@polkadot/util": ^12.5.1 - "@polkadot/util-crypto": ^12.5.1 - eventemitter3: ^5.0.1 - rxjs: ^7.8.1 - tslib: ^2.6.2 - checksum: de1aa727b63fb921854840fe2d18b55b99f512f4d3e08d3b895fceea7891f6dd0febe6aa5fb7b1778494c78d6a6a7ebd17d426ba2e3df459aa974b7bb8fee19c - languageName: node - linkType: hard - "@polkadot/api@npm:10.11.2, @polkadot/api@npm:^10.9.1": version: 10.11.2 resolution: "@polkadot/api@npm:10.11.2" @@ -3327,19 +3256,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/rpc-augment@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/rpc-augment@npm:10.10.1" - dependencies: - "@polkadot/rpc-core": 10.10.1 - "@polkadot/types": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/util": ^12.5.1 - tslib: ^2.6.2 - checksum: d19ff447fea298387e8af9b7ac44c8eb8438b0e939608414c0ddc642fbd5c2d657d199a66741d9e330f28aa587486a163238cdf058cc69994178b121a0d26738 - languageName: node - linkType: hard - "@polkadot/rpc-augment@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/rpc-augment@npm:10.11.2" @@ -3353,20 +3269,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/rpc-core@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/rpc-core@npm:10.10.1" - dependencies: - "@polkadot/rpc-augment": 10.10.1 - "@polkadot/rpc-provider": 10.10.1 - "@polkadot/types": 10.10.1 - "@polkadot/util": ^12.5.1 - rxjs: ^7.8.1 - tslib: ^2.6.2 - checksum: 5ab21029fbafa13e50bb48161a82c023f7015b633e258b76c2cff25bf648d7f69baf18efc291ebec8dd635b38da8223e853e15b53478268b1f6b40d2ab0b3e09 - languageName: node - linkType: hard - "@polkadot/rpc-core@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/rpc-core@npm:10.11.2" @@ -3381,30 +3283,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/rpc-provider@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/rpc-provider@npm:10.10.1" - dependencies: - "@polkadot/keyring": ^12.5.1 - "@polkadot/types": 10.10.1 - "@polkadot/types-support": 10.10.1 - "@polkadot/util": ^12.5.1 - "@polkadot/util-crypto": ^12.5.1 - "@polkadot/x-fetch": ^12.5.1 - "@polkadot/x-global": ^12.5.1 - "@polkadot/x-ws": ^12.5.1 - "@substrate/connect": 0.7.33 - eventemitter3: ^5.0.1 - mock-socket: ^9.3.1 - nock: ^13.3.4 - tslib: ^2.6.2 - dependenciesMeta: - "@substrate/connect": - optional: true - checksum: 44147ad7ce4bb0fccf5272bbe56b3b65c1e907f02109c8e18a5b5da8c658f84c1d7741c5e6878adacd06514254b0a7fb8254d5a222f55f25f7a573b2ba970449 - languageName: node - linkType: hard - "@polkadot/rpc-provider@npm:10.11.2, @polkadot/rpc-provider@npm:^10.9.1": version: 10.11.2 resolution: "@polkadot/rpc-provider@npm:10.11.2" @@ -3429,18 +3307,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-augment@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types-augment@npm:10.10.1" - dependencies: - "@polkadot/types": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/util": ^12.5.1 - tslib: ^2.6.2 - checksum: 40440fc2a9568c9e636f478c4f191cbb38f07256f4db7f1bb9bdbcf0b928280315afee2843090a006a3dfd16e000f22dd6a9bd5687dd6400a1fc3dcd6ee59a25 - languageName: node - linkType: hard - "@polkadot/types-augment@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/types-augment@npm:10.11.2" @@ -3453,17 +3319,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-codec@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types-codec@npm:10.10.1" - dependencies: - "@polkadot/util": ^12.5.1 - "@polkadot/x-bigint": ^12.5.1 - tslib: ^2.6.2 - checksum: 17ceb561e6a82784febd5c8b0219050a9b8aeeb766ffbae8255ab586120063ca9fea1c89df776047e861aba87e43048a8060d5c469bcd42c169830a69416d62f - languageName: node - linkType: hard - "@polkadot/types-codec@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/types-codec@npm:10.11.2" @@ -3475,17 +3330,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-create@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types-create@npm:10.10.1" - dependencies: - "@polkadot/types-codec": 10.10.1 - "@polkadot/util": ^12.5.1 - tslib: ^2.6.2 - checksum: 1dedef441218a0786774033c2d587b8ccdf184a72da671c7da70ced9f765073bfec4a15d8937b00d5d50cb0eb1b4bd25886ace3f7e024c95b46f58a1c318efd1 - languageName: node - linkType: hard - "@polkadot/types-create@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/types-create@npm:10.11.2" @@ -3497,20 +3341,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-known@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types-known@npm:10.10.1" - dependencies: - "@polkadot/networks": ^12.5.1 - "@polkadot/types": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/types-create": 10.10.1 - "@polkadot/util": ^12.5.1 - tslib: ^2.6.2 - checksum: 25489967fcd6022f11a64c20884dd1ef49494f3b3e514034a08cc07f61267121adf8b96b307edc3381c445de58842d515aa8440ee888bc37120775deffae671a - languageName: node - linkType: hard - "@polkadot/types-known@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/types-known@npm:10.11.2" @@ -3525,16 +3355,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-support@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types-support@npm:10.10.1" - dependencies: - "@polkadot/util": ^12.5.1 - tslib: ^2.6.2 - checksum: 391857f39463fcc9bbc34a6bafd191e12eb3fd28f386d4094cc3cdcbcd0fcc8799c6e99a49b0e634c6a1b78d07188eb6e1e01299f55df2593c3f30fcb241631c - languageName: node - linkType: hard - "@polkadot/types-support@npm:10.11.2": version: 10.11.2 resolution: "@polkadot/types-support@npm:10.11.2" @@ -3545,22 +3365,6 @@ __metadata: languageName: node linkType: hard -"@polkadot/types@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types@npm:10.10.1" - dependencies: - "@polkadot/keyring": ^12.5.1 - "@polkadot/types-augment": 10.10.1 - "@polkadot/types-codec": 10.10.1 - "@polkadot/types-create": 10.10.1 - "@polkadot/util": ^12.5.1 - "@polkadot/util-crypto": ^12.5.1 - rxjs: ^7.8.1 - tslib: ^2.6.2 - checksum: 58b8b026e25f8156f270bdd240a2e384b64db93a6abe7c15abe9acdc2ce06a724328a8bf4d5b3047f5e398eef3d4961447d8511c52105c082dddd1b9d8fb0cb4 - languageName: node - linkType: hard - "@polkadot/types@npm:10.11.2, @polkadot/types@npm:^10.9.1": version: 10.11.2 resolution: "@polkadot/types@npm:10.11.2" @@ -3746,7 +3550,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/x-bigint@npm:12.6.2, @polkadot/x-bigint@npm:^12.5.1, @polkadot/x-bigint@npm:^12.6.2": +"@polkadot/x-bigint@npm:12.6.2, @polkadot/x-bigint@npm:^12.6.2": version: 12.6.2 resolution: "@polkadot/x-bigint@npm:12.6.2" dependencies: @@ -3756,7 +3560,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/x-fetch@npm:^12.5.1, @polkadot/x-fetch@npm:^12.6.2": +"@polkadot/x-fetch@npm:^12.6.2": version: 12.6.2 resolution: "@polkadot/x-fetch@npm:12.6.2" dependencies: @@ -3776,7 +3580,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/x-global@npm:12.6.2, @polkadot/x-global@npm:^12.3.2, @polkadot/x-global@npm:^12.5.1, @polkadot/x-global@npm:^12.6.2": +"@polkadot/x-global@npm:12.6.2, @polkadot/x-global@npm:^12.3.2, @polkadot/x-global@npm:^12.6.2": version: 12.6.2 resolution: "@polkadot/x-global@npm:12.6.2" dependencies: @@ -3838,7 +3642,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/x-ws@npm:^12.5.1, @polkadot/x-ws@npm:^12.6.2": +"@polkadot/x-ws@npm:^12.6.2": version: 12.6.2 resolution: "@polkadot/x-ws@npm:12.6.2" dependencies: @@ -12362,7 +12166,7 @@ __metadata: languageName: node linkType: hard -"nock@npm:^13.3.4, nock@npm:^13.4.0": +"nock@npm:^13.4.0": version: 13.5.0 resolution: "nock@npm:13.5.0" dependencies: From e98510566f0ef807183c75b0bcebd9009fcdafc6 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 23 Jan 2024 15:51:52 +0300 Subject: [PATCH 02/34] Drop 1010 runtime support --- .../useMessageActions/useMessageActions.ts | 16 ++------ .../useProgramActions/useProgramActions.tsx | 8 ++-- .../frontend/src/widgets/menu/model/consts.ts | 8 +--- .../widgets/messageForm/ui/MessageForm.tsx | 17 +++----- .../widgets/programForm/ui/ProgramForm.tsx | 7 +--- utils/gear-hooks/src/context/Api.tsx | 7 +--- utils/gear-hooks/src/hooks/api/index.ts | 3 +- .../src/hooks/api/useSendMessage.ts | 39 ++++--------------- .../handlers/use-send-message-handler.ts | 10 +---- 9 files changed, 26 insertions(+), 89 deletions(-) diff --git a/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts b/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts index 0ce9217594..80e37838ae 100644 --- a/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts +++ b/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts @@ -14,7 +14,7 @@ import { PROGRAM_ERRORS, TransactionStatus, TransactionName } from '@/shared/con import { ParamsToSendMessage, ParamsToSignAndSend, ParamsToReplyMessage } from './types'; const useMessageActions = () => { - const { api, isApiReady, isVaraVersion } = useApi(); + const { api, isApiReady } = useApi(); const { account } = useAccount(); const alert = useAlert(); const { showModal } = useModal(); @@ -82,13 +82,8 @@ const useMessageActions = () => { const { meta, address } = account!; const sendExtrinsic = api.message.send(message, metadata, payloadType); - let extrinsic: SubmittableExtrinsic<'promise', ISubmittableResult>; - if (isVaraVersion) { - extrinsic = sendExtrinsic; - } else { - extrinsic = withVoucher ? api.voucher.call({ SendMessage: sendExtrinsic }) : sendExtrinsic; - } + const extrinsic = withVoucher ? api.voucher.call({ SendMessage: sendExtrinsic }) : sendExtrinsic; const { signer } = await web3FromSource(meta.source); const { partialFee } = await api.message.paymentInfo(address, { signer }); @@ -124,13 +119,8 @@ const useMessageActions = () => { const { meta, address } = account!; const replyExtrinsic = await api.message.sendReply(reply, metadata, payloadType); - let extrinsic: SubmittableExtrinsic<'promise', ISubmittableResult>; - if (isVaraVersion) { - extrinsic = replyExtrinsic; - } else { - extrinsic = withVoucher ? api.voucher.call({ SendReply: replyExtrinsic }) : replyExtrinsic; - } + const extrinsic = withVoucher ? api.voucher.call({ SendReply: replyExtrinsic }) : replyExtrinsic; const { signer } = await web3FromSource(meta.source); const { partialFee } = await api.message.paymentInfo(address, { signer }); diff --git a/idea/frontend/src/hooks/useProgramActions/useProgramActions.tsx b/idea/frontend/src/hooks/useProgramActions/useProgramActions.tsx index 36edc2e0ee..807cb3ddb1 100644 --- a/idea/frontend/src/hooks/useProgramActions/useProgramActions.tsx +++ b/idea/frontend/src/hooks/useProgramActions/useProgramActions.tsx @@ -29,7 +29,7 @@ import { Payload, ParamsToCreate, ParamsToUpload, ParamsToSignAndUpload } from ' const useProgramActions = () => { const alert = useAlert(); - const { api, isApiReady, isVaraVersion } = useApi(); + const { api, isApiReady } = useApi(); const { account } = useAccount(); const { isDevChain } = useChain(); @@ -48,8 +48,7 @@ const useProgramActions = () => { const { gasLimit, value, initPayload, metadata, payloadType, keepAlive } = payload; - const baseProgram = { value, codeId, gasLimit, initPayload }; - const program = isVaraVersion ? baseProgram : { ...baseProgram, keepAlive }; + const program = { value, codeId, gasLimit, initPayload, keepAlive }; const result = api.program.create(program, metadata, payloadType); @@ -61,8 +60,7 @@ const useProgramActions = () => { const { gasLimit, value, initPayload, metadata, payloadType, keepAlive } = payload; - const baseProgram = { code: optBuffer, value, gasLimit, initPayload }; - const program = isVaraVersion ? baseProgram : { ...baseProgram, keepAlive }; + const program = { code: optBuffer, value, gasLimit, initPayload, keepAlive }; return api.program.upload(program, metadata, payloadType); }; diff --git a/idea/frontend/src/widgets/menu/model/consts.ts b/idea/frontend/src/widgets/menu/model/consts.ts index eeeacad16a..86bbae570a 100644 --- a/idea/frontend/src/widgets/menu/model/consts.ts +++ b/idea/frontend/src/widgets/menu/model/consts.ts @@ -1,5 +1,3 @@ -import { VARA_GENESIS, VARA_TESTNET_GENESIS } from '@gear-js/api'; - import GearSVG from '@/shared/assets/images/logos/gear.svg?react'; import ShortGearSVG from '@/shared/assets/images/logos/shortcut/gear.svg?react'; import GearNetworkSVG from '@/shared/assets/images/logos/networks/gear.svg?react'; @@ -10,9 +8,8 @@ import NetworkVaraSVG from '@/shared/assets/images/logos/networks/vara.svg?react const DEVELOPMENT_SECTION = 'development'; const GENESIS = { - VARA: VARA_GENESIS, - VARA_TESTNET: VARA_TESTNET_GENESIS, - VARA_VIT: '0x9b86ea7366584c5ddf67de243433fcc05732864933258de9467db46eb9bef8b5', + VARA: '0xfe1b4c55fd4d668101126434206571a7838a8b6b93a6d1b95d607e78e6c53763', + VARA_TESTNET: '0x525639f713f397dcf839bd022cd821f367ebcf179de7b9253531f8adbe5436d6', }; // TODO: think about naming of ICON and LOGO, and overall structure @@ -24,7 +21,6 @@ const ICON = { const LOGO = { [GENESIS.VARA]: ICON.vara, [GENESIS.VARA_TESTNET]: ICON.vara, - [GENESIS.VARA_VIT]: ICON.vara, }; export { DEVELOPMENT_SECTION, ICON, LOGO }; diff --git a/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx b/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx index 075e522cb0..37c9935a0b 100644 --- a/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx +++ b/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx @@ -1,6 +1,6 @@ import { ProgramMetadata } from '@gear-js/api'; import { Button, Input, Textarea } from '@gear-js/ui'; -import { useAccount, useApi, useBalanceFormat } from '@gear-js/react-hooks'; +import { useBalanceFormat } from '@gear-js/react-hooks'; import { HexString } from '@polkadot/util/types'; import { yupResolver } from '@hookform/resolvers/yup'; import { useMemo, useState } from 'react'; @@ -30,8 +30,6 @@ type Props = { }; const MessageForm = ({ id, programId, isReply, metadata, isLoading }: Props) => { - const { isVaraVersion } = useApi(); - const { account } = useAccount(); const { getChainBalanceValue, getFormattedGasValue, getChainGasValue } = useBalanceFormat(); const schema = useValidationSchema(); @@ -80,17 +78,14 @@ const MessageForm = ({ id, programId, isReply, metadata, isLoading }: Props) => value: getChainBalanceValue(values.value).toFixed(), payload: getSubmitPayload(values.payload), gasLimit: getChainGasValue(values.gasLimit).toFixed(), + keepAlive, }; - const commonValues = isVaraVersion - ? { ...baseValues, prepaid: withVoucher, account: withVoucher ? account?.decodedAddress : undefined } - : { ...baseValues, keepAlive }; - if (isReply) { - const reply = { ...commonValues, replyToId: id }; + const reply = { ...baseValues, replyToId: id }; replyMessage({ reply, metadata, payloadType, withVoucher, reject: enableSubmitButton, resolve: resetForm }); } else { - const message = { ...commonValues, destination: id }; + const message = { ...baseValues, destination: id }; sendMessage({ message, metadata, payloadType, withVoucher, reject: enableSubmitButton, resolve: resetForm }); } }; @@ -153,9 +148,7 @@ const MessageForm = ({ id, programId, isReply, metadata, isLoading }: Props) => /> )} - {!isVaraVersion && ( - - )} + diff --git a/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx b/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx index fc2308b8ab..58f8eb2fed 100644 --- a/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx +++ b/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx @@ -1,5 +1,5 @@ import { ProgramMetadata } from '@gear-js/api'; -import { useApi, useBalanceFormat } from '@gear-js/react-hooks'; +import { useBalanceFormat } from '@gear-js/react-hooks'; import { Input } from '@gear-js/ui'; import { HexString } from '@polkadot/util/types'; import { yupResolver } from '@hookform/resolvers/yup'; @@ -31,7 +31,6 @@ type Props = { const ProgramForm = (props: Props) => { const { gasMethod, metaHex, metadata, source, fileName = '', renderButtons, onSubmit } = props; - const { isVaraVersion } = useApi(); const { getChainBalanceValue, getFormattedGasValue, getChainGasValue } = useBalanceFormat(); const schema = useValidationSchema(); @@ -133,9 +132,7 @@ const ProgramForm = (props: Props) => { block /> - {!isVaraVersion && ( - - )} +
{renderButtons({ isDisabled })}
diff --git a/utils/gear-hooks/src/context/Api.tsx b/utils/gear-hooks/src/context/Api.tsx index 33bf7b6a89..ab548610b7 100644 --- a/utils/gear-hooks/src/context/Api.tsx +++ b/utils/gear-hooks/src/context/Api.tsx @@ -24,12 +24,10 @@ type Value = { } & ( | { api: undefined; - isVaraVersion: false; isApiReady: false; } | { api: GearApi; - isVaraVersion: boolean; isApiReady: true; } ); @@ -41,7 +39,6 @@ type Props = ProviderProps & { const initialValue = { api: undefined, isApiReady: false as const, - isVaraVersion: false as const, switchNetwork: () => Promise.resolve(), }; @@ -91,9 +88,7 @@ function ApiProvider({ initialArgs, children }: Props) { const value = useMemo( () => - api - ? { api, isApiReady: true as const, isVaraVersion: api.specVersion < 1010, switchNetwork } - : { api, isApiReady: false as const, isVaraVersion: false as const, switchNetwork }, + api ? { api, isApiReady: true as const, switchNetwork } : { api, isApiReady: false as const, switchNetwork }, [api], ); diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index e297a86496..5db979ef52 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -1,6 +1,6 @@ import { useReadFullState, useReadWasmState } from './useReadState'; -import { useSendMessage, SendMessageOptions, UseSendMessageOptions, VaraSendMessageOptions } from './useSendMessage'; +import { useSendMessage, SendMessageOptions, UseSendMessageOptions } from './useSendMessage'; import { useUploadProgram, useCreateProgram } from './useProgram'; import { @@ -43,5 +43,4 @@ export { useAccountDeriveBalancesAll, SendMessageOptions, UseSendMessageOptions, - VaraSendMessageOptions, }; diff --git a/utils/gear-hooks/src/hooks/api/useSendMessage.ts b/utils/gear-hooks/src/hooks/api/useSendMessage.ts index 3c3c43ea10..1f4ef5a1f2 100644 --- a/utils/gear-hooks/src/hooks/api/useSendMessage.ts +++ b/utils/gear-hooks/src/hooks/api/useSendMessage.ts @@ -1,16 +1,8 @@ -import { - GasLimit, - IMessageSendOptions, - MessageQueued, - ProgramMetadata, - VaraMessageSendOptions, - decodeAddress, -} from '@gear-js/api'; +import { GasLimit, MessageQueued, ProgramMetadata } from '@gear-js/api'; import { web3FromSource } from '@polkadot/extension-dapp'; import { EventRecord } from '@polkadot/types/interfaces'; import { AnyJson, IKeyringPair, ISubmittableResult } from '@polkadot/types/types'; import { HexString } from '@polkadot/util/types'; -import { SubmittableExtrinsic } from '@polkadot/api/types'; import BigNumber from 'bignumber.js'; import { useContext } from 'react'; import { AccountContext, AlertContext, ApiContext } from 'context'; @@ -36,14 +28,12 @@ type SendMessageOptions = { onError?: () => void; }; -type VaraSendMessageOptions = Omit; - function useSendMessage( destination: HexString, metadata: ProgramMetadata | undefined, { disableAlerts, disableCheckBalance, pair }: UseSendMessageOptions = {}, ) { - const { api, isApiReady, isVaraVersion } = useContext(ApiContext); // сircular dependency fix + const { api, isApiReady } = useContext(ApiContext); // сircular dependency fix const { account } = useContext(AccountContext); const alert = useContext(AlertContext); @@ -134,7 +124,7 @@ function useSendMessage( return BigNumber(balance).isGreaterThanOrEqualTo(transactionCost); }; - const sendMessage = async (args: SendMessageOptions | VaraSendMessageOptions) => { + const sendMessage = async (args: SendMessageOptions) => { if (!isApiReady) throw new Error('API is not initialized'); if (!account) throw new Error('No account address'); if (!metadata) throw new Error('Metadata not found'); @@ -161,27 +151,12 @@ function useSendMessage( const baseMessage = { destination, payload, gasLimit, value }; - let message: IMessageSendOptions | VaraMessageSendOptions; - - if (isVaraVersion) { - const _account = pair ? decodeAddress(pair.address) : decodedAddress; - - message = { ...baseMessage, prepaid: withVoucher, account: withVoucher ? _account : undefined }; - } else { - const keepAlive = 'keepAlive' in args ? args.keepAlive : false; - message = { ...baseMessage, keepAlive }; - } + const keepAlive = 'keepAlive' in args ? args.keepAlive : false; + const message = { ...baseMessage, keepAlive }; try { const sendExtrinsic = api.message.send(message, metadata); - let extrinsic: SubmittableExtrinsic<'promise', ISubmittableResult>; - - if (isVaraVersion) { - extrinsic = sendExtrinsic; - } else { - // TODO: voucher call into standalone hook? - extrinsic = withVoucher ? api.voucher.call({ SendMessage: sendExtrinsic }) : sendExtrinsic; - } + const extrinsic = withVoucher ? api.voucher.call({ SendMessage: sendExtrinsic }) : sendExtrinsic; const callback = (result: ISubmittableResult) => handleStatus(result, alertId, onSuccess, onInBlock, onError); @@ -210,4 +185,4 @@ function useSendMessage( return sendMessage; } -export { useSendMessage, SendMessageOptions, UseSendMessageOptions, VaraSendMessageOptions }; +export { useSendMessage, SendMessageOptions, UseSendMessageOptions }; diff --git a/utils/gear-hooks/src/hooks/handlers/use-send-message-handler.ts b/utils/gear-hooks/src/hooks/handlers/use-send-message-handler.ts index b3f672b9e6..87e5a97d13 100644 --- a/utils/gear-hooks/src/hooks/handlers/use-send-message-handler.ts +++ b/utils/gear-hooks/src/hooks/handlers/use-send-message-handler.ts @@ -3,13 +3,7 @@ import { HexString } from '@polkadot/util/types'; import { useContext } from 'react'; import { AlertContext, ApiContext } from 'context'; import { getAutoGasLimit } from 'utils'; -import { - SendMessageOptions, - UseSendMessageOptions, - useHandleCalculateGas, - useSendMessage, - VaraSendMessageOptions, -} from '../api'; +import { SendMessageOptions, UseSendMessageOptions, useHandleCalculateGas, useSendMessage } from '../api'; function useSendMessageHandler( destination: HexString, @@ -22,7 +16,7 @@ function useSendMessageHandler( const calculateGas = useHandleCalculateGas(destination, metadata); const sendMessage = useSendMessage(destination, metadata, options); - return (args: Omit | Omit) => { + return (args: Omit) => { if (!isApiReady) throw new Error('API is not initialized'); const { payload, value } = args; From 65aec76879bafef336153a415212c9a31437f4cd Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 23 Jan 2024 16:08:42 +0300 Subject: [PATCH 03/34] Update getExtrinsicFailedMessage --- idea/frontend/src/hooks/useMessageActions/types.ts | 12 +++--------- idea/frontend/src/shared/helpers/index.ts | 6 ++---- utils/gear-hooks/src/utils/index.ts | 5 ++--- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/idea/frontend/src/hooks/useMessageActions/types.ts b/idea/frontend/src/hooks/useMessageActions/types.ts index ce329aacb9..bb65ff30f6 100644 --- a/idea/frontend/src/hooks/useMessageActions/types.ts +++ b/idea/frontend/src/hooks/useMessageActions/types.ts @@ -1,24 +1,18 @@ -import { - ProgramMetadata, - IMessageSendOptions, - IMessageSendReplyOptions, - VaraMessageSendOptions, - VaraMessageSendReplyOptions, -} from '@gear-js/api'; +import { ProgramMetadata, MessageSendOptions, MessageSendReplyOptions } from '@gear-js/api'; import { SubmittableExtrinsic } from '@polkadot/api/types'; import { ISubmittableResult } from '@polkadot/types/types'; import { OperationCallbacks, ParamsToSignAndSend as CommonParamsToSignAndSend } from '@/entities/hooks'; type ParamsToSendMessage = OperationCallbacks & { - message: IMessageSendOptions | VaraMessageSendOptions; + message: MessageSendOptions; withVoucher: boolean; metadata?: ProgramMetadata; payloadType?: string; }; type ParamsToReplyMessage = OperationCallbacks & { - reply: IMessageSendReplyOptions | VaraMessageSendReplyOptions; + reply: MessageSendReplyOptions; withVoucher: boolean; metadata?: ProgramMetadata; payloadType?: string; diff --git a/idea/frontend/src/shared/helpers/index.ts b/idea/frontend/src/shared/helpers/index.ts index 8de6dec82d..06ab1b479e 100644 --- a/idea/frontend/src/shared/helpers/index.ts +++ b/idea/frontend/src/shared/helpers/index.ts @@ -1,7 +1,6 @@ import type { Event } from '@polkadot/types/interfaces'; import { GearApi } from '@gear-js/api'; import { Account, AlertContainerFactory } from '@gear-js/react-hooks'; -import { AnySchema, ValidationError } from 'yup'; import isString from 'lodash.isstring'; import { ACCOUNT_ERRORS, NODE_ADRESS_URL_PARAM, FileTypes } from '@/shared/config'; @@ -81,10 +80,9 @@ const getShortName = (filename: string, maxLength = 24) => { const getPreformattedText = (data: unknown) => JSON.stringify(data, null, 4); const getExtrinsicFailedMessage = (api: GearApi, event: Event) => { - const { docs, method: errorMethod } = api.getExtrinsicFailedError(event); - const formattedDocs = docs.filter(Boolean).join('. '); + const { docs, method } = api.getExtrinsicFailedError(event); - return `${errorMethod}: ${formattedDocs}`; + return `${method}: ${docs}`; }; const isNodeAddressValid = (address: string) => { diff --git a/utils/gear-hooks/src/utils/index.ts b/utils/gear-hooks/src/utils/index.ts index f0372ef4c9..86f1b70a29 100644 --- a/utils/gear-hooks/src/utils/index.ts +++ b/utils/gear-hooks/src/utils/index.ts @@ -10,10 +10,9 @@ const getAutoGasLimit = ({ waited, min_limit }: GasInfo) => const withoutCommas = (value: string) => value.replace(/,/g, ''); const getExtrinsicFailedMessage = (api: GearApi, event: Event) => { - const { docs, method: errorMethod } = api.getExtrinsicFailedError(event); - const formattedDocs = docs.filter(Boolean).join('. '); + const { docs, method } = api.getExtrinsicFailedError(event); - return `${errorMethod}: ${formattedDocs}`; + return `${method}: ${docs}`; }; const getVaraAddress = (address: string) => new Keyring().encodeAddress(address, VARA_SS58_FORMAT); From 1c46aa956a9162e7a8c772f3cdb1779481599474 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 23 Jan 2024 17:02:52 +0300 Subject: [PATCH 04/34] Add useVouchers hook --- utils/gear-hooks/src/hooks/api/index.ts | 4 +++ .../gear-hooks/src/hooks/api/voucher/index.ts | 13 ++++---- .../voucher/use-account-voucher-balance.ts | 12 ------- .../hooks/api/voucher/use-account-voucher.ts | 12 ------- .../voucher/use-is-account-voucher-exists.ts | 12 ------- .../api/voucher/use-is-voucher-exists.ts | 11 +++++-- .../hooks/api/voucher/use-voucher-balance.ts | 10 +++++- .../src/hooks/api/voucher/use-voucher.ts | 10 +++++- .../src/hooks/api/voucher/use-vouchers.ts | 31 +++++++++++++++++++ utils/gear-hooks/src/hooks/index.ts | 4 +++ utils/gear-hooks/src/index.ts | 4 +++ 11 files changed, 76 insertions(+), 47 deletions(-) delete mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-account-voucher-balance.ts delete mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-account-voucher.ts delete mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-is-account-voucher-exists.ts create mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index 5db979ef52..838b00325d 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -17,6 +17,8 @@ import { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, } from './voucher'; import { useBalance, useBalanceFormat, useDeriveBalancesAll, useAccountDeriveBalancesAll } from './balance'; @@ -37,6 +39,8 @@ export { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, useBalance, useBalanceFormat, useDeriveBalancesAll, diff --git a/utils/gear-hooks/src/hooks/api/voucher/index.ts b/utils/gear-hooks/src/hooks/api/voucher/index.ts index 219412fd49..5533089f38 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/index.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/index.ts @@ -1,10 +1,7 @@ -import { useIsVoucherExists } from './use-is-voucher-exists'; -import { useVoucherBalance } from './use-voucher-balance'; -import { useVoucher } from './use-voucher'; - -import { useIsAccountVoucherExists } from './use-is-account-voucher-exists'; -import { useAccountVoucherBalance } from './use-account-voucher-balance'; -import { useAccountVoucher } from './use-account-voucher'; +import { useIsVoucherExists, useIsAccountVoucherExists } from './use-is-voucher-exists'; +import { useVoucherBalance, useAccountVoucherBalance } from './use-voucher-balance'; +import { useVoucher, useAccountVoucher } from './use-voucher'; +import { useVouchers, useAccountVouchers } from './use-vouchers'; export { useIsVoucherExists, @@ -13,4 +10,6 @@ export { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-account-voucher-balance.ts b/utils/gear-hooks/src/hooks/api/voucher/use-account-voucher-balance.ts deleted file mode 100644 index ebf19e7376..0000000000 --- a/utils/gear-hooks/src/hooks/api/voucher/use-account-voucher-balance.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { AccountContext } from 'context'; -import { useContext } from 'react'; -import { useVoucherBalance } from './use-voucher-balance'; -import { HexString } from '@gear-js/api'; - -function useAccountVoucherBalance(programId: HexString | undefined) { - const { account } = useContext(AccountContext); - - return useVoucherBalance(programId, account?.decodedAddress); -} - -export { useAccountVoucherBalance }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-account-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-account-voucher.ts deleted file mode 100644 index 1ae7172d4b..0000000000 --- a/utils/gear-hooks/src/hooks/api/voucher/use-account-voucher.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { useContext } from 'react'; -import { useVoucher } from './use-voucher'; -import { AccountContext } from 'context'; -import { HexString } from '@gear-js/api'; - -function useAccountVoucher(programId: HexString | undefined) { - const { account } = useContext(AccountContext); - - return useVoucher(programId, account?.decodedAddress); -} - -export { useAccountVoucher }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-is-account-voucher-exists.ts b/utils/gear-hooks/src/hooks/api/voucher/use-is-account-voucher-exists.ts deleted file mode 100644 index eed5c3ab91..0000000000 --- a/utils/gear-hooks/src/hooks/api/voucher/use-is-account-voucher-exists.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { HexString } from '@gear-js/api'; -import { useIsVoucherExists } from './use-is-voucher-exists'; -import { useContext } from 'react'; -import { AccountContext } from 'context'; - -function useIsAccountVoucherExists(programId: HexString | undefined) { - const { account } = useContext(AccountContext); - - return useIsVoucherExists(programId, account?.decodedAddress); -} - -export { useIsAccountVoucherExists }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts b/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts index e6cb2e290d..6bcbe1f62c 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts @@ -1,7 +1,8 @@ import { HexString } from '@gear-js/api'; -import { AlertContext, ApiContext } from 'context'; import { useState, useEffect, useContext } from 'react'; +import { AccountContext, AlertContext, ApiContext } from 'context'; + function useIsVoucherExists(programId: HexString | undefined, accountAddress: HexString | undefined) { const { api, isApiReady } = useContext(ApiContext); // сircular dependency fix const alert = useContext(AlertContext); @@ -25,4 +26,10 @@ function useIsVoucherExists(programId: HexString | undefined, accountAddress: He return { isVoucherExists, isVoucherExistsReady }; } -export { useIsVoucherExists }; +function useIsAccountVoucherExists(programId: HexString | undefined) { + const { account } = useContext(AccountContext); + + return useIsVoucherExists(programId, account?.decodedAddress); +} + +export { useIsVoucherExists, useIsAccountVoucherExists }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts index 598c9b536e..9bea94da27 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts @@ -1,6 +1,8 @@ import { HexString, generateVoucherId } from '@gear-js/api'; +import { useContext } from 'react'; import { useBalance } from '../balance/use-balance'; +import { AccountContext } from 'context'; function useVoucherBalance(programId: HexString | undefined, accountAddress: HexString | undefined) { const { balance, isBalanceReady } = useBalance( @@ -10,4 +12,10 @@ function useVoucherBalance(programId: HexString | undefined, accountAddress: Hex return { voucherBalance: balance, isVoucherBalanceReady: isBalanceReady }; } -export { useVoucherBalance }; +function useAccountVoucherBalance(programId: HexString | undefined) { + const { account } = useContext(AccountContext); + + return useVoucherBalance(programId, account?.decodedAddress); +} + +export { useVoucherBalance, useAccountVoucherBalance }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts index b5af30f55d..89f6192af6 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts @@ -1,7 +1,9 @@ import { HexString } from '@polkadot/util/types'; +import { useContext } from 'react'; import { useIsVoucherExists } from './use-is-voucher-exists'; import { useVoucherBalance } from './use-voucher-balance'; +import { AccountContext } from 'context'; function useVoucher(programId: HexString | undefined, accountAddress: HexString | undefined) { const { isVoucherExists, isVoucherExistsReady } = useIsVoucherExists(programId, accountAddress); @@ -12,4 +14,10 @@ function useVoucher(programId: HexString | undefined, accountAddress: HexString return { isVoucherExists, voucherBalance, isVoucherReady }; } -export { useVoucher }; +function useAccountVoucher(programId: HexString | undefined) { + const { account } = useContext(AccountContext); + + return useVoucher(programId, account?.decodedAddress); +} + +export { useVoucher, useAccountVoucher }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts new file mode 100644 index 0000000000..54840b9814 --- /dev/null +++ b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts @@ -0,0 +1,31 @@ +import { HexString } from '@gear-js/api'; +import { useApi, useAlert, useAccount } from 'hooks/context'; +import { useState, useEffect } from 'react'; + +function useVouchers(accountAddress: string | undefined) { + const { api } = useApi(); + const alert = useAlert(); + + const [vouchers, setVouchers] = useState>(); + + useEffect(() => { + if (!api || !accountAddress) return setVouchers(undefined); + + api.voucher + .getAllForAccount(accountAddress) + .then((result) => setVouchers(result)) + .catch(({ message }) => alert.error(message)); + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [api, accountAddress]); + + return vouchers; +} + +function useAccountVouchers() { + const { account } = useAccount(); + + return useVouchers(account?.address); +} + +export { useVouchers, useAccountVouchers }; diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 36791f7e56..8afac3f0b7 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -14,6 +14,8 @@ import { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -40,6 +42,8 @@ export { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, useBalance, useBalanceFormat, useDeriveBalancesAll, diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index 1fb2aa195c..1008aa357c 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -14,6 +14,8 @@ import { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -69,6 +71,8 @@ export { useIsAccountVoucherExists, useAccountVoucherBalance, useAccountVoucher, + useVouchers, + useAccountVouchers, useBalance, useBalanceFormat, useDeriveBalancesAll, From 593afc5caa8fccc1bba002e7b13837f7091aaf4c Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 23 Jan 2024 17:32:04 +0300 Subject: [PATCH 05/34] Update useVoucher hook with getDetails method --- utils/gear-hooks/src/hooks/api/index.ts | 4 --- .../gear-hooks/src/hooks/api/voucher/index.ts | 3 -- .../hooks/api/voucher/use-voucher-balance.ts | 21 ------------ .../src/hooks/api/voucher/use-voucher.ts | 32 ++++++++++++------- .../src/hooks/api/voucher/use-vouchers.ts | 14 ++++---- utils/gear-hooks/src/hooks/index.ts | 4 --- utils/gear-hooks/src/index.ts | 4 --- 7 files changed, 28 insertions(+), 54 deletions(-) delete mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index 838b00325d..51d696893a 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -12,10 +12,8 @@ import { import { useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, @@ -34,10 +32,8 @@ export { useHandleCalculateGas, useReplyCalculateGas, useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, diff --git a/utils/gear-hooks/src/hooks/api/voucher/index.ts b/utils/gear-hooks/src/hooks/api/voucher/index.ts index 5533089f38..a3fc3fd9fc 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/index.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/index.ts @@ -1,14 +1,11 @@ import { useIsVoucherExists, useIsAccountVoucherExists } from './use-is-voucher-exists'; -import { useVoucherBalance, useAccountVoucherBalance } from './use-voucher-balance'; import { useVoucher, useAccountVoucher } from './use-voucher'; import { useVouchers, useAccountVouchers } from './use-vouchers'; export { useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts deleted file mode 100644 index 9bea94da27..0000000000 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { HexString, generateVoucherId } from '@gear-js/api'; -import { useContext } from 'react'; - -import { useBalance } from '../balance/use-balance'; -import { AccountContext } from 'context'; - -function useVoucherBalance(programId: HexString | undefined, accountAddress: HexString | undefined) { - const { balance, isBalanceReady } = useBalance( - programId && accountAddress ? generateVoucherId(accountAddress, programId) : undefined, - ); - - return { voucherBalance: balance, isVoucherBalanceReady: isBalanceReady }; -} - -function useAccountVoucherBalance(programId: HexString | undefined) { - const { account } = useContext(AccountContext); - - return useVoucherBalance(programId, account?.decodedAddress); -} - -export { useVoucherBalance, useAccountVoucherBalance }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts index 89f6192af6..ccb499261f 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts @@ -1,23 +1,31 @@ -import { HexString } from '@polkadot/util/types'; -import { useContext } from 'react'; +import { HexString, IVoucherDetails } from '@gear-js/api'; +import { useContext, useEffect, useState } from 'react'; -import { useIsVoucherExists } from './use-is-voucher-exists'; -import { useVoucherBalance } from './use-voucher-balance'; -import { AccountContext } from 'context'; +import { AccountContext, AlertContext, ApiContext } from 'context'; -function useVoucher(programId: HexString | undefined, accountAddress: HexString | undefined) { - const { isVoucherExists, isVoucherExistsReady } = useIsVoucherExists(programId, accountAddress); - const { voucherBalance, isVoucherBalanceReady } = useVoucherBalance(programId, accountAddress); +function useVoucher(accountAddress: string | undefined, voucherId: HexString | undefined) { + const { api } = useContext(ApiContext); + const alert = useContext(AlertContext); - const isVoucherReady = isVoucherExistsReady && isVoucherBalanceReady; + const [voucher, setVoucher] = useState(); + const isVoucherReady = voucher !== undefined; - return { isVoucherExists, voucherBalance, isVoucherReady }; + useEffect(() => { + if (!api || !accountAddress || !voucherId) return setVoucher(undefined); + + api.voucher + .getDetails(accountAddress, voucherId) + .then((result) => setVoucher(result)) + .catch(({ message }) => alert.error(message)); + }, [api, accountAddress, voucherId]); + + return { voucher, isVoucherReady }; } -function useAccountVoucher(programId: HexString | undefined) { +function useAccountVoucher(voucherId: HexString | undefined) { const { account } = useContext(AccountContext); - return useVoucher(programId, account?.decodedAddress); + return useVoucher(account?.address, voucherId); } export { useVoucher, useAccountVoucher }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts index 54840b9814..55f1cd5920 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts @@ -1,12 +1,14 @@ import { HexString } from '@gear-js/api'; -import { useApi, useAlert, useAccount } from 'hooks/context'; -import { useState, useEffect } from 'react'; +import { useState, useEffect, useContext } from 'react'; + +import { AccountContext, AlertContext, ApiContext } from 'context'; function useVouchers(accountAddress: string | undefined) { - const { api } = useApi(); - const alert = useAlert(); + const { api } = useContext(ApiContext); + const alert = useContext(AlertContext); const [vouchers, setVouchers] = useState>(); + const isEachVoucherReady = vouchers !== undefined; useEffect(() => { if (!api || !accountAddress) return setVouchers(undefined); @@ -19,11 +21,11 @@ function useVouchers(accountAddress: string | undefined) { // eslint-disable-next-line react-hooks/exhaustive-deps }, [api, accountAddress]); - return vouchers; + return { vouchers, isEachVoucherReady }; } function useAccountVouchers() { - const { account } = useAccount(); + const { account } = useContext(AccountContext); return useVouchers(account?.address); } diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 8afac3f0b7..60f39442f3 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -9,10 +9,8 @@ import { useHandleCalculateGas, useReplyCalculateGas, useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, @@ -37,10 +35,8 @@ export { useHandleCalculateGas, useReplyCalculateGas, useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index 1008aa357c..872e67f506 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -9,10 +9,8 @@ import { useHandleCalculateGas, useReplyCalculateGas, useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, @@ -66,10 +64,8 @@ export { useHandleCalculateGas, useReplyCalculateGas, useIsVoucherExists, - useVoucherBalance, useVoucher, useIsAccountVoucherExists, - useAccountVoucherBalance, useAccountVoucher, useVouchers, useAccountVouchers, From 1f1cbc2230813da5bbb6640b9d7fe1bf06ddee72 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 23 Jan 2024 19:37:13 +0300 Subject: [PATCH 06/34] Add useVoucherId hook --- utils/gear-hooks/src/hooks/api/index.ts | 4 +++ .../gear-hooks/src/hooks/api/voucher/index.ts | 3 ++ .../src/hooks/api/voucher/use-voucher-id.ts | 32 +++++++++++++++++++ utils/gear-hooks/src/hooks/index.ts | 4 +++ utils/gear-hooks/src/index.ts | 9 +++++- utils/gear-hooks/src/types/index.ts | 7 ++++ utils/gear-hooks/src/utils/index.ts | 6 +++- 7 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index 51d696893a..55f358600a 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -17,6 +17,8 @@ import { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, } from './voucher'; import { useBalance, useBalanceFormat, useDeriveBalancesAll, useAccountDeriveBalancesAll } from './balance'; @@ -37,6 +39,8 @@ export { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, useBalance, useBalanceFormat, useDeriveBalancesAll, diff --git a/utils/gear-hooks/src/hooks/api/voucher/index.ts b/utils/gear-hooks/src/hooks/api/voucher/index.ts index a3fc3fd9fc..8d282f5f76 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/index.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/index.ts @@ -1,6 +1,7 @@ import { useIsVoucherExists, useIsAccountVoucherExists } from './use-is-voucher-exists'; import { useVoucher, useAccountVoucher } from './use-voucher'; import { useVouchers, useAccountVouchers } from './use-vouchers'; +import { useVoucherId, useAccountVoucherId } from './use-voucher-id'; export { useIsVoucherExists, @@ -9,4 +10,6 @@ export { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts new file mode 100644 index 0000000000..08b7e18242 --- /dev/null +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts @@ -0,0 +1,32 @@ +import { HexString } from '@gear-js/api'; +import { useContext, useEffect, useState } from 'react'; + +import { AccountContext } from 'context'; +import { getTypedEntries } from 'utils'; + +import { useVouchers } from './use-vouchers'; + +function useVoucherId(accountAddress: string | undefined, programId: HexString | undefined) { + const { vouchers } = useVouchers(accountAddress); + + const [voucherId, setVoucherId] = useState(); + const isVoucherIdReady = voucherId !== undefined; + + useEffect(() => { + if (!vouchers || !programId) return setVoucherId(undefined); + + const [result] = getTypedEntries(vouchers).find(([, programIds]) => programIds.includes(programId)) || ['']; + + setVoucherId(result); + }, [vouchers, accountAddress, programId]); + + return { voucherId, isVoucherIdReady }; +} + +function useAccountVoucherId(programId: HexString | undefined) { + const { account } = useContext(AccountContext); + + return useVoucherId(account?.address, programId); +} + +export { useVoucherId, useAccountVoucherId }; diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 60f39442f3..14a6904ed1 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -14,6 +14,8 @@ import { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -40,6 +42,8 @@ export { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, useBalance, useBalanceFormat, useDeriveBalancesAll, diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index 872e67f506..d2187c7ae5 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -14,6 +14,8 @@ import { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -27,7 +29,7 @@ import { UseSendMessageOptions, } from './hooks'; -import { withoutCommas, getVaraAddress } from './utils'; +import { withoutCommas, getVaraAddress, getTypedEntries } from './utils'; import { AccountProvider, ApiProvider, AlertProvider } from './context'; @@ -51,6 +53,7 @@ import { DefaultTemplateOptions, ProviderProps, Account, + Entries, } from './types'; export { @@ -69,6 +72,8 @@ export { useAccountVoucher, useVouchers, useAccountVouchers, + useVoucherId, + useAccountVoucherId, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -83,6 +88,7 @@ export { useSendMessageHandler, withoutCommas, getVaraAddress, + getTypedEntries, DEFAULT_OPTIONS, DEFAULT_INFO_OPTIONS, DEFAULT_ERROR_OPTIONS, @@ -104,4 +110,5 @@ export type { Account, SendMessageOptions, UseSendMessageOptions, + Entries, }; diff --git a/utils/gear-hooks/src/types/index.ts b/utils/gear-hooks/src/types/index.ts index 5aa7b4a9eb..91a8d98fea 100644 --- a/utils/gear-hooks/src/types/index.ts +++ b/utils/gear-hooks/src/types/index.ts @@ -17,6 +17,12 @@ type Account = InjectedAccountWithMeta & { decodedAddress: HexString; }; +// in case Object.entries return value is immutable +// ref: https://stackoverflow.com/a/60142095 +type Entries = { + [K in keyof T]: [K, T[K]]; +}[keyof T][]; + export { AlertType }; export type { AlertOptions, @@ -28,4 +34,5 @@ export type { DefaultTemplateOptions, ProviderProps, Account, + Entries, }; diff --git a/utils/gear-hooks/src/utils/index.ts b/utils/gear-hooks/src/utils/index.ts index 86f1b70a29..cfea1e3ec1 100644 --- a/utils/gear-hooks/src/utils/index.ts +++ b/utils/gear-hooks/src/utils/index.ts @@ -2,6 +2,8 @@ import { GasInfo, GearApi } from '@gear-js/api'; import { Keyring } from '@polkadot/api'; import { Event } from '@polkadot/types/interfaces'; import { bnToBn } from '@polkadot/util'; + +import { Entries } from 'types'; import { VARA_SS58_FORMAT } from 'consts'; const getAutoGasLimit = ({ waited, min_limit }: GasInfo) => @@ -17,4 +19,6 @@ const getExtrinsicFailedMessage = (api: GearApi, event: Event) => { const getVaraAddress = (address: string) => new Keyring().encodeAddress(address, VARA_SS58_FORMAT); -export { getAutoGasLimit, withoutCommas, getExtrinsicFailedMessage, getVaraAddress }; +const getTypedEntries = (value: T) => Object.entries(value) as Entries; + +export { getAutoGasLimit, withoutCommas, getExtrinsicFailedMessage, getVaraAddress, getTypedEntries }; From 993694bb8c90c774f0ed0263b0c16135cbb5ba2d Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Wed, 24 Jan 2024 17:19:55 +0300 Subject: [PATCH 07/34] Update useVoucherBalance hook --- utils/gear-hooks/src/hooks/api/index.ts | 4 +++ .../src/hooks/api/useProgram/types.ts | 8 +++--- .../gear-hooks/src/hooks/api/voucher/index.ts | 3 +++ .../api/voucher/use-is-voucher-exists.ts | 2 +- .../hooks/api/voucher/use-voucher-balance.ts | 25 +++++++++++++++++++ .../src/hooks/api/voucher/use-voucher-id.ts | 8 +++--- .../src/hooks/api/voucher/use-voucher.ts | 4 +-- utils/gear-hooks/src/hooks/index.ts | 4 +++ utils/gear-hooks/src/index.ts | 4 +++ 9 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index 55f358600a..3440da564c 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -19,6 +19,8 @@ import { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, } from './voucher'; import { useBalance, useBalanceFormat, useDeriveBalancesAll, useAccountDeriveBalancesAll } from './balance'; @@ -41,6 +43,8 @@ export { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, useBalance, useBalanceFormat, useDeriveBalancesAll, diff --git a/utils/gear-hooks/src/hooks/api/useProgram/types.ts b/utils/gear-hooks/src/hooks/api/useProgram/types.ts index 4fc8b9065c..a4f00a124e 100644 --- a/utils/gear-hooks/src/hooks/api/useProgram/types.ts +++ b/utils/gear-hooks/src/hooks/api/useProgram/types.ts @@ -1,5 +1,5 @@ -import { GasLimit, IProgramCreateOptions, IProgramUploadOptions, Value } from '@gear-js/api'; -import { AnyJson, ISubmittableResult, Signer } from '@polkadot/types/types'; +import { GasLimit, ProgramCreateOptions, ProgramUploadOptions, Value } from '@gear-js/api'; +import { AnyJson, ISubmittableResult } from '@polkadot/types/types'; import { HexString } from '@polkadot/util/types'; enum Method { @@ -77,8 +77,8 @@ type Options = { salt?: HexString; }; -type Code = IProgramUploadOptions['code']; -type CodeId = IProgramCreateOptions['codeId']; +type Code = ProgramUploadOptions['code']; +type CodeId = ProgramCreateOptions['codeId']; type UseProgram = (initPayload: AnyJson, gasLimit: GasLimit, options?: Options) => Promise; diff --git a/utils/gear-hooks/src/hooks/api/voucher/index.ts b/utils/gear-hooks/src/hooks/api/voucher/index.ts index 8d282f5f76..d84bd809ba 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/index.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/index.ts @@ -2,6 +2,7 @@ import { useIsVoucherExists, useIsAccountVoucherExists } from './use-is-voucher- import { useVoucher, useAccountVoucher } from './use-voucher'; import { useVouchers, useAccountVouchers } from './use-vouchers'; import { useVoucherId, useAccountVoucherId } from './use-voucher-id'; +import { useVoucherBalance, useAccountVoucherBalance } from './use-voucher-balance'; export { useIsVoucherExists, @@ -12,4 +13,6 @@ export { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts b/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts index 6bcbe1f62c..13b49b7c8f 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-is-voucher-exists.ts @@ -16,7 +16,7 @@ function useIsVoucherExists(programId: HexString | undefined, accountAddress: He if (!programId || !isApiReady || !accountAddress) return; api.voucher - .exists(programId, accountAddress) + .exists(accountAddress, programId) .then((result) => setIsVoucherExists(result)) .catch(({ message }: Error) => alert.error(message)); diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts new file mode 100644 index 0000000000..8c4a073618 --- /dev/null +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts @@ -0,0 +1,25 @@ +import { HexString } from '@gear-js/api'; +import { useContext } from 'react'; + +import { AccountContext } from 'context'; + +import { useBalance } from '../balance'; +import { useVoucherId } from './use-voucher-id'; + +function useVoucherBalance(programId: HexString | undefined, accountAddress: string | undefined) { + const { voucherId, isVoucherIdReady, isVoucherExists } = useVoucherId(programId, accountAddress); + const { balance, isBalanceReady } = useBalance(voucherId); + + const voucherBalance = balance; + const isVoucherBalanceReady = isBalanceReady || isVoucherIdReady; + + return { voucherBalance, isVoucherBalanceReady, isVoucherExists }; +} + +function useAccountVoucherBalance(programId: HexString | undefined) { + const { account } = useContext(AccountContext); + + return useVoucherBalance(programId, account?.address); +} + +export { useVoucherBalance, useAccountVoucherBalance }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts index 08b7e18242..9a52d17269 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts @@ -6,11 +6,13 @@ import { getTypedEntries } from 'utils'; import { useVouchers } from './use-vouchers'; -function useVoucherId(accountAddress: string | undefined, programId: HexString | undefined) { +function useVoucherId(programId: HexString | undefined, accountAddress: string | undefined) { const { vouchers } = useVouchers(accountAddress); const [voucherId, setVoucherId] = useState(); + const isVoucherIdReady = voucherId !== undefined; + const isVoucherExists = !!(isVoucherIdReady && voucherId); useEffect(() => { if (!vouchers || !programId) return setVoucherId(undefined); @@ -20,13 +22,13 @@ function useVoucherId(accountAddress: string | undefined, programId: HexString | setVoucherId(result); }, [vouchers, accountAddress, programId]); - return { voucherId, isVoucherIdReady }; + return { voucherId, isVoucherIdReady, isVoucherExists }; } function useAccountVoucherId(programId: HexString | undefined) { const { account } = useContext(AccountContext); - return useVoucherId(account?.address, programId); + return useVoucherId(programId, account?.address); } export { useVoucherId, useAccountVoucherId }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts index ccb499261f..18293fbc63 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts @@ -3,7 +3,7 @@ import { useContext, useEffect, useState } from 'react'; import { AccountContext, AlertContext, ApiContext } from 'context'; -function useVoucher(accountAddress: string | undefined, voucherId: HexString | undefined) { +function useVoucher(voucherId: HexString | undefined, accountAddress: string | undefined) { const { api } = useContext(ApiContext); const alert = useContext(AlertContext); @@ -25,7 +25,7 @@ function useVoucher(accountAddress: string | undefined, voucherId: HexString | u function useAccountVoucher(voucherId: HexString | undefined) { const { account } = useContext(AccountContext); - return useVoucher(account?.address, voucherId); + return useVoucher(voucherId, account?.address); } export { useVoucher, useAccountVoucher }; diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 14a6904ed1..24ccbc3f15 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -16,6 +16,8 @@ import { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -44,6 +46,8 @@ export { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, useBalance, useBalanceFormat, useDeriveBalancesAll, diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index d2187c7ae5..91ffbb3bae 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -16,6 +16,8 @@ import { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, useBalance, useBalanceFormat, useDeriveBalancesAll, @@ -74,6 +76,8 @@ export { useAccountVouchers, useVoucherId, useAccountVoucherId, + useVoucherBalance, + useAccountVoucherBalance, useBalance, useBalanceFormat, useDeriveBalancesAll, From 9ccde8c62845eda788ebc0737a181b81eec4d833 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Wed, 24 Jan 2024 17:48:42 +0300 Subject: [PATCH 08/34] Fix voucher table and checkbox display --- .../ui/use-voucher-checkbox/use-voucher-checkbox.tsx | 4 ++-- .../voucher/ui/voucher-table/voucher-table.tsx | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx b/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx index dfc29df792..40bcefc80a 100644 --- a/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx +++ b/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx @@ -1,5 +1,5 @@ import { HexString } from '@gear-js/api'; -import { useAccountVoucher, useBalanceFormat } from '@gear-js/react-hooks'; +import { useAccountVoucherBalance, useBalanceFormat } from '@gear-js/react-hooks'; import { LabeledCheckbox } from '@/shared/ui'; @@ -10,7 +10,7 @@ type Props = { }; const UseVoucherCheckbox = ({ programId }: Props) => { - const { isVoucherExists, voucherBalance } = useAccountVoucher(programId); + const { voucherBalance, isVoucherExists } = useAccountVoucherBalance(programId); const { getFormattedBalance } = useBalanceFormat(); const formattedBalance = voucherBalance ? getFormattedBalance(voucherBalance) : undefined; diff --git a/idea/frontend/src/features/voucher/ui/voucher-table/voucher-table.tsx b/idea/frontend/src/features/voucher/ui/voucher-table/voucher-table.tsx index 9d54b7f802..a8d199a25e 100644 --- a/idea/frontend/src/features/voucher/ui/voucher-table/voucher-table.tsx +++ b/idea/frontend/src/features/voucher/ui/voucher-table/voucher-table.tsx @@ -1,5 +1,5 @@ import { HexString } from '@gear-js/api'; -import { useAccountVoucher, useBalanceFormat } from '@gear-js/react-hooks'; +import { useAccountVoucherBalance, useBalanceFormat } from '@gear-js/react-hooks'; import VoucherPlaceholderSVG from '@/features/voucher/assets/voucher-placeholder.svg?react'; import { ContentLoader } from '@/shared/ui/contentLoader'; @@ -14,22 +14,22 @@ type Props = { }; const VoucherTable = withAccount(({ programId }: Props) => { - const { isVoucherReady, isVoucherExists, voucherBalance } = useAccountVoucher(programId); + const { voucherBalance, isVoucherBalanceReady, isVoucherExists } = useAccountVoucherBalance(programId); const { getFormattedBalance } = useBalanceFormat(); const status = isVoucherExists ? BulbStatus.Success : BulbStatus.Error; const text = isVoucherExists ? 'Available' : 'Not available'; - const balance = voucherBalance ? getFormattedBalance(voucherBalance) : undefined; + const formattedBalance = getFormattedBalance(voucherBalance || '0'); - return isVoucherReady ? ( + return isVoucherBalanceReady ? ( - {balance?.value} - {balance?.unit} + {formattedBalance.value} + {formattedBalance.unit}
) : ( From 7275bb2f2769674a54801c4e33b5106380af605e Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Wed, 24 Jan 2024 17:50:02 +0300 Subject: [PATCH 09/34] Fix voucher ready statuses reset --- utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts | 2 +- utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts | 4 +++- utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts | 4 +++- utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts index 8c4a073618..087fd1f52c 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts @@ -11,7 +11,7 @@ function useVoucherBalance(programId: HexString | undefined, accountAddress: str const { balance, isBalanceReady } = useBalance(voucherId); const voucherBalance = balance; - const isVoucherBalanceReady = isBalanceReady || isVoucherIdReady; + const isVoucherBalanceReady = isVoucherExists ? isBalanceReady : isVoucherIdReady; return { voucherBalance, isVoucherBalanceReady, isVoucherExists }; } diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts index 9a52d17269..7d04d324b1 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts @@ -15,7 +15,9 @@ function useVoucherId(programId: HexString | undefined, accountAddress: string | const isVoucherExists = !!(isVoucherIdReady && voucherId); useEffect(() => { - if (!vouchers || !programId) return setVoucherId(undefined); + setVoucherId(undefined); + + if (!vouchers || !programId) return; const [result] = getTypedEntries(vouchers).find(([, programIds]) => programIds.includes(programId)) || ['']; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts index 18293fbc63..bccffe868c 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts @@ -11,7 +11,9 @@ function useVoucher(voucherId: HexString | undefined, accountAddress: string | u const isVoucherReady = voucher !== undefined; useEffect(() => { - if (!api || !accountAddress || !voucherId) return setVoucher(undefined); + setVoucher(undefined); + + if (!api || !accountAddress || !voucherId) return; api.voucher .getDetails(accountAddress, voucherId) diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts index 55f1cd5920..6412fac86d 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts @@ -11,7 +11,9 @@ function useVouchers(accountAddress: string | undefined) { const isEachVoucherReady = vouchers !== undefined; useEffect(() => { - if (!api || !accountAddress) return setVouchers(undefined); + setVouchers(undefined); + + if (!api || !accountAddress) return; api.voucher .getAllForAccount(accountAddress) From 51c71825c49430d3fa4dc7579c0bd3c7ee8456b8 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Wed, 24 Jan 2024 21:34:23 +0300 Subject: [PATCH 10/34] Fix issue voucher --- .../src/features/voucher/hooks/use-issue-voucher.ts | 12 +++++++++--- .../ui/issue-voucher-modal/issue-voucher-modal.tsx | 2 +- utils/gear-hooks/src/context/Api.tsx | 7 ++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts b/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts index 9103d500f7..dfe7229893 100644 --- a/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts +++ b/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts @@ -9,7 +9,7 @@ import { PROGRAM_ERRORS } from '@/shared/config'; import { getExtrinsicFailedMessage } from '@/shared/helpers'; function useIssueVoucher() { - const { api, isApiReady } = useApi(); + const { api, isApiReady, isV110Runtime } = useApi(); const { account } = useAccount(); const alert = useAlert(); @@ -39,7 +39,13 @@ function useIssueVoucher() { if (!isApiReady || !account) return; try { - const { extrinsic } = api.voucher.issue(address, programId, value); + const duration = undefined; + const programs = [programId]; + const isValidForCode = false; + + const { extrinsic } = isV110Runtime + ? await api.voucher.issue(address, value, duration, programs, isValidForCode) + : api.voucher.issueDeprecated(address, programId, value); const { signer } = await web3FromSource(account.meta.source); @@ -49,7 +55,7 @@ function useIssueVoucher() { } }; - return issueVoucher; + return { issueVoucher }; } export { useIssueVoucher }; diff --git a/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx b/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx index 51dcfedd7a..b03ed9f533 100644 --- a/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx +++ b/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx @@ -32,7 +32,7 @@ const resolver = yupResolver(schema); const IssueVoucherModal = ({ programId, close }: Props) => { const { getChainBalanceValue } = useBalanceFormat(); - const issueVoucher = useIssueVoucher(); + const { issueVoucher } = useIssueVoucher(); const methods = useForm({ defaultValues, resolver }); diff --git a/utils/gear-hooks/src/context/Api.tsx b/utils/gear-hooks/src/context/Api.tsx index ab548610b7..68fa846848 100644 --- a/utils/gear-hooks/src/context/Api.tsx +++ b/utils/gear-hooks/src/context/Api.tsx @@ -25,10 +25,12 @@ type Value = { | { api: undefined; isApiReady: false; + isV110Runtime: false; } | { api: GearApi; isApiReady: true; + isV110Runtime: boolean; } ); @@ -39,6 +41,7 @@ type Props = ProviderProps & { const initialValue = { api: undefined, isApiReady: false as const, + isV110Runtime: false as const, switchNetwork: () => Promise.resolve(), }; @@ -88,7 +91,9 @@ function ApiProvider({ initialArgs, children }: Props) { const value = useMemo( () => - api ? { api, isApiReady: true as const, switchNetwork } : { api, isApiReady: false as const, switchNetwork }, + api + ? { api, isApiReady: true as const, isV110Runtime: api.specVersion >= 1100, switchNetwork } + : { api, isApiReady: false as const, isV110Runtime: false as const, switchNetwork }, [api], ); From 258ff7cc04fca4e9ae43496c0dca523fba2de1ee Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Wed, 24 Jan 2024 22:01:53 +0300 Subject: [PATCH 11/34] Add useProgramVoucher and useAccountProgramVoucher hooks --- utils/gear-hooks/src/hooks/api/index.ts | 4 ++++ .../gear-hooks/src/hooks/api/useSendMessage.ts | 16 ++++++++-------- utils/gear-hooks/src/hooks/api/voucher/index.ts | 4 +++- .../src/hooks/api/voucher/use-voucher-id.ts | 6 ++++-- .../src/hooks/api/voucher/use-voucher.ts | 17 ++++++++++++++++- utils/gear-hooks/src/hooks/index.ts | 4 ++++ utils/gear-hooks/src/index.ts | 4 ++++ 7 files changed, 43 insertions(+), 12 deletions(-) diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index 3440da564c..14affeaa62 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -14,6 +14,8 @@ import { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, @@ -38,6 +40,8 @@ export { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, diff --git a/utils/gear-hooks/src/hooks/api/useSendMessage.ts b/utils/gear-hooks/src/hooks/api/useSendMessage.ts index 1f4ef5a1f2..0894be4f2b 100644 --- a/utils/gear-hooks/src/hooks/api/useSendMessage.ts +++ b/utils/gear-hooks/src/hooks/api/useSendMessage.ts @@ -9,7 +9,7 @@ import { AccountContext, AlertContext, ApiContext } from 'context'; import { DEFAULT_ERROR_OPTIONS, DEFAULT_SUCCESS_OPTIONS } from 'consts'; import { getExtrinsicFailedMessage } from 'utils'; import { useAccountDeriveBalancesAll, useBalanceFormat } from './balance'; -import { useAccountVoucherBalance } from './voucher'; +// import { useAccountVoucherBalance } from './voucher'; type UseSendMessageOptions = { disableAlerts?: boolean; @@ -38,7 +38,7 @@ function useSendMessage( const alert = useContext(AlertContext); const balances = useAccountDeriveBalancesAll(); - const { voucherBalance } = useAccountVoucherBalance(destination); + // const { voucherBalance } = useAccountVoucherBalance(destination); const { getChainBalanceValue } = useBalanceFormat(); const title = 'gear.sendMessage'; @@ -108,12 +108,12 @@ function useSendMessage( const isBalanceValid = (gasLimit: string, withVoucher: boolean) => { if (disableCheckBalance) return true; if (!isApiReady) throw new Error('API is not initialized'); - if (!balances || !voucherBalance) throw new Error('No balance for account'); + // if (!balances || !voucherBalance) throw new Error('No balance for account'); const existentialDeposit = api.existentialDeposit.toString(); - - const { freeBalance } = balances; - const balance = (withVoucher ? voucherBalance : freeBalance).toString(); + // + // const { freeBalance } = balances; + // const balance = (withVoucher ? voucherBalance : freeBalance).toString(); const valuePerGas = api.valuePerGas.toString(); const gasLimitValue = BigNumber(gasLimit).plus(gasLimit).multipliedBy(valuePerGas); @@ -121,7 +121,7 @@ function useSendMessage( const extraCost = getChainBalanceValue(5); const transactionCost = gasLimitValue.plus(existentialDeposit).plus(extraCost); - return BigNumber(balance).isGreaterThanOrEqualTo(transactionCost); + // return BigNumber(balance).isGreaterThanOrEqualTo(transactionCost); }; const sendMessage = async (args: SendMessageOptions) => { @@ -156,7 +156,7 @@ function useSendMessage( try { const sendExtrinsic = api.message.send(message, metadata); - const extrinsic = withVoucher ? api.voucher.call({ SendMessage: sendExtrinsic }) : sendExtrinsic; + const extrinsic = withVoucher ? api.voucher.call('0x00', { SendMessage: sendExtrinsic }) : sendExtrinsic; const callback = (result: ISubmittableResult) => handleStatus(result, alertId, onSuccess, onInBlock, onError); diff --git a/utils/gear-hooks/src/hooks/api/voucher/index.ts b/utils/gear-hooks/src/hooks/api/voucher/index.ts index d84bd809ba..51faf4f9b7 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/index.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/index.ts @@ -1,5 +1,5 @@ import { useIsVoucherExists, useIsAccountVoucherExists } from './use-is-voucher-exists'; -import { useVoucher, useAccountVoucher } from './use-voucher'; +import { useVoucher, useAccountVoucher, useProgramVoucher, useAccountProgramVoucher } from './use-voucher'; import { useVouchers, useAccountVouchers } from './use-vouchers'; import { useVoucherId, useAccountVoucherId } from './use-voucher-id'; import { useVoucherBalance, useAccountVoucherBalance } from './use-voucher-balance'; @@ -8,6 +8,8 @@ export { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts index 7d04d324b1..acbc5118d3 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts @@ -9,7 +9,7 @@ import { useVouchers } from './use-vouchers'; function useVoucherId(programId: HexString | undefined, accountAddress: string | undefined) { const { vouchers } = useVouchers(accountAddress); - const [voucherId, setVoucherId] = useState(); + const [voucherId, setVoucherId] = useState(); const isVoucherIdReady = voucherId !== undefined; const isVoucherExists = !!(isVoucherIdReady && voucherId); @@ -19,7 +19,9 @@ function useVoucherId(programId: HexString | undefined, accountAddress: string | if (!vouchers || !programId) return; - const [result] = getTypedEntries(vouchers).find(([, programIds]) => programIds.includes(programId)) || ['']; + const [result] = getTypedEntries(vouchers).find(([, programIds]) => programIds.includes(programId)) || [ + '' as HexString, + ]; setVoucherId(result); }, [vouchers, accountAddress, programId]); diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts index bccffe868c..08011fc887 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts @@ -3,6 +3,8 @@ import { useContext, useEffect, useState } from 'react'; import { AccountContext, AlertContext, ApiContext } from 'context'; +import { useVoucherId } from './use-voucher-id'; + function useVoucher(voucherId: HexString | undefined, accountAddress: string | undefined) { const { api } = useContext(ApiContext); const alert = useContext(AlertContext); @@ -30,4 +32,17 @@ function useAccountVoucher(voucherId: HexString | undefined) { return useVoucher(voucherId, account?.address); } -export { useVoucher, useAccountVoucher }; +function useProgramVoucher(programId: HexString | undefined, accountAddress: string | undefined) { + const { voucherId, isVoucherIdReady, isVoucherExists } = useVoucherId(programId, accountAddress); + const { voucher, isVoucherReady } = useVoucher(voucherId, accountAddress); + + return { voucher, isVoucherReady: isVoucherExists ? isVoucherReady : isVoucherIdReady, isVoucherExists }; +} + +function useAccountProgramVoucher(programId: HexString | undefined) { + const { account } = useContext(AccountContext); + + return useProgramVoucher(programId, account?.address); +} + +export { useVoucher, useAccountVoucher, useProgramVoucher, useAccountProgramVoucher }; diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 24ccbc3f15..7e662cc780 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -11,6 +11,8 @@ import { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, @@ -41,6 +43,8 @@ export { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index 91ffbb3bae..179955d66a 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -11,6 +11,8 @@ import { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, @@ -71,6 +73,8 @@ export { useIsVoucherExists, useVoucher, useIsAccountVoucherExists, + useProgramVoucher, + useAccountProgramVoucher, useAccountVoucher, useVouchers, useAccountVouchers, From 51dd7c5c63aa9d2ca9a8e7c34f99469fb8d218f8 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Thu, 25 Jan 2024 14:57:54 +0300 Subject: [PATCH 12/34] Fix send transactions with voucher --- .../use-voucher-checkbox.tsx | 6 ++-- .../src/hooks/useMessageActions/types.ts | 6 ++-- .../useMessageActions/useMessageActions.ts | 33 ++++++++++++++----- .../ui/labeled-checkbox/labeled-checkbox.tsx | 5 +-- .../src/widgets/messageForm/model/const.ts | 2 +- .../src/widgets/messageForm/model/types.ts | 4 ++- .../widgets/messageForm/ui/MessageForm.tsx | 6 ++-- .../hooks/api/voucher/use-voucher-balance.ts | 2 +- .../src/hooks/api/voucher/use-voucher.ts | 2 +- 9 files changed, 42 insertions(+), 24 deletions(-) diff --git a/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx b/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx index 40bcefc80a..9b38d35d25 100644 --- a/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx +++ b/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx @@ -10,15 +10,15 @@ type Props = { }; const UseVoucherCheckbox = ({ programId }: Props) => { - const { voucherBalance, isVoucherExists } = useAccountVoucherBalance(programId); + const { voucherBalance, isVoucherExists, voucherId } = useAccountVoucherBalance(programId); const { getFormattedBalance } = useBalanceFormat(); const formattedBalance = voucherBalance ? getFormattedBalance(voucherBalance) : undefined; return isVoucherExists ? ( - + - ( {formattedBalance?.value} {formattedBalance?.unit}) + ({formattedBalance?.value} {formattedBalance?.unit}) ) : null; diff --git a/idea/frontend/src/hooks/useMessageActions/types.ts b/idea/frontend/src/hooks/useMessageActions/types.ts index bb65ff30f6..8553671cda 100644 --- a/idea/frontend/src/hooks/useMessageActions/types.ts +++ b/idea/frontend/src/hooks/useMessageActions/types.ts @@ -1,4 +1,4 @@ -import { ProgramMetadata, MessageSendOptions, MessageSendReplyOptions } from '@gear-js/api'; +import { ProgramMetadata, MessageSendOptions, MessageSendReplyOptions, HexString } from '@gear-js/api'; import { SubmittableExtrinsic } from '@polkadot/api/types'; import { ISubmittableResult } from '@polkadot/types/types'; @@ -6,14 +6,14 @@ import { OperationCallbacks, ParamsToSignAndSend as CommonParamsToSignAndSend } type ParamsToSendMessage = OperationCallbacks & { message: MessageSendOptions; - withVoucher: boolean; + voucherId: HexString | false; metadata?: ProgramMetadata; payloadType?: string; }; type ParamsToReplyMessage = OperationCallbacks & { reply: MessageSendReplyOptions; - withVoucher: boolean; + voucherId: HexString | false; metadata?: ProgramMetadata; payloadType?: string; }; diff --git a/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts b/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts index 80e37838ae..604c3030db 100644 --- a/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts +++ b/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts @@ -1,9 +1,10 @@ -import { useCallback } from 'react'; -import { web3FromSource } from '@polkadot/extension-dapp'; -import { EventRecord } from '@polkadot/types/interfaces'; +import { HexString } from '@gear-js/api'; +import { useApi, useAlert, useAccount, DEFAULT_ERROR_OPTIONS, DEFAULT_SUCCESS_OPTIONS } from '@gear-js/react-hooks'; import { SubmittableExtrinsic } from '@polkadot/api/types'; import { ISubmittableResult } from '@polkadot/types/types'; -import { useApi, useAlert, useAccount, DEFAULT_ERROR_OPTIONS, DEFAULT_SUCCESS_OPTIONS } from '@gear-js/react-hooks'; +import { web3FromSource } from '@polkadot/extension-dapp'; +import { EventRecord } from '@polkadot/types/interfaces'; +import { useCallback } from 'react'; import { useModal } from '@/hooks'; import { Method } from '@/features/explorer'; @@ -14,7 +15,7 @@ import { PROGRAM_ERRORS, TransactionStatus, TransactionName } from '@/shared/con import { ParamsToSendMessage, ParamsToSignAndSend, ParamsToReplyMessage } from './types'; const useMessageActions = () => { - const { api, isApiReady } = useApi(); + const { api, isApiReady, isV110Runtime } = useApi(); const { account } = useAccount(); const alert = useAlert(); const { showModal } = useModal(); @@ -73,8 +74,20 @@ const useMessageActions = () => { } }; + const getVoucherExtrinsic = ( + id: HexString, + sendExtrinsic: SubmittableExtrinsic<'promise', ISubmittableResult>, + type: 'message' | 'reply' = 'message', + ) => { + if (!isApiReady) throw new Error('API is not initialized'); + + const args = type === 'message' ? { SendMessage: sendExtrinsic } : { SendReply: sendExtrinsic }; + + return isV110Runtime ? api.voucher.call(id, args) : api.voucher.callDeprecated(args); + }; + const sendMessage = useCallback( - async ({ metadata, message, payloadType, withVoucher, reject, resolve }: ParamsToSendMessage) => { + async ({ metadata, message, payloadType, voucherId, reject, resolve }: ParamsToSendMessage) => { try { if (!isApiReady) throw new Error('API is not initialized'); checkWallet(account); @@ -83,7 +96,9 @@ const useMessageActions = () => { const sendExtrinsic = api.message.send(message, metadata, payloadType); - const extrinsic = withVoucher ? api.voucher.call({ SendMessage: sendExtrinsic }) : sendExtrinsic; + console.log('voucherId: ', voucherId); + + const extrinsic = voucherId ? getVoucherExtrinsic(voucherId, sendExtrinsic) : sendExtrinsic; const { signer } = await web3FromSource(meta.source); const { partialFee } = await api.message.paymentInfo(address, { signer }); @@ -111,7 +126,7 @@ const useMessageActions = () => { ); const replyMessage = useCallback( - async ({ reply, metadata, payloadType, withVoucher, reject, resolve }: ParamsToReplyMessage) => { + async ({ reply, metadata, payloadType, voucherId, reject, resolve }: ParamsToReplyMessage) => { try { if (!isApiReady) throw new Error('API is not initialized'); checkWallet(account); @@ -120,7 +135,7 @@ const useMessageActions = () => { const replyExtrinsic = await api.message.sendReply(reply, metadata, payloadType); - const extrinsic = withVoucher ? api.voucher.call({ SendReply: replyExtrinsic }) : replyExtrinsic; + const extrinsic = voucherId ? getVoucherExtrinsic(voucherId, replyExtrinsic, 'reply') : replyExtrinsic; const { signer } = await web3FromSource(meta.source); const { partialFee } = await api.message.paymentInfo(address, { signer }); diff --git a/idea/frontend/src/shared/ui/labeled-checkbox/labeled-checkbox.tsx b/idea/frontend/src/shared/ui/labeled-checkbox/labeled-checkbox.tsx index 71510e6c3d..017bb05c13 100644 --- a/idea/frontend/src/shared/ui/labeled-checkbox/labeled-checkbox.tsx +++ b/idea/frontend/src/shared/ui/labeled-checkbox/labeled-checkbox.tsx @@ -8,18 +8,19 @@ type Props = { name: string; label: string; inputLabel: string; + value?: string; direction?: InputProps['direction']; gap?: InputProps['gap']; children?: ReactNode; }; -const LabeledCheckbox = ({ name, label, inputLabel, direction = 'x', gap, children }: Props) => { +const LabeledCheckbox = ({ name, label, inputLabel, value, direction = 'x', gap, children }: Props) => { const { register } = useFormContext(); return (
- + {children}
diff --git a/idea/frontend/src/widgets/messageForm/model/const.ts b/idea/frontend/src/widgets/messageForm/model/const.ts index 439a6de73e..62cf0ad0ac 100644 --- a/idea/frontend/src/widgets/messageForm/model/const.ts +++ b/idea/frontend/src/widgets/messageForm/model/const.ts @@ -5,7 +5,7 @@ const INITIAL_VALUES: FormValues = { payload: '0x', gasLimit: '0', payloadType: 'Bytes', - withVoucher: false, + voucherId: false, keepAlive: true, }; diff --git a/idea/frontend/src/widgets/messageForm/model/types.ts b/idea/frontend/src/widgets/messageForm/model/types.ts index 480bad04c5..b8c5ab5ea4 100644 --- a/idea/frontend/src/widgets/messageForm/model/types.ts +++ b/idea/frontend/src/widgets/messageForm/model/types.ts @@ -1,3 +1,5 @@ +import { HexString } from '@gear-js/api'; + import { PayloadValue } from '@/entities/formPayload'; type FormValues = { @@ -5,7 +7,7 @@ type FormValues = { gasLimit: string; payload: PayloadValue; payloadType: string; - withVoucher: boolean; + voucherId: HexString | false; keepAlive: boolean; }; diff --git a/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx b/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx index 37c9935a0b..980d63348c 100644 --- a/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx +++ b/idea/frontend/src/widgets/messageForm/ui/MessageForm.tsx @@ -72,7 +72,7 @@ const MessageForm = ({ id, programId, isReply, metadata, isLoading }: Props) => disableSubmitButton(); const payloadType = metadata ? undefined : values.payloadType; - const { withVoucher, keepAlive } = values; + const { voucherId, keepAlive } = values; const baseValues = { value: getChainBalanceValue(values.value).toFixed(), @@ -83,10 +83,10 @@ const MessageForm = ({ id, programId, isReply, metadata, isLoading }: Props) => if (isReply) { const reply = { ...baseValues, replyToId: id }; - replyMessage({ reply, metadata, payloadType, withVoucher, reject: enableSubmitButton, resolve: resetForm }); + replyMessage({ reply, metadata, payloadType, voucherId, reject: enableSubmitButton, resolve: resetForm }); } else { const message = { ...baseValues, destination: id }; - sendMessage({ message, metadata, payloadType, withVoucher, reject: enableSubmitButton, resolve: resetForm }); + sendMessage({ message, metadata, payloadType, voucherId, reject: enableSubmitButton, resolve: resetForm }); } }; diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts index 087fd1f52c..28fd2870f9 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts @@ -13,7 +13,7 @@ function useVoucherBalance(programId: HexString | undefined, accountAddress: str const voucherBalance = balance; const isVoucherBalanceReady = isVoucherExists ? isBalanceReady : isVoucherIdReady; - return { voucherBalance, isVoucherBalanceReady, isVoucherExists }; + return { voucherBalance, isVoucherBalanceReady, voucherId, isVoucherExists }; } function useAccountVoucherBalance(programId: HexString | undefined) { diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts index 08011fc887..bdc08daf52 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts @@ -36,7 +36,7 @@ function useProgramVoucher(programId: HexString | undefined, accountAddress: str const { voucherId, isVoucherIdReady, isVoucherExists } = useVoucherId(programId, accountAddress); const { voucher, isVoucherReady } = useVoucher(voucherId, accountAddress); - return { voucher, isVoucherReady: isVoucherExists ? isVoucherReady : isVoucherIdReady, isVoucherExists }; + return { voucher, isVoucherReady: isVoucherExists ? isVoucherReady : isVoucherIdReady, voucherId, isVoucherExists }; } function useAccountProgramVoucher(programId: HexString | undefined) { From 541866a0c8e0780c701d5655652369cc681aae7f Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Thu, 25 Jan 2024 15:34:01 +0300 Subject: [PATCH 13/34] Fix voucher reset on account change --- .../ui/use-voucher-checkbox/use-voucher-checkbox.tsx | 11 ++++++++++- .../src/hooks/useMessageActions/useMessageActions.ts | 2 -- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx b/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx index 9b38d35d25..a2f9b66e3d 100644 --- a/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx +++ b/idea/frontend/src/features/voucher/ui/use-voucher-checkbox/use-voucher-checkbox.tsx @@ -1,5 +1,7 @@ import { HexString } from '@gear-js/api'; import { useAccountVoucherBalance, useBalanceFormat } from '@gear-js/react-hooks'; +import { useEffect } from 'react'; +import { useFormContext } from 'react-hook-form'; import { LabeledCheckbox } from '@/shared/ui'; @@ -15,8 +17,15 @@ const UseVoucherCheckbox = ({ programId }: Props) => { const formattedBalance = voucherBalance ? getFormattedBalance(voucherBalance) : undefined; + const name = 'voucherId'; + const { setValue } = useFormContext(); + + useEffect(() => { + setValue(name, false); + }, [isVoucherExists, setValue]); + return isVoucherExists ? ( - + ({formattedBalance?.value} {formattedBalance?.unit}) diff --git a/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts b/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts index 604c3030db..b66ef7ae6d 100644 --- a/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts +++ b/idea/frontend/src/hooks/useMessageActions/useMessageActions.ts @@ -96,8 +96,6 @@ const useMessageActions = () => { const sendExtrinsic = api.message.send(message, metadata, payloadType); - console.log('voucherId: ', voucherId); - const extrinsic = voucherId ? getVoucherExtrinsic(voucherId, sendExtrinsic) : sendExtrinsic; const { signer } = await web3FromSource(meta.source); From 716f034c8e557cbd5897844a141f4f1495dfdbc9 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Thu, 25 Jan 2024 16:11:19 +0300 Subject: [PATCH 14/34] Bump api --- idea/frontend/package.json | 2 +- utils/gear-hooks/package.json | 4 ++-- .../src/hooks/api/voucher/use-voucher-id.ts | 22 +++++-------------- .../src/hooks/api/voucher/use-vouchers.ts | 14 ++++++------ yarn.lock | 14 ++++++------ 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/idea/frontend/package.json b/idea/frontend/package.json index 23bb7b7157..77bfc8943c 100644 --- a/idea/frontend/package.json +++ b/idea/frontend/package.json @@ -9,7 +9,7 @@ "preview": "vite preview" }, "dependencies": { - "@gear-js/api": "0.36.1", + "@gear-js/api": "0.36.2", "@gear-js/react-hooks": "workspace:^", "@gear-js/ui": "workspace:^", "@hcaptcha/react-hcaptcha": "1.8.1", diff --git a/utils/gear-hooks/package.json b/utils/gear-hooks/package.json index fce1e9b92c..9793b112e4 100644 --- a/utils/gear-hooks/package.json +++ b/utils/gear-hooks/package.json @@ -21,7 +21,7 @@ "prepare": "yarn build" }, "peerDependencies": { - "@gear-js/api": "0.36.1", + "@gear-js/api": "0.36.2", "@polkadot/api": "10.11.2", "@polkadot/extension-dapp": "0.46.5", "react": "18.2.0", @@ -37,7 +37,7 @@ "react-transition-group": "4.4.5" }, "devDependencies": { - "@gear-js/api": "0.36.1", + "@gear-js/api": "0.36.2", "@polkadot/api": "10.11.2", "@polkadot/extension-dapp": "0.46.5", "@polkadot/types": "10.11.2", diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts index acbc5118d3..644b79ab2c 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-voucher-id.ts @@ -1,31 +1,19 @@ import { HexString } from '@gear-js/api'; -import { useContext, useEffect, useState } from 'react'; +import { useContext } from 'react'; import { AccountContext } from 'context'; -import { getTypedEntries } from 'utils'; import { useVouchers } from './use-vouchers'; function useVoucherId(programId: HexString | undefined, accountAddress: string | undefined) { - const { vouchers } = useVouchers(accountAddress); + const { vouchers, isEachVoucherReady } = useVouchers(accountAddress, programId); - const [voucherId, setVoucherId] = useState(); + const ids = Object.keys(vouchers || {}); + const voucherId = ids[0] as HexString | undefined; - const isVoucherIdReady = voucherId !== undefined; + const isVoucherIdReady = isEachVoucherReady; const isVoucherExists = !!(isVoucherIdReady && voucherId); - useEffect(() => { - setVoucherId(undefined); - - if (!vouchers || !programId) return; - - const [result] = getTypedEntries(vouchers).find(([, programIds]) => programIds.includes(programId)) || [ - '' as HexString, - ]; - - setVoucherId(result); - }, [vouchers, accountAddress, programId]); - return { voucherId, isVoucherIdReady, isVoucherExists }; } diff --git a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts index 6412fac86d..88542740ed 100644 --- a/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts +++ b/utils/gear-hooks/src/hooks/api/voucher/use-vouchers.ts @@ -1,13 +1,13 @@ -import { HexString } from '@gear-js/api'; +import { HexString, IVoucherDetails } from '@gear-js/api'; import { useState, useEffect, useContext } from 'react'; import { AccountContext, AlertContext, ApiContext } from 'context'; -function useVouchers(accountAddress: string | undefined) { +function useVouchers(accountAddress: string | undefined, programId?: HexString | undefined) { const { api } = useContext(ApiContext); const alert = useContext(AlertContext); - const [vouchers, setVouchers] = useState>(); + const [vouchers, setVouchers] = useState>(); const isEachVoucherReady = vouchers !== undefined; useEffect(() => { @@ -16,20 +16,20 @@ function useVouchers(accountAddress: string | undefined) { if (!api || !accountAddress) return; api.voucher - .getAllForAccount(accountAddress) + .getAllForAccount(accountAddress, programId) .then((result) => setVouchers(result)) .catch(({ message }) => alert.error(message)); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [api, accountAddress]); + }, [api, accountAddress, programId]); return { vouchers, isEachVoucherReady }; } -function useAccountVouchers() { +function useAccountVouchers(programId?: HexString | undefined) { const { account } = useContext(AccountContext); - return useVouchers(account?.address); + return useVouchers(account?.address, programId); } export { useVouchers, useAccountVouchers }; diff --git a/yarn.lock b/yarn.lock index f371bad611..94cd07d8d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2324,14 +2324,14 @@ __metadata: languageName: node linkType: hard -"@gear-js/api@npm:0.36.1": - version: 0.36.1 - resolution: "@gear-js/api@npm:0.36.1" +"@gear-js/api@npm:0.36.2": + version: 0.36.2 + resolution: "@gear-js/api@npm:0.36.2" peerDependencies: "@polkadot/api": 10.11.2 "@polkadot/wasm-crypto": 7.3.2 rxjs: 7.8.1 - checksum: c1a28dea074ae3883ba4e05a1130286e59cba6f1677a1f39a2b9d10fe86eee41ca339338f4ec772c7dcb21ad86cf9dfbc235bf8e04fb1f13cb72c9767f12b316 + checksum: fcb73edcae08e33b95bc8e8701379529125180836d52af5e8bded11546a0eadb6e50e66fc9a5305fa1c9e3a63f9de8852b456bcc0fcf40596571528b680e3de1 languageName: node linkType: hard @@ -2350,7 +2350,7 @@ __metadata: version: 0.0.0-use.local resolution: "@gear-js/frontend@workspace:idea/frontend" dependencies: - "@gear-js/api": 0.36.1 + "@gear-js/api": 0.36.2 "@gear-js/react-hooks": "workspace:^" "@gear-js/ui": "workspace:^" "@hcaptcha/react-hcaptcha": 1.8.1 @@ -2451,7 +2451,7 @@ __metadata: version: 0.0.0-use.local resolution: "@gear-js/react-hooks@workspace:utils/gear-hooks" dependencies: - "@gear-js/api": 0.36.1 + "@gear-js/api": 0.36.2 "@polkadot/api": 10.11.2 "@polkadot/api-derive": 10.11.2 "@polkadot/extension-dapp": 0.46.5 @@ -2475,7 +2475,7 @@ __metadata: rollup-plugin-typescript2: 0.36.0 typescript: 5.2.2 peerDependencies: - "@gear-js/api": 0.36.1 + "@gear-js/api": 0.36.2 "@polkadot/api": 10.11.2 "@polkadot/extension-dapp": 0.46.5 react: 18.2.0 From 2a7db803a4ac3cee8c601640d66aa0c5ddaae124 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Thu, 25 Jan 2024 19:37:10 +0300 Subject: [PATCH 15/34] Add issue voucher duration --- idea/frontend/package.json | 3 +- .../transfer-balance-modal.tsx | 4 +- idea/frontend/src/features/voucher/consts.ts | 18 +++++ .../src/features/voucher/hooks/index.ts | 4 +- .../voucher/hooks/use-balance-schema.ts | 28 ++++++++ .../voucher/hooks/use-duration-schema.ts | 20 ++++++ .../voucher/hooks/use-issue-voucher.ts | 9 ++- .../issue-voucher-modal.tsx | 72 +++++++++---------- idea/frontend/src/features/voucher/utils.ts | 0 idea/frontend/src/shared/helpers/form.ts | 3 +- .../src/shared/ui/form/Form.module.scss | 6 -- .../frontend/src/shared/ui/form/form/form.tsx | 43 +++++++++++ .../frontend/src/shared/ui/form/form/index.ts | 3 + .../shared/ui/form/formInput/FormInput.tsx | 26 ------- .../src/shared/ui/form/formInput/index.ts | 3 - idea/frontend/src/shared/ui/form/index.ts | 7 +- .../src/shared/ui/form/input/index.ts | 3 + .../src/shared/ui/form/input/input.tsx | 18 +++++ idea/frontend/src/shared/ui/index.ts | 3 +- .../widgets/programForm/ui/ProgramForm.tsx | 10 ++- yarn.lock | 8 +++ 21 files changed, 200 insertions(+), 91 deletions(-) create mode 100644 idea/frontend/src/features/voucher/consts.ts create mode 100644 idea/frontend/src/features/voucher/hooks/use-balance-schema.ts create mode 100644 idea/frontend/src/features/voucher/hooks/use-duration-schema.ts create mode 100644 idea/frontend/src/features/voucher/utils.ts create mode 100644 idea/frontend/src/shared/ui/form/form/form.tsx create mode 100644 idea/frontend/src/shared/ui/form/form/index.ts delete mode 100644 idea/frontend/src/shared/ui/form/formInput/FormInput.tsx delete mode 100644 idea/frontend/src/shared/ui/form/formInput/index.ts create mode 100644 idea/frontend/src/shared/ui/form/input/index.ts create mode 100644 idea/frontend/src/shared/ui/form/input/input.tsx diff --git a/idea/frontend/package.json b/idea/frontend/package.json index 77bfc8943c..2ae2447acf 100644 --- a/idea/frontend/package.json +++ b/idea/frontend/package.json @@ -38,7 +38,8 @@ "react-router-dom": "6.16.0", "react-transition-group": "4.4.5", "simplebar-react": "3.2.4", - "yup": "1.3.2" + "yup": "1.3.2", + "zod": "3.22.4" }, "devDependencies": { "@types/lodash.isequal": "4.5.6", diff --git a/idea/frontend/src/features/balance/ui/transfer-balance-modal/transfer-balance-modal.tsx b/idea/frontend/src/features/balance/ui/transfer-balance-modal/transfer-balance-modal.tsx index 1cb9f91b92..c90df4a1b6 100644 --- a/idea/frontend/src/features/balance/ui/transfer-balance-modal/transfer-balance-modal.tsx +++ b/idea/frontend/src/features/balance/ui/transfer-balance-modal/transfer-balance-modal.tsx @@ -6,7 +6,7 @@ import { FormProvider, useForm } from 'react-hook-form'; import * as yup from 'yup'; import CloseSVG from '@/shared/assets/images/actions/close.svg?react'; -import { FormInput, ValueField } from '@/shared/ui/form'; +import { Input, ValueField } from '@/shared/ui/form'; import { isAccountAddressValid } from '@/shared/helpers'; import { useBalanceTransfer } from '@/hooks'; @@ -53,7 +53,7 @@ const TransferBalanceModal = ({ close }: Props) => {
- +
diff --git a/idea/frontend/src/features/voucher/consts.ts b/idea/frontend/src/features/voucher/consts.ts new file mode 100644 index 0000000000..4ce7447cb9 --- /dev/null +++ b/idea/frontend/src/features/voucher/consts.ts @@ -0,0 +1,18 @@ +import { isAccountAddressValid } from '@/shared/helpers'; +import { decodeAddress } from '@gear-js/api'; +import { z } from 'zod'; + +const DEFAULT_VALUES = { + address: '', + value: '', + duration: '', +}; + +const ADDRESS_SCHEMA = z + .string() + .trim() + .min(0) + .refine((value) => isAccountAddressValid(value), 'Invalid address') + .transform((value) => decodeAddress(value)); + +export { DEFAULT_VALUES, ADDRESS_SCHEMA }; diff --git a/idea/frontend/src/features/voucher/hooks/index.ts b/idea/frontend/src/features/voucher/hooks/index.ts index fbea87c67a..9a80382874 100644 --- a/idea/frontend/src/features/voucher/hooks/index.ts +++ b/idea/frontend/src/features/voucher/hooks/index.ts @@ -1,3 +1,5 @@ import { useIssueVoucher } from './use-issue-voucher'; +import { useBalanceSchema } from './use-balance-schema'; +import { useDurationSchema } from './use-duration-schema'; -export { useIssueVoucher }; +export { useIssueVoucher, useBalanceSchema, useDurationSchema }; diff --git a/idea/frontend/src/features/voucher/hooks/use-balance-schema.ts b/idea/frontend/src/features/voucher/hooks/use-balance-schema.ts new file mode 100644 index 0000000000..956a6dc65e --- /dev/null +++ b/idea/frontend/src/features/voucher/hooks/use-balance-schema.ts @@ -0,0 +1,28 @@ +import { useApi, useBalanceFormat } from '@gear-js/react-hooks'; +import { z } from 'zod'; + +function useBalanceSchema() { + const { api } = useApi(); + const { getChainBalanceValue, getFormattedBalanceValue } = useBalanceFormat(); + + const getBalanceSchema = () => { + if (!api) throw new Error('API is not initialized'); + + const decimals = api.registry.chainDecimals.toString(); + const existentialDeposit = api.existentialDeposit.toString(); + + const minValueMessage = `Minimum value is ${getFormattedBalanceValue(existentialDeposit).toFixed()}`; + const integerMessage = `Maximum amount of decimal places is ${decimals}`; + + return z + .string() + .transform((value) => getChainBalanceValue(value)) + .refine((value) => value.isGreaterThanOrEqualTo(existentialDeposit), minValueMessage) + .refine((value) => value.isInteger(), integerMessage) + .transform((value) => value.toFixed()); + }; + + return getBalanceSchema(); +} + +export { useBalanceSchema }; diff --git a/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts b/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts new file mode 100644 index 0000000000..3dea97108c --- /dev/null +++ b/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts @@ -0,0 +1,20 @@ +import { useApi } from '@gear-js/react-hooks'; +import { z } from 'zod'; + +function useDurationSchema() { + const { api, isV110Runtime } = useApi(); + + const getDurationSchema = () => { + if (!api) throw new Error('API is not initialized'); + + const { minDuration } = api.voucher; + + return z.coerce.number().min(minDuration); + }; + + const getDurationPlaceholderSchema = () => z.coerce.number().transform(() => 0); + + return isV110Runtime ? getDurationSchema() : getDurationPlaceholderSchema(); +} + +export { useDurationSchema }; diff --git a/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts b/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts index dfe7229893..67c1c4ddf3 100644 --- a/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts +++ b/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts @@ -35,11 +35,16 @@ function useIssueVoucher() { if (status.isInvalid) alert.error(PROGRAM_ERRORS.INVALID_TRANSACTION); }; - const issueVoucher = async (address: HexString, programId: HexString, value: string, onSuccess: () => void) => { + const issueVoucher = async ( + address: HexString, + programId: HexString, + value: string, + duration: number, + onSuccess: () => void, + ) => { if (!isApiReady || !account) return; try { - const duration = undefined; const programs = [programId]; const isValidForCode = false; diff --git a/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx b/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx index b03ed9f533..cc6947e5df 100644 --- a/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx +++ b/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx @@ -1,16 +1,14 @@ +import { useApi } from '@gear-js/react-hooks'; import { Button, Modal } from '@gear-js/ui'; -import { HexString, decodeAddress } from '@gear-js/api'; -import { useBalanceFormat } from '@gear-js/react-hooks'; -import { yupResolver } from '@hookform/resolvers/yup'; -import { FormProvider, useForm } from 'react-hook-form'; -import * as yup from 'yup'; +import { HexString } from '@gear-js/api'; +import { z } from 'zod'; import ApplySVG from '@/shared/assets/images/actions/apply.svg?react'; import CloseSVG from '@/shared/assets/images/actions/close.svg?react'; -import { isAccountAddressValid } from '@/shared/helpers'; -import { FormInput, ValueField } from '@/shared/ui/form'; +import { Form, Input, ValueField } from '@/shared/ui'; -import { useIssueVoucher } from '../../hooks'; +import { ADDRESS_SCHEMA, DEFAULT_VALUES } from '../../consts'; +import { useBalanceSchema, useDurationSchema, useIssueVoucher } from '../../hooks'; import styles from './issue-voucher-modal.module.scss'; type Props = { @@ -18,44 +16,42 @@ type Props = { close: () => void; }; -const defaultValues = { address: '', value: '' }; - -const schema = yup.object().shape({ - address: yup - .string() - .test('is-address-valid', 'Invalid address', isAccountAddressValid) - .required('This field is required'), - value: yup.string().required('This field is required'), -}); - -const resolver = yupResolver(schema); - const IssueVoucherModal = ({ programId, close }: Props) => { - const { getChainBalanceValue } = useBalanceFormat(); + const { isV110Runtime } = useApi(); const { issueVoucher } = useIssueVoucher(); - const methods = useForm({ defaultValues, resolver }); + const balanceSchema = useBalanceSchema(); + const durationSchema = useDurationSchema(); + + const schema = z.object({ + address: ADDRESS_SCHEMA, + value: balanceSchema, + duration: durationSchema, + }); - const handleSubmit = ({ address, value }: typeof defaultValues) => { - const decodedAddress = decodeAddress(address); - const chainValue = getChainBalanceValue(value).toFixed(); + type Values = typeof DEFAULT_VALUES; + type Schema = z.infer; - issueVoucher(decodedAddress, programId, chainValue, close); - }; + const handleSubmit = ({ address, value, duration }: Schema) => + issueVoucher(address, programId, value, duration, close); return ( - - - - - -
-
- -
+ + defaultValues={DEFAULT_VALUES} + schema={schema} + onSubmit={handleSubmit} + className={styles.form}> + + + + {isV110Runtime && } + +
+
+
); }; diff --git a/idea/frontend/src/features/voucher/utils.ts b/idea/frontend/src/features/voucher/utils.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/idea/frontend/src/shared/helpers/form.ts b/idea/frontend/src/shared/helpers/form.ts index 5702113e68..c0af093d08 100644 --- a/idea/frontend/src/shared/helpers/form.ts +++ b/idea/frontend/src/shared/helpers/form.ts @@ -10,12 +10,11 @@ const isDecimal = (value: string) => { return decimalRegex.test(value); }; -// yup const isAccountAddressValid = (value = '') => { try { decodeAddress(value); return true; - } catch (error) { + } catch { return false; } }; diff --git a/idea/frontend/src/shared/ui/form/Form.module.scss b/idea/frontend/src/shared/ui/form/Form.module.scss index eca9e230d8..9255f54f80 100644 --- a/idea/frontend/src/shared/ui/form/Form.module.scss +++ b/idea/frontend/src/shared/ui/form/Form.module.scss @@ -16,12 +16,6 @@ } } -.field { - label { - @extend %labelStyles; - } -} - .text { @extend %labelStyles; diff --git a/idea/frontend/src/shared/ui/form/form/form.tsx b/idea/frontend/src/shared/ui/form/form/form.tsx new file mode 100644 index 0000000000..6a9ad5bfef --- /dev/null +++ b/idea/frontend/src/shared/ui/form/form/form.tsx @@ -0,0 +1,43 @@ +import { zodResolver } from '@hookform/resolvers/zod'; +import { ReactNode } from 'react'; +import { DefaultValues, FieldValues, FormProvider, SubmitHandler, useForm } from 'react-hook-form'; +import { ZodType } from 'zod'; + +type FormProps = { + children: ReactNode; + className?: string; + onSubmit: TTransformedValues extends FieldValues ? SubmitHandler : SubmitHandler; +}; + +type UseFormProps = { + defaultValues: DefaultValues; + schema?: ZodType; +}; + +type Props< + TFieldValues extends FieldValues, + TTransformedValues extends FieldValues | undefined = undefined, +> = FormProps & UseFormProps; + +const Form = ({ + defaultValues, + schema, + children, + className, + onSubmit, +}: Props) => { + const resolver = schema ? zodResolver(schema) : undefined; + + const methods = useForm({ defaultValues, resolver }); + const { handleSubmit } = methods; + + return ( + +
+ {children} +
+
+ ); +}; + +export { Form }; diff --git a/idea/frontend/src/shared/ui/form/form/index.ts b/idea/frontend/src/shared/ui/form/form/index.ts new file mode 100644 index 0000000000..2a2f8a9159 --- /dev/null +++ b/idea/frontend/src/shared/ui/form/form/index.ts @@ -0,0 +1,3 @@ +import { Form } from './form'; + +export { Form }; diff --git a/idea/frontend/src/shared/ui/form/formInput/FormInput.tsx b/idea/frontend/src/shared/ui/form/formInput/FormInput.tsx deleted file mode 100644 index 282b0246f3..0000000000 --- a/idea/frontend/src/shared/ui/form/formInput/FormInput.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { Input, InputProps } from '@gear-js/ui'; -import clsx from 'clsx'; -import { useFormContext } from 'react-hook-form'; - -import styles from '../Form.module.scss'; - -type Props = InputProps & { - name: string; -}; - -const FormInput = ({ name, label, className, ...other }: Props) => { - const { register, getFieldState, formState } = useFormContext(); - const { error } = getFieldState(name, formState); - - return ( - - ); -}; - -export { FormInput }; diff --git a/idea/frontend/src/shared/ui/form/formInput/index.ts b/idea/frontend/src/shared/ui/form/formInput/index.ts deleted file mode 100644 index c063d5f037..0000000000 --- a/idea/frontend/src/shared/ui/form/formInput/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { FormInput } from './FormInput'; - -export { FormInput }; diff --git a/idea/frontend/src/shared/ui/form/index.ts b/idea/frontend/src/shared/ui/form/index.ts index 2d52f69e62..c0ce8edbc5 100644 --- a/idea/frontend/src/shared/ui/form/index.ts +++ b/idea/frontend/src/shared/ui/form/index.ts @@ -1,5 +1,6 @@ import formStyles from './Form.module.scss'; -import { FormInput } from './formInput'; -import { ValueField } from './value-field/ValueField'; +import { Input } from './input'; +import { ValueField } from './value-field'; +import { Form } from './form'; -export { formStyles, FormInput, ValueField }; +export { formStyles, Input, ValueField, Form }; diff --git a/idea/frontend/src/shared/ui/form/input/index.ts b/idea/frontend/src/shared/ui/form/input/index.ts new file mode 100644 index 0000000000..ab2fea2c11 --- /dev/null +++ b/idea/frontend/src/shared/ui/form/input/index.ts @@ -0,0 +1,3 @@ +import { Input } from './input'; + +export { Input }; diff --git a/idea/frontend/src/shared/ui/form/input/input.tsx b/idea/frontend/src/shared/ui/form/input/input.tsx new file mode 100644 index 0000000000..b5ef854eed --- /dev/null +++ b/idea/frontend/src/shared/ui/form/input/input.tsx @@ -0,0 +1,18 @@ +import { Input as GearInput, InputProps } from '@gear-js/ui'; +import { useFormContext } from 'react-hook-form'; + +// TODO: omit onChange and onBlur, direction types start to freak out +type Props = InputProps & { + name: string; +}; + +const Input = ({ name, ...props }: Props) => { + const { register, formState } = useFormContext(); + const { errors } = formState; + + const error = errors[name]?.message?.toString(); + + return ; +}; + +export { Input }; diff --git a/idea/frontend/src/shared/ui/index.ts b/idea/frontend/src/shared/ui/index.ts index a0b13b3ed9..d1a8f64934 100644 --- a/idea/frontend/src/shared/ui/index.ts +++ b/idea/frontend/src/shared/ui/index.ts @@ -1,4 +1,5 @@ import { withAccount } from './with-account'; import { LabeledCheckbox } from './labeled-checkbox'; +import { Form, Input, ValueField } from './form'; -export { LabeledCheckbox, withAccount }; +export { LabeledCheckbox, withAccount, Form, Input, ValueField }; diff --git a/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx b/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx index 58f8eb2fed..488368888b 100644 --- a/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx +++ b/idea/frontend/src/widgets/programForm/ui/ProgramForm.tsx @@ -1,6 +1,5 @@ import { ProgramMetadata } from '@gear-js/api'; import { useBalanceFormat } from '@gear-js/react-hooks'; -import { Input } from '@gear-js/ui'; import { HexString } from '@polkadot/util/types'; import { yupResolver } from '@hookform/resolvers/yup'; import { useState, useMemo, ReactNode } from 'react'; @@ -12,8 +11,7 @@ import { Payload } from '@/hooks/useProgramActions/types'; import { FormPayload, getSubmitPayload, getPayloadFormValues, getResetPayloadValue } from '@/features/formPayload'; import { GasField } from '@/features/gasField'; import { GasMethod } from '@/shared/config'; -import { FormInput, ValueField } from '@/shared/ui/form'; -import { LabeledCheckbox } from '@/shared/ui'; +import { Input, ValueField, LabeledCheckbox } from '@/shared/ui'; import { INITIAL_VALUES, FormValues, RenderButtonsProps, SubmitHelpers } from '../model'; import styles from './ProgramForm.module.scss'; @@ -39,7 +37,7 @@ const ProgramForm = (props: Props) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const methods = useForm({ defaultValues, resolver: yupResolver(schema) }); - const { register, getValues, setValue, reset } = methods; + const { getValues, setValue, reset } = methods; const [gasInfo, setGasinfo] = useState(); const [isDisabled, setIsDisables] = useState(false); @@ -113,11 +111,11 @@ const ProgramForm = (props: Props) => {
- + - {!metadata && } + {!metadata && } diff --git a/yarn.lock b/yarn.lock index 94cd07d8d7..71c0d13ae7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2405,6 +2405,7 @@ __metadata: vite-plugin-node-polyfills: 0.15.0 vite-plugin-svgr: 4.1.0 yup: 1.3.2 + zod: 3.22.4 languageName: unknown linkType: soft @@ -17261,3 +17262,10 @@ __metadata: checksum: 08150c6e05b944b128dd27d423757d21a2b5dd3d000abd431c7f22349101d373b2ed5e5f66b5b308fe0579cd67ee788ed6935f810842d8c0fa46b0f7a1be0023 languageName: node linkType: hard + +"zod@npm:3.22.4": + version: 3.22.4 + resolution: "zod@npm:3.22.4" + checksum: 80bfd7f8039b24fddeb0718a2ec7c02aa9856e4838d6aa4864335a047b6b37a3273b191ef335bf0b2002e5c514ef261ffcda5a589fb084a48c336ffc4cdbab7f + languageName: node + linkType: hard From 43261ac3fac707f26040d1b85a49f996fa9eaef0 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Fri, 26 Jan 2024 00:51:53 +0300 Subject: [PATCH 16/34] Add allow code uploading checkbox --- idea/frontend/src/features/voucher/consts.ts | 1 + .../features/voucher/hooks/use-duration-schema.ts | 2 +- .../features/voucher/hooks/use-issue-voucher.ts | 4 ++-- .../ui/issue-voucher-modal/issue-voucher-modal.tsx | 14 ++++++++++---- .../src/shared/ui/form/checkbox/checkbox.tsx | 14 ++++++++++++++ idea/frontend/src/shared/ui/form/checkbox/index.ts | 3 +++ idea/frontend/src/shared/ui/form/index.ts | 3 ++- idea/frontend/src/shared/ui/index.ts | 4 ++-- 8 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 idea/frontend/src/shared/ui/form/checkbox/checkbox.tsx create mode 100644 idea/frontend/src/shared/ui/form/checkbox/index.ts diff --git a/idea/frontend/src/features/voucher/consts.ts b/idea/frontend/src/features/voucher/consts.ts index 4ce7447cb9..cf7e952829 100644 --- a/idea/frontend/src/features/voucher/consts.ts +++ b/idea/frontend/src/features/voucher/consts.ts @@ -6,6 +6,7 @@ const DEFAULT_VALUES = { address: '', value: '', duration: '', + isCodeUploadEnabled: false, }; const ADDRESS_SCHEMA = z diff --git a/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts b/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts index 3dea97108c..750bd60645 100644 --- a/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts +++ b/idea/frontend/src/features/voucher/hooks/use-duration-schema.ts @@ -9,7 +9,7 @@ function useDurationSchema() { const { minDuration } = api.voucher; - return z.coerce.number().min(minDuration); + return z.coerce.number().min(minDuration).int(); }; const getDurationPlaceholderSchema = () => z.coerce.number().transform(() => 0); diff --git a/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts b/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts index 67c1c4ddf3..1f6944b4c3 100644 --- a/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts +++ b/idea/frontend/src/features/voucher/hooks/use-issue-voucher.ts @@ -40,16 +40,16 @@ function useIssueVoucher() { programId: HexString, value: string, duration: number, + isCodeUploadEnabled: boolean, onSuccess: () => void, ) => { if (!isApiReady || !account) return; try { const programs = [programId]; - const isValidForCode = false; const { extrinsic } = isV110Runtime - ? await api.voucher.issue(address, value, duration, programs, isValidForCode) + ? await api.voucher.issue(address, value, duration, programs, isCodeUploadEnabled) : api.voucher.issueDeprecated(address, programId, value); const { signer } = await web3FromSource(account.meta.source); diff --git a/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx b/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx index cc6947e5df..68e83de77c 100644 --- a/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx +++ b/idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal.tsx @@ -5,7 +5,7 @@ import { z } from 'zod'; import ApplySVG from '@/shared/assets/images/actions/apply.svg?react'; import CloseSVG from '@/shared/assets/images/actions/close.svg?react'; -import { Form, Input, ValueField } from '@/shared/ui'; +import { Form, Input, ValueField, Checkbox } from '@/shared/ui'; import { ADDRESS_SCHEMA, DEFAULT_VALUES } from '../../consts'; import { useBalanceSchema, useDurationSchema, useIssueVoucher } from '../../hooks'; @@ -27,13 +27,14 @@ const IssueVoucherModal = ({ programId, close }: Props) => { address: ADDRESS_SCHEMA, value: balanceSchema, duration: durationSchema, + isCodeUploadEnabled: z.boolean(), }); type Values = typeof DEFAULT_VALUES; type Schema = z.infer; - const handleSubmit = ({ address, value, duration }: Schema) => - issueVoucher(address, programId, value, duration, close); + const handleSubmit = ({ address, value, duration, isCodeUploadEnabled }: Schema) => + issueVoucher(address, programId, value, duration, isCodeUploadEnabled, close); return ( @@ -45,7 +46,12 @@ const IssueVoucherModal = ({ programId, close }: Props) => { - {isV110Runtime && } + {isV110Runtime && ( + <> + + + + )}