@ritbit/v4-client-js 2.4.1 → 2.4.2
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/build/cjs/src/clients/composite-client.js +10 -9
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/esm/src/clients/composite-client.d.ts +3 -3
- package/build/esm/src/clients/composite-client.d.ts.map +1 -1
- package/build/esm/src/clients/composite-client.js +10 -9
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +11 -3
- package/.env +0 -1
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -15
- package/.gitleaks.toml +0 -24
- package/.gitleaksignore +0 -1
- package/.nvmrc +0 -1
- package/.prettierignore +0 -3
- package/.prettierrc.json +0 -6
- package/.releaserc +0 -10
- package/.vscode/launch.json +0 -11
- package/__native__/__ios__/v4-native-client.js +0 -251395
- package/__native__/__ios__/v4-native-client.js.map +0 -1
- package/__tests__/clients/composite-client.test.ts +0 -140
- package/__tests__/helpers/baseClients.ts +0 -23
- package/__tests__/helpers/constants.ts +0 -131
- package/__tests__/lib/helpers.test.ts +0 -55
- package/__tests__/lib/util.test.ts +0 -77
- package/__tests__/lib/validation.test.ts +0 -289
- package/__tests__/modules/client/AccountEndpoints.test.ts +0 -236
- package/__tests__/modules/client/FaucetEndpoint.test.ts +0 -14
- package/__tests__/modules/client/MarketsEndpoints.test.ts +0 -84
- package/__tests__/modules/client/Transfers.test.ts +0 -44
- package/__tests__/modules/client/UtilityEndpoints.test.ts +0 -29
- package/__tests__/modules/client/ValidatorGetEndpoints.test.ts +0 -50
- package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +0 -52
- package/__tests__/modules/client/constants.ts +0 -5
- package/__tests__/modules/onboarding.test.ts +0 -66
- package/build/cjs/__tests__/clients/composite-client.test.js +0 -130
- package/build/cjs/__tests__/helpers/baseClients.js +0 -29
- package/build/cjs/__tests__/helpers/constants.js +0 -110
- package/build/cjs/__tests__/lib/helpers.test.js +0 -46
- package/build/cjs/__tests__/lib/util.test.js +0 -67
- package/build/cjs/__tests__/lib/validation.test.js +0 -254
- package/build/cjs/__tests__/modules/client/AccountEndpoints.test.js +0 -171
- package/build/cjs/__tests__/modules/client/FaucetEndpoint.test.js +0 -15
- package/build/cjs/__tests__/modules/client/MarketsEndpoints.test.js +0 -69
- package/build/cjs/__tests__/modules/client/Transfers.test.js +0 -44
- package/build/cjs/__tests__/modules/client/UtilityEndpoints.test.js +0 -28
- package/build/cjs/__tests__/modules/client/ValidatorGetEndpoints.test.js +0 -46
- package/build/cjs/__tests__/modules/client/ValidatorPostEndpoints.test.js +0 -53
- package/build/cjs/__tests__/modules/client/constants.js +0 -7
- package/build/cjs/__tests__/modules/onboarding.test.js +0 -50
- package/build/cjs/examples/account_endpoints.js +0 -129
- package/build/cjs/examples/batch_cancel_orders_example.js +0 -93
- package/build/cjs/examples/composite_example.js +0 -50
- package/build/cjs/examples/constants.js +0 -35
- package/build/cjs/examples/faucet_endpoint.js +0 -23
- package/build/cjs/examples/gov_add_new_market.js +0 -77
- package/build/cjs/examples/human_readable_orders.json +0 -86
- package/build/cjs/examples/human_readable_short_term_orders.json +0 -42
- package/build/cjs/examples/json-encoding.js +0 -32
- package/build/cjs/examples/long_term_order_cancel_example.js +0 -63
- package/build/cjs/examples/markets_endpoints.js +0 -134
- package/build/cjs/examples/native_examples.js +0 -112
- package/build/cjs/examples/noble_example.js +0 -88
- package/build/cjs/examples/optimal_node.js +0 -70
- package/build/cjs/examples/permissioned_keys_example.js +0 -104
- package/build/cjs/examples/raw_orders.json +0 -130
- package/build/cjs/examples/short_term_order_cancel_example.js +0 -54
- package/build/cjs/examples/short_term_order_composite_example.js +0 -68
- package/build/cjs/examples/test.js +0 -50
- package/build/cjs/examples/transfer_example_deposit.js +0 -29
- package/build/cjs/examples/transfer_example_send.js +0 -42
- package/build/cjs/examples/transfer_example_subaccount_transfer.js +0 -29
- package/build/cjs/examples/transfer_example_withdraw.js +0 -30
- package/build/cjs/examples/transfer_example_withdraw_other.js +0 -42
- package/build/cjs/examples/utility_endpoints.js +0 -42
- package/build/cjs/examples/validator_get_example.js +0 -158
- package/build/cjs/examples/validator_post_example.js +0 -76
- package/build/cjs/examples/wallet_address.js +0 -22
- package/build/cjs/examples/websocket_example.js +0 -36
- package/build/cjs/examples/websocket_orderbook_example.js +0 -196
- package/build/esm/__tests__/clients/composite-client.test.d.ts +0 -2
- package/build/esm/__tests__/clients/composite-client.test.d.ts.map +0 -1
- package/build/esm/__tests__/clients/composite-client.test.js +0 -128
- package/build/esm/__tests__/helpers/baseClients.d.ts +0 -19
- package/build/esm/__tests__/helpers/baseClients.d.ts.map +0 -1
- package/build/esm/__tests__/helpers/baseClients.js +0 -22
- package/build/esm/__tests__/helpers/constants.d.ts +0 -19
- package/build/esm/__tests__/helpers/constants.d.ts.map +0 -1
- package/build/esm/__tests__/helpers/constants.js +0 -104
- package/build/esm/__tests__/lib/helpers.test.d.ts +0 -2
- package/build/esm/__tests__/lib/helpers.test.d.ts.map +0 -1
- package/build/esm/__tests__/lib/helpers.test.js +0 -41
- package/build/esm/__tests__/lib/util.test.d.ts +0 -2
- package/build/esm/__tests__/lib/util.test.d.ts.map +0 -1
- package/build/esm/__tests__/lib/util.test.js +0 -65
- package/build/esm/__tests__/lib/validation.test.d.ts +0 -2
- package/build/esm/__tests__/lib/validation.test.d.ts.map +0 -1
- package/build/esm/__tests__/lib/validation.test.js +0 -249
- package/build/esm/__tests__/modules/client/AccountEndpoints.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/AccountEndpoints.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/AccountEndpoints.test.js +0 -169
- package/build/esm/__tests__/modules/client/FaucetEndpoint.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/FaucetEndpoint.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/FaucetEndpoint.test.js +0 -13
- package/build/esm/__tests__/modules/client/MarketsEndpoints.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/MarketsEndpoints.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/MarketsEndpoints.test.js +0 -66
- package/build/esm/__tests__/modules/client/Transfers.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/Transfers.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/Transfers.test.js +0 -39
- package/build/esm/__tests__/modules/client/UtilityEndpoints.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/UtilityEndpoints.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/UtilityEndpoints.test.js +0 -26
- package/build/esm/__tests__/modules/client/ValidatorGetEndpoints.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/ValidatorGetEndpoints.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/ValidatorGetEndpoints.test.js +0 -44
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.d.ts +0 -2
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.js +0 -48
- package/build/esm/__tests__/modules/client/constants.d.ts +0 -4
- package/build/esm/__tests__/modules/client/constants.d.ts.map +0 -1
- package/build/esm/__tests__/modules/client/constants.js +0 -4
- package/build/esm/__tests__/modules/onboarding.test.d.ts +0 -2
- package/build/esm/__tests__/modules/onboarding.test.d.ts.map +0 -1
- package/build/esm/__tests__/modules/onboarding.test.js +0 -48
- package/build/esm/examples/account_endpoints.d.ts +0 -5
- package/build/esm/examples/account_endpoints.d.ts.map +0 -1
- package/build/esm/examples/account_endpoints.js +0 -127
- package/build/esm/examples/batch_cancel_orders_example.d.ts +0 -2
- package/build/esm/examples/batch_cancel_orders_example.d.ts.map +0 -1
- package/build/esm/examples/batch_cancel_orders_example.js +0 -88
- package/build/esm/examples/composite_example.d.ts +0 -2
- package/build/esm/examples/composite_example.d.ts.map +0 -1
- package/build/esm/examples/composite_example.js +0 -45
- package/build/esm/examples/constants.d.ts +0 -13
- package/build/esm/examples/constants.d.ts.map +0 -1
- package/build/esm/examples/constants.js +0 -29
- package/build/esm/examples/faucet_endpoint.d.ts +0 -5
- package/build/esm/examples/faucet_endpoint.d.ts.map +0 -1
- package/build/esm/examples/faucet_endpoint.js +0 -21
- package/build/esm/examples/gov_add_new_market.d.ts +0 -2
- package/build/esm/examples/gov_add_new_market.d.ts.map +0 -1
- package/build/esm/examples/gov_add_new_market.js +0 -72
- package/build/esm/examples/human_readable_orders.json +0 -86
- package/build/esm/examples/human_readable_short_term_orders.json +0 -42
- package/build/esm/examples/json-encoding.d.ts +0 -2
- package/build/esm/examples/json-encoding.d.ts.map +0 -1
- package/build/esm/examples/json-encoding.js +0 -27
- package/build/esm/examples/long_term_order_cancel_example.d.ts +0 -2
- package/build/esm/examples/long_term_order_cancel_example.d.ts.map +0 -1
- package/build/esm/examples/long_term_order_cancel_example.js +0 -58
- package/build/esm/examples/markets_endpoints.d.ts +0 -5
- package/build/esm/examples/markets_endpoints.d.ts.map +0 -1
- package/build/esm/examples/markets_endpoints.js +0 -131
- package/build/esm/examples/native_examples.d.ts +0 -2
- package/build/esm/examples/native_examples.d.ts.map +0 -1
- package/build/esm/examples/native_examples.js +0 -110
- package/build/esm/examples/noble_example.d.ts +0 -2
- package/build/esm/examples/noble_example.d.ts.map +0 -1
- package/build/esm/examples/noble_example.js +0 -83
- package/build/esm/examples/optimal_node.d.ts +0 -2
- package/build/esm/examples/optimal_node.d.ts.map +0 -1
- package/build/esm/examples/optimal_node.js +0 -68
- package/build/esm/examples/permissioned_keys_example.d.ts +0 -2
- package/build/esm/examples/permissioned_keys_example.d.ts.map +0 -1
- package/build/esm/examples/permissioned_keys_example.js +0 -99
- package/build/esm/examples/raw_orders.json +0 -130
- package/build/esm/examples/short_term_order_cancel_example.d.ts +0 -2
- package/build/esm/examples/short_term_order_cancel_example.d.ts.map +0 -1
- package/build/esm/examples/short_term_order_cancel_example.js +0 -49
- package/build/esm/examples/short_term_order_composite_example.d.ts +0 -2
- package/build/esm/examples/short_term_order_composite_example.d.ts.map +0 -1
- package/build/esm/examples/short_term_order_composite_example.js +0 -63
- package/build/esm/examples/test.d.ts +0 -2
- package/build/esm/examples/test.d.ts.map +0 -1
- package/build/esm/examples/test.js +0 -45
- package/build/esm/examples/transfer_example_deposit.d.ts +0 -2
- package/build/esm/examples/transfer_example_deposit.d.ts.map +0 -1
- package/build/esm/examples/transfer_example_deposit.js +0 -24
- package/build/esm/examples/transfer_example_send.d.ts +0 -2
- package/build/esm/examples/transfer_example_send.d.ts.map +0 -1
- package/build/esm/examples/transfer_example_send.js +0 -37
- package/build/esm/examples/transfer_example_subaccount_transfer.d.ts +0 -2
- package/build/esm/examples/transfer_example_subaccount_transfer.d.ts.map +0 -1
- package/build/esm/examples/transfer_example_subaccount_transfer.js +0 -24
- package/build/esm/examples/transfer_example_withdraw.d.ts +0 -2
- package/build/esm/examples/transfer_example_withdraw.d.ts.map +0 -1
- package/build/esm/examples/transfer_example_withdraw.js +0 -25
- package/build/esm/examples/transfer_example_withdraw_other.d.ts +0 -2
- package/build/esm/examples/transfer_example_withdraw_other.d.ts.map +0 -1
- package/build/esm/examples/transfer_example_withdraw_other.js +0 -37
- package/build/esm/examples/utility_endpoints.d.ts +0 -5
- package/build/esm/examples/utility_endpoints.d.ts.map +0 -1
- package/build/esm/examples/utility_endpoints.js +0 -40
- package/build/esm/examples/validator_get_example.d.ts +0 -2
- package/build/esm/examples/validator_get_example.d.ts.map +0 -1
- package/build/esm/examples/validator_get_example.js +0 -156
- package/build/esm/examples/validator_post_example.d.ts +0 -2
- package/build/esm/examples/validator_post_example.d.ts.map +0 -1
- package/build/esm/examples/validator_post_example.js +0 -71
- package/build/esm/examples/wallet_address.d.ts +0 -2
- package/build/esm/examples/wallet_address.d.ts.map +0 -1
- package/build/esm/examples/wallet_address.js +0 -17
- package/build/esm/examples/websocket_example.d.ts +0 -2
- package/build/esm/examples/websocket_example.d.ts.map +0 -1
- package/build/esm/examples/websocket_example.js +0 -34
- package/build/esm/examples/websocket_orderbook_example.d.ts +0 -2
- package/build/esm/examples/websocket_orderbook_example.d.ts.map +0 -1
- package/build/esm/examples/websocket_orderbook_example.js +0 -194
- package/commitlint.config.js +0 -8
- package/examples/account_endpoints.ts +0 -129
- package/examples/batch_cancel_orders_example.ts +0 -130
- package/examples/composite_example.ts +0 -65
- package/examples/constants.ts +0 -42
- package/examples/faucet_endpoint.ts +0 -24
- package/examples/gov_add_new_market.json +0 -64
- package/examples/gov_add_new_market.ts +0 -95
- package/examples/human_readable_orders.json +0 -86
- package/examples/human_readable_short_term_orders.json +0 -42
- package/examples/json-encoding.ts +0 -33
- package/examples/long_term_order_cancel_example.ts +0 -87
- package/examples/markets_endpoints.ts +0 -134
- package/examples/native_examples.ts +0 -151
- package/examples/noble_example.ts +0 -107
- package/examples/optimal_node.ts +0 -70
- package/examples/permissioned_keys_example.ts +0 -162
- package/examples/raw_orders.json +0 -130
- package/examples/short_term_order_cancel_example.ts +0 -68
- package/examples/short_term_order_composite_example.ts +0 -80
- package/examples/test.ts +0 -65
- package/examples/transfer_example_deposit.ts +0 -28
- package/examples/transfer_example_send.ts +0 -60
- package/examples/transfer_example_subaccount_transfer.ts +0 -29
- package/examples/transfer_example_withdraw.ts +0 -31
- package/examples/transfer_example_withdraw_other.ts +0 -60
- package/examples/utility_endpoints.ts +0 -42
- package/examples/validator_get_example.ts +0 -160
- package/examples/validator_post_example.ts +0 -81
- package/examples/wallet_address.ts +0 -18
- package/examples/websocket_example.ts +0 -44
- package/examples/websocket_orderbook_example.ts +0 -239
- package/jest.config.js +0 -12
- package/jest.globalSetup.js +0 -5
- package/jest.setup.js +0 -1
- package/ritbit-v4-client-js-2.4.0.tgz +0 -0
- package/scripts/bump_version.sh +0 -16
- package/scripts/generate-cctp-protos.sh +0 -41
- package/scripts/mobile_build_pack.sh +0 -5
- package/scripts/publish-if-not-exists.sh +0 -21
- package/src/clients/composite-client.ts +0 -1469
- package/src/clients/constants.ts +0 -361
- package/src/clients/faucet-client.ts +0 -48
- package/src/clients/helpers/chain-helpers.ts +0 -231
- package/src/clients/helpers/request-helpers.ts +0 -71
- package/src/clients/indexer-client.ts +0 -61
- package/src/clients/lib/axios/axiosRequest.ts +0 -41
- package/src/clients/lib/axios/errors.ts +0 -41
- package/src/clients/lib/axios/index.ts +0 -7
- package/src/clients/lib/axios/types.ts +0 -6
- package/src/clients/lib/cctpProto.ts +0 -4197
- package/src/clients/lib/errors.ts +0 -63
- package/src/clients/lib/registry.ts +0 -93
- package/src/clients/modules/account.ts +0 -438
- package/src/clients/modules/composer.ts +0 -669
- package/src/clients/modules/get.ts +0 -791
- package/src/clients/modules/local-wallet.ts +0 -79
- package/src/clients/modules/markets.ts +0 -76
- package/src/clients/modules/post.ts +0 -1043
- package/src/clients/modules/proto-includes.ts +0 -34
- package/src/clients/modules/rest.ts +0 -40
- package/src/clients/modules/signer.ts +0 -139
- package/src/clients/modules/tendermintClient.ts +0 -168
- package/src/clients/modules/utility.ts +0 -47
- package/src/clients/modules/vault.ts +0 -23
- package/src/clients/native.ts +0 -1689
- package/src/clients/noble-client.ts +0 -108
- package/src/clients/socket-client.ts +0 -331
- package/src/clients/subaccount.ts +0 -57
- package/src/clients/types.ts +0 -191
- package/src/clients/validator-client.ts +0 -106
- package/src/index.ts +0 -19
- package/src/lib/constants.ts +0 -28
- package/src/lib/errors.ts +0 -38
- package/src/lib/helpers.ts +0 -99
- package/src/lib/onboarding.ts +0 -96
- package/src/lib/trading-key-utils.ts +0 -136
- package/src/lib/utils.ts +0 -84
- package/src/lib/validation.ts +0 -197
- package/src/network_optimizer.ts +0 -132
- package/src/types.ts +0 -44
- package/tasks.json +0 -16
- package/tsconfig.base.json +0 -21
- package/tsconfig.cjs.json +0 -10
- package/tsconfig.eslint.json +0 -14
- package/tsconfig.esm.json +0 -11
- package/tsconfig.json +0 -6
- package/webpack.config.js +0 -56
package/src/clients/native.ts
DELETED
|
@@ -1,1689 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/*
|
|
3
|
-
Native app can call JS functions with primitives.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { Secp256k1, sha256 } from '@cosmjs/crypto';
|
|
7
|
-
import { EncodeObject, coin as createCoin } from '@cosmjs/proto-signing';
|
|
8
|
-
import { MsgTransferEncodeObject, accountFromAny } from '@cosmjs/stargate';
|
|
9
|
-
import { Method } from '@cosmjs/tendermint-rpc';
|
|
10
|
-
import {
|
|
11
|
-
Order_Side,
|
|
12
|
-
Order_TimeInForce,
|
|
13
|
-
} from '@ritbit/v4-proto/src/codegen/ritbit/clob/order';
|
|
14
|
-
import * as AuthModule from 'cosmjs-types/cosmos/auth/v1beta1/query';
|
|
15
|
-
import Long from 'long';
|
|
16
|
-
|
|
17
|
-
import { BECH32_PREFIX, GAS_MULTIPLIER, NOBLE_BECH32_PREFIX } from '../lib/constants';
|
|
18
|
-
import { UserError } from '../lib/errors';
|
|
19
|
-
import { ByteArrayEncoding, encodeJson } from '../lib/helpers';
|
|
20
|
-
import { deriveHDKeyFromEthereumSignature, deriveHDKeyFromMnemonic } from '../lib/onboarding';
|
|
21
|
-
import { NetworkOptimizer } from '../network_optimizer';
|
|
22
|
-
import { CompositeClient, MarketInfo } from './composite-client';
|
|
23
|
-
import {
|
|
24
|
-
Network,
|
|
25
|
-
OrderType,
|
|
26
|
-
OrderSide,
|
|
27
|
-
OrderTimeInForce,
|
|
28
|
-
OrderExecution,
|
|
29
|
-
IndexerConfig,
|
|
30
|
-
ValidatorConfig,
|
|
31
|
-
SelectedGasDenom,
|
|
32
|
-
} from './constants';
|
|
33
|
-
import { FaucetClient } from './faucet-client';
|
|
34
|
-
import { parseToPrimitives } from './helpers/request-helpers';
|
|
35
|
-
import { Response } from './lib/axios';
|
|
36
|
-
import { FeeTierModule } from './modules/proto-includes';
|
|
37
|
-
import LocalWallet from './modules/local-wallet';
|
|
38
|
-
import { NobleClient } from './noble-client';
|
|
39
|
-
import { SubaccountInfo } from './subaccount';
|
|
40
|
-
import { OrderFlags, SquidIBCPayload } from './types';
|
|
41
|
-
|
|
42
|
-
declare global {
|
|
43
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
44
|
-
var client: CompositeClient;
|
|
45
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
46
|
-
var faucetClient: FaucetClient | null;
|
|
47
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
48
|
-
var wallet: LocalWallet;
|
|
49
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
50
|
-
var hdKey: {
|
|
51
|
-
privateKey: Uint8Array | null;
|
|
52
|
-
publicKey: Uint8Array | null;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
56
|
-
var nobleClient: NobleClient | undefined;
|
|
57
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
58
|
-
var nobleWallet: LocalWallet | undefined;
|
|
59
|
-
// eslint-disable-next-line vars-on-top, no-var
|
|
60
|
-
var validatorUrl: string | undefined;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export async function connectClient(network: Network): Promise<string> {
|
|
64
|
-
try {
|
|
65
|
-
globalThis.client = await CompositeClient.connect(network);
|
|
66
|
-
return encodeJson(network);
|
|
67
|
-
} catch (e) {
|
|
68
|
-
return wrappedError(e);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export async function connectNetwork(paramsJSON: string): Promise<string> {
|
|
73
|
-
try {
|
|
74
|
-
const params = JSON.parse(paramsJSON);
|
|
75
|
-
const {
|
|
76
|
-
indexerUrl,
|
|
77
|
-
websocketUrl,
|
|
78
|
-
validatorUrl,
|
|
79
|
-
chainId,
|
|
80
|
-
faucetUrl,
|
|
81
|
-
nobleValidatorUrl,
|
|
82
|
-
USDC_DENOM,
|
|
83
|
-
USDC_DECIMALS,
|
|
84
|
-
USDC_GAS_DENOM,
|
|
85
|
-
CHAINTOKEN_DENOM,
|
|
86
|
-
CHAINTOKEN_DECIMALS,
|
|
87
|
-
CHAINTOKEN_GAS_DENOM,
|
|
88
|
-
txnMemo,
|
|
89
|
-
} = params;
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
indexerUrl === undefined ||
|
|
93
|
-
websocketUrl === undefined ||
|
|
94
|
-
validatorUrl === undefined ||
|
|
95
|
-
chainId === undefined
|
|
96
|
-
) {
|
|
97
|
-
throw new UserError('Missing required network params');
|
|
98
|
-
}
|
|
99
|
-
if (
|
|
100
|
-
USDC_DENOM === undefined ||
|
|
101
|
-
USDC_DECIMALS === undefined ||
|
|
102
|
-
CHAINTOKEN_DENOM === undefined ||
|
|
103
|
-
CHAINTOKEN_DECIMALS === undefined
|
|
104
|
-
) {
|
|
105
|
-
throw new UserError('Missing required token params');
|
|
106
|
-
}
|
|
107
|
-
if (txnMemo === undefined) {
|
|
108
|
-
throw new UserError('Missing required transaction memo (`txnMemo`)');
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const denomConfig = {
|
|
112
|
-
USDC_DENOM,
|
|
113
|
-
USDC_DECIMALS,
|
|
114
|
-
USDC_GAS_DENOM,
|
|
115
|
-
CHAINTOKEN_DENOM,
|
|
116
|
-
CHAINTOKEN_DECIMALS,
|
|
117
|
-
CHAINTOKEN_GAS_DENOM,
|
|
118
|
-
};
|
|
119
|
-
const indexerConfig = new IndexerConfig(indexerUrl, websocketUrl);
|
|
120
|
-
const validatorConfig = new ValidatorConfig(
|
|
121
|
-
validatorUrl,
|
|
122
|
-
chainId,
|
|
123
|
-
denomConfig,
|
|
124
|
-
undefined,
|
|
125
|
-
txnMemo,
|
|
126
|
-
);
|
|
127
|
-
const config = new Network('native', indexerConfig, validatorConfig);
|
|
128
|
-
globalThis.client = await CompositeClient.connect(config);
|
|
129
|
-
// Сохраняем для waitForBridgeWithdrawal — опрос cosmos-tx через CometBFT RPC.
|
|
130
|
-
globalThis.validatorUrl = validatorUrl;
|
|
131
|
-
if (faucetUrl !== undefined) {
|
|
132
|
-
globalThis.faucetClient = new FaucetClient(faucetUrl);
|
|
133
|
-
} else {
|
|
134
|
-
globalThis.faucetClient = null;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
globalThis.nobleClient = new NobleClient(nobleValidatorUrl, txnMemo);
|
|
139
|
-
if (globalThis.nobleWallet) {
|
|
140
|
-
await globalThis.nobleClient.connect(globalThis.nobleWallet);
|
|
141
|
-
}
|
|
142
|
-
} catch (e) {
|
|
143
|
-
console.error('Failed to connect to noble validator');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return encodeJson(config);
|
|
147
|
-
} catch (e) {
|
|
148
|
-
return wrappedError(e);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export async function connectWallet(mnemonic: string): Promise<string> {
|
|
153
|
-
try {
|
|
154
|
-
globalThis.wallet = await LocalWallet.fromMnemonic(mnemonic, BECH32_PREFIX);
|
|
155
|
-
globalThis.nobleWallet = await LocalWallet.fromMnemonic(mnemonic, NOBLE_BECH32_PREFIX);
|
|
156
|
-
|
|
157
|
-
const { privateKey, publicKey } = deriveHDKeyFromMnemonic(mnemonic);
|
|
158
|
-
globalThis.hdKey = {
|
|
159
|
-
privateKey,
|
|
160
|
-
publicKey,
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
await globalThis.nobleClient?.connect(globalThis.nobleWallet);
|
|
165
|
-
} catch (e) {
|
|
166
|
-
console.error('Failed to connect to noble validator');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const address = globalThis.wallet.address!;
|
|
170
|
-
return encodeJson({ address });
|
|
171
|
-
} catch (e) {
|
|
172
|
-
return wrappedError(e);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export async function connect(network: Network, mnemonic: string): Promise<string> {
|
|
177
|
-
try {
|
|
178
|
-
await connectClient(network);
|
|
179
|
-
return connectWallet(mnemonic);
|
|
180
|
-
} catch (e) {
|
|
181
|
-
return wrappedError(e);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export async function deriveMnemomicFromEthereumSignature(signature: string): Promise<string> {
|
|
186
|
-
try {
|
|
187
|
-
const { mnemonic, privateKey, publicKey } = deriveHDKeyFromEthereumSignature(signature);
|
|
188
|
-
const wallet = await LocalWallet.fromMnemonic(mnemonic, BECH32_PREFIX);
|
|
189
|
-
globalThis.hdKey = {
|
|
190
|
-
privateKey,
|
|
191
|
-
publicKey,
|
|
192
|
-
};
|
|
193
|
-
const result = { mnemonic, address: wallet.address! };
|
|
194
|
-
return new Promise((resolve) => {
|
|
195
|
-
resolve(encodeJson(result));
|
|
196
|
-
});
|
|
197
|
-
} catch (e) {
|
|
198
|
-
return wrappedError(e);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Abacus/Kotlin ждут `absoluteVolumeRequirement` как строку (decimal).
|
|
204
|
-
* `JSON.stringify` protobuf Long в WKWebView часто даёт `{low,high,unsigned}` → kotlinx не парсит `OnChainFeeTier`.
|
|
205
|
-
*/
|
|
206
|
-
function perpetualFeeParamsForAbacusJson(
|
|
207
|
-
response: FeeTierModule.QueryPerpetualFeeParamsResponse,
|
|
208
|
-
): { params: { tiers: Array<Record<string, unknown>> } } {
|
|
209
|
-
const tiers = response.params?.tiers ?? [];
|
|
210
|
-
return {
|
|
211
|
-
params: {
|
|
212
|
-
tiers: tiers.map((t) => {
|
|
213
|
-
const raw = t.absoluteVolumeRequirement as unknown;
|
|
214
|
-
let absVol = '0';
|
|
215
|
-
if (raw !== undefined && raw !== null) {
|
|
216
|
-
if (Long.isLong(raw)) {
|
|
217
|
-
absVol = (raw as Long).toString();
|
|
218
|
-
} else if (typeof (raw as { toString?: () => string }).toString === 'function') {
|
|
219
|
-
absVol = (raw as { toString: () => string }).toString();
|
|
220
|
-
} else {
|
|
221
|
-
absVol = String(raw);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
return {
|
|
225
|
-
name: t.name ?? '',
|
|
226
|
-
absoluteVolumeRequirement: absVol,
|
|
227
|
-
totalVolumeShareRequirementPpm: t.totalVolumeShareRequirementPpm ?? 0,
|
|
228
|
-
makerVolumeShareRequirementPpm: t.makerVolumeShareRequirementPpm ?? 0,
|
|
229
|
-
makerFeePpm: t.makerFeePpm ?? 0,
|
|
230
|
-
takerFeePpm: t.takerFeePpm ?? 0,
|
|
231
|
-
};
|
|
232
|
-
}),
|
|
233
|
-
},
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export async function getHeight(): Promise<string> {
|
|
238
|
-
try {
|
|
239
|
-
const client = globalThis.client;
|
|
240
|
-
if (client === undefined) {
|
|
241
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
242
|
-
}
|
|
243
|
-
const block = await globalThis.client?.validatorClient.get.latestBlock();
|
|
244
|
-
return encodeJson(block);
|
|
245
|
-
} catch (e) {
|
|
246
|
-
return wrappedError(e);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export async function getFeeTiers(): Promise<string> {
|
|
251
|
-
try {
|
|
252
|
-
const client = globalThis.client;
|
|
253
|
-
if (client === undefined) {
|
|
254
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
255
|
-
}
|
|
256
|
-
const feeTiers = await globalThis.client?.validatorClient.get.getFeeTiers();
|
|
257
|
-
return encodeJson(perpetualFeeParamsForAbacusJson(feeTiers));
|
|
258
|
-
} catch (e) {
|
|
259
|
-
return wrappedError(e);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export async function getUserFeeTier(payload: string): Promise<string> {
|
|
264
|
-
try {
|
|
265
|
-
const client = globalThis.client;
|
|
266
|
-
if (client === undefined) {
|
|
267
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
268
|
-
}
|
|
269
|
-
const json = JSON.parse(payload);
|
|
270
|
-
const address = json.address;
|
|
271
|
-
if (address === undefined) {
|
|
272
|
-
throw new UserError('address is not set');
|
|
273
|
-
}
|
|
274
|
-
const feeTiers = await globalThis.client?.validatorClient.get.getUserFeeTier(address);
|
|
275
|
-
return encodeJson(feeTiers);
|
|
276
|
-
} catch (e) {
|
|
277
|
-
return wrappedError(e);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export async function getEquityTiers(): Promise<string> {
|
|
282
|
-
try {
|
|
283
|
-
const client = globalThis.client;
|
|
284
|
-
if (client === undefined) {
|
|
285
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
286
|
-
}
|
|
287
|
-
const equityTiers =
|
|
288
|
-
await globalThis.client?.validatorClient.get.getEquityTierLimitConfiguration();
|
|
289
|
-
return encodeJson(equityTiers, ByteArrayEncoding.BIGINT);
|
|
290
|
-
} catch (e) {
|
|
291
|
-
return wrappedError(e);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export async function getPerpetualMarkets(): Promise<string> {
|
|
296
|
-
try {
|
|
297
|
-
const client = globalThis.client;
|
|
298
|
-
if (client === undefined) {
|
|
299
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
300
|
-
}
|
|
301
|
-
const markets = await globalThis.client?.indexerClient.markets.getPerpetualMarkets();
|
|
302
|
-
return encodeJson(markets);
|
|
303
|
-
} catch (e) {
|
|
304
|
-
return wrappedError(e);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
export async function isWalletConnected(): Promise<string> {
|
|
309
|
-
try {
|
|
310
|
-
const client = globalThis.client;
|
|
311
|
-
if (client === undefined) {
|
|
312
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
313
|
-
}
|
|
314
|
-
const wallet = globalThis.wallet;
|
|
315
|
-
if (wallet === undefined) {
|
|
316
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
317
|
-
}
|
|
318
|
-
return await encodeJson({ result: true });
|
|
319
|
-
} catch (e) {
|
|
320
|
-
return wrappedError(e);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export async function placeOrder(payload: string): Promise<string> {
|
|
325
|
-
try {
|
|
326
|
-
const client = globalThis.client;
|
|
327
|
-
if (client === undefined) {
|
|
328
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
329
|
-
}
|
|
330
|
-
const wallet = globalThis.wallet;
|
|
331
|
-
if (wallet === undefined) {
|
|
332
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
333
|
-
}
|
|
334
|
-
const json = JSON.parse(payload);
|
|
335
|
-
|
|
336
|
-
const subaccountNumber = json.subaccountNumber;
|
|
337
|
-
if (subaccountNumber === undefined) {
|
|
338
|
-
throw new UserError('subaccountNumber is not set');
|
|
339
|
-
}
|
|
340
|
-
const marketId = json.marketId;
|
|
341
|
-
if (marketId === undefined) {
|
|
342
|
-
throw new UserError('marketId is not set');
|
|
343
|
-
}
|
|
344
|
-
const type = json.type;
|
|
345
|
-
if (type === undefined) {
|
|
346
|
-
throw new UserError('type is not set');
|
|
347
|
-
}
|
|
348
|
-
const side = json.side;
|
|
349
|
-
if (side === undefined) {
|
|
350
|
-
throw new UserError('side is not set');
|
|
351
|
-
}
|
|
352
|
-
const price = json.price;
|
|
353
|
-
if (price === undefined) {
|
|
354
|
-
throw new UserError('price is not set');
|
|
355
|
-
}
|
|
356
|
-
// trigger_price: number, // not used for MARKET and LIMIT
|
|
357
|
-
const size = json.size;
|
|
358
|
-
if (size === undefined) {
|
|
359
|
-
throw new UserError('size is not set');
|
|
360
|
-
}
|
|
361
|
-
const clientId = json.clientId;
|
|
362
|
-
if (clientId === undefined) {
|
|
363
|
-
throw new UserError('clientId is not set');
|
|
364
|
-
}
|
|
365
|
-
const timeInForce = json.timeInForce;
|
|
366
|
-
const goodTilTimeInSeconds = json.goodTilTimeInSeconds ?? 0;
|
|
367
|
-
const goodTilBlock = json.goodTilBlock ?? undefined;
|
|
368
|
-
const execution = json.execution;
|
|
369
|
-
const postOnly = json.postOnly ?? false;
|
|
370
|
-
const reduceOnly = json.reduceOnly ?? false;
|
|
371
|
-
const triggerPrice = json.triggerPrice ?? undefined;
|
|
372
|
-
|
|
373
|
-
const marketInfo = (json.marketInfo as MarketInfo) ?? undefined;
|
|
374
|
-
const currentHeight = (json.currentHeight as number) ?? undefined;
|
|
375
|
-
|
|
376
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
377
|
-
const tx = await client.placeOrder(
|
|
378
|
-
subaccount,
|
|
379
|
-
marketId,
|
|
380
|
-
type,
|
|
381
|
-
side,
|
|
382
|
-
price,
|
|
383
|
-
size,
|
|
384
|
-
clientId,
|
|
385
|
-
timeInForce,
|
|
386
|
-
goodTilTimeInSeconds,
|
|
387
|
-
execution,
|
|
388
|
-
postOnly,
|
|
389
|
-
reduceOnly,
|
|
390
|
-
triggerPrice,
|
|
391
|
-
marketInfo,
|
|
392
|
-
currentHeight,
|
|
393
|
-
goodTilBlock,
|
|
394
|
-
);
|
|
395
|
-
return encodeJson(tx);
|
|
396
|
-
} catch (error) {
|
|
397
|
-
return wrappedError(error);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
export function wrappedError(error: Error): string {
|
|
402
|
-
const text = JSON.stringify(error, Object.getOwnPropertyNames(error));
|
|
403
|
-
return `{"error": ${text}}`;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
export async function cancelOrder(payload: string): Promise<string> {
|
|
407
|
-
try {
|
|
408
|
-
const client = globalThis.client;
|
|
409
|
-
if (client === undefined) {
|
|
410
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
411
|
-
}
|
|
412
|
-
const wallet = globalThis.wallet;
|
|
413
|
-
if (wallet === undefined) {
|
|
414
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
415
|
-
}
|
|
416
|
-
const json = JSON.parse(payload);
|
|
417
|
-
|
|
418
|
-
const subaccountNumber = json.subaccountNumber;
|
|
419
|
-
if (subaccountNumber === undefined) {
|
|
420
|
-
throw new UserError('subaccountNumber is not set');
|
|
421
|
-
}
|
|
422
|
-
const clientId = json.clientId;
|
|
423
|
-
if (clientId === undefined) {
|
|
424
|
-
throw new UserError('clientId is not set');
|
|
425
|
-
}
|
|
426
|
-
const orderFlags = json.orderFlags;
|
|
427
|
-
if (orderFlags === undefined) {
|
|
428
|
-
throw new UserError('orderFlags is not set');
|
|
429
|
-
}
|
|
430
|
-
const clobPairId = json.clobPairId;
|
|
431
|
-
if (clobPairId === undefined) {
|
|
432
|
-
throw new UserError('clobPairId is not set');
|
|
433
|
-
}
|
|
434
|
-
const goodTilBlock = json.goodTilBlock;
|
|
435
|
-
const goodTilBlockTime = json.goodTilBlockTime;
|
|
436
|
-
|
|
437
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
438
|
-
const tx = await client.cancelRawOrder(
|
|
439
|
-
subaccount,
|
|
440
|
-
clientId,
|
|
441
|
-
orderFlags,
|
|
442
|
-
clobPairId,
|
|
443
|
-
goodTilBlock !== 0 ? goodTilBlock : undefined,
|
|
444
|
-
goodTilBlockTime !== 0 ? goodTilBlockTime : undefined,
|
|
445
|
-
);
|
|
446
|
-
return encodeJson(tx);
|
|
447
|
-
} catch (error) {
|
|
448
|
-
return wrappedError(error);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
export async function deposit(payload: string): Promise<string> {
|
|
453
|
-
try {
|
|
454
|
-
const client = globalThis.client;
|
|
455
|
-
if (client === undefined) {
|
|
456
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
457
|
-
}
|
|
458
|
-
const wallet = globalThis.wallet;
|
|
459
|
-
if (wallet === undefined) {
|
|
460
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const json = JSON.parse(payload);
|
|
464
|
-
const subaccountNumber = json.subaccountNumber;
|
|
465
|
-
if (subaccountNumber === undefined) {
|
|
466
|
-
throw new UserError('subaccountNumber is not set');
|
|
467
|
-
}
|
|
468
|
-
const amount = json.amount;
|
|
469
|
-
if (amount === undefined) {
|
|
470
|
-
throw new UserError('amount is not set');
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
474
|
-
const tx = await client.depositToSubaccount(subaccount, amount);
|
|
475
|
-
return encodeJson(tx);
|
|
476
|
-
} catch (error) {
|
|
477
|
-
return wrappedError(error);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
export async function withdraw(payload: string): Promise<string> {
|
|
482
|
-
try {
|
|
483
|
-
const client = globalThis.client;
|
|
484
|
-
if (client === undefined) {
|
|
485
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
486
|
-
}
|
|
487
|
-
const wallet = globalThis.wallet;
|
|
488
|
-
if (wallet === undefined) {
|
|
489
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
const json = JSON.parse(payload);
|
|
493
|
-
const subaccountNumber = json.subaccountNumber;
|
|
494
|
-
if (subaccountNumber === undefined) {
|
|
495
|
-
throw new UserError('subaccountNumber is not set');
|
|
496
|
-
}
|
|
497
|
-
const amount = json.amount;
|
|
498
|
-
if (amount === undefined) {
|
|
499
|
-
throw new UserError('amount is not set');
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
503
|
-
const tx = await client.withdrawFromSubaccount(subaccount, amount, json.recipient, json.memo);
|
|
504
|
-
return encodeJson(tx);
|
|
505
|
-
} catch (error) {
|
|
506
|
-
return wrappedError(error);
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
export async function faucet(payload: string): Promise<string> {
|
|
511
|
-
try {
|
|
512
|
-
const faucetClient = globalThis.faucetClient;
|
|
513
|
-
if (!faucetClient) {
|
|
514
|
-
throw new UserError('faucetClient is not connected. Call connectNetwork() first');
|
|
515
|
-
}
|
|
516
|
-
const wallet = globalThis.wallet;
|
|
517
|
-
if (wallet === undefined) {
|
|
518
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
const json = JSON.parse(payload);
|
|
522
|
-
const subaccountNumber = json.subaccountNumber;
|
|
523
|
-
if (subaccountNumber === undefined) {
|
|
524
|
-
throw new UserError('subaccountNumber is not set');
|
|
525
|
-
}
|
|
526
|
-
const amount = json.amount;
|
|
527
|
-
if (amount === undefined) {
|
|
528
|
-
throw new UserError('amount is not set');
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
const response = await faucetClient.fill(wallet.address!, subaccountNumber, amount);
|
|
532
|
-
const sanitized: Response = {
|
|
533
|
-
data: response.data,
|
|
534
|
-
status: response.status,
|
|
535
|
-
headers: response.headers,
|
|
536
|
-
};
|
|
537
|
-
|
|
538
|
-
return encodeJson(sanitized);
|
|
539
|
-
} catch (error) {
|
|
540
|
-
return wrappedError(error);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
export async function withdrawToIBC(
|
|
545
|
-
subaccountNumber: number,
|
|
546
|
-
amount: string,
|
|
547
|
-
payload: string,
|
|
548
|
-
): Promise<string> {
|
|
549
|
-
try {
|
|
550
|
-
const client = globalThis.client;
|
|
551
|
-
if (client === undefined) {
|
|
552
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
553
|
-
}
|
|
554
|
-
const wallet = globalThis.wallet;
|
|
555
|
-
if (wallet === undefined) {
|
|
556
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
const decode = (str: string): string => Buffer.from(str, 'base64').toString('binary');
|
|
560
|
-
const decoded = decode(payload);
|
|
561
|
-
|
|
562
|
-
const json: SquidIBCPayload = JSON.parse(decoded);
|
|
563
|
-
|
|
564
|
-
const ibcMsg: MsgTransferEncodeObject = {
|
|
565
|
-
typeUrl: json.msgTypeUrl, // '/ibc.applications.transfer.v1.MsgTransfer',
|
|
566
|
-
value: {
|
|
567
|
-
...json.msg,
|
|
568
|
-
// Squid returns timeoutTimestamp as Long, but the signer expects BigInt
|
|
569
|
-
timeoutTimestamp: json.msg.timeoutTimestamp
|
|
570
|
-
? BigInt(Long.fromValue(json.msg.timeoutTimestamp).toString())
|
|
571
|
-
: undefined,
|
|
572
|
-
},
|
|
573
|
-
};
|
|
574
|
-
|
|
575
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
576
|
-
const subaccountMsg = client.withdrawFromSubaccountMessage(subaccount, amount);
|
|
577
|
-
|
|
578
|
-
const msgs = [subaccountMsg, ibcMsg];
|
|
579
|
-
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
580
|
-
|
|
581
|
-
const tx = await client.send(
|
|
582
|
-
subaccount,
|
|
583
|
-
() => {
|
|
584
|
-
return encodeObjects;
|
|
585
|
-
},
|
|
586
|
-
false,
|
|
587
|
-
undefined,
|
|
588
|
-
undefined,
|
|
589
|
-
);
|
|
590
|
-
return encodeJson(tx);
|
|
591
|
-
} catch (error) {
|
|
592
|
-
return wrappedError(error);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
export async function transferNativeToken(payload: string): Promise<string> {
|
|
597
|
-
try {
|
|
598
|
-
const client = globalThis.client;
|
|
599
|
-
if (client === undefined) {
|
|
600
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
601
|
-
}
|
|
602
|
-
const wallet = globalThis.wallet;
|
|
603
|
-
if (wallet === undefined) {
|
|
604
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
const json = JSON.parse(payload);
|
|
608
|
-
const amount = json.amount;
|
|
609
|
-
if (amount === undefined) {
|
|
610
|
-
throw new UserError('amount is not set');
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
const msg: EncodeObject = client.sendTokenMessage(wallet, amount, json.recipient);
|
|
614
|
-
const msgs = [msg];
|
|
615
|
-
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
616
|
-
|
|
617
|
-
const tx = await client.send(
|
|
618
|
-
SubaccountInfo.forLocalWallet(wallet),
|
|
619
|
-
() => {
|
|
620
|
-
return encodeObjects;
|
|
621
|
-
},
|
|
622
|
-
false,
|
|
623
|
-
client.validatorClient.post.defaultChainGasPrice,
|
|
624
|
-
json.memo,
|
|
625
|
-
);
|
|
626
|
-
return encodeJson(tx);
|
|
627
|
-
} catch (error) {
|
|
628
|
-
return wrappedError(error);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
export async function getAccountBalance(): Promise<String> {
|
|
633
|
-
try {
|
|
634
|
-
const client = globalThis.client;
|
|
635
|
-
if (client === undefined) {
|
|
636
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
637
|
-
}
|
|
638
|
-
const wallet = globalThis.wallet;
|
|
639
|
-
if (wallet === undefined) {
|
|
640
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
641
|
-
}
|
|
642
|
-
const address = globalThis.wallet.address!;
|
|
643
|
-
|
|
644
|
-
const tx = await client.validatorClient.get.getAccountBalance(
|
|
645
|
-
address,
|
|
646
|
-
client.validatorClient.config.denoms.USDC_DENOM,
|
|
647
|
-
);
|
|
648
|
-
return encodeJson(tx);
|
|
649
|
-
} catch (error) {
|
|
650
|
-
return wrappedError(error);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
export async function getAccountBalances(): Promise<String> {
|
|
655
|
-
try {
|
|
656
|
-
const client = globalThis.client;
|
|
657
|
-
if (client === undefined) {
|
|
658
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
659
|
-
}
|
|
660
|
-
const wallet = globalThis.wallet;
|
|
661
|
-
if (wallet === undefined) {
|
|
662
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
663
|
-
}
|
|
664
|
-
const address = globalThis.wallet.address!;
|
|
665
|
-
|
|
666
|
-
const tx = await client.validatorClient.get.getAccountBalances(address);
|
|
667
|
-
return encodeJson(tx);
|
|
668
|
-
} catch (error) {
|
|
669
|
-
return wrappedError(error);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
export async function getUserStats(payload: string): Promise<String> {
|
|
674
|
-
try {
|
|
675
|
-
const client = globalThis.client;
|
|
676
|
-
if (client === undefined) {
|
|
677
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
678
|
-
}
|
|
679
|
-
const json = JSON.parse(payload);
|
|
680
|
-
const address = json.address;
|
|
681
|
-
if (address === undefined) {
|
|
682
|
-
throw new UserError('address is not set');
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
const tx = await client.validatorClient.get.getUserStats(address);
|
|
686
|
-
return encodeJson(tx);
|
|
687
|
-
} catch (error) {
|
|
688
|
-
return wrappedError(error);
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
export async function simulateDeposit(payload: string): Promise<string> {
|
|
693
|
-
try {
|
|
694
|
-
const client = globalThis.client;
|
|
695
|
-
if (client === undefined) {
|
|
696
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
697
|
-
}
|
|
698
|
-
const wallet = globalThis.wallet;
|
|
699
|
-
if (wallet === undefined) {
|
|
700
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
701
|
-
}
|
|
702
|
-
const json = JSON.parse(payload);
|
|
703
|
-
const subaccountNumber = json.subaccountNumber;
|
|
704
|
-
if (subaccountNumber === undefined) {
|
|
705
|
-
throw new UserError('subaccountNumber is not set');
|
|
706
|
-
}
|
|
707
|
-
const amount = json.amount;
|
|
708
|
-
if (amount === undefined) {
|
|
709
|
-
throw new UserError('amount is not set');
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
713
|
-
const msg: EncodeObject = client.depositToSubaccountMessage(subaccount, amount);
|
|
714
|
-
const msgs: EncodeObject[] = [msg];
|
|
715
|
-
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
716
|
-
|
|
717
|
-
const stdFee = await client.simulate(subaccount, () => {
|
|
718
|
-
return encodeObjects;
|
|
719
|
-
});
|
|
720
|
-
return JSON.stringify(stdFee);
|
|
721
|
-
} catch (error) {
|
|
722
|
-
return wrappedError(error);
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
export async function simulateWithdraw(payload: string): Promise<string> {
|
|
727
|
-
try {
|
|
728
|
-
const client = globalThis.client;
|
|
729
|
-
if (client === undefined) {
|
|
730
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
731
|
-
}
|
|
732
|
-
const wallet = globalThis.wallet;
|
|
733
|
-
if (wallet === undefined) {
|
|
734
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
735
|
-
}
|
|
736
|
-
const json = JSON.parse(payload);
|
|
737
|
-
const subaccountNumber = json.subaccountNumber;
|
|
738
|
-
if (subaccountNumber === undefined) {
|
|
739
|
-
throw new UserError('subaccountNumber is not set');
|
|
740
|
-
}
|
|
741
|
-
const amount = json.amount;
|
|
742
|
-
if (amount === undefined) {
|
|
743
|
-
throw new UserError('amount is not set');
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
747
|
-
const msg: EncodeObject = client.withdrawFromSubaccountMessage(
|
|
748
|
-
subaccount,
|
|
749
|
-
amount,
|
|
750
|
-
json.recipient,
|
|
751
|
-
);
|
|
752
|
-
const msgs: EncodeObject[] = [msg];
|
|
753
|
-
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
754
|
-
|
|
755
|
-
const stdFee = await client.simulate(subaccount, () => {
|
|
756
|
-
return encodeObjects;
|
|
757
|
-
});
|
|
758
|
-
return encodeJson(stdFee);
|
|
759
|
-
} catch (error) {
|
|
760
|
-
return wrappedError(error);
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
export async function simulateTransferNativeToken(payload: string): Promise<string> {
|
|
765
|
-
try {
|
|
766
|
-
const client = globalThis.client;
|
|
767
|
-
if (client === undefined) {
|
|
768
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
769
|
-
}
|
|
770
|
-
const wallet = globalThis.wallet;
|
|
771
|
-
if (wallet === undefined) {
|
|
772
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
773
|
-
}
|
|
774
|
-
const json = JSON.parse(payload);
|
|
775
|
-
const recipient = json.recipient;
|
|
776
|
-
if (recipient === undefined) {
|
|
777
|
-
throw new UserError('recipient is not set');
|
|
778
|
-
}
|
|
779
|
-
const amount = json.amount;
|
|
780
|
-
if (amount === undefined) {
|
|
781
|
-
throw new UserError('amount is not set');
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
const msg: EncodeObject = client.sendTokenMessage(wallet, amount, json.recipient);
|
|
785
|
-
const msgs: EncodeObject[] = [msg];
|
|
786
|
-
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
787
|
-
|
|
788
|
-
const stdFee = await client.simulate(
|
|
789
|
-
SubaccountInfo.forLocalWallet(wallet),
|
|
790
|
-
() => {
|
|
791
|
-
return encodeObjects;
|
|
792
|
-
},
|
|
793
|
-
client.validatorClient.post.defaultChainGasPrice,
|
|
794
|
-
);
|
|
795
|
-
return encodeJson(stdFee);
|
|
796
|
-
} catch (error) {
|
|
797
|
-
return wrappedError(error);
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
export async function signRawPlaceOrder(
|
|
802
|
-
subaccountNumber: number,
|
|
803
|
-
clientId: number,
|
|
804
|
-
clobPairId: number,
|
|
805
|
-
side: Order_Side,
|
|
806
|
-
quantums: Long,
|
|
807
|
-
subticks: Long,
|
|
808
|
-
timeInForce: Order_TimeInForce,
|
|
809
|
-
orderFlags: number,
|
|
810
|
-
reduceOnly: boolean,
|
|
811
|
-
goodTilBlock: number,
|
|
812
|
-
goodTilBlockTime: number,
|
|
813
|
-
clientMetadata: number,
|
|
814
|
-
): Promise<string> {
|
|
815
|
-
try {
|
|
816
|
-
const client = globalThis.client;
|
|
817
|
-
if (client === undefined) {
|
|
818
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
819
|
-
}
|
|
820
|
-
const wallet = globalThis.wallet;
|
|
821
|
-
if (wallet === undefined) {
|
|
822
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
|
|
826
|
-
const msg = client.validatorClient.post.composer.composeMsgPlaceOrder(
|
|
827
|
-
wallet.address!,
|
|
828
|
-
subaccountNumber,
|
|
829
|
-
clientId,
|
|
830
|
-
clobPairId,
|
|
831
|
-
orderFlags,
|
|
832
|
-
goodTilBlock,
|
|
833
|
-
goodTilBlockTime,
|
|
834
|
-
side,
|
|
835
|
-
quantums,
|
|
836
|
-
subticks,
|
|
837
|
-
timeInForce,
|
|
838
|
-
reduceOnly,
|
|
839
|
-
clientMetadata ?? 0,
|
|
840
|
-
);
|
|
841
|
-
resolve([msg]);
|
|
842
|
-
});
|
|
843
|
-
const signed = await client.sign(
|
|
844
|
-
SubaccountInfo.forLocalWallet(wallet, subaccountNumber),
|
|
845
|
-
() => msgs,
|
|
846
|
-
true,
|
|
847
|
-
);
|
|
848
|
-
return Buffer.from(signed).toString('base64');
|
|
849
|
-
} catch (error) {
|
|
850
|
-
return wrappedError(error);
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
export async function signPlaceOrder(
|
|
855
|
-
subaccountNumber: number,
|
|
856
|
-
marketId: string,
|
|
857
|
-
type: OrderType,
|
|
858
|
-
side: OrderSide,
|
|
859
|
-
price: number,
|
|
860
|
-
// trigger_price: number, // not used for MARKET and LIMIT
|
|
861
|
-
size: number,
|
|
862
|
-
clientId: number,
|
|
863
|
-
timeInForce: OrderTimeInForce,
|
|
864
|
-
goodTilTimeInSeconds: number,
|
|
865
|
-
execution: OrderExecution,
|
|
866
|
-
postOnly: boolean,
|
|
867
|
-
reduceOnly: boolean,
|
|
868
|
-
): Promise<string> {
|
|
869
|
-
try {
|
|
870
|
-
const client = globalThis.client;
|
|
871
|
-
if (client === undefined) {
|
|
872
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
873
|
-
}
|
|
874
|
-
const wallet = globalThis.wallet;
|
|
875
|
-
if (wallet === undefined) {
|
|
876
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
880
|
-
const signed = await client.signPlaceOrder(
|
|
881
|
-
subaccount,
|
|
882
|
-
marketId,
|
|
883
|
-
type,
|
|
884
|
-
side,
|
|
885
|
-
price,
|
|
886
|
-
size,
|
|
887
|
-
clientId,
|
|
888
|
-
timeInForce,
|
|
889
|
-
goodTilTimeInSeconds,
|
|
890
|
-
execution,
|
|
891
|
-
postOnly,
|
|
892
|
-
reduceOnly,
|
|
893
|
-
);
|
|
894
|
-
return signed;
|
|
895
|
-
} catch (error) {
|
|
896
|
-
return wrappedError(error);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
export async function signCancelOrder(
|
|
901
|
-
subaccountNumber: number,
|
|
902
|
-
clientId: number,
|
|
903
|
-
orderFlags: OrderFlags,
|
|
904
|
-
clobPairId: number,
|
|
905
|
-
goodTilBlock: number,
|
|
906
|
-
goodTilBlockTime: number,
|
|
907
|
-
): Promise<string> {
|
|
908
|
-
try {
|
|
909
|
-
const client = globalThis.client;
|
|
910
|
-
if (client === undefined) {
|
|
911
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
912
|
-
}
|
|
913
|
-
const wallet = globalThis.wallet;
|
|
914
|
-
if (wallet === undefined) {
|
|
915
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
919
|
-
const signed = await client.signCancelOrder(
|
|
920
|
-
subaccount,
|
|
921
|
-
clientId,
|
|
922
|
-
orderFlags,
|
|
923
|
-
clobPairId,
|
|
924
|
-
goodTilBlock,
|
|
925
|
-
goodTilBlockTime,
|
|
926
|
-
);
|
|
927
|
-
return signed;
|
|
928
|
-
} catch (error) {
|
|
929
|
-
return wrappedError(error);
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
export async function encodeAccountRequestData(address: string): Promise<string> {
|
|
934
|
-
return new Promise((resolve, reject) => {
|
|
935
|
-
try {
|
|
936
|
-
const requestData: Uint8Array = Uint8Array.from(
|
|
937
|
-
AuthModule.QueryAccountRequest.encode({ address }).finish(),
|
|
938
|
-
);
|
|
939
|
-
resolve(Buffer.from(requestData).toString('hex'));
|
|
940
|
-
} catch (error) {
|
|
941
|
-
reject(error);
|
|
942
|
-
}
|
|
943
|
-
});
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
export async function decodeAccountResponseValue(value: string): Promise<string> {
|
|
947
|
-
return new Promise((resolve, reject) => {
|
|
948
|
-
try {
|
|
949
|
-
const rawData = Buffer.from(value, 'base64');
|
|
950
|
-
const rawAccount = AuthModule.QueryAccountResponse.decode(rawData).account;
|
|
951
|
-
// The promise should have been rejected if the rawAccount was undefined.
|
|
952
|
-
if (rawAccount === undefined) {
|
|
953
|
-
throw Error('rawAccount is undefined');
|
|
954
|
-
}
|
|
955
|
-
const account = accountFromAny(rawAccount);
|
|
956
|
-
resolve(encodeJson(account));
|
|
957
|
-
} catch (error) {
|
|
958
|
-
reject(error);
|
|
959
|
-
}
|
|
960
|
-
});
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
export async function getOptimalNode(endpointUrlsAsJson: string): Promise<string> {
|
|
964
|
-
/*
|
|
965
|
-
param:
|
|
966
|
-
endpointUrlsAsJson:
|
|
967
|
-
{
|
|
968
|
-
"endpointUrls": [
|
|
969
|
-
"https://rpc.testnet.near.org"
|
|
970
|
-
],
|
|
971
|
-
"chainId": "testnet"
|
|
972
|
-
}
|
|
973
|
-
*/
|
|
974
|
-
try {
|
|
975
|
-
const param = JSON.parse(endpointUrlsAsJson);
|
|
976
|
-
const endpointUrls = param.endpointUrls;
|
|
977
|
-
const chainId = param.chainId;
|
|
978
|
-
const networkOptimizer = new NetworkOptimizer();
|
|
979
|
-
const optimalUrl = await networkOptimizer.findOptimalNode(endpointUrls, chainId);
|
|
980
|
-
const url = {
|
|
981
|
-
url: optimalUrl,
|
|
982
|
-
};
|
|
983
|
-
return encodeJson(url);
|
|
984
|
-
} catch (error) {
|
|
985
|
-
return wrappedError(error);
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
export async function getOptimalIndexer(endpointUrlsAsJson: string): Promise<string> {
|
|
990
|
-
/*
|
|
991
|
-
param:
|
|
992
|
-
endpointUrlsAsJson:
|
|
993
|
-
{
|
|
994
|
-
"endpointUrls": [
|
|
995
|
-
"https://api.example.org"
|
|
996
|
-
]
|
|
997
|
-
}
|
|
998
|
-
*/
|
|
999
|
-
try {
|
|
1000
|
-
const param = JSON.parse(endpointUrlsAsJson);
|
|
1001
|
-
const endpointUrls = param.endpointUrls;
|
|
1002
|
-
const networkOptimizer = new NetworkOptimizer();
|
|
1003
|
-
const optimalUrl = await networkOptimizer.findOptimalIndexer(endpointUrls);
|
|
1004
|
-
const url = {
|
|
1005
|
-
url: optimalUrl,
|
|
1006
|
-
};
|
|
1007
|
-
return encodeJson(url);
|
|
1008
|
-
} catch (error) {
|
|
1009
|
-
return wrappedError(error);
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
export async function getRewardsParams(): Promise<string> {
|
|
1014
|
-
try {
|
|
1015
|
-
const client = globalThis.client;
|
|
1016
|
-
if (client === undefined) {
|
|
1017
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1018
|
-
}
|
|
1019
|
-
const rewardsParams = await globalThis.client?.validatorClient.get.getRewardsParams();
|
|
1020
|
-
return encodeJson(rewardsParams);
|
|
1021
|
-
} catch (e) {
|
|
1022
|
-
return wrappedError(e);
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
|
-
export async function getDelegatorDelegations(payload: string): Promise<string> {
|
|
1027
|
-
try {
|
|
1028
|
-
const client = globalThis.client;
|
|
1029
|
-
if (client === undefined) {
|
|
1030
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1031
|
-
}
|
|
1032
|
-
const json = JSON.parse(payload);
|
|
1033
|
-
const address = json.address;
|
|
1034
|
-
if (address === undefined) {
|
|
1035
|
-
throw new UserError('address is not set');
|
|
1036
|
-
}
|
|
1037
|
-
const delegations =
|
|
1038
|
-
await globalThis.client?.validatorClient.get.getDelegatorDelegations(address);
|
|
1039
|
-
return encodeJson(delegations);
|
|
1040
|
-
} catch (e) {
|
|
1041
|
-
return wrappedError(e);
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
|
-
export async function getStakingRewards(payload: string): Promise<string> {
|
|
1046
|
-
try {
|
|
1047
|
-
const client = globalThis.client;
|
|
1048
|
-
if (client === undefined) {
|
|
1049
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1050
|
-
}
|
|
1051
|
-
const json = JSON.parse(payload);
|
|
1052
|
-
const address = json.address;
|
|
1053
|
-
if (address === undefined) {
|
|
1054
|
-
throw new UserError('address is not set');
|
|
1055
|
-
}
|
|
1056
|
-
const delegations =
|
|
1057
|
-
await globalThis.client?.validatorClient.get.getDelegationTotalRewards(address);
|
|
1058
|
-
return encodeJson(delegations);
|
|
1059
|
-
} catch (e) {
|
|
1060
|
-
return wrappedError(e);
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
export async function getCurrentUnstaking(payload: string): Promise<string> {
|
|
1065
|
-
try {
|
|
1066
|
-
const client = globalThis.client;
|
|
1067
|
-
if (client === undefined) {
|
|
1068
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1069
|
-
}
|
|
1070
|
-
const json = JSON.parse(payload);
|
|
1071
|
-
const address = json.address;
|
|
1072
|
-
if (address === undefined) {
|
|
1073
|
-
throw new UserError('address is not set');
|
|
1074
|
-
}
|
|
1075
|
-
const delegations =
|
|
1076
|
-
await globalThis.client?.validatorClient.get.getDelegatorUnbondingDelegations(address);
|
|
1077
|
-
return encodeJson(delegations);
|
|
1078
|
-
} catch (e) {
|
|
1079
|
-
return wrappedError(e);
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
export async function getMarketPrice(payload: string): Promise<string> {
|
|
1084
|
-
try {
|
|
1085
|
-
const client = globalThis.client;
|
|
1086
|
-
if (client === undefined) {
|
|
1087
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1088
|
-
}
|
|
1089
|
-
const json = JSON.parse(payload);
|
|
1090
|
-
const marketId = json.marketId;
|
|
1091
|
-
if (marketId === undefined) {
|
|
1092
|
-
throw new UserError('marketId is not set');
|
|
1093
|
-
}
|
|
1094
|
-
const marketPrice = await client.validatorClient.get.getPrice(marketId);
|
|
1095
|
-
return encodeJson(marketPrice);
|
|
1096
|
-
} catch (e) {
|
|
1097
|
-
return wrappedError(e);
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
export async function getNobleBalance(): Promise<String> {
|
|
1102
|
-
try {
|
|
1103
|
-
const client = globalThis.nobleClient;
|
|
1104
|
-
if (client === undefined || !client.isConnected) {
|
|
1105
|
-
throw new UserError('client is not connected.');
|
|
1106
|
-
}
|
|
1107
|
-
const coin = await client.getAccountBalance('uusdc');
|
|
1108
|
-
return encodeJson(coin);
|
|
1109
|
-
} catch (error) {
|
|
1110
|
-
return wrappedError(error);
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
|
-
export async function sendNobleIBC(squidPayload: string): Promise<String> {
|
|
1115
|
-
try {
|
|
1116
|
-
const client = globalThis.nobleClient;
|
|
1117
|
-
if (client === undefined || !client.isConnected) {
|
|
1118
|
-
throw new UserError('client is not connected.');
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
const json: SquidIBCPayload = JSON.parse(squidPayload);
|
|
1122
|
-
|
|
1123
|
-
const ibcMsg: MsgTransferEncodeObject = {
|
|
1124
|
-
typeUrl: json.msgTypeUrl,
|
|
1125
|
-
value: {
|
|
1126
|
-
...json.msg,
|
|
1127
|
-
// Squid returns timeoutTimestamp as Long, but the signer expects BigInt
|
|
1128
|
-
timeoutTimestamp: json.msg.timeoutTimestamp
|
|
1129
|
-
? BigInt(Long.fromValue(json.msg.timeoutTimestamp).toString())
|
|
1130
|
-
: undefined,
|
|
1131
|
-
},
|
|
1132
|
-
};
|
|
1133
|
-
const fee = await client.simulateTransaction([ibcMsg]);
|
|
1134
|
-
|
|
1135
|
-
if (!ibcMsg.value.token) {
|
|
1136
|
-
throw new UserError('Payload missing token field');
|
|
1137
|
-
}
|
|
1138
|
-
|
|
1139
|
-
// take out fee from amount before sweeping
|
|
1140
|
-
const amount =
|
|
1141
|
-
parseInt(ibcMsg.value.token.amount, 10) -
|
|
1142
|
-
Math.floor(parseInt(fee.amount[0].amount, 10) * GAS_MULTIPLIER);
|
|
1143
|
-
|
|
1144
|
-
if (amount <= 0) {
|
|
1145
|
-
throw new UserError('noble balance does not cover fees');
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
ibcMsg.value.token = createCoin(amount.toString(), ibcMsg.value.token.denom);
|
|
1149
|
-
const tx = await client.IBCTransfer(ibcMsg);
|
|
1150
|
-
return encodeJson(tx);
|
|
1151
|
-
} catch (error) {
|
|
1152
|
-
return wrappedError(error);
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
export async function withdrawToNobleIBC(payload: string): Promise<String> {
|
|
1157
|
-
try {
|
|
1158
|
-
const client = globalThis.client;
|
|
1159
|
-
if (client === undefined) {
|
|
1160
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1161
|
-
}
|
|
1162
|
-
const wallet = globalThis.wallet;
|
|
1163
|
-
if (wallet === undefined) {
|
|
1164
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1165
|
-
}
|
|
1166
|
-
const json = JSON.parse(payload);
|
|
1167
|
-
|
|
1168
|
-
const { subaccountNumber, amount, ibcPayload } = json ?? {};
|
|
1169
|
-
|
|
1170
|
-
const decode = (str: string): string => Buffer.from(str, 'base64').toString('binary');
|
|
1171
|
-
const decoded = decode(ibcPayload);
|
|
1172
|
-
|
|
1173
|
-
const parsedIbcPayload: SquidIBCPayload = JSON.parse(decoded);
|
|
1174
|
-
|
|
1175
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1176
|
-
const msg = client.withdrawFromSubaccountMessage(
|
|
1177
|
-
subaccount,
|
|
1178
|
-
parseFloat(amount).toFixed(client.validatorClient.config.denoms.USDC_DECIMALS),
|
|
1179
|
-
);
|
|
1180
|
-
const ibcMsg: MsgTransferEncodeObject = {
|
|
1181
|
-
typeUrl: parsedIbcPayload.msgTypeUrl,
|
|
1182
|
-
value: {
|
|
1183
|
-
...parsedIbcPayload.msg,
|
|
1184
|
-
// Squid returns timeoutTimestamp as Long, but the signer expects BigInt
|
|
1185
|
-
timeoutTimestamp: parsedIbcPayload.msg.timeoutTimestamp
|
|
1186
|
-
? BigInt(Long.fromValue(parsedIbcPayload.msg.timeoutTimestamp).toString())
|
|
1187
|
-
: undefined,
|
|
1188
|
-
},
|
|
1189
|
-
};
|
|
1190
|
-
|
|
1191
|
-
const tx = await client.send(subaccount, () => Promise.resolve([msg, ibcMsg]), false);
|
|
1192
|
-
|
|
1193
|
-
return encodeJson({
|
|
1194
|
-
txHash: `0x${Buffer.from(tx?.hash).toString('hex')}`,
|
|
1195
|
-
});
|
|
1196
|
-
} catch (error) {
|
|
1197
|
-
return wrappedError(error);
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
|
|
1201
|
-
export async function cctpWithdraw(squidPayload: string): Promise<String> {
|
|
1202
|
-
try {
|
|
1203
|
-
const client = globalThis.nobleClient;
|
|
1204
|
-
if (client === undefined || !client.isConnected) {
|
|
1205
|
-
throw new UserError('client is not connected.');
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
const json = JSON.parse(squidPayload);
|
|
1209
|
-
|
|
1210
|
-
const ibcMsg = {
|
|
1211
|
-
typeUrl: json.typeUrl, // '/circle.cctp.v1.MsgDepositForBurn',
|
|
1212
|
-
value: json.value,
|
|
1213
|
-
};
|
|
1214
|
-
const fee = await client.simulateTransaction([ibcMsg]);
|
|
1215
|
-
|
|
1216
|
-
// take out fee from amount before sweeping
|
|
1217
|
-
const amount =
|
|
1218
|
-
parseInt(ibcMsg.value.amount, 10) -
|
|
1219
|
-
Math.floor(parseInt(fee.amount[0].amount, 10) * GAS_MULTIPLIER);
|
|
1220
|
-
|
|
1221
|
-
if (amount <= 0) {
|
|
1222
|
-
throw new Error('noble balance does not cover fees');
|
|
1223
|
-
}
|
|
1224
|
-
|
|
1225
|
-
ibcMsg.value.amount = amount.toString();
|
|
1226
|
-
|
|
1227
|
-
const tx = await client.send([ibcMsg]);
|
|
1228
|
-
|
|
1229
|
-
return encodeJson(tx);
|
|
1230
|
-
} catch (error) {
|
|
1231
|
-
return wrappedError(error);
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
export async function cctpMultiMsgWithdraw(cosmosPayload: string): Promise<string> {
|
|
1236
|
-
try {
|
|
1237
|
-
const client = globalThis.nobleClient;
|
|
1238
|
-
const messages: { typeUrl: string; value: { amount: string } }[] = JSON.parse(cosmosPayload);
|
|
1239
|
-
if (client === undefined || !client.isConnected) {
|
|
1240
|
-
throw new UserError('client is not connected.');
|
|
1241
|
-
}
|
|
1242
|
-
const ibcMsgs = messages.map(({ typeUrl, value }) => ({
|
|
1243
|
-
typeUrl, // '/circle.cctp.v1.MsgDepositForBurnWithCaller', '/cosmos.bank.v1beta1.MsgSend'
|
|
1244
|
-
value,
|
|
1245
|
-
}));
|
|
1246
|
-
|
|
1247
|
-
const fee = await client.simulateTransaction(ibcMsgs);
|
|
1248
|
-
|
|
1249
|
-
// take out fee from amount before sweeping
|
|
1250
|
-
const amount =
|
|
1251
|
-
parseInt(ibcMsgs[0].value.amount, 10) -
|
|
1252
|
-
Math.floor(parseInt(fee.amount[0].amount, 10) * GAS_MULTIPLIER);
|
|
1253
|
-
|
|
1254
|
-
if (amount <= 0) {
|
|
1255
|
-
throw new Error('noble balance does not cover fees');
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
ibcMsgs[0].value.amount = amount.toString();
|
|
1259
|
-
|
|
1260
|
-
const tx = await client.send(ibcMsgs);
|
|
1261
|
-
|
|
1262
|
-
return encodeJson(tx);
|
|
1263
|
-
} catch (error) {
|
|
1264
|
-
return wrappedError(error);
|
|
1265
|
-
}
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1268
|
-
export async function getWithdrawalCapacityByDenom(payload: string): Promise<string> {
|
|
1269
|
-
try {
|
|
1270
|
-
const client = globalThis.client;
|
|
1271
|
-
if (client === undefined) {
|
|
1272
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1273
|
-
}
|
|
1274
|
-
const json = JSON.parse(payload);
|
|
1275
|
-
const denom = json.denom;
|
|
1276
|
-
if (denom === undefined) {
|
|
1277
|
-
throw new UserError('denom is not set');
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
const response = await client.validatorClient.get.getWithdrawalCapacityByDenom(denom);
|
|
1281
|
-
return encodeJson(response, ByteArrayEncoding.BIGINT);
|
|
1282
|
-
} catch (error) {
|
|
1283
|
-
return wrappedError(error);
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
|
-
/** Вызывается из iOS `JavascriptRunner.invoke` как глобальная функция (без `client.`). */
|
|
1288
|
-
export async function getBridgeEventParams(): Promise<string> {
|
|
1289
|
-
try {
|
|
1290
|
-
const client = globalThis.client;
|
|
1291
|
-
if (client === undefined) {
|
|
1292
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
1293
|
-
}
|
|
1294
|
-
const response = await client.validatorClient.get.getBridgeEventParams();
|
|
1295
|
-
return encodeJson(response, ByteArrayEncoding.BIGINT);
|
|
1296
|
-
} catch (error) {
|
|
1297
|
-
return wrappedError(error);
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
|
|
1301
|
-
/** Вызывается из iOS `JavascriptRunner.invoke` как глобальная функция (без `client.`). */
|
|
1302
|
-
export async function getBridgeWithdrawalParams(): Promise<string> {
|
|
1303
|
-
try {
|
|
1304
|
-
const client = globalThis.client;
|
|
1305
|
-
if (client === undefined) {
|
|
1306
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
1307
|
-
}
|
|
1308
|
-
const response = await client.validatorClient.get.getBridgeWithdrawalParams();
|
|
1309
|
-
return encodeJson(response, ByteArrayEncoding.BIGINT);
|
|
1310
|
-
} catch (error) {
|
|
1311
|
-
return wrappedError(error);
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
/** Обратный мост: cosmos → EVM. Создаёт MsgWithdrawal и рассылает транзакцию. */
|
|
1316
|
-
export async function sendBridgeWithdrawal(payload: string): Promise<string> {
|
|
1317
|
-
try {
|
|
1318
|
-
const client = globalThis.client;
|
|
1319
|
-
if (client === undefined) {
|
|
1320
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
1321
|
-
}
|
|
1322
|
-
const wallet = globalThis.wallet;
|
|
1323
|
-
if (wallet === undefined) {
|
|
1324
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
|
-
const json = JSON.parse(payload);
|
|
1328
|
-
const sender = json.sender;
|
|
1329
|
-
if (!sender) {
|
|
1330
|
-
throw new UserError('sender is required');
|
|
1331
|
-
}
|
|
1332
|
-
const amount = json.amount;
|
|
1333
|
-
if (!amount) {
|
|
1334
|
-
throw new UserError('amount is required');
|
|
1335
|
-
}
|
|
1336
|
-
const recipient = json.recipient;
|
|
1337
|
-
if (!recipient) {
|
|
1338
|
-
throw new UserError('recipient is required');
|
|
1339
|
-
}
|
|
1340
|
-
const chainId = parseInt(json.chainId, 10);
|
|
1341
|
-
if (isNaN(chainId)) {
|
|
1342
|
-
throw new UserError('chainId must be a valid number');
|
|
1343
|
-
}
|
|
1344
|
-
const assetId = parseInt(json.assetId, 10);
|
|
1345
|
-
if (isNaN(assetId)) {
|
|
1346
|
-
throw new UserError('assetId must be a valid number');
|
|
1347
|
-
}
|
|
1348
|
-
|
|
1349
|
-
// Комиссия за вывод обязательна: ante-handler модуля x/bridge отвергает tx
|
|
1350
|
-
// с пустым fee. Передаём выбранный coin как additionalFeeCoins и ставим
|
|
1351
|
-
// zeroFee=true — иначе send() сперва симулирует tx без fee, и симуляция
|
|
1352
|
-
// падает на том же ante-handler.
|
|
1353
|
-
const feeCoins = Array.isArray(json.feeCoins)
|
|
1354
|
-
? json.feeCoins
|
|
1355
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1356
|
-
.filter((c: any) => c && c.denom && c.amount)
|
|
1357
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1358
|
-
.map((c: any) => ({ denom: String(c.denom), amount: String(c.amount) }))
|
|
1359
|
-
: [];
|
|
1360
|
-
|
|
1361
|
-
const msg: EncodeObject = client.withdrawalMessage(sender, amount, recipient, chainId, assetId);
|
|
1362
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
|
|
1363
|
-
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve([msg]));
|
|
1364
|
-
|
|
1365
|
-
// BroadcastTxSync: возвращается быстро с хешем (результат CheckTx). Финальный
|
|
1366
|
-
// (DeliverTx) исход опрашивает waitForBridgeWithdrawal уже на экране статуса —
|
|
1367
|
-
// это даёт промежуточный pending-стейт с хешем.
|
|
1368
|
-
const tx = await client.send(
|
|
1369
|
-
subaccount,
|
|
1370
|
-
() => encodeObjects,
|
|
1371
|
-
feeCoins.length > 0,
|
|
1372
|
-
undefined,
|
|
1373
|
-
undefined,
|
|
1374
|
-
undefined,
|
|
1375
|
-
undefined,
|
|
1376
|
-
undefined,
|
|
1377
|
-
feeCoins,
|
|
1378
|
-
);
|
|
1379
|
-
return encodeJson(tx);
|
|
1380
|
-
} catch (error) {
|
|
1381
|
-
return wrappedError(error);
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
/** Опрашивает CometBFT RPC (`/tx?hash=`) пока cosmos-tx вывода не закоммитится.
|
|
1386
|
-
* Skip-клиента для моста нет — финальный статус определяется только исходом
|
|
1387
|
-
* cosmos-tx: `tx_result.code === 0` → success, иначе error. Аналог cosmosTx.ts
|
|
1388
|
-
* (waitForCosmosTx) в v4-web. Вызывается с экрана статуса в pending-стейте. */
|
|
1389
|
-
export async function waitForBridgeWithdrawal(payload: string): Promise<string> {
|
|
1390
|
-
try {
|
|
1391
|
-
const json = JSON.parse(payload);
|
|
1392
|
-
const txHash: string = json.hash;
|
|
1393
|
-
if (!txHash) {
|
|
1394
|
-
throw new UserError('hash is required');
|
|
1395
|
-
}
|
|
1396
|
-
const validatorUrl = globalThis.validatorUrl;
|
|
1397
|
-
if (validatorUrl === undefined) {
|
|
1398
|
-
throw new UserError('validatorUrl is not set. Call connectNetwork() first');
|
|
1399
|
-
}
|
|
1400
|
-
const hash = txHash.startsWith('0x') ? txHash.slice(2) : txHash;
|
|
1401
|
-
const url = `${validatorUrl.replace(/\/$/, '')}/tx?hash=0x${hash}`;
|
|
1402
|
-
|
|
1403
|
-
for (let attempt = 0; attempt < 60; attempt += 1) {
|
|
1404
|
-
const res = await fetch(url);
|
|
1405
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1406
|
-
const data: any = await res.json();
|
|
1407
|
-
// tx ещё не в блоке → RPC отдаёт error с "not found", либо result пустой.
|
|
1408
|
-
const txResult = data?.error == null ? data?.result?.tx_result : undefined;
|
|
1409
|
-
if (txResult != null) {
|
|
1410
|
-
const code: number = txResult.code ?? 0;
|
|
1411
|
-
return encodeJson({
|
|
1412
|
-
status: code === 0 ? 'success' : 'error',
|
|
1413
|
-
code,
|
|
1414
|
-
rawLog: txResult.log ?? '',
|
|
1415
|
-
});
|
|
1416
|
-
}
|
|
1417
|
-
await new Promise((resolve) => {
|
|
1418
|
-
setTimeout(resolve, 3000);
|
|
1419
|
-
});
|
|
1420
|
-
}
|
|
1421
|
-
return encodeJson({ status: 'error', rawLog: 'Timeout waiting for transaction' });
|
|
1422
|
-
} catch (error) {
|
|
1423
|
-
return wrappedError(error);
|
|
1424
|
-
}
|
|
1425
|
-
}
|
|
1426
|
-
|
|
1427
|
-
export async function getWithdrawalAndTransferGatingStatus(perpetualId: number): Promise<string> {
|
|
1428
|
-
try {
|
|
1429
|
-
const client = globalThis.client;
|
|
1430
|
-
if (client === undefined) {
|
|
1431
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1432
|
-
}
|
|
1433
|
-
|
|
1434
|
-
const response =
|
|
1435
|
-
await client.validatorClient.get.getWithdrawalAndTransferGatingStatus(perpetualId);
|
|
1436
|
-
return encodeJson(response);
|
|
1437
|
-
} catch (error) {
|
|
1438
|
-
return wrappedError(error);
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
|
|
1442
|
-
export async function subaccountTransfer(payload: string): Promise<string> {
|
|
1443
|
-
try {
|
|
1444
|
-
const client = globalThis.client;
|
|
1445
|
-
if (client === undefined) {
|
|
1446
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
1447
|
-
}
|
|
1448
|
-
const wallet = globalThis.wallet;
|
|
1449
|
-
if (wallet === undefined) {
|
|
1450
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1451
|
-
}
|
|
1452
|
-
|
|
1453
|
-
const json = JSON.parse(payload);
|
|
1454
|
-
const subaccountNumber = json.subaccountNumber;
|
|
1455
|
-
if (subaccountNumber === undefined) {
|
|
1456
|
-
throw new UserError('subaccountNumber is not set');
|
|
1457
|
-
}
|
|
1458
|
-
const amount = json.amount;
|
|
1459
|
-
if (amount === undefined) {
|
|
1460
|
-
throw new UserError('amount is not set');
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
|
-
let destinationAddress = json.destinationAddress;
|
|
1464
|
-
if (destinationAddress === undefined) {
|
|
1465
|
-
destinationAddress = wallet.address!;
|
|
1466
|
-
}
|
|
1467
|
-
|
|
1468
|
-
const destinationSubaccountNumber = json.destinationSubaccountNumber;
|
|
1469
|
-
if (destinationSubaccountNumber === undefined) {
|
|
1470
|
-
throw new UserError('destinationSubaccountNumber is not set');
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1474
|
-
const tx = await client.transferToSubaccount(
|
|
1475
|
-
subaccount,
|
|
1476
|
-
destinationAddress,
|
|
1477
|
-
destinationSubaccountNumber,
|
|
1478
|
-
parseFloat(amount).toFixed(6),
|
|
1479
|
-
undefined,
|
|
1480
|
-
Method.BroadcastTxCommit,
|
|
1481
|
-
);
|
|
1482
|
-
return encodeJson(tx);
|
|
1483
|
-
} catch (error) {
|
|
1484
|
-
return wrappedError(error);
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
|
|
1488
|
-
export async function signCompliancePayload(payload: string): Promise<string> {
|
|
1489
|
-
try {
|
|
1490
|
-
const json = JSON.parse(payload);
|
|
1491
|
-
const message = json.message;
|
|
1492
|
-
if (message === undefined) {
|
|
1493
|
-
throw new UserError('message is not set');
|
|
1494
|
-
}
|
|
1495
|
-
const action = json.action;
|
|
1496
|
-
if (action === undefined) {
|
|
1497
|
-
throw new UserError('action is not set');
|
|
1498
|
-
}
|
|
1499
|
-
const currentStatus = json.status;
|
|
1500
|
-
if (currentStatus === undefined) {
|
|
1501
|
-
throw new UserError('status is not set');
|
|
1502
|
-
}
|
|
1503
|
-
if (!globalThis.hdKey?.privateKey || !globalThis.hdKey?.publicKey) {
|
|
1504
|
-
throw new Error('Missing hdKey');
|
|
1505
|
-
}
|
|
1506
|
-
|
|
1507
|
-
const timestampInSeconds = Math.floor(Date.now() / 1000);
|
|
1508
|
-
const messageToSign: string = `${message}:${action}"${currentStatus ?? ''}:${timestampInSeconds}`;
|
|
1509
|
-
const messageHash = sha256(Buffer.from(messageToSign));
|
|
1510
|
-
|
|
1511
|
-
const signed = await Secp256k1.createSignature(messageHash, globalThis.hdKey.privateKey);
|
|
1512
|
-
const signedMessage = signed.toFixedLength();
|
|
1513
|
-
|
|
1514
|
-
return encodeJson({
|
|
1515
|
-
signedMessage: Buffer.from(signedMessage).toString('base64'),
|
|
1516
|
-
publicKey: Buffer.from(globalThis.hdKey.publicKey).toString('base64'),
|
|
1517
|
-
timestamp: timestampInSeconds,
|
|
1518
|
-
});
|
|
1519
|
-
} catch (error) {
|
|
1520
|
-
return wrappedError(error);
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
|
-
export async function signPushNotificationTokenRegistrationPayload(
|
|
1525
|
-
payload: string,
|
|
1526
|
-
): Promise<string> {
|
|
1527
|
-
try {
|
|
1528
|
-
const json = JSON.parse(payload);
|
|
1529
|
-
const message = json.message;
|
|
1530
|
-
if (message === undefined) {
|
|
1531
|
-
throw new UserError('message is not set');
|
|
1532
|
-
}
|
|
1533
|
-
if (!globalThis.hdKey?.privateKey || !globalThis.hdKey?.publicKey) {
|
|
1534
|
-
throw new Error('Missing hdKey');
|
|
1535
|
-
}
|
|
1536
|
-
|
|
1537
|
-
const timestampInSeconds = Math.floor(Date.now() / 1000);
|
|
1538
|
-
const messageToSign: string = `${message}:REGISTER_TOKEN"${''}:${timestampInSeconds}`;
|
|
1539
|
-
const messageHash = sha256(Buffer.from(messageToSign));
|
|
1540
|
-
|
|
1541
|
-
const signed = await Secp256k1.createSignature(messageHash, globalThis.hdKey.privateKey);
|
|
1542
|
-
const signedMessage = signed.toFixedLength();
|
|
1543
|
-
|
|
1544
|
-
return encodeJson({
|
|
1545
|
-
signedMessage: Buffer.from(signedMessage).toString('base64'),
|
|
1546
|
-
publicKey: Buffer.from(globalThis.hdKey.publicKey).toString('base64'),
|
|
1547
|
-
timestamp: timestampInSeconds,
|
|
1548
|
-
});
|
|
1549
|
-
} catch (error) {
|
|
1550
|
-
return wrappedError(error);
|
|
1551
|
-
}
|
|
1552
|
-
}
|
|
1553
|
-
|
|
1554
|
-
export async function setSelectedGasDenom(gasDenom: string): Promise<string> {
|
|
1555
|
-
try {
|
|
1556
|
-
const client = globalThis.client;
|
|
1557
|
-
if (client === undefined) {
|
|
1558
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1559
|
-
}
|
|
1560
|
-
await client.setSelectedGasDenom(gasDenom as SelectedGasDenom);
|
|
1561
|
-
return encodeJson('success');
|
|
1562
|
-
} catch (error) {
|
|
1563
|
-
return wrappedError(error);
|
|
1564
|
-
}
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
export async function getMegavaultOwnerShares(payload: string): Promise<string> {
|
|
1568
|
-
try {
|
|
1569
|
-
const client = globalThis.client;
|
|
1570
|
-
if (client === undefined) {
|
|
1571
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1572
|
-
}
|
|
1573
|
-
const json = JSON.parse(payload);
|
|
1574
|
-
const address = json.address;
|
|
1575
|
-
if (address === undefined) {
|
|
1576
|
-
throw new UserError('address is not set');
|
|
1577
|
-
}
|
|
1578
|
-
const response = await globalThis.client?.validatorClient.get.getMegavaultOwnerShares(address);
|
|
1579
|
-
return encodeJson(parseToPrimitives(response));
|
|
1580
|
-
} catch (e) {
|
|
1581
|
-
return wrappedError(e);
|
|
1582
|
-
}
|
|
1583
|
-
}
|
|
1584
|
-
|
|
1585
|
-
export async function getMegavaultWithdrawalInfo(sharesToWithdraw: bigint): Promise<string> {
|
|
1586
|
-
try {
|
|
1587
|
-
const client = globalThis.client;
|
|
1588
|
-
if (client === undefined) {
|
|
1589
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1590
|
-
}
|
|
1591
|
-
const response =
|
|
1592
|
-
await globalThis.client?.validatorClient.get.getMegavaultWithdrawalInfo(sharesToWithdraw);
|
|
1593
|
-
return encodeJson(parseToPrimitives(response));
|
|
1594
|
-
} catch (e) {
|
|
1595
|
-
return wrappedError(e);
|
|
1596
|
-
}
|
|
1597
|
-
}
|
|
1598
|
-
|
|
1599
|
-
export async function depositToMegavault(
|
|
1600
|
-
subaccountNumber: number,
|
|
1601
|
-
amountUsdc: number,
|
|
1602
|
-
): Promise<string> {
|
|
1603
|
-
try {
|
|
1604
|
-
const client = globalThis.client;
|
|
1605
|
-
if (client === undefined) {
|
|
1606
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
1607
|
-
}
|
|
1608
|
-
const wallet = globalThis.wallet;
|
|
1609
|
-
if (wallet === undefined) {
|
|
1610
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1611
|
-
}
|
|
1612
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1613
|
-
const tx = await client.depositToMegavault(subaccount, amountUsdc, Method.BroadcastTxCommit);
|
|
1614
|
-
return encodeJson(parseToPrimitives(tx));
|
|
1615
|
-
} catch (error) {
|
|
1616
|
-
return wrappedError(error);
|
|
1617
|
-
}
|
|
1618
|
-
}
|
|
1619
|
-
|
|
1620
|
-
export async function withdrawFromMegavault(
|
|
1621
|
-
subaccountNumber: number,
|
|
1622
|
-
shares: number,
|
|
1623
|
-
minAmount: number,
|
|
1624
|
-
): Promise<string> {
|
|
1625
|
-
try {
|
|
1626
|
-
const client = globalThis.client;
|
|
1627
|
-
if (client === undefined) {
|
|
1628
|
-
throw new UserError('client is not connected. Call connectNetwork() first');
|
|
1629
|
-
}
|
|
1630
|
-
const wallet = globalThis.wallet;
|
|
1631
|
-
if (wallet === undefined) {
|
|
1632
|
-
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1633
|
-
}
|
|
1634
|
-
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1635
|
-
const tx = await client.withdrawFromMegavault(
|
|
1636
|
-
subaccount,
|
|
1637
|
-
shares,
|
|
1638
|
-
minAmount,
|
|
1639
|
-
Method.BroadcastTxCommit,
|
|
1640
|
-
);
|
|
1641
|
-
return encodeJson(parseToPrimitives(tx));
|
|
1642
|
-
} catch (error) {
|
|
1643
|
-
return wrappedError(error);
|
|
1644
|
-
}
|
|
1645
|
-
}
|
|
1646
|
-
|
|
1647
|
-
export async function getAllPerpMarketFeeDiscounts(): Promise<string> {
|
|
1648
|
-
try {
|
|
1649
|
-
const client = globalThis.client;
|
|
1650
|
-
if (client === undefined) {
|
|
1651
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1652
|
-
}
|
|
1653
|
-
const response = await globalThis.client?.validatorClient.get.getAllPerpMarketFeeDiscounts();
|
|
1654
|
-
return encodeJson(response);
|
|
1655
|
-
} catch (error) {
|
|
1656
|
-
return wrappedError(error);
|
|
1657
|
-
}
|
|
1658
|
-
}
|
|
1659
|
-
|
|
1660
|
-
export async function getAllStakingTiers(): Promise<string> {
|
|
1661
|
-
try {
|
|
1662
|
-
const client = globalThis.client;
|
|
1663
|
-
if (client === undefined) {
|
|
1664
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1665
|
-
}
|
|
1666
|
-
const response = await globalThis.client?.validatorClient.get.getAllStakingTiers();
|
|
1667
|
-
return encodeJson(response);
|
|
1668
|
-
} catch (error) {
|
|
1669
|
-
return wrappedError(error);
|
|
1670
|
-
}
|
|
1671
|
-
}
|
|
1672
|
-
|
|
1673
|
-
export async function getUserStakingTier(payload: string): Promise<string> {
|
|
1674
|
-
try {
|
|
1675
|
-
const client = globalThis.client;
|
|
1676
|
-
if (client === undefined) {
|
|
1677
|
-
throw new UserError('client is not connected. Call connectClient() first');
|
|
1678
|
-
}
|
|
1679
|
-
const json = JSON.parse(payload);
|
|
1680
|
-
const address = json.address;
|
|
1681
|
-
if (address === undefined) {
|
|
1682
|
-
throw new UserError('address is not set');
|
|
1683
|
-
}
|
|
1684
|
-
const response = await globalThis.client?.validatorClient.get.getUserStakingTier(address);
|
|
1685
|
-
return encodeJson(response);
|
|
1686
|
-
} catch (error) {
|
|
1687
|
-
return wrappedError(error);
|
|
1688
|
-
}
|
|
1689
|
-
}
|