@ritbit/v4-client-js 1.3.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +1 -0
- package/.eslintignore +2 -0
- package/.eslintrc.js +15 -0
- package/.gitleaks.toml +24 -0
- package/.gitleaksignore +1 -0
- package/.nvmrc +1 -0
- package/.prettierignore +3 -0
- package/.prettierrc.json +6 -0
- package/.releaserc +10 -0
- package/.vscode/launch.json +11 -0
- package/LICENSE +13 -0
- package/README.md +83 -0
- package/__native__/__ios__/v4-native-client.js +209891 -0
- package/__native__/__ios__/v4-native-client.js.map +1 -0
- package/__tests__/clients/composite-client.test.ts +140 -0
- package/__tests__/helpers/baseClients.ts +23 -0
- package/__tests__/helpers/constants.ts +130 -0
- package/__tests__/lib/helpers.test.ts +53 -0
- package/__tests__/lib/util.test.ts +77 -0
- package/__tests__/lib/validation.test.ts +289 -0
- package/__tests__/modules/client/AccountEndpoints.test.ts +236 -0
- package/__tests__/modules/client/FaucetEndpoint.test.ts +14 -0
- package/__tests__/modules/client/MarketsEndpoints.test.ts +84 -0
- package/__tests__/modules/client/Transfers.test.ts +44 -0
- package/__tests__/modules/client/UtilityEndpoints.test.ts +29 -0
- package/__tests__/modules/client/ValidatorGetEndpoints.test.ts +50 -0
- package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +52 -0
- package/__tests__/modules/client/constants.ts +5 -0
- package/__tests__/modules/onboarding.test.ts +66 -0
- package/build/cjs/clients/composite-client.js +788 -0
- package/build/cjs/clients/constants.js +293 -0
- package/build/cjs/clients/faucet-client.js +36 -0
- package/build/cjs/clients/helpers/chain-helpers.js +178 -0
- package/build/cjs/clients/helpers/request-helpers.js +65 -0
- package/build/cjs/clients/indexer-client.js +56 -0
- package/build/cjs/clients/lib/axios/axiosRequest.js +34 -0
- package/build/cjs/clients/lib/axios/errors.js +28 -0
- package/build/cjs/clients/lib/axios/index.js +23 -0
- package/build/cjs/clients/lib/axios/types.js +11 -0
- package/build/cjs/clients/lib/cctpProto.js +3242 -0
- package/build/cjs/clients/lib/errors.js +58 -0
- package/build/cjs/clients/lib/registry.js +51 -0
- package/build/cjs/clients/modules/account.js +223 -0
- package/build/cjs/clients/modules/composer.js +404 -0
- package/build/cjs/clients/modules/get.js +469 -0
- package/build/cjs/clients/modules/local-wallet.js +59 -0
- package/build/cjs/clients/modules/markets.js +54 -0
- package/build/cjs/clients/modules/post.js +391 -0
- package/build/cjs/clients/modules/proto-includes.js +72 -0
- package/build/cjs/clients/modules/rest.js +33 -0
- package/build/cjs/clients/modules/signer.js +93 -0
- package/build/cjs/clients/modules/tendermintClient.js +137 -0
- package/build/cjs/clients/modules/utility.js +44 -0
- package/build/cjs/clients/modules/vault.js +25 -0
- package/build/cjs/clients/noble-client.js +75 -0
- package/build/cjs/clients/socket-client.js +301 -0
- package/build/cjs/clients/subaccount.js +33 -0
- package/build/cjs/clients/types.js +43 -0
- package/build/cjs/clients/validator-client.js +81 -0
- package/build/cjs/index.js +73 -0
- package/build/cjs/lib/constants.js +23 -0
- package/build/cjs/lib/errors.js +37 -0
- package/build/cjs/lib/helpers.js +98 -0
- package/build/cjs/lib/onboarding.js +75 -0
- package/build/cjs/lib/trading-key-utils.js +108 -0
- package/build/cjs/lib/utils.js +79 -0
- package/build/cjs/lib/validation.js +150 -0
- package/build/cjs/network_optimizer.js +96 -0
- package/build/cjs/package.json +1 -0
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -0
- package/build/cjs/types.js +25 -0
- package/build/esm/__tests__/clients/composite-client.test.d.ts +2 -0
- package/build/esm/__tests__/clients/composite-client.test.d.ts.map +1 -0
- package/build/esm/__tests__/clients/composite-client.test.js +128 -0
- package/build/esm/__tests__/helpers/baseClients.d.ts +19 -0
- package/build/esm/__tests__/helpers/baseClients.d.ts.map +1 -0
- package/build/esm/__tests__/helpers/baseClients.js +22 -0
- package/build/esm/__tests__/helpers/constants.d.ts +19 -0
- package/build/esm/__tests__/helpers/constants.d.ts.map +1 -0
- package/build/esm/__tests__/helpers/constants.js +104 -0
- package/build/esm/__tests__/lib/helpers.test.d.ts +2 -0
- package/build/esm/__tests__/lib/helpers.test.d.ts.map +1 -0
- package/build/esm/__tests__/lib/helpers.test.js +40 -0
- package/build/esm/__tests__/lib/util.test.d.ts +2 -0
- package/build/esm/__tests__/lib/util.test.d.ts.map +1 -0
- package/build/esm/__tests__/lib/util.test.js +65 -0
- package/build/esm/__tests__/lib/validation.test.d.ts +2 -0
- package/build/esm/__tests__/lib/validation.test.d.ts.map +1 -0
- package/build/esm/__tests__/lib/validation.test.js +249 -0
- package/build/esm/__tests__/modules/client/AccountEndpoints.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/AccountEndpoints.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/AccountEndpoints.test.js +169 -0
- package/build/esm/__tests__/modules/client/FaucetEndpoint.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/FaucetEndpoint.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/FaucetEndpoint.test.js +13 -0
- package/build/esm/__tests__/modules/client/MarketsEndpoints.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/MarketsEndpoints.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/MarketsEndpoints.test.js +66 -0
- package/build/esm/__tests__/modules/client/Transfers.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/Transfers.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/Transfers.test.js +39 -0
- package/build/esm/__tests__/modules/client/UtilityEndpoints.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/UtilityEndpoints.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/UtilityEndpoints.test.js +26 -0
- package/build/esm/__tests__/modules/client/ValidatorGetEndpoints.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/ValidatorGetEndpoints.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/ValidatorGetEndpoints.test.js +44 -0
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.d.ts +2 -0
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.js +48 -0
- package/build/esm/__tests__/modules/client/constants.d.ts +4 -0
- package/build/esm/__tests__/modules/client/constants.d.ts.map +1 -0
- package/build/esm/__tests__/modules/client/constants.js +4 -0
- package/build/esm/__tests__/modules/onboarding.test.d.ts +2 -0
- package/build/esm/__tests__/modules/onboarding.test.d.ts.map +1 -0
- package/build/esm/__tests__/modules/onboarding.test.js +48 -0
- package/build/esm/examples/account_endpoints.d.ts +5 -0
- package/build/esm/examples/account_endpoints.d.ts.map +1 -0
- package/build/esm/examples/account_endpoints.js +127 -0
- package/build/esm/examples/batch_cancel_orders_example.d.ts +2 -0
- package/build/esm/examples/batch_cancel_orders_example.d.ts.map +1 -0
- package/build/esm/examples/batch_cancel_orders_example.js +88 -0
- package/build/esm/examples/composite_example.d.ts +2 -0
- package/build/esm/examples/composite_example.d.ts.map +1 -0
- package/build/esm/examples/composite_example.js +45 -0
- package/build/esm/examples/constants.d.ts +13 -0
- package/build/esm/examples/constants.d.ts.map +1 -0
- package/build/esm/examples/constants.js +29 -0
- package/build/esm/examples/faucet_endpoint.d.ts +5 -0
- package/build/esm/examples/faucet_endpoint.d.ts.map +1 -0
- package/build/esm/examples/faucet_endpoint.js +21 -0
- package/build/esm/examples/gov_add_new_market.d.ts +2 -0
- package/build/esm/examples/gov_add_new_market.d.ts.map +1 -0
- package/build/esm/examples/gov_add_new_market.js +72 -0
- package/build/esm/examples/human_readable_orders.json +86 -0
- package/build/esm/examples/human_readable_short_term_orders.json +42 -0
- package/build/esm/examples/json-encoding.d.ts +2 -0
- package/build/esm/examples/json-encoding.d.ts.map +1 -0
- package/build/esm/examples/json-encoding.js +27 -0
- package/build/esm/examples/long_term_order_cancel_example.d.ts +2 -0
- package/build/esm/examples/long_term_order_cancel_example.d.ts.map +1 -0
- package/build/esm/examples/long_term_order_cancel_example.js +58 -0
- package/build/esm/examples/markets_endpoints.d.ts +5 -0
- package/build/esm/examples/markets_endpoints.d.ts.map +1 -0
- package/build/esm/examples/markets_endpoints.js +131 -0
- package/build/esm/examples/native_examples.d.ts +2 -0
- package/build/esm/examples/native_examples.d.ts.map +1 -0
- package/build/esm/examples/native_examples.js +110 -0
- package/build/esm/examples/noble_example.d.ts +2 -0
- package/build/esm/examples/noble_example.d.ts.map +1 -0
- package/build/esm/examples/noble_example.js +83 -0
- package/build/esm/examples/optimal_node.d.ts +2 -0
- package/build/esm/examples/optimal_node.d.ts.map +1 -0
- package/build/esm/examples/optimal_node.js +68 -0
- package/build/esm/examples/permissioned_keys_example.d.ts +2 -0
- package/build/esm/examples/permissioned_keys_example.d.ts.map +1 -0
- package/build/esm/examples/permissioned_keys_example.js +99 -0
- package/build/esm/examples/raw_orders.json +130 -0
- package/build/esm/examples/short_term_order_cancel_example.d.ts +2 -0
- package/build/esm/examples/short_term_order_cancel_example.d.ts.map +1 -0
- package/build/esm/examples/short_term_order_cancel_example.js +49 -0
- package/build/esm/examples/short_term_order_composite_example.d.ts +2 -0
- package/build/esm/examples/short_term_order_composite_example.d.ts.map +1 -0
- package/build/esm/examples/short_term_order_composite_example.js +63 -0
- package/build/esm/examples/test.d.ts +2 -0
- package/build/esm/examples/test.d.ts.map +1 -0
- package/build/esm/examples/test.js +45 -0
- package/build/esm/examples/transfer_example_deposit.d.ts +2 -0
- package/build/esm/examples/transfer_example_deposit.d.ts.map +1 -0
- package/build/esm/examples/transfer_example_deposit.js +24 -0
- package/build/esm/examples/transfer_example_send.d.ts +2 -0
- package/build/esm/examples/transfer_example_send.d.ts.map +1 -0
- package/build/esm/examples/transfer_example_send.js +37 -0
- package/build/esm/examples/transfer_example_subaccount_transfer.d.ts +2 -0
- package/build/esm/examples/transfer_example_subaccount_transfer.d.ts.map +1 -0
- package/build/esm/examples/transfer_example_subaccount_transfer.js +24 -0
- package/build/esm/examples/transfer_example_withdraw.d.ts +2 -0
- package/build/esm/examples/transfer_example_withdraw.d.ts.map +1 -0
- package/build/esm/examples/transfer_example_withdraw.js +25 -0
- package/build/esm/examples/transfer_example_withdraw_other.d.ts +2 -0
- package/build/esm/examples/transfer_example_withdraw_other.d.ts.map +1 -0
- package/build/esm/examples/transfer_example_withdraw_other.js +37 -0
- package/build/esm/examples/utility_endpoints.d.ts +5 -0
- package/build/esm/examples/utility_endpoints.d.ts.map +1 -0
- package/build/esm/examples/utility_endpoints.js +40 -0
- package/build/esm/examples/validator_get_example.d.ts +2 -0
- package/build/esm/examples/validator_get_example.d.ts.map +1 -0
- package/build/esm/examples/validator_get_example.js +156 -0
- package/build/esm/examples/validator_post_example.d.ts +2 -0
- package/build/esm/examples/validator_post_example.d.ts.map +1 -0
- package/build/esm/examples/validator_post_example.js +71 -0
- package/build/esm/examples/wallet_address.d.ts +2 -0
- package/build/esm/examples/wallet_address.d.ts.map +1 -0
- package/build/esm/examples/wallet_address.js +17 -0
- package/build/esm/examples/websocket_example.d.ts +2 -0
- package/build/esm/examples/websocket_example.d.ts.map +1 -0
- package/build/esm/examples/websocket_example.js +34 -0
- package/build/esm/examples/websocket_orderbook_example.d.ts +2 -0
- package/build/esm/examples/websocket_orderbook_example.d.ts.map +1 -0
- package/build/esm/examples/websocket_orderbook_example.js +194 -0
- package/build/esm/src/clients/composite-client.d.ts +406 -0
- package/build/esm/src/clients/composite-client.d.ts.map +1 -0
- package/build/esm/src/clients/composite-client.js +781 -0
- package/build/esm/src/clients/constants.d.ts +170 -0
- package/build/esm/src/clients/constants.d.ts.map +1 -0
- package/build/esm/src/clients/constants.js +269 -0
- package/build/esm/src/clients/faucet-client.d.ts +18 -0
- package/build/esm/src/clients/faucet-client.d.ts.map +1 -0
- package/build/esm/src/clients/faucet-client.js +29 -0
- package/build/esm/src/clients/helpers/chain-helpers.d.ts +15 -0
- package/build/esm/src/clients/helpers/chain-helpers.d.ts.map +1 -0
- package/build/esm/src/clients/helpers/chain-helpers.js +163 -0
- package/build/esm/src/clients/helpers/request-helpers.d.ts +3 -0
- package/build/esm/src/clients/helpers/request-helpers.d.ts.map +1 -0
- package/build/esm/src/clients/helpers/request-helpers.js +58 -0
- package/build/esm/src/clients/indexer-client.d.ts +40 -0
- package/build/esm/src/clients/indexer-client.d.ts.map +1 -0
- package/build/esm/src/clients/indexer-client.js +49 -0
- package/build/esm/src/clients/lib/axios/axiosRequest.d.ts +9 -0
- package/build/esm/src/clients/lib/axios/axiosRequest.d.ts.map +1 -0
- package/build/esm/src/clients/lib/axios/axiosRequest.js +28 -0
- package/build/esm/src/clients/lib/axios/errors.d.ts +31 -0
- package/build/esm/src/clients/lib/axios/errors.d.ts.map +1 -0
- package/build/esm/src/clients/lib/axios/errors.js +23 -0
- package/build/esm/src/clients/lib/axios/index.d.ts +7 -0
- package/build/esm/src/clients/lib/axios/index.d.ts.map +1 -0
- package/build/esm/src/clients/lib/axios/index.js +7 -0
- package/build/esm/src/clients/lib/axios/types.d.ts +7 -0
- package/build/esm/src/clients/lib/axios/types.d.ts.map +1 -0
- package/build/esm/src/clients/lib/axios/types.js +8 -0
- package/build/esm/src/clients/lib/cctpProto.d.ts +670 -0
- package/build/esm/src/clients/lib/cctpProto.d.ts.map +1 -0
- package/build/esm/src/clients/lib/cctpProto.js +3234 -0
- package/build/esm/src/clients/lib/errors.d.ts +38 -0
- package/build/esm/src/clients/lib/errors.d.ts.map +1 -0
- package/build/esm/src/clients/lib/errors.js +50 -0
- package/build/esm/src/clients/lib/registry.d.ts +4 -0
- package/build/esm/src/clients/lib/registry.d.ts.map +1 -0
- package/build/esm/src/clients/lib/registry.js +47 -0
- package/build/esm/src/clients/modules/account.d.ts +30 -0
- package/build/esm/src/clients/modules/account.d.ts.map +1 -0
- package/build/esm/src/clients/modules/account.js +217 -0
- package/build/esm/src/clients/modules/composer.d.ts +37 -0
- package/build/esm/src/clients/modules/composer.d.ts.map +1 -0
- package/build/esm/src/clients/modules/composer.js +397 -0
- package/build/esm/src/clients/modules/get.d.ts +193 -0
- package/build/esm/src/clients/modules/get.d.ts.map +1 -0
- package/build/esm/src/clients/modules/get.js +429 -0
- package/build/esm/src/clients/modules/local-wallet.d.ts +19 -0
- package/build/esm/src/clients/modules/local-wallet.d.ts.map +1 -0
- package/build/esm/src/clients/modules/local-wallet.js +53 -0
- package/build/esm/src/clients/modules/markets.d.ts +14 -0
- package/build/esm/src/clients/modules/markets.d.ts.map +1 -0
- package/build/esm/src/clients/modules/markets.js +48 -0
- package/build/esm/src/clients/modules/post.d.ts +133 -0
- package/build/esm/src/clients/modules/post.d.ts.map +1 -0
- package/build/esm/src/clients/modules/post.js +384 -0
- package/build/esm/src/clients/modules/proto-includes.d.ts +30 -0
- package/build/esm/src/clients/modules/proto-includes.d.ts.map +1 -0
- package/build/esm/src/clients/modules/proto-includes.js +32 -0
- package/build/esm/src/clients/modules/rest.d.ts +12 -0
- package/build/esm/src/clients/modules/rest.d.ts.map +1 -0
- package/build/esm/src/clients/modules/rest.js +27 -0
- package/build/esm/src/clients/modules/signer.d.ts +19 -0
- package/build/esm/src/clients/modules/signer.d.ts.map +1 -0
- package/build/esm/src/clients/modules/signer.js +86 -0
- package/build/esm/src/clients/modules/tendermintClient.d.ts +54 -0
- package/build/esm/src/clients/modules/tendermintClient.d.ts.map +1 -0
- package/build/esm/src/clients/modules/tendermintClient.js +133 -0
- package/build/esm/src/clients/modules/utility.d.ts +27 -0
- package/build/esm/src/clients/modules/utility.d.ts.map +1 -0
- package/build/esm/src/clients/modules/utility.js +38 -0
- package/build/esm/src/clients/modules/vault.d.ts +12 -0
- package/build/esm/src/clients/modules/vault.d.ts.map +1 -0
- package/build/esm/src/clients/modules/vault.js +19 -0
- package/build/esm/src/clients/native.d.ts +75 -0
- package/build/esm/src/clients/native.d.ts.map +1 -0
- package/build/esm/src/clients/native.js +1237 -0
- package/build/esm/src/clients/noble-client.d.ts +18 -0
- package/build/esm/src/clients/noble-client.d.ts.map +1 -0
- package/build/esm/src/clients/noble-client.js +71 -0
- package/build/esm/src/clients/socket-client.d.ts +146 -0
- package/build/esm/src/clients/socket-client.d.ts.map +1 -0
- package/build/esm/src/clients/socket-client.js +294 -0
- package/build/esm/src/clients/subaccount.d.ts +14 -0
- package/build/esm/src/clients/subaccount.d.ts.map +1 -0
- package/build/esm/src/clients/subaccount.js +29 -0
- package/build/esm/src/clients/types.d.ts +137 -0
- package/build/esm/src/clients/types.d.ts.map +1 -0
- package/build/esm/src/clients/types.js +26 -0
- package/build/esm/src/clients/validator-client.d.ts +35 -0
- package/build/esm/src/clients/validator-client.d.ts.map +1 -0
- package/build/esm/src/clients/validator-client.js +74 -0
- package/build/esm/src/index.d.ts +17 -0
- package/build/esm/src/index.d.ts.map +1 -0
- package/build/esm/src/index.js +19 -0
- package/build/esm/src/lib/constants.d.ts +13 -0
- package/build/esm/src/lib/constants.d.ts.map +1 -0
- package/build/esm/src/lib/constants.js +20 -0
- package/build/esm/src/lib/errors.d.ts +23 -0
- package/build/esm/src/lib/errors.d.ts.map +1 -0
- package/build/esm/src/lib/errors.js +31 -0
- package/build/esm/src/lib/helpers.d.ts +22 -0
- package/build/esm/src/lib/helpers.d.ts.map +1 -0
- package/build/esm/src/lib/helpers.js +88 -0
- package/build/esm/src/lib/onboarding.d.ts +46 -0
- package/build/esm/src/lib/onboarding.d.ts.map +1 -0
- package/build/esm/src/lib/onboarding.js +69 -0
- package/build/esm/src/lib/trading-key-utils.d.ts +22 -0
- package/build/esm/src/lib/trading-key-utils.d.ts.map +1 -0
- package/build/esm/src/lib/trading-key-utils.js +99 -0
- package/build/esm/src/lib/utils.d.ts +39 -0
- package/build/esm/src/lib/utils.d.ts.map +1 -0
- package/build/esm/src/lib/utils.js +69 -0
- package/build/esm/src/lib/validation.d.ts +26 -0
- package/build/esm/src/lib/validation.d.ts.map +1 -0
- package/build/esm/src/lib/validation.js +138 -0
- package/build/esm/src/network_optimizer.d.ts +8 -0
- package/build/esm/src/network_optimizer.d.ts.map +1 -0
- package/build/esm/src/network_optimizer.js +91 -0
- package/build/esm/src/types.d.ts +26 -0
- package/build/esm/src/types.d.ts.map +1 -0
- package/build/esm/src/types.js +8 -0
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -0
- package/commitlint.config.js +8 -0
- package/examples/account_endpoints.ts +129 -0
- package/examples/batch_cancel_orders_example.ts +130 -0
- package/examples/composite_example.ts +65 -0
- package/examples/constants.ts +42 -0
- package/examples/faucet_endpoint.ts +24 -0
- package/examples/gov_add_new_market.json +64 -0
- package/examples/gov_add_new_market.ts +95 -0
- package/examples/human_readable_orders.json +86 -0
- package/examples/human_readable_short_term_orders.json +42 -0
- package/examples/json-encoding.ts +33 -0
- package/examples/long_term_order_cancel_example.ts +87 -0
- package/examples/markets_endpoints.ts +134 -0
- package/examples/native_examples.ts +151 -0
- package/examples/noble_example.ts +107 -0
- package/examples/optimal_node.ts +70 -0
- package/examples/permissioned_keys_example.ts +162 -0
- package/examples/raw_orders.json +130 -0
- package/examples/short_term_order_cancel_example.ts +68 -0
- package/examples/short_term_order_composite_example.ts +80 -0
- package/examples/test.ts +65 -0
- package/examples/transfer_example_deposit.ts +28 -0
- package/examples/transfer_example_send.ts +60 -0
- package/examples/transfer_example_subaccount_transfer.ts +29 -0
- package/examples/transfer_example_withdraw.ts +31 -0
- package/examples/transfer_example_withdraw_other.ts +60 -0
- package/examples/utility_endpoints.ts +42 -0
- package/examples/validator_get_example.ts +160 -0
- package/examples/validator_post_example.ts +81 -0
- package/examples/wallet_address.ts +18 -0
- package/examples/websocket_example.ts +44 -0
- package/examples/websocket_orderbook_example.ts +239 -0
- package/jest.config.js +12 -0
- package/jest.globalSetup.js +5 -0
- package/jest.setup.js +1 -0
- package/package.json +113 -0
- package/ritbit-v4-client-js-1.3.17.tgz +0 -0
- package/scripts/bump_version.sh +16 -0
- package/scripts/generate-cctp-protos.sh +41 -0
- package/scripts/mobile_build_pack.sh +5 -0
- package/scripts/publish-if-not-exists.sh +21 -0
- package/src/clients/composite-client.ts +1431 -0
- package/src/clients/constants.ts +358 -0
- package/src/clients/faucet-client.ts +48 -0
- package/src/clients/helpers/chain-helpers.ts +231 -0
- package/src/clients/helpers/request-helpers.ts +71 -0
- package/src/clients/indexer-client.ts +61 -0
- package/src/clients/lib/axios/axiosRequest.ts +41 -0
- package/src/clients/lib/axios/errors.ts +41 -0
- package/src/clients/lib/axios/index.ts +7 -0
- package/src/clients/lib/axios/types.ts +6 -0
- package/src/clients/lib/cctpProto.ts +4197 -0
- package/src/clients/lib/errors.ts +63 -0
- package/src/clients/lib/registry.ts +88 -0
- package/src/clients/modules/account.ts +388 -0
- package/src/clients/modules/composer.ts +645 -0
- package/src/clients/modules/get.ts +740 -0
- package/src/clients/modules/local-wallet.ts +79 -0
- package/src/clients/modules/markets.ts +71 -0
- package/src/clients/modules/post.ts +1016 -0
- package/src/clients/modules/proto-includes.ts +33 -0
- package/src/clients/modules/rest.ts +40 -0
- package/src/clients/modules/signer.ts +139 -0
- package/src/clients/modules/tendermintClient.ts +168 -0
- package/src/clients/modules/utility.ts +47 -0
- package/src/clients/modules/vault.ts +23 -0
- package/src/clients/native.ts +1508 -0
- package/src/clients/noble-client.ts +108 -0
- package/src/clients/socket-client.ts +331 -0
- package/src/clients/subaccount.ts +57 -0
- package/src/clients/types.ts +191 -0
- package/src/clients/validator-client.ts +103 -0
- package/src/index.ts +20 -0
- package/src/lib/constants.ts +28 -0
- package/src/lib/errors.ts +38 -0
- package/src/lib/helpers.ts +99 -0
- package/src/lib/onboarding.ts +96 -0
- package/src/lib/trading-key-utils.ts +136 -0
- package/src/lib/utils.ts +84 -0
- package/src/lib/validation.ts +197 -0
- package/src/network_optimizer.ts +132 -0
- package/src/types.ts +44 -0
- package/tasks.json +16 -0
- package/tsconfig.base.json +15 -0
- package/tsconfig.cjs.json +20 -0
- package/tsconfig.eslint.json +14 -0
- package/tsconfig.esm.json +11 -0
- package/webpack.config.js +56 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { QueryClient, TxExtension, setupTxExtension } from '@cosmjs/stargate';
|
|
2
|
+
import { Tendermint37Client } from '@cosmjs/tendermint-rpc';
|
|
3
|
+
import Long from 'long';
|
|
4
|
+
import protobuf from 'protobufjs';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
ValidatorConfig,
|
|
8
|
+
BROADCAST_POLL_INTERVAL_MS,
|
|
9
|
+
BROADCAST_TIMEOUT_MS,
|
|
10
|
+
SelectedGasDenom,
|
|
11
|
+
} from './constants';
|
|
12
|
+
import { Get } from './modules/get';
|
|
13
|
+
import { Post } from './modules/post';
|
|
14
|
+
import { TendermintClient } from './modules/tendermintClient';
|
|
15
|
+
|
|
16
|
+
// Required for encoding and decoding queries that are of type Long.
|
|
17
|
+
// Must be done once but since the individal modules should be usable
|
|
18
|
+
// - must be set in each module that encounters encoding/decoding Longs.
|
|
19
|
+
// Reference: https://github.com/protobufjs/protobuf.js/issues/921
|
|
20
|
+
protobuf.util.Long = Long;
|
|
21
|
+
protobuf.configure();
|
|
22
|
+
|
|
23
|
+
export class ValidatorClient {
|
|
24
|
+
public readonly config: ValidatorConfig;
|
|
25
|
+
private _get?: Get;
|
|
26
|
+
private _post?: Post;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @description Connect to a validator client
|
|
30
|
+
*
|
|
31
|
+
* @returns The validator client
|
|
32
|
+
*/
|
|
33
|
+
static async connect(config: ValidatorConfig): Promise<ValidatorClient> {
|
|
34
|
+
const client = new ValidatorClient(config);
|
|
35
|
+
await client.initialize();
|
|
36
|
+
return client;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private constructor(config: ValidatorConfig) {
|
|
40
|
+
this.config = config;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @description Get the query module, used for retrieving on-chain data.
|
|
45
|
+
*
|
|
46
|
+
* @returns The query module
|
|
47
|
+
*/
|
|
48
|
+
get get(): Get {
|
|
49
|
+
return this._get!;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @description transaction module, used for sending transactions.
|
|
54
|
+
*
|
|
55
|
+
* @returns The transaction module
|
|
56
|
+
*/
|
|
57
|
+
get post(): Post {
|
|
58
|
+
return this._post!;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get selectedGasDenom(): SelectedGasDenom | undefined {
|
|
62
|
+
if (!this._post) return undefined;
|
|
63
|
+
return this._post.selectedGasDenom;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setSelectedGasDenom(gasDenom: SelectedGasDenom): void {
|
|
67
|
+
if (!this._post) throw new Error('Post module not initialized');
|
|
68
|
+
|
|
69
|
+
this._post.setSelectedGasDenom(gasDenom);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @description populate account number cache in the Post module for performance.
|
|
74
|
+
*/
|
|
75
|
+
async populateAccountNumberCache(address: string): Promise<void> {
|
|
76
|
+
if (!this._post) throw new Error('Post module not initialized');
|
|
77
|
+
await this._post.populateAccountNumberCache(address);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private async initialize(): Promise<void> {
|
|
81
|
+
const tendermint37Client: Tendermint37Client = await Tendermint37Client.connect(
|
|
82
|
+
this.config.restEndpoint,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const tendermintClient = new TendermintClient(tendermint37Client, {
|
|
86
|
+
broadcastPollIntervalMs: BROADCAST_POLL_INTERVAL_MS,
|
|
87
|
+
broadcastTimeoutMs: BROADCAST_TIMEOUT_MS,
|
|
88
|
+
});
|
|
89
|
+
const queryClient: QueryClient & TxExtension = QueryClient.withExtensions(
|
|
90
|
+
tendermint37Client,
|
|
91
|
+
setupTxExtension,
|
|
92
|
+
);
|
|
93
|
+
this._get = new Get(tendermintClient, queryClient);
|
|
94
|
+
this._post = new Post(
|
|
95
|
+
this._get!,
|
|
96
|
+
this.config.chainId,
|
|
97
|
+
this.config.denoms,
|
|
98
|
+
this.config.defaultClientMemo,
|
|
99
|
+
this.config.useTimestampNonce,
|
|
100
|
+
this.config.timestampNonceOffsetMs,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Types.
|
|
2
|
+
export * from './types';
|
|
3
|
+
|
|
4
|
+
// Utility functions.
|
|
5
|
+
export * as helpers from './lib/helpers';
|
|
6
|
+
export * as onboarding from './lib/onboarding';
|
|
7
|
+
export * as tradingKeyUtils from './lib/trading-key-utils';
|
|
8
|
+
export * as utils from './lib/utils';
|
|
9
|
+
export * as validation from './lib/validation';
|
|
10
|
+
|
|
11
|
+
export { CompositeClient } from './clients/composite-client';
|
|
12
|
+
export { FaucetClient } from './clients/faucet-client';
|
|
13
|
+
export { IndexerClient } from './clients/indexer-client';
|
|
14
|
+
export { default as LocalWallet } from './clients/modules/local-wallet';
|
|
15
|
+
export { NobleClient } from './clients/noble-client';
|
|
16
|
+
export { SocketClient } from './clients/socket-client';
|
|
17
|
+
export { SubaccountInfo as SubaccountClient, SubaccountInfo } from './clients/subaccount';
|
|
18
|
+
export { ValidatorClient } from './clients/validator-client';
|
|
19
|
+
export { encodeJson, ByteArrayEncoding } from './lib/helpers';
|
|
20
|
+
export { NetworkOptimizer } from './network_optimizer';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StdFee } from '@cosmjs/stargate';
|
|
2
|
+
|
|
3
|
+
// Bech32 Prefix
|
|
4
|
+
export const BECH32_PREFIX = 'rit';
|
|
5
|
+
export const NOBLE_BECH32_PREFIX = 'noble';
|
|
6
|
+
|
|
7
|
+
// Broadcast Defaults
|
|
8
|
+
export const BROADCAST_POLL_INTERVAL_MS: number = 300;
|
|
9
|
+
export const BROADCAST_TIMEOUT_MS: number = 8_000;
|
|
10
|
+
|
|
11
|
+
// API Defaults
|
|
12
|
+
export const API_TIMEOUT_DEFAULT_MS: number = 5_000;
|
|
13
|
+
|
|
14
|
+
// Gas
|
|
15
|
+
export const GAS_MULTIPLIER: number = 1.6;
|
|
16
|
+
|
|
17
|
+
export const ZERO_FEE: StdFee = {
|
|
18
|
+
amount: [],
|
|
19
|
+
gas: '0',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Validation
|
|
23
|
+
export const MAX_UINT_32 = 4_294_967_295;
|
|
24
|
+
export const MAX_SUBACCOUNT_NUMBER = 128_000;
|
|
25
|
+
|
|
26
|
+
export const DEFAULT_SEQUENCE: number = 0;
|
|
27
|
+
|
|
28
|
+
export const SERIALIZED_INT_ZERO: Uint8Array = Uint8Array.from([0x02]);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BroadcastTxSyncResponse } from '@cosmjs/tendermint-rpc';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An edge-case was hit in the client that should never have been reached.
|
|
5
|
+
*/
|
|
6
|
+
export class UnexpectedClientError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super('An unexpected error occurred on the client');
|
|
9
|
+
this.name = 'UnexpectedClientError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* An error occurred during the broadcasting process.
|
|
15
|
+
*/
|
|
16
|
+
export class BroadcastErrorObject extends Error {
|
|
17
|
+
result: BroadcastTxSyncResponse;
|
|
18
|
+
code: number;
|
|
19
|
+
codespace?: string;
|
|
20
|
+
|
|
21
|
+
constructor(message: string, result: BroadcastTxSyncResponse) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = 'BroadcastError';
|
|
24
|
+
this.result = result;
|
|
25
|
+
this.code = result.code;
|
|
26
|
+
this.codespace = result.codespace;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* User error occurred during a client operation.
|
|
32
|
+
*/
|
|
33
|
+
export class UserError extends Error {
|
|
34
|
+
constructor(message: string) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = 'UserError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { toHex } from '@cosmjs/encoding';
|
|
2
|
+
import BigNumber from 'bignumber.js';
|
|
3
|
+
import Long from 'long';
|
|
4
|
+
|
|
5
|
+
import { PartialTransactionOptions, TransactionOptions } from '../types';
|
|
6
|
+
import { DEFAULT_SEQUENCE } from './constants';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @description Either return undefined or insert default sequence value into
|
|
10
|
+
* `partialTransactionOptions` if it does not exist.
|
|
11
|
+
*
|
|
12
|
+
* @returns undefined or full TransactionOptions.
|
|
13
|
+
*/
|
|
14
|
+
export function convertPartialTransactionOptionsToFull(
|
|
15
|
+
partialTransactionOptions?: PartialTransactionOptions,
|
|
16
|
+
): TransactionOptions | undefined {
|
|
17
|
+
if (partialTransactionOptions === undefined) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
sequence: DEFAULT_SEQUENCE,
|
|
23
|
+
...partialTransactionOptions,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @description Strip '0x' prefix from input string. If there is no '0x' prefix, return the original
|
|
29
|
+
* input.
|
|
30
|
+
*
|
|
31
|
+
* @returns input without '0x' prefix or original input if no prefix.
|
|
32
|
+
*/
|
|
33
|
+
export function stripHexPrefix(input: string): string {
|
|
34
|
+
if (input.indexOf('0x') === 0) {
|
|
35
|
+
return input.slice(2);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return input;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toBigInt(u: Uint8Array): BigInt {
|
|
42
|
+
if (u.length <= 1) {
|
|
43
|
+
return BigInt(0);
|
|
44
|
+
}
|
|
45
|
+
// eslint-disable-next-line no-bitwise
|
|
46
|
+
const negated: boolean = (u[0] & 1) === 1;
|
|
47
|
+
const hex: string = Buffer.from(u.slice(1)).toString('hex');
|
|
48
|
+
const abs: bigint = BigInt(`0x${hex}`);
|
|
49
|
+
return negated ? -abs : abs;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function bigIntToBytes(value: bigint): Uint8Array {
|
|
53
|
+
const absoluteValue = value < 0 ? value * BigInt(-1) : value;
|
|
54
|
+
const nonPaddedHexValue = absoluteValue.toString(16);
|
|
55
|
+
const paddedHexValue =
|
|
56
|
+
nonPaddedHexValue.length % 2 === 0 ? nonPaddedHexValue : `0${nonPaddedHexValue}`;
|
|
57
|
+
const numberBytes = Buffer.from(paddedHexValue, 'hex');
|
|
58
|
+
const signedBytes = Uint8Array.of(value < 0 ? 3 : 2, ...numberBytes);
|
|
59
|
+
return signedBytes;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export enum ByteArrayEncoding {
|
|
63
|
+
HEX = 'hex',
|
|
64
|
+
BIGINT = 'bigint',
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function encodeJson(
|
|
68
|
+
object?: Object,
|
|
69
|
+
byteArrayEncoding: ByteArrayEncoding = ByteArrayEncoding.HEX,
|
|
70
|
+
): string {
|
|
71
|
+
// eslint-disable-next-line prefer-arrow-callback
|
|
72
|
+
return JSON.stringify(object, function replacer(_key, value) {
|
|
73
|
+
// Even though we set the an UInt8Array as the value,
|
|
74
|
+
// it comes in here as an object with UInt8Array as the buffer property.
|
|
75
|
+
if (value instanceof BigNumber) {
|
|
76
|
+
return value.toString();
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === 'bigint') {
|
|
79
|
+
return value.toString();
|
|
80
|
+
}
|
|
81
|
+
if (value instanceof Long) {
|
|
82
|
+
return value.toString();
|
|
83
|
+
}
|
|
84
|
+
if (value?.buffer instanceof Uint8Array) {
|
|
85
|
+
if (byteArrayEncoding === ByteArrayEncoding.HEX) {
|
|
86
|
+
return toHex(value.buffer);
|
|
87
|
+
} else {
|
|
88
|
+
return toBigInt(value.buffer).toString();
|
|
89
|
+
}
|
|
90
|
+
} else if (value instanceof Uint8Array) {
|
|
91
|
+
if (byteArrayEncoding === ByteArrayEncoding.HEX) {
|
|
92
|
+
return toHex(value);
|
|
93
|
+
} else {
|
|
94
|
+
return toBigInt(value).toString();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { HDKey } from '@scure/bip32';
|
|
2
|
+
import { entropyToMnemonic, mnemonicToSeedSync } from '@scure/bip39';
|
|
3
|
+
import { wordlist } from '@scure/bip39/wordlists/english';
|
|
4
|
+
import { keccak256 } from 'ethereum-cryptography/keccak';
|
|
5
|
+
|
|
6
|
+
import { stripHexPrefix } from './helpers';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @description Get Mnemonic and priv/pub keys from privateKeyBytes and BIP44 HD path
|
|
10
|
+
*
|
|
11
|
+
* @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
|
|
12
|
+
*
|
|
13
|
+
* @param entropy used to generate mnemonic
|
|
14
|
+
*
|
|
15
|
+
* @param path BIP44 HD Path. Default is The Cosmos Hub path
|
|
16
|
+
*
|
|
17
|
+
* @throws Error if the hdkey does not exist
|
|
18
|
+
*
|
|
19
|
+
* @returns Mnemonic and priv/pub keys
|
|
20
|
+
*/
|
|
21
|
+
export const exportMnemonicAndPrivateKey = (
|
|
22
|
+
entropy: Uint8Array,
|
|
23
|
+
path: string = "m/44'/118'/0'/0/0",
|
|
24
|
+
): {
|
|
25
|
+
mnemonic: string;
|
|
26
|
+
privateKey: Uint8Array | null;
|
|
27
|
+
publicKey: Uint8Array | null;
|
|
28
|
+
} => {
|
|
29
|
+
const mnemonic = entropyToMnemonic(entropy, wordlist);
|
|
30
|
+
const { privateKey, publicKey } = deriveHDKeyFromMnemonic(mnemonic, path);
|
|
31
|
+
return {
|
|
32
|
+
mnemonic,
|
|
33
|
+
privateKey,
|
|
34
|
+
publicKey,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @description Get private information for onboarding using an Ethereum Signature.
|
|
40
|
+
*
|
|
41
|
+
* @returns Mnemonic and Public/Private HD keys
|
|
42
|
+
*/
|
|
43
|
+
export const deriveHDKeyFromEthereumSignature = (
|
|
44
|
+
signature: string,
|
|
45
|
+
): {
|
|
46
|
+
mnemonic: string;
|
|
47
|
+
privateKey: Uint8Array | null;
|
|
48
|
+
publicKey: Uint8Array | null;
|
|
49
|
+
} => {
|
|
50
|
+
const buffer = Buffer.from(stripHexPrefix(signature), 'hex');
|
|
51
|
+
|
|
52
|
+
if (buffer.length !== 65) {
|
|
53
|
+
throw new Error('Signature must be 65 bytes');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Remove the 'v' value by taking only the first 64 bytes of the signature
|
|
57
|
+
const rsValues = buffer.subarray(0, 64);
|
|
58
|
+
// Hash the 'r' and 's' values down to 32 bytes (256 bits) using Keccak-256
|
|
59
|
+
const entropy = keccak256(rsValues);
|
|
60
|
+
return exportMnemonicAndPrivateKey(entropy);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @description Derive priv/pub keys from mnemonic and BIP44 HD path
|
|
65
|
+
*
|
|
66
|
+
* @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
|
|
67
|
+
*
|
|
68
|
+
* @param mnemonic used to generate seed
|
|
69
|
+
*
|
|
70
|
+
* @param path BIP44 HD Path. Default is The Cosmos Hub path
|
|
71
|
+
*
|
|
72
|
+
* @throws Error if the hdkey does not exist
|
|
73
|
+
*
|
|
74
|
+
* @returns Priv/pub keys
|
|
75
|
+
*/
|
|
76
|
+
export const deriveHDKeyFromMnemonic = (
|
|
77
|
+
mnemonic: string,
|
|
78
|
+
path: string = "m/44'/118'/0'/0/0",
|
|
79
|
+
): {
|
|
80
|
+
privateKey: Uint8Array | null;
|
|
81
|
+
publicKey: Uint8Array | null;
|
|
82
|
+
} => {
|
|
83
|
+
const seed = mnemonicToSeedSync(mnemonic);
|
|
84
|
+
|
|
85
|
+
const hdkey = HDKey.fromMasterSeed(seed);
|
|
86
|
+
const derivedHdkey = hdkey.derive(path);
|
|
87
|
+
|
|
88
|
+
if (!hdkey.privateKey) {
|
|
89
|
+
throw new Error('null hd key');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
privateKey: derivedHdkey.privateKey,
|
|
94
|
+
publicKey: derivedHdkey.publicKey,
|
|
95
|
+
};
|
|
96
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { fromBase64, toBase64, toBech32, toHex } from '@cosmjs/encoding';
|
|
2
|
+
import { rawSecp256k1PubkeyToRawAddress } from '@cosmjs/tendermint-rpc';
|
|
3
|
+
import { generateMnemonic } from '@scure/bip39';
|
|
4
|
+
import { wordlist } from '@scure/bip39/wordlists/english';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Authenticator,
|
|
8
|
+
AuthenticatorType,
|
|
9
|
+
TYPE_URL_BATCH_CANCEL,
|
|
10
|
+
TYPE_URL_MSG_CANCEL_ORDER,
|
|
11
|
+
TYPE_URL_MSG_PLACE_ORDER,
|
|
12
|
+
} from '../clients/constants';
|
|
13
|
+
import { type Get } from '../clients/modules/get';
|
|
14
|
+
import LocalWallet from '../clients/modules/local-wallet';
|
|
15
|
+
import { BECH32_PREFIX } from './constants';
|
|
16
|
+
import { deriveHDKeyFromMnemonic } from './onboarding';
|
|
17
|
+
|
|
18
|
+
export const createNewRandomDydxWallet = async (): Promise<
|
|
19
|
+
{ privateKeyHex: string; mnemonic: string; publicKey: string; address: string } | undefined
|
|
20
|
+
> => {
|
|
21
|
+
const mnemonic = generateMnemonic(wordlist, 128);
|
|
22
|
+
const { privateKey: privateKeyRaw } = deriveHDKeyFromMnemonic(mnemonic);
|
|
23
|
+
const wallet = await LocalWallet.fromMnemonic(mnemonic, BECH32_PREFIX);
|
|
24
|
+
|
|
25
|
+
const privateKeyHex = privateKeyRaw != null ? `0x${toHex(privateKeyRaw)}` : undefined;
|
|
26
|
+
const publicKey = wallet.pubKey;
|
|
27
|
+
const address = wallet.address;
|
|
28
|
+
|
|
29
|
+
if (privateKeyHex == null || publicKey == null || address == null) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
// 12 english words
|
|
35
|
+
mnemonic,
|
|
36
|
+
// toHex of the raw private key bits
|
|
37
|
+
privateKeyHex,
|
|
38
|
+
// base64, not hex
|
|
39
|
+
publicKey: publicKey.value,
|
|
40
|
+
// valid dydx address corresponding to the key pair
|
|
41
|
+
address,
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// arguments to authorize a given wallet public key to trade on behalf of the user.
|
|
46
|
+
// allows place order, cancel order, batch cancel on subaccount 0 only.
|
|
47
|
+
export const getAuthorizeNewTradingKeyArguments = ({
|
|
48
|
+
generatedWalletPubKey,
|
|
49
|
+
}: {
|
|
50
|
+
generatedWalletPubKey: string;
|
|
51
|
+
}): { type: AuthenticatorType; data: Uint8Array } => {
|
|
52
|
+
const wrapAndEncode64 = (s: string): string => toBase64(new TextEncoder().encode(s));
|
|
53
|
+
|
|
54
|
+
const messageFilterSubAuth = [
|
|
55
|
+
{
|
|
56
|
+
type: AuthenticatorType.MESSAGE_FILTER,
|
|
57
|
+
config: wrapAndEncode64(TYPE_URL_MSG_PLACE_ORDER),
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
type: AuthenticatorType.MESSAGE_FILTER,
|
|
61
|
+
config: wrapAndEncode64(TYPE_URL_MSG_CANCEL_ORDER),
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
type: AuthenticatorType.MESSAGE_FILTER,
|
|
65
|
+
config: wrapAndEncode64(TYPE_URL_BATCH_CANCEL),
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
const anyOfMessageFilterConfigB64 = wrapAndEncode64(JSON.stringify(messageFilterSubAuth));
|
|
70
|
+
|
|
71
|
+
const subAuth = [
|
|
72
|
+
{
|
|
73
|
+
type: AuthenticatorType.SIGNATURE_VERIFICATION,
|
|
74
|
+
config: generatedWalletPubKey,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
type: AuthenticatorType.ANY_OF,
|
|
78
|
+
config: anyOfMessageFilterConfigB64,
|
|
79
|
+
},
|
|
80
|
+
// we limit to cross markets to make it slightly harder to drain user funds on low liquidity markets with trading keys
|
|
81
|
+
// could undo this in the future if people are angry about it
|
|
82
|
+
{
|
|
83
|
+
type: AuthenticatorType.SUBACCOUNT_FILTER,
|
|
84
|
+
config: wrapAndEncode64('0'),
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
const jsonString = JSON.stringify(subAuth);
|
|
89
|
+
const encodedData = new TextEncoder().encode(jsonString);
|
|
90
|
+
const topLevelType = AuthenticatorType.ALL_OF;
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
type: topLevelType,
|
|
94
|
+
data: encodedData,
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// redeclared to keep sub bundle size small
|
|
99
|
+
type Awaited<T> = T extends Promise<infer U> ? U : T;
|
|
100
|
+
const isTruthy = <T>(n?: T | false | null | undefined | 0): n is T => Boolean(n);
|
|
101
|
+
|
|
102
|
+
// just parses out keys that match the format created in getAuthorizeNewTradingKeyArguments
|
|
103
|
+
export const getAuthorizedTradingKeysMetadata = (
|
|
104
|
+
authorizedKeys: Awaited<ReturnType<Get['getAuthenticators']>>['accountAuthenticators'],
|
|
105
|
+
): Array<{ id: string; publicKey: string; address: string }> => {
|
|
106
|
+
return authorizedKeys
|
|
107
|
+
.map(({ config, id, type }) => {
|
|
108
|
+
if (type !== AuthenticatorType.ALL_OF) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const parsedConfig = JSON.parse(new TextDecoder().decode(config)) as
|
|
112
|
+
| Authenticator[]
|
|
113
|
+
| undefined;
|
|
114
|
+
if (parsedConfig == null) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const publicKey = parsedConfig.find(
|
|
119
|
+
(t) => t.type === AuthenticatorType.SIGNATURE_VERIFICATION,
|
|
120
|
+
)?.config;
|
|
121
|
+
if (publicKey == null || typeof publicKey !== 'string') {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const address = toBech32(
|
|
126
|
+
BECH32_PREFIX,
|
|
127
|
+
rawSecp256k1PubkeyToRawAddress(fromBase64(publicKey)),
|
|
128
|
+
);
|
|
129
|
+
return {
|
|
130
|
+
id: id.toString(),
|
|
131
|
+
publicKey,
|
|
132
|
+
address,
|
|
133
|
+
};
|
|
134
|
+
})
|
|
135
|
+
.filter(isTruthy);
|
|
136
|
+
};
|
package/src/lib/utils.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { AxiosProxyConfig } from 'axios';
|
|
2
|
+
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
3
|
+
|
|
4
|
+
import { MAX_UINT_32 } from '../clients/constants';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns a random integer value between 0 and (n-1).
|
|
8
|
+
*/
|
|
9
|
+
export function randomInt(n: number): number {
|
|
10
|
+
return Math.floor(Math.random() * n);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Generate a random clientId.
|
|
15
|
+
*/
|
|
16
|
+
export function generateRandomClientId(): number {
|
|
17
|
+
return randomInt(MAX_UINT_32 + 1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Deterministically generate a valid clientId from an arbitrary string by performing a
|
|
22
|
+
* quick hashing function on the string.
|
|
23
|
+
*/
|
|
24
|
+
export function clientIdFromString(input: string): number {
|
|
25
|
+
let hash: number = 0;
|
|
26
|
+
if (input.length === 0) return hash;
|
|
27
|
+
for (let i = 0; i < input.length; i++) {
|
|
28
|
+
hash = (hash << 5) - hash + input.charCodeAt(i); // eslint-disable-line no-bitwise
|
|
29
|
+
hash |= 0; // eslint-disable-line no-bitwise
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Bitwise operators covert the value to a 32-bit integer.
|
|
33
|
+
// We must coerce this into a 32-bit unsigned integer.
|
|
34
|
+
return hash + 2 ** 31;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Pauses the execution of the program for a specified time.
|
|
39
|
+
* @param ms - The number of milliseconds to pause the program.
|
|
40
|
+
* @returns A promise that resolves after the specified number of milliseconds.
|
|
41
|
+
*/
|
|
42
|
+
export async function sleep(ms: number): Promise<void> {
|
|
43
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns a title to use for a gov proposal that adds a new market.
|
|
48
|
+
*
|
|
49
|
+
* @param ticker ticker symbol for the new market.
|
|
50
|
+
* @returns title for the gov proposal.
|
|
51
|
+
*/
|
|
52
|
+
export function getGovAddNewMarketTitle(ticker: string): string {
|
|
53
|
+
return `Add ${ticker} perpetual market`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns a summary to use for a gov proposal that adds a new market.
|
|
58
|
+
*
|
|
59
|
+
* @param ticker ticker symbol for the new market.
|
|
60
|
+
* @param delayBlocks number of blocks to wait before activating the market.
|
|
61
|
+
* @returns summary for the gov proposal.
|
|
62
|
+
*/
|
|
63
|
+
export function getGovAddNewMarketSummary(ticker: string, delayBlocks: number): string {
|
|
64
|
+
return `Add the x/prices, x/perpetuals and x/clob parameters needed for a ${ticker} perpetual market. Create the market in INITIALIZING status and transition it to ACTIVE status after ${delayBlocks} blocks.`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function calculateClockOffsetFromFetchDateHeader(
|
|
68
|
+
clientRequestStartTime: number,
|
|
69
|
+
serverReportedTime: number,
|
|
70
|
+
clientRequestEndTime: number,
|
|
71
|
+
): number {
|
|
72
|
+
// use midpoint, so assume that time for request reach server === time for response to reach us
|
|
73
|
+
const estimatedLocalTimeAtServerArrival = (clientRequestStartTime + clientRequestEndTime) / 2;
|
|
74
|
+
|
|
75
|
+
// we need an offset such that estimatedLocalTimeAtServerArrival + offset = serverReportedTime
|
|
76
|
+
const offset = serverReportedTime - estimatedLocalTimeAtServerArrival;
|
|
77
|
+
|
|
78
|
+
return Math.floor(offset);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function getProxyAgent(proxy: AxiosProxyConfig): HttpsProxyAgent<string> {
|
|
82
|
+
const auth = proxy.auth ? `${proxy.auth.username}:${proxy.auth.password}@` : '';
|
|
83
|
+
return new HttpsProxyAgent(`${proxy.protocol}://${auth}${proxy.host}:${proxy.port}`);
|
|
84
|
+
}
|