@dydxprotocol/v4-client-js 1.0.8 → 1.0.10

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.
@@ -1029,10 +1029,7 @@ export async function sendNobleIBC(squidPayload: string): Promise<String> {
1029
1029
  );
1030
1030
  }
1031
1031
 
1032
- const decode = (str: string): string => Buffer.from(str, 'base64').toString('binary');
1033
- const decoded = decode(squidPayload);
1034
-
1035
- const json = JSON.parse(decoded);
1032
+ const json = JSON.parse(squidPayload);
1036
1033
 
1037
1034
  const ibcMsg: EncodeObject = {
1038
1035
  typeUrl: json.msgTypeUrl, // '/ibc.applications.transfer.v1.MsgTransfer',
@@ -1055,3 +1052,76 @@ export async function sendNobleIBC(squidPayload: string): Promise<String> {
1055
1052
  return wrappedError(error);
1056
1053
  }
1057
1054
  }
1055
+
1056
+ export async function withdrawToNobleIBC(payload: string): Promise<String> {
1057
+ try {
1058
+ const client = globalThis.client;
1059
+ if (client === undefined) {
1060
+ throw new UserError('client is not connected. Call connectClient() first');
1061
+ }
1062
+ const wallet = globalThis.wallet;
1063
+ if (wallet === undefined) {
1064
+ throw new UserError('wallet is not set. Call connectWallet() first');
1065
+ }
1066
+ const json = JSON.parse(payload);
1067
+
1068
+ const { subaccountNumber, amount, ibcPayload } = json ?? {};
1069
+ const parsedIbcPayload = JSON.parse(ibcPayload);
1070
+
1071
+ const msg = client.withdrawFromSubaccountMessage(
1072
+ new SubaccountInfo(wallet, subaccountNumber),
1073
+ parseFloat(amount).toFixed(client.validatorClient.config.denoms.USDC_DECIMALS),
1074
+ );
1075
+ const ibcMsg: EncodeObject = {
1076
+ typeUrl: parsedIbcPayload.msgTypeUrl,
1077
+ value: parsedIbcPayload.msg,
1078
+ };
1079
+
1080
+ const tx = await client.send(
1081
+ wallet,
1082
+ () => Promise.resolve([msg, ibcMsg]),
1083
+ false,
1084
+ );
1085
+
1086
+ return encodeJson({
1087
+ txHash: `0x${Buffer.from(tx?.hash).toString('hex')}`,
1088
+ });
1089
+ } catch (error) {
1090
+ return wrappedError(error);
1091
+ }
1092
+ }
1093
+
1094
+ export async function cctpWithdraw(squidPayload: string): Promise<String> {
1095
+ try {
1096
+ const client = globalThis.nobleClient;
1097
+ if (client === undefined || !client.isConnected) {
1098
+ throw new UserError(
1099
+ 'client is not connected.',
1100
+ );
1101
+ }
1102
+
1103
+ const json = JSON.parse(squidPayload);
1104
+
1105
+ const ibcMsg = {
1106
+ typeUrl: json.typeUrl, // '/circle.cctp.v1.MsgDepositForBurn',
1107
+ value: json.value,
1108
+ };
1109
+ const fee = await client.simulateTransaction([ibcMsg]);
1110
+
1111
+ // take out fee from amount before sweeping
1112
+ const amount = parseInt(ibcMsg.value.amount, 10) -
1113
+ Math.floor(parseInt(fee.amount[0].amount, 10) * GAS_MULTIPLIER);
1114
+
1115
+ if (amount <= 0) {
1116
+ throw new Error('noble balance does not cover fees');
1117
+ }
1118
+
1119
+ ibcMsg.value.amount = amount.toString();
1120
+
1121
+ const tx = await client.send([ibcMsg]);
1122
+
1123
+ return encodeJson(tx);
1124
+ } catch (error) {
1125
+ return wrappedError(error);
1126
+ }
1127
+ }
@@ -9,6 +9,7 @@ import {
9
9
  } from '@cosmjs/stargate';
10
10
 
11
11
  import { GAS_MULTIPLIER } from './constants';
12
+ import { MsgDepositForBurn } from './lib/cctpProto';
12
13
  import LocalWallet from './modules/local-wallet';
13
14
 
14
15
  export class NobleClient {
@@ -24,9 +25,7 @@ export class NobleClient {
24
25
  return Boolean(this.stargateClient);
25
26
  }
26
27
 
27
- async connect(
28
- wallet: LocalWallet,
29
- ): Promise<void> {
28
+ async connect(wallet: LocalWallet): Promise<void> {
30
29
  if (wallet?.offlineSigner === undefined) {
31
30
  throw new Error('Wallet signer not found');
32
31
  }
@@ -34,7 +33,12 @@ export class NobleClient {
34
33
  this.stargateClient = await SigningStargateClient.connectWithSigner(
35
34
  this.restEndpoint,
36
35
  wallet.offlineSigner,
37
- { registry: new Registry(defaultRegistryTypes) },
36
+ {
37
+ registry: new Registry([
38
+ ['/circle.cctp.v1.MsgDepositForBurn', MsgDepositForBurn],
39
+ ...defaultRegistryTypes,
40
+ ]),
41
+ },
38
42
  );
39
43
  }
40
44
 
package/src/index.ts CHANGED
@@ -16,3 +16,4 @@ export { FaucetClient } from './clients/faucet-client';
16
16
  export { SocketClient } from './clients/socket-client';
17
17
  export { NetworkOptimizer } from './network_optimizer';
18
18
  export { encodeJson } from './lib/helpers';
19
+ export { SubaccountInfo } from './clients/subaccount';