@dydxprotocol/v4-client-js 1.1.13 → 1.1.14
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/.eslintrc.js +3 -5
- package/.prettierignore +3 -0
- package/.prettierrc.json +6 -0
- package/.telescope.json +2 -4
- package/.vscode/launch.json +10 -10
- package/README.md +3 -1
- package/__native__/__ios__/v4-native-client.js +2640 -797
- package/__tests__/helpers/constants.ts +10 -8
- package/__tests__/lib/helpers.test.ts +22 -23
- package/__tests__/lib/util.test.ts +2 -8
- package/__tests__/lib/validation.test.ts +23 -26
- package/__tests__/modules/client/Transfers.test.ts +4 -19
- package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +2 -5
- package/__tests__/modules/client/constants.ts +4 -2
- package/__tests__/modules/onboarding.test.ts +9 -7
- package/build/__tests__/helpers/constants.js +7 -7
- package/build/__tests__/lib/helpers.test.js +2 -6
- package/build/__tests__/lib/util.test.js +1 -1
- package/build/__tests__/lib/validation.test.js +3 -11
- package/build/__tests__/modules/client/Transfers.test.js +1 -1
- package/build/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
- package/build/__tests__/modules/client/constants.js +1 -1
- package/build/__tests__/modules/onboarding.test.js +2 -3
- package/build/examples/account_endpoints.js +4 -3
- package/build/examples/composite_example.js +5 -4
- package/build/examples/constants.js +1 -1
- package/build/examples/faucet_endpoint.js +4 -3
- package/build/examples/long_term_order_cancel_example.js +4 -3
- package/build/examples/markets_endpoints.js +4 -3
- package/build/examples/native_examples.js +4 -3
- package/build/examples/noble_example.js +1 -1
- package/build/examples/optimal_node.js +4 -12
- package/build/examples/short_term_order_cancel_example.js +4 -3
- package/build/examples/short_term_order_composite_example.js +4 -3
- package/build/examples/test.js +5 -4
- package/build/examples/transfer_example_deposit.js +4 -3
- package/build/examples/transfer_example_send.js +1 -1
- package/build/examples/transfer_example_subaccount_transfer.js +4 -3
- package/build/examples/transfer_example_withdraw.js +4 -3
- package/build/examples/transfer_example_withdraw_other.js +1 -1
- package/build/examples/utility_endpoints.js +4 -3
- package/build/examples/validator_get_example.js +15 -9
- package/build/examples/validator_post_example.js +4 -3
- package/build/examples/wallet_address.js +5 -4
- package/build/examples/websocket_example.js +1 -1
- package/build/src/clients/composite-client.d.ts +222 -222
- package/build/src/clients/composite-client.js +234 -228
- package/build/src/clients/constants.d.ts +3 -0
- package/build/src/clients/constants.js +6 -2
- package/build/src/clients/faucet-client.d.ts +4 -4
- package/build/src/clients/faucet-client.js +5 -5
- package/build/src/clients/helpers/chain-helpers.js +1 -1
- package/build/src/clients/helpers/request-helpers.js +5 -4
- package/build/src/clients/indexer-client.js +1 -1
- package/build/src/clients/lib/axios/axiosRequest.js +1 -1
- package/build/src/clients/lib/axios/errors.js +1 -1
- package/build/src/clients/lib/cctpProto.d.ts +2 -2
- package/build/src/clients/lib/cctpProto.js +287 -237
- package/build/src/clients/lib/errors.js +1 -1
- package/build/src/clients/modules/account.js +1 -1
- package/build/src/clients/modules/composer.d.ts +4 -0
- package/build/src/clients/modules/composer.js +39 -3
- package/build/src/clients/modules/get.d.ts +10 -3
- package/build/src/clients/modules/get.js +41 -54
- package/build/src/clients/modules/local-wallet.js +1 -1
- package/build/src/clients/modules/post.d.ts +3 -0
- package/build/src/clients/modules/post.js +34 -18
- package/build/src/clients/modules/proto-includes.d.ts +1 -0
- package/build/src/clients/modules/proto-includes.js +3 -2
- package/build/src/clients/modules/rest.js +1 -1
- package/build/src/clients/modules/signer.js +1 -1
- package/build/src/clients/modules/tendermintClient.d.ts +5 -5
- package/build/src/clients/modules/tendermintClient.js +6 -6
- package/build/src/clients/native.js +4 -7
- package/build/src/clients/noble-client.js +1 -1
- package/build/src/clients/socket-client.js +1 -1
- package/build/src/clients/subaccount.js +1 -1
- package/build/src/clients/types.d.ts +1 -1
- package/build/src/clients/types.js +1 -1
- package/build/src/clients/validator-client.d.ts +12 -12
- package/build/src/clients/validator-client.js +13 -13
- package/build/src/lib/errors.js +1 -1
- package/build/src/lib/onboarding.js +1 -1
- package/build/src/lib/utils.js +3 -3
- package/build/src/lib/validation.js +5 -4
- package/build/src/network_optimizer.js +10 -11
- package/build/src/types.d.ts +1 -1
- package/build/src/types.js +1 -1
- package/examples/account_endpoints.ts +5 -4
- package/examples/composite_example.ts +12 -7
- package/examples/constants.ts +10 -4
- package/examples/faucet_endpoint.ts +5 -4
- package/examples/human_readable_orders.json +85 -85
- package/examples/human_readable_short_term_orders.json +41 -41
- package/examples/long_term_order_cancel_example.ts +11 -6
- package/examples/markets_endpoints.ts +5 -4
- package/examples/native_examples.ts +8 -7
- package/examples/noble_example.ts +11 -23
- package/examples/optimal_node.ts +4 -14
- package/examples/raw_orders.json +128 -128
- package/examples/short_term_order_cancel_example.ts +7 -8
- package/examples/short_term_order_composite_example.ts +7 -8
- package/examples/test.ts +12 -7
- package/examples/transfer_example_deposit.ts +6 -9
- package/examples/transfer_example_send.ts +2 -10
- package/examples/transfer_example_subaccount_transfer.ts +6 -11
- package/examples/transfer_example_withdraw.ts +6 -9
- package/examples/transfer_example_withdraw_other.ts +1 -5
- package/examples/utility_endpoints.ts +5 -4
- package/examples/validator_get_example.ts +19 -10
- package/examples/validator_post_example.ts +7 -9
- package/examples/wallet_address.ts +6 -5
- package/examples/websocket_example.ts +5 -1
- package/package.json +4 -1
- package/src/clients/composite-client.ts +272 -329
- package/src/clients/constants.ts +38 -26
- package/src/clients/faucet-client.ts +5 -8
- package/src/clients/helpers/chain-helpers.ts +10 -6
- package/src/clients/helpers/request-helpers.ts +6 -5
- package/src/clients/indexer-client.ts +34 -34
- package/src/clients/lib/axios/axiosRequest.ts +4 -7
- package/src/clients/lib/axios/errors.ts +16 -21
- package/src/clients/lib/cctpProto.ts +655 -387
- package/src/clients/lib/errors.ts +8 -17
- package/src/clients/modules/account.ts +4 -12
- package/src/clients/modules/composer.ts +61 -20
- package/src/clients/modules/get.ts +71 -89
- package/src/clients/modules/local-wallet.ts +41 -56
- package/src/clients/modules/post.ts +664 -634
- package/src/clients/modules/proto-includes.ts +1 -0
- package/src/clients/modules/rest.ts +23 -26
- package/src/clients/modules/signer.ts +8 -22
- package/src/clients/modules/tendermintClient.ts +13 -36
- package/src/clients/native.ts +73 -139
- package/src/clients/noble-client.ts +1 -5
- package/src/clients/socket-client.ts +216 -216
- package/src/clients/subaccount.ts +16 -16
- package/src/clients/types.ts +14 -10
- package/src/clients/validator-client.ts +14 -16
- package/src/codegen/helpers.ts +149 -150
- package/src/codegen/index.ts +4 -5
- package/src/lib/errors.ts +3 -9
- package/src/lib/onboarding.ts +7 -2
- package/src/lib/utils.ts +5 -12
- package/src/lib/validation.ts +21 -19
- package/src/network_optimizer.ts +78 -65
- package/src/types.ts +5 -6
- package/tasks.json +15 -15
- package/tsconfig.eslint.json +1 -4
- package/tsconfig.json +2 -6
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
import { Coin, Secp256k1Pubkey } from '@cosmjs/amino';
|
|
2
2
|
import { Uint53 } from '@cosmjs/math';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from '@cosmjs/proto-signing';
|
|
7
|
-
import {
|
|
8
|
-
Account,
|
|
9
|
-
calculateFee,
|
|
10
|
-
GasPrice,
|
|
11
|
-
IndexedTx,
|
|
12
|
-
StdFee,
|
|
13
|
-
} from '@cosmjs/stargate';
|
|
14
|
-
import {
|
|
15
|
-
Method,
|
|
16
|
-
} from '@cosmjs/tendermint-rpc';
|
|
3
|
+
import { EncodeObject, Registry } from '@cosmjs/proto-signing';
|
|
4
|
+
import { Account, calculateFee, GasPrice, IndexedTx, StdFee } from '@cosmjs/stargate';
|
|
5
|
+
import { Method } from '@cosmjs/tendermint-rpc';
|
|
17
6
|
import {
|
|
18
7
|
BroadcastTxAsyncResponse,
|
|
19
8
|
BroadcastTxSyncResponse,
|
|
@@ -38,7 +27,12 @@ import { Composer } from './composer';
|
|
|
38
27
|
import { Get } from './get';
|
|
39
28
|
import LocalWallet from './local-wallet';
|
|
40
29
|
import {
|
|
41
|
-
Order_Side,
|
|
30
|
+
Order_Side,
|
|
31
|
+
Order_TimeInForce,
|
|
32
|
+
Any,
|
|
33
|
+
MsgPlaceOrder,
|
|
34
|
+
MsgCancelOrder,
|
|
35
|
+
Order_ConditionType,
|
|
42
36
|
} from './proto-includes';
|
|
43
37
|
|
|
44
38
|
// Required for encoding and decoding queries that are of type Long.
|
|
@@ -49,667 +43,703 @@ protobuf.util.Long = Long;
|
|
|
49
43
|
protobuf.configure();
|
|
50
44
|
|
|
51
45
|
export class Post {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
46
|
+
public readonly composer: Composer;
|
|
47
|
+
private readonly registry: Registry;
|
|
48
|
+
private readonly chainId: string;
|
|
49
|
+
public readonly get: Get;
|
|
50
|
+
public readonly denoms: DenomConfig;
|
|
51
|
+
public readonly defaultClientMemo?: string;
|
|
52
|
+
|
|
53
|
+
public selectedGasDenom: SelectedGasDenom = SelectedGasDenom.USDC;
|
|
54
|
+
public readonly defaultGasPrice: GasPrice;
|
|
55
|
+
public readonly defaultDydxGasPrice: GasPrice;
|
|
56
|
+
|
|
57
|
+
private accountNumberCache: Map<string, Account> = new Map();
|
|
58
|
+
|
|
59
|
+
constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string) {
|
|
60
|
+
this.get = get;
|
|
61
|
+
this.chainId = chainId;
|
|
62
|
+
this.registry = generateRegistry();
|
|
63
|
+
this.composer = new Composer();
|
|
64
|
+
this.denoms = denoms;
|
|
65
|
+
this.defaultClientMemo = defaultClientMemo;
|
|
66
|
+
this.defaultGasPrice = GasPrice.fromString(
|
|
67
|
+
`0.025${denoms.USDC_GAS_DENOM !== undefined ? denoms.USDC_GAS_DENOM : denoms.USDC_DENOM}`,
|
|
68
|
+
);
|
|
69
|
+
this.defaultDydxGasPrice = GasPrice.fromString(
|
|
70
|
+
`25000000000${
|
|
71
|
+
denoms.CHAINTOKEN_GAS_DENOM !== undefined
|
|
72
|
+
? denoms.CHAINTOKEN_GAS_DENOM
|
|
73
|
+
: denoms.CHAINTOKEN_DENOM
|
|
74
|
+
}`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
setSelectedGasDenom(selectedGasDenom: SelectedGasDenom): void {
|
|
79
|
+
this.selectedGasDenom = selectedGasDenom;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getGasPrice(): GasPrice {
|
|
83
|
+
return this.selectedGasDenom === SelectedGasDenom.USDC
|
|
84
|
+
? this.defaultGasPrice
|
|
85
|
+
: this.defaultDydxGasPrice;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @description Simulate a transaction
|
|
90
|
+
* the calling function is responsible for creating the messages.
|
|
91
|
+
*
|
|
92
|
+
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
93
|
+
* at any point.
|
|
94
|
+
* @returns The Fee for broadcasting a transaction.
|
|
95
|
+
*/
|
|
96
|
+
async simulate(
|
|
97
|
+
wallet: LocalWallet,
|
|
98
|
+
messaging: () => Promise<EncodeObject[]>,
|
|
99
|
+
gasPrice: GasPrice = this.getGasPrice(),
|
|
100
|
+
memo?: string,
|
|
101
|
+
account?: () => Promise<Account>,
|
|
102
|
+
): Promise<StdFee> {
|
|
103
|
+
const msgsPromise = messaging();
|
|
104
|
+
const accountPromise = account ? await account() : this.account(wallet.address!);
|
|
105
|
+
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
106
|
+
const msgs = msgsAndAccount[0];
|
|
107
|
+
|
|
108
|
+
return this.simulateTransaction(
|
|
109
|
+
wallet.pubKey!,
|
|
110
|
+
msgsAndAccount[1].sequence,
|
|
111
|
+
msgs,
|
|
112
|
+
gasPrice,
|
|
113
|
+
memo,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @description Sign a transaction
|
|
119
|
+
* the calling function is responsible for creating the messages.
|
|
120
|
+
*
|
|
121
|
+
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
122
|
+
* at any point.
|
|
123
|
+
* @returns The Signature.
|
|
124
|
+
*/
|
|
125
|
+
async sign(
|
|
126
|
+
wallet: LocalWallet,
|
|
127
|
+
messaging: () => Promise<EncodeObject[]>,
|
|
128
|
+
zeroFee: boolean,
|
|
129
|
+
gasPrice: GasPrice = this.getGasPrice(),
|
|
130
|
+
memo?: string,
|
|
131
|
+
account?: () => Promise<Account>,
|
|
132
|
+
): Promise<Uint8Array> {
|
|
133
|
+
const msgsPromise = await messaging();
|
|
134
|
+
const accountPromise = account ? await account() : this.account(wallet.address!);
|
|
135
|
+
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
136
|
+
const msgs = msgsAndAccount[0];
|
|
137
|
+
return this.signTransaction(wallet, msgs, msgsAndAccount[1], zeroFee, gasPrice, memo);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @description Send a transaction
|
|
142
|
+
* the calling function is responsible for creating the messages.
|
|
143
|
+
*
|
|
144
|
+
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
145
|
+
* at any point.
|
|
146
|
+
* @returns The Tx Hash.
|
|
147
|
+
*/
|
|
148
|
+
async send(
|
|
149
|
+
wallet: LocalWallet,
|
|
150
|
+
messaging: () => Promise<EncodeObject[]>,
|
|
151
|
+
zeroFee: boolean,
|
|
152
|
+
gasPrice: GasPrice = this.getGasPrice(),
|
|
153
|
+
memo?: string,
|
|
154
|
+
broadcastMode?: BroadcastMode,
|
|
155
|
+
account?: () => Promise<Account>,
|
|
156
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
157
|
+
const msgsPromise = messaging();
|
|
158
|
+
const accountPromise = account ? await account() : this.account(wallet.address!);
|
|
159
|
+
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
160
|
+
const msgs = msgsAndAccount[0];
|
|
161
|
+
|
|
162
|
+
return this.signAndSendTransaction(
|
|
163
|
+
wallet,
|
|
164
|
+
msgsAndAccount[1],
|
|
165
|
+
msgs,
|
|
166
|
+
zeroFee,
|
|
167
|
+
gasPrice,
|
|
168
|
+
memo ?? this.defaultClientMemo,
|
|
169
|
+
broadcastMode ?? this.defaultBroadcastMode(msgs),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @description Calculate the default broadcast mode.
|
|
175
|
+
*/
|
|
176
|
+
private defaultBroadcastMode(msgs: EncodeObject[]): BroadcastMode {
|
|
177
|
+
if (
|
|
178
|
+
msgs.length === 1 &&
|
|
179
|
+
(msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder' ||
|
|
180
|
+
msgs[0].typeUrl === '/dydxprotocol.clob.MsgCancelOrder')
|
|
70
181
|
) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.registry = generateRegistry();
|
|
74
|
-
this.composer = new Composer();
|
|
75
|
-
this.denoms = denoms;
|
|
76
|
-
this.defaultClientMemo = defaultClientMemo;
|
|
77
|
-
this.defaultGasPrice = GasPrice
|
|
78
|
-
.fromString(`0.025${denoms.USDC_GAS_DENOM !== undefined ? denoms.USDC_GAS_DENOM : denoms.USDC_DENOM}`);
|
|
79
|
-
this.defaultDydxGasPrice = GasPrice
|
|
80
|
-
.fromString(`25000000000${denoms.CHAINTOKEN_GAS_DENOM !== undefined ? denoms.CHAINTOKEN_GAS_DENOM : denoms.CHAINTOKEN_DENOM}`);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
setSelectedGasDenom(selectedGasDenom: SelectedGasDenom): void {
|
|
84
|
-
this.selectedGasDenom = selectedGasDenom;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
getGasPrice(): GasPrice {
|
|
88
|
-
return this.selectedGasDenom === SelectedGasDenom.USDC
|
|
89
|
-
? this.defaultGasPrice
|
|
90
|
-
: this.defaultDydxGasPrice;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* @description Simulate a transaction
|
|
95
|
-
* the calling function is responsible for creating the messages.
|
|
96
|
-
*
|
|
97
|
-
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
98
|
-
* at any point.
|
|
99
|
-
* @returns The Fee for broadcasting a transaction.
|
|
100
|
-
*/
|
|
101
|
-
async simulate(
|
|
102
|
-
wallet: LocalWallet,
|
|
103
|
-
messaging: () => Promise<EncodeObject[]>,
|
|
104
|
-
gasPrice: GasPrice = this.getGasPrice(),
|
|
105
|
-
memo?: string,
|
|
106
|
-
account?: () => Promise<Account>,
|
|
107
|
-
): Promise<StdFee> {
|
|
108
|
-
const msgsPromise = messaging();
|
|
109
|
-
const accountPromise = account ? (await account()) : this.account(wallet.address!);
|
|
110
|
-
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
111
|
-
const msgs = msgsAndAccount[0];
|
|
112
|
-
|
|
113
|
-
return this.simulateTransaction(
|
|
114
|
-
wallet.pubKey!,
|
|
115
|
-
msgsAndAccount[1].sequence,
|
|
116
|
-
msgs,
|
|
117
|
-
gasPrice,
|
|
118
|
-
memo,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @description Sign a transaction
|
|
124
|
-
* the calling function is responsible for creating the messages.
|
|
125
|
-
*
|
|
126
|
-
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
127
|
-
* at any point.
|
|
128
|
-
* @returns The Signature.
|
|
129
|
-
*/
|
|
130
|
-
async sign(
|
|
131
|
-
wallet: LocalWallet,
|
|
132
|
-
messaging: () => Promise<EncodeObject[]>,
|
|
133
|
-
zeroFee: boolean,
|
|
134
|
-
gasPrice: GasPrice = this.getGasPrice(),
|
|
135
|
-
memo?: string,
|
|
136
|
-
account?: () => Promise<Account>,
|
|
137
|
-
): Promise<Uint8Array> {
|
|
138
|
-
const msgsPromise = await messaging();
|
|
139
|
-
const accountPromise = account ? (await account()) : this.account(wallet.address!);
|
|
140
|
-
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
141
|
-
const msgs = msgsAndAccount[0];
|
|
142
|
-
return this.signTransaction(wallet, msgs, msgsAndAccount[1], zeroFee, gasPrice, memo);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* @description Send a transaction
|
|
147
|
-
* the calling function is responsible for creating the messages.
|
|
148
|
-
*
|
|
149
|
-
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
150
|
-
* at any point.
|
|
151
|
-
* @returns The Tx Hash.
|
|
152
|
-
*/
|
|
153
|
-
async send(
|
|
154
|
-
wallet: LocalWallet,
|
|
155
|
-
messaging: () => Promise<EncodeObject[]>,
|
|
156
|
-
zeroFee: boolean,
|
|
157
|
-
gasPrice: GasPrice = this.getGasPrice(),
|
|
158
|
-
memo?: string,
|
|
159
|
-
broadcastMode?: BroadcastMode,
|
|
160
|
-
account?: () => Promise<Account>,
|
|
161
|
-
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
162
|
-
const msgsPromise = messaging();
|
|
163
|
-
const accountPromise = account ? (await account()) : this.account(wallet.address!);
|
|
164
|
-
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
165
|
-
const msgs = msgsAndAccount[0];
|
|
166
|
-
|
|
167
|
-
return this.signAndSendTransaction(
|
|
168
|
-
wallet,
|
|
169
|
-
msgsAndAccount[1],
|
|
170
|
-
msgs,
|
|
171
|
-
zeroFee,
|
|
172
|
-
gasPrice,
|
|
173
|
-
memo ?? this.defaultClientMemo,
|
|
174
|
-
broadcastMode ?? this.defaultBroadcastMode(msgs),
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* @description Calculate the default broadcast mode.
|
|
180
|
-
*/
|
|
181
|
-
private defaultBroadcastMode(msgs: EncodeObject[]): BroadcastMode {
|
|
182
|
-
if (
|
|
183
|
-
msgs.length === 1 &&
|
|
184
|
-
(msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder' ||
|
|
185
|
-
msgs[0].typeUrl === '/dydxprotocol.clob.MsgCancelOrder')
|
|
186
|
-
) {
|
|
187
|
-
const orderFlags = msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder'
|
|
182
|
+
const orderFlags =
|
|
183
|
+
msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder'
|
|
188
184
|
? (msgs[0].value as MsgPlaceOrder).order?.orderId?.orderFlags
|
|
189
185
|
: (msgs[0].value as MsgCancelOrder).orderId?.orderFlags;
|
|
190
186
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
187
|
+
switch (orderFlags) {
|
|
188
|
+
case OrderFlags.SHORT_TERM:
|
|
189
|
+
return Method.BroadcastTxSync;
|
|
194
190
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
191
|
+
case OrderFlags.LONG_TERM:
|
|
192
|
+
case OrderFlags.CONDITIONAL:
|
|
193
|
+
return Method.BroadcastTxCommit;
|
|
198
194
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
195
|
+
default:
|
|
196
|
+
break;
|
|
202
197
|
}
|
|
203
|
-
return Method.BroadcastTxSync;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @description Sign and send a message
|
|
208
|
-
*
|
|
209
|
-
* @returns The Tx Response.
|
|
210
|
-
*/
|
|
211
|
-
private async signTransaction(
|
|
212
|
-
wallet: LocalWallet,
|
|
213
|
-
messages: EncodeObject[],
|
|
214
|
-
account: Account,
|
|
215
|
-
zeroFee: boolean,
|
|
216
|
-
gasPrice: GasPrice = this.getGasPrice(),
|
|
217
|
-
memo?: string,
|
|
218
|
-
): Promise<Uint8Array> {
|
|
219
|
-
// Simulate transaction if no fee is specified.
|
|
220
|
-
const fee: StdFee = zeroFee ? {
|
|
221
|
-
amount: [],
|
|
222
|
-
gas: '1000000',
|
|
223
|
-
} : await this.simulateTransaction(
|
|
224
|
-
wallet.pubKey!,
|
|
225
|
-
account.sequence,
|
|
226
|
-
messages,
|
|
227
|
-
gasPrice,
|
|
228
|
-
memo,
|
|
229
|
-
);
|
|
230
|
-
|
|
231
|
-
const txOptions: TransactionOptions = {
|
|
232
|
-
sequence: account.sequence,
|
|
233
|
-
accountNumber: account.accountNumber,
|
|
234
|
-
chainId: this.chainId,
|
|
235
|
-
};
|
|
236
|
-
// Generate signed transaction.
|
|
237
|
-
return wallet.signTransaction(
|
|
238
|
-
messages,
|
|
239
|
-
txOptions,
|
|
240
|
-
fee,
|
|
241
|
-
memo,
|
|
242
|
-
);
|
|
243
198
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
199
|
+
return Method.BroadcastTxSync;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @description Sign and send a message
|
|
204
|
+
*
|
|
205
|
+
* @returns The Tx Response.
|
|
206
|
+
*/
|
|
207
|
+
private async signTransaction(
|
|
208
|
+
wallet: LocalWallet,
|
|
209
|
+
messages: EncodeObject[],
|
|
210
|
+
account: Account,
|
|
211
|
+
zeroFee: boolean,
|
|
212
|
+
gasPrice: GasPrice = this.getGasPrice(),
|
|
213
|
+
memo?: string,
|
|
214
|
+
): Promise<Uint8Array> {
|
|
215
|
+
// Simulate transaction if no fee is specified.
|
|
216
|
+
const fee: StdFee = zeroFee
|
|
217
|
+
? {
|
|
218
|
+
amount: [],
|
|
219
|
+
gas: '1000000',
|
|
255
220
|
}
|
|
221
|
+
: await this.simulateTransaction(wallet.pubKey!, account.sequence, messages, gasPrice, memo);
|
|
222
|
+
|
|
223
|
+
const txOptions: TransactionOptions = {
|
|
224
|
+
sequence: account.sequence,
|
|
225
|
+
accountNumber: account.accountNumber,
|
|
226
|
+
chainId: this.chainId,
|
|
227
|
+
};
|
|
228
|
+
// Generate signed transaction.
|
|
229
|
+
return wallet.signTransaction(messages, txOptions, fee, memo);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @description Retrieve an account structure for transactions.
|
|
234
|
+
* For short term orders, the sequence doesn't matter. Use cached if available.
|
|
235
|
+
* For long term and conditional orders, a round trip to validator must be made.
|
|
236
|
+
*/
|
|
237
|
+
public async account(address: string, orderFlags?: OrderFlags): Promise<Account> {
|
|
238
|
+
if (orderFlags === OrderFlags.SHORT_TERM) {
|
|
239
|
+
if (this.accountNumberCache.has(address)) {
|
|
240
|
+
// For SHORT_TERM orders, the sequence doesn't matter
|
|
241
|
+
return this.accountNumberCache.get(address)!;
|
|
256
242
|
}
|
|
257
|
-
const account = await this.get.getAccount(address);
|
|
258
|
-
this.accountNumberCache.set(address, account);
|
|
259
|
-
return account;
|
|
260
243
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
244
|
+
const account = await this.get.getAccount(address);
|
|
245
|
+
this.accountNumberCache.set(address, account);
|
|
246
|
+
return account;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @description Sign and send a message
|
|
251
|
+
*
|
|
252
|
+
* @returns The Tx Response.
|
|
253
|
+
*/
|
|
254
|
+
private async signAndSendTransaction(
|
|
255
|
+
wallet: LocalWallet,
|
|
256
|
+
account: Account,
|
|
257
|
+
messages: EncodeObject[],
|
|
258
|
+
zeroFee: boolean,
|
|
259
|
+
gasPrice: GasPrice = this.getGasPrice(),
|
|
260
|
+
memo?: string,
|
|
261
|
+
broadcastMode?: BroadcastMode,
|
|
262
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
263
|
+
const signedTransaction = await this.signTransaction(
|
|
264
|
+
wallet,
|
|
265
|
+
messages,
|
|
266
|
+
account,
|
|
267
|
+
zeroFee,
|
|
268
|
+
gasPrice,
|
|
269
|
+
memo,
|
|
270
|
+
);
|
|
271
|
+
return this.sendSignedTransaction(signedTransaction, broadcastMode);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @description Send signed transaction.
|
|
276
|
+
*
|
|
277
|
+
* @returns The Tx Response.
|
|
278
|
+
*/
|
|
279
|
+
async sendSignedTransaction(
|
|
280
|
+
signedTransaction: Uint8Array,
|
|
281
|
+
broadcastMode?: BroadcastMode,
|
|
282
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
283
|
+
return this.get.tendermintClient.broadcastTransaction(
|
|
284
|
+
signedTransaction,
|
|
285
|
+
broadcastMode !== undefined ? broadcastMode : Method.BroadcastTxSync,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* @description Simulate broadcasting a transaction.
|
|
291
|
+
*
|
|
292
|
+
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
293
|
+
* at any point.
|
|
294
|
+
* @returns The Fee for broadcasting a transaction.
|
|
295
|
+
*/
|
|
296
|
+
private async simulateTransaction(
|
|
297
|
+
pubKey: Secp256k1Pubkey,
|
|
298
|
+
sequence: number,
|
|
299
|
+
messages: readonly EncodeObject[],
|
|
300
|
+
gasPrice: GasPrice = this.getGasPrice(),
|
|
301
|
+
memo?: string,
|
|
302
|
+
): Promise<StdFee> {
|
|
303
|
+
// Get simulated response.
|
|
304
|
+
const encodedMessages: Any[] = messages.map((message: EncodeObject) =>
|
|
305
|
+
this.registry.encodeAsAny(message),
|
|
306
|
+
);
|
|
307
|
+
const simulationResponse = await this.get.stargateQueryClient.tx.simulate(
|
|
308
|
+
encodedMessages,
|
|
309
|
+
memo,
|
|
310
|
+
pubKey,
|
|
311
|
+
sequence,
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
// The promise should have been rejected if the gasInfo was undefined.
|
|
315
|
+
if (simulationResponse.gasInfo === undefined) {
|
|
316
|
+
throw new UnexpectedClientError();
|
|
285
317
|
}
|
|
286
318
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
:
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* @description Simulate broadcasting a transaction.
|
|
306
|
-
*
|
|
307
|
-
* @throws UnexpectedClientError if a malformed response is returned with no GRPC error
|
|
308
|
-
* at any point.
|
|
309
|
-
* @returns The Fee for broadcasting a transaction.
|
|
310
|
-
*/
|
|
311
|
-
private async simulateTransaction(
|
|
312
|
-
pubKey: Secp256k1Pubkey,
|
|
313
|
-
sequence: number,
|
|
314
|
-
messages: readonly EncodeObject[],
|
|
315
|
-
gasPrice: GasPrice = this.getGasPrice(),
|
|
316
|
-
memo?: string,
|
|
317
|
-
): Promise<StdFee> {
|
|
318
|
-
// Get simulated response.
|
|
319
|
-
const encodedMessages: Any[] = messages.map(
|
|
320
|
-
(message: EncodeObject) => this.registry.encodeAsAny(message),
|
|
321
|
-
);
|
|
322
|
-
const simulationResponse = await this.get.stargateQueryClient.tx.simulate(
|
|
323
|
-
encodedMessages,
|
|
324
|
-
memo,
|
|
325
|
-
pubKey,
|
|
326
|
-
sequence,
|
|
327
|
-
);
|
|
328
|
-
|
|
329
|
-
// The promise should have been rejected if the gasInfo was undefined.
|
|
330
|
-
if (simulationResponse.gasInfo === undefined) {
|
|
331
|
-
throw new UnexpectedClientError();
|
|
319
|
+
// Calculate and return fee from gasEstimate.
|
|
320
|
+
const gasEstimate: number = Uint53.fromString(
|
|
321
|
+
simulationResponse.gasInfo.gasUsed.toString(),
|
|
322
|
+
).toNumber();
|
|
323
|
+
const fee = calculateFee(Math.floor(gasEstimate * GAS_MULTIPLIER), gasPrice);
|
|
324
|
+
|
|
325
|
+
// TODO(TRCL-2550): Temporary workaround before IBC denom is supported in '@cosmjs/stargate'.
|
|
326
|
+
// The '@cosmjs/stargate' does not support denom with '/', so currently GAS_PRICE is
|
|
327
|
+
// represented in 'uusdc', and the output of `calculateFee` is in '', which is replaced
|
|
328
|
+
// below by USDC_DENOM string.
|
|
329
|
+
const amount: Coin[] = _.map(fee.amount, (coin: Coin) => {
|
|
330
|
+
if (coin.denom === 'uusdc') {
|
|
331
|
+
return {
|
|
332
|
+
amount: coin.amount,
|
|
333
|
+
denom: this.denoms.USDC_DENOM,
|
|
334
|
+
};
|
|
332
335
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
336
|
+
return coin;
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
return {
|
|
340
|
+
...fee,
|
|
341
|
+
amount,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// ------ State-Changing Requests ------ //
|
|
346
|
+
|
|
347
|
+
async placeOrder(
|
|
348
|
+
subaccount: SubaccountInfo,
|
|
349
|
+
clientId: number,
|
|
350
|
+
clobPairId: number,
|
|
351
|
+
side: Order_Side,
|
|
352
|
+
quantums: Long,
|
|
353
|
+
subticks: Long,
|
|
354
|
+
timeInForce: Order_TimeInForce,
|
|
355
|
+
orderFlags: number,
|
|
356
|
+
reduceOnly: boolean,
|
|
357
|
+
goodTilBlock?: number,
|
|
358
|
+
goodTilBlockTime?: number,
|
|
359
|
+
clientMetadata: number = 0,
|
|
360
|
+
conditionType: Order_ConditionType = Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
|
|
361
|
+
conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
|
|
362
|
+
broadcastMode?: BroadcastMode,
|
|
363
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
364
|
+
const msg = await this.placeOrderMsg(
|
|
365
|
+
subaccount.address,
|
|
366
|
+
subaccount.subaccountNumber,
|
|
367
|
+
clientId,
|
|
368
|
+
clobPairId,
|
|
369
|
+
side,
|
|
370
|
+
quantums,
|
|
371
|
+
subticks,
|
|
372
|
+
timeInForce,
|
|
373
|
+
orderFlags,
|
|
374
|
+
reduceOnly,
|
|
375
|
+
goodTilBlock,
|
|
376
|
+
goodTilBlockTime,
|
|
377
|
+
clientMetadata,
|
|
378
|
+
conditionType,
|
|
379
|
+
conditionalOrderTriggerSubticks,
|
|
380
|
+
);
|
|
381
|
+
const account: Promise<Account> = this.account(subaccount.address, orderFlags);
|
|
382
|
+
return this.send(
|
|
383
|
+
subaccount.wallet,
|
|
384
|
+
() => Promise.resolve([msg]),
|
|
385
|
+
true,
|
|
386
|
+
undefined,
|
|
387
|
+
undefined,
|
|
388
|
+
broadcastMode,
|
|
389
|
+
() => account,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async placeOrderMsg(
|
|
394
|
+
address: string,
|
|
395
|
+
subaccountNumber: number,
|
|
396
|
+
clientId: number,
|
|
397
|
+
clobPairId: number,
|
|
398
|
+
side: Order_Side,
|
|
399
|
+
quantums: Long,
|
|
400
|
+
subticks: Long,
|
|
401
|
+
timeInForce: Order_TimeInForce,
|
|
402
|
+
orderFlags: number,
|
|
403
|
+
reduceOnly: boolean,
|
|
404
|
+
goodTilBlock?: number,
|
|
405
|
+
goodTilBlockTime?: number,
|
|
406
|
+
clientMetadata: number = 0,
|
|
407
|
+
conditionType: Order_ConditionType = Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
|
|
408
|
+
conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
|
|
409
|
+
): Promise<EncodeObject> {
|
|
410
|
+
return new Promise((resolve) => {
|
|
411
|
+
const msg = this.composer.composeMsgPlaceOrder(
|
|
412
|
+
address,
|
|
413
|
+
subaccountNumber,
|
|
385
414
|
clientId,
|
|
386
415
|
clobPairId,
|
|
416
|
+
orderFlags,
|
|
417
|
+
goodTilBlock ?? 0,
|
|
418
|
+
goodTilBlockTime ?? 0,
|
|
387
419
|
side,
|
|
388
420
|
quantums,
|
|
389
421
|
subticks,
|
|
390
422
|
timeInForce,
|
|
391
|
-
orderFlags,
|
|
392
423
|
reduceOnly,
|
|
393
|
-
goodTilBlock,
|
|
394
|
-
goodTilBlockTime,
|
|
395
424
|
clientMetadata,
|
|
396
425
|
conditionType,
|
|
397
426
|
conditionalOrderTriggerSubticks,
|
|
398
427
|
);
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
placeOrder.conditionType ?? Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
|
|
469
|
-
placeOrder.conditionalOrderTriggerSubticks ?? Long.fromInt(0),
|
|
470
|
-
broadcastMode,
|
|
471
|
-
);
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
async cancelOrder(
|
|
475
|
-
subaccount: SubaccountInfo,
|
|
476
|
-
clientId: number,
|
|
477
|
-
orderFlags: OrderFlags,
|
|
478
|
-
clobPairId: number,
|
|
479
|
-
goodTilBlock?: number,
|
|
480
|
-
goodTilBlockTime?: number,
|
|
481
|
-
broadcastMode?: BroadcastMode,
|
|
482
|
-
) : Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
483
|
-
const msg = await this.cancelOrderMsg(
|
|
484
|
-
subaccount.address,
|
|
485
|
-
subaccount.subaccountNumber,
|
|
428
|
+
resolve(msg);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async placeOrderObject(
|
|
433
|
+
subaccount: SubaccountInfo,
|
|
434
|
+
placeOrder: IPlaceOrder,
|
|
435
|
+
broadcastMode?: BroadcastMode,
|
|
436
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
437
|
+
return this.placeOrder(
|
|
438
|
+
subaccount,
|
|
439
|
+
placeOrder.clientId,
|
|
440
|
+
placeOrder.clobPairId,
|
|
441
|
+
placeOrder.side,
|
|
442
|
+
placeOrder.quantums,
|
|
443
|
+
placeOrder.subticks,
|
|
444
|
+
placeOrder.timeInForce,
|
|
445
|
+
placeOrder.orderFlags,
|
|
446
|
+
placeOrder.reduceOnly,
|
|
447
|
+
placeOrder.goodTilBlock,
|
|
448
|
+
placeOrder.goodTilBlockTime,
|
|
449
|
+
placeOrder.clientMetadata,
|
|
450
|
+
placeOrder.conditionType ?? Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
|
|
451
|
+
placeOrder.conditionalOrderTriggerSubticks ?? Long.fromInt(0),
|
|
452
|
+
broadcastMode,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async cancelOrder(
|
|
457
|
+
subaccount: SubaccountInfo,
|
|
458
|
+
clientId: number,
|
|
459
|
+
orderFlags: OrderFlags,
|
|
460
|
+
clobPairId: number,
|
|
461
|
+
goodTilBlock?: number,
|
|
462
|
+
goodTilBlockTime?: number,
|
|
463
|
+
broadcastMode?: BroadcastMode,
|
|
464
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
465
|
+
const msg = await this.cancelOrderMsg(
|
|
466
|
+
subaccount.address,
|
|
467
|
+
subaccount.subaccountNumber,
|
|
468
|
+
clientId,
|
|
469
|
+
orderFlags,
|
|
470
|
+
clobPairId,
|
|
471
|
+
goodTilBlock ?? 0,
|
|
472
|
+
goodTilBlockTime ?? 0,
|
|
473
|
+
);
|
|
474
|
+
return this.send(
|
|
475
|
+
subaccount.wallet,
|
|
476
|
+
() => Promise.resolve([msg]),
|
|
477
|
+
true,
|
|
478
|
+
undefined,
|
|
479
|
+
undefined,
|
|
480
|
+
broadcastMode,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async cancelOrderMsg(
|
|
485
|
+
address: string,
|
|
486
|
+
subaccountNumber: number,
|
|
487
|
+
clientId: number,
|
|
488
|
+
orderFlags: OrderFlags,
|
|
489
|
+
clobPairId: number,
|
|
490
|
+
goodTilBlock?: number,
|
|
491
|
+
goodTilBlockTime?: number,
|
|
492
|
+
): Promise<EncodeObject> {
|
|
493
|
+
return new Promise((resolve) => {
|
|
494
|
+
const msg = this.composer.composeMsgCancelOrder(
|
|
495
|
+
address,
|
|
496
|
+
subaccountNumber,
|
|
486
497
|
clientId,
|
|
487
|
-
orderFlags,
|
|
488
498
|
clobPairId,
|
|
499
|
+
orderFlags,
|
|
489
500
|
goodTilBlock ?? 0,
|
|
490
501
|
goodTilBlockTime ?? 0,
|
|
491
502
|
);
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
)
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
subaccount.subaccountNumber,
|
|
503
|
+
resolve(msg);
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
async cancelOrderObject(
|
|
508
|
+
subaccount: SubaccountInfo,
|
|
509
|
+
cancelOrder: ICancelOrder,
|
|
510
|
+
broadcastMode?: BroadcastMode,
|
|
511
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
512
|
+
return this.cancelOrder(
|
|
513
|
+
subaccount,
|
|
514
|
+
cancelOrder.clientId,
|
|
515
|
+
cancelOrder.orderFlags,
|
|
516
|
+
cancelOrder.clobPairId,
|
|
517
|
+
cancelOrder.goodTilBlock,
|
|
518
|
+
cancelOrder.goodTilBlockTime,
|
|
519
|
+
broadcastMode,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
async transfer(
|
|
524
|
+
subaccount: SubaccountInfo,
|
|
525
|
+
recipientAddress: string,
|
|
526
|
+
recipientSubaccountNumber: number,
|
|
527
|
+
assetId: number,
|
|
528
|
+
amount: Long,
|
|
529
|
+
broadcastMode?: BroadcastMode,
|
|
530
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
531
|
+
const msg = await this.transferMsg(
|
|
532
|
+
subaccount.address,
|
|
533
|
+
subaccount.subaccountNumber,
|
|
534
|
+
recipientAddress,
|
|
535
|
+
recipientSubaccountNumber,
|
|
536
|
+
assetId,
|
|
537
|
+
amount,
|
|
538
|
+
);
|
|
539
|
+
return this.send(
|
|
540
|
+
subaccount.wallet,
|
|
541
|
+
() => Promise.resolve([msg]),
|
|
542
|
+
false,
|
|
543
|
+
undefined,
|
|
544
|
+
undefined,
|
|
545
|
+
broadcastMode,
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async transferMsg(
|
|
550
|
+
address: string,
|
|
551
|
+
subaccountNumber: number,
|
|
552
|
+
recipientAddress: string,
|
|
553
|
+
recipientSubaccountNumber: number,
|
|
554
|
+
assetId: number,
|
|
555
|
+
amount: Long,
|
|
556
|
+
): Promise<EncodeObject> {
|
|
557
|
+
return new Promise((resolve) => {
|
|
558
|
+
const msg = this.composer.composeMsgTransfer(
|
|
559
|
+
address,
|
|
560
|
+
subaccountNumber,
|
|
551
561
|
recipientAddress,
|
|
552
562
|
recipientSubaccountNumber,
|
|
553
563
|
assetId,
|
|
554
564
|
amount,
|
|
555
565
|
);
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
address
|
|
568
|
-
subaccountNumber
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
593
|
-
const msg = await this.depositMsg(
|
|
594
|
-
subaccount.address,
|
|
595
|
-
subaccount.subaccountNumber,
|
|
566
|
+
resolve(msg);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async deposit(
|
|
571
|
+
subaccount: SubaccountInfo,
|
|
572
|
+
assetId: number,
|
|
573
|
+
quantums: Long,
|
|
574
|
+
broadcastMode?: BroadcastMode,
|
|
575
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
576
|
+
const msg = await this.depositMsg(
|
|
577
|
+
subaccount.address,
|
|
578
|
+
subaccount.subaccountNumber,
|
|
579
|
+
assetId,
|
|
580
|
+
quantums,
|
|
581
|
+
);
|
|
582
|
+
return this.send(
|
|
583
|
+
subaccount.wallet,
|
|
584
|
+
() => Promise.resolve([msg]),
|
|
585
|
+
false,
|
|
586
|
+
undefined,
|
|
587
|
+
undefined,
|
|
588
|
+
broadcastMode,
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
async depositMsg(
|
|
593
|
+
address: string,
|
|
594
|
+
subaccountNumber: number,
|
|
595
|
+
assetId: number,
|
|
596
|
+
quantums: Long,
|
|
597
|
+
): Promise<EncodeObject> {
|
|
598
|
+
return new Promise((resolve) => {
|
|
599
|
+
const msg = this.composer.composeMsgDepositToSubaccount(
|
|
600
|
+
address,
|
|
601
|
+
subaccountNumber,
|
|
596
602
|
assetId,
|
|
597
603
|
quantums,
|
|
598
604
|
);
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
605
|
+
resolve(msg);
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
async withdraw(
|
|
610
|
+
subaccount: SubaccountInfo,
|
|
611
|
+
assetId: number,
|
|
612
|
+
quantums: Long,
|
|
613
|
+
recipient?: string,
|
|
614
|
+
broadcastMode?: BroadcastMode,
|
|
615
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
616
|
+
const msg = await this.withdrawMsg(
|
|
617
|
+
subaccount.address,
|
|
618
|
+
subaccount.subaccountNumber,
|
|
619
|
+
assetId,
|
|
620
|
+
quantums,
|
|
621
|
+
recipient,
|
|
622
|
+
);
|
|
623
|
+
return this.send(
|
|
624
|
+
subaccount.wallet,
|
|
625
|
+
() => Promise.resolve([msg]),
|
|
626
|
+
false,
|
|
627
|
+
undefined,
|
|
628
|
+
undefined,
|
|
629
|
+
broadcastMode,
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
async withdrawMsg(
|
|
634
|
+
address: string,
|
|
635
|
+
subaccountNumber: number,
|
|
636
|
+
assetId: number,
|
|
637
|
+
quantums: Long,
|
|
638
|
+
recipient?: string,
|
|
639
|
+
): Promise<EncodeObject> {
|
|
640
|
+
return new Promise((resolve) => {
|
|
641
|
+
const msg = this.composer.composeMsgWithdrawFromSubaccount(
|
|
642
|
+
address,
|
|
643
|
+
subaccountNumber,
|
|
636
644
|
assetId,
|
|
637
645
|
quantums,
|
|
638
646
|
recipient,
|
|
639
647
|
);
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
recipient: string,
|
|
672
|
-
coinDenom: string,
|
|
673
|
-
quantums: string,
|
|
674
|
-
zeroFee: boolean = true,
|
|
675
|
-
broadcastMode?: BroadcastMode,
|
|
676
|
-
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
677
|
-
const msg = await this.sendTokenMsg(
|
|
678
|
-
subaccount.address,
|
|
679
|
-
recipient,
|
|
680
|
-
coinDenom,
|
|
681
|
-
quantums,
|
|
682
|
-
);
|
|
683
|
-
return this.send(
|
|
684
|
-
subaccount.wallet,
|
|
685
|
-
() => Promise.resolve([msg]),
|
|
686
|
-
zeroFee,
|
|
687
|
-
coinDenom === this.denoms.CHAINTOKEN_DENOM
|
|
688
|
-
? this.defaultDydxGasPrice
|
|
689
|
-
: this.defaultGasPrice,
|
|
690
|
-
undefined,
|
|
691
|
-
broadcastMode,
|
|
692
|
-
);
|
|
648
|
+
resolve(msg);
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
async sendToken(
|
|
653
|
+
subaccount: SubaccountInfo,
|
|
654
|
+
recipient: string,
|
|
655
|
+
coinDenom: string,
|
|
656
|
+
quantums: string,
|
|
657
|
+
zeroFee: boolean = true,
|
|
658
|
+
broadcastMode?: BroadcastMode,
|
|
659
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
660
|
+
const msg = await this.sendTokenMsg(subaccount.address, recipient, coinDenom, quantums);
|
|
661
|
+
return this.send(
|
|
662
|
+
subaccount.wallet,
|
|
663
|
+
() => Promise.resolve([msg]),
|
|
664
|
+
zeroFee,
|
|
665
|
+
coinDenom === this.denoms.CHAINTOKEN_DENOM ? this.defaultDydxGasPrice : this.defaultGasPrice,
|
|
666
|
+
undefined,
|
|
667
|
+
broadcastMode,
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
async sendTokenMsg(
|
|
672
|
+
address: string,
|
|
673
|
+
recipient: string,
|
|
674
|
+
coinDenom: string,
|
|
675
|
+
quantums: string,
|
|
676
|
+
): Promise<EncodeObject> {
|
|
677
|
+
if (coinDenom !== this.denoms.CHAINTOKEN_DENOM && coinDenom !== this.denoms.USDC_DENOM) {
|
|
678
|
+
throw new Error('Unsupported coinDenom');
|
|
693
679
|
}
|
|
694
680
|
|
|
695
|
-
|
|
696
|
-
address
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
681
|
+
return new Promise((resolve) => {
|
|
682
|
+
const msg = this.composer.composeMsgSendToken(address, recipient, coinDenom, quantums);
|
|
683
|
+
resolve(msg);
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
async delegate(
|
|
688
|
+
subaccount: SubaccountInfo,
|
|
689
|
+
delegator: string,
|
|
690
|
+
validator: string,
|
|
691
|
+
amount: string,
|
|
692
|
+
broadcastMode?: BroadcastMode,
|
|
693
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
694
|
+
const msg = this.composer.composeMsgDelegate(delegator, validator, {
|
|
695
|
+
denom: this.denoms.CHAINTOKEN_DENOM,
|
|
696
|
+
amount,
|
|
697
|
+
});
|
|
698
|
+
return this.send(
|
|
699
|
+
subaccount.wallet,
|
|
700
|
+
() => Promise.resolve([msg]),
|
|
701
|
+
false,
|
|
702
|
+
undefined,
|
|
703
|
+
undefined,
|
|
704
|
+
broadcastMode,
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
async undelegate(
|
|
709
|
+
subaccount: SubaccountInfo,
|
|
710
|
+
delegator: string,
|
|
711
|
+
validator: string,
|
|
712
|
+
amount: string,
|
|
713
|
+
broadcastMode?: BroadcastMode,
|
|
714
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
715
|
+
const msg = this.composer.composeMsgUndelegate(delegator, validator, {
|
|
716
|
+
denom: this.denoms.CHAINTOKEN_DENOM,
|
|
717
|
+
amount,
|
|
718
|
+
});
|
|
719
|
+
return this.send(
|
|
720
|
+
subaccount.wallet,
|
|
721
|
+
() => Promise.resolve([msg]),
|
|
722
|
+
false,
|
|
723
|
+
undefined,
|
|
724
|
+
undefined,
|
|
725
|
+
broadcastMode,
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
async withdrawDelegatorReward(
|
|
730
|
+
subaccount: SubaccountInfo,
|
|
731
|
+
delegator: string,
|
|
732
|
+
validator: string,
|
|
733
|
+
broadcastMode?: BroadcastMode,
|
|
734
|
+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
735
|
+
const msg = this.composer.composeMsgWithdrawDelegatorReward(delegator, validator);
|
|
736
|
+
return this.send(
|
|
737
|
+
subaccount.wallet,
|
|
738
|
+
() => Promise.resolve([msg]),
|
|
739
|
+
false,
|
|
740
|
+
undefined,
|
|
741
|
+
undefined,
|
|
742
|
+
broadcastMode,
|
|
743
|
+
);
|
|
744
|
+
}
|
|
715
745
|
}
|