@paradex/sdk 0.3.0 → 0.4.0

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.
@@ -46,6 +46,11 @@ export declare function getSocializedLossFactor(params: GetSocializedLossFactorP
46
46
  interface GetReceivableAmountParams {
47
47
  readonly config: ParadexConfig;
48
48
  readonly provider: Pick<ParaclearProvider, 'callContract'>;
49
+ /**
50
+ * Token symbol.
51
+ * @example 'USDC'
52
+ */
53
+ readonly token: string;
49
54
  /**
50
55
  * Amount of to withdraw from Paradex, as a decimal string.
51
56
  * The receivable amount will be calculated based on this amount and
package/dist/paraclear.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { BigNumber } from 'bignumber.js';
2
2
  import * as Starknet from 'starknet';
3
+ const MAX_FEE = BigNumber('5e17'); // 5e17 WEI = 0.5 ETH
3
4
  /**
4
5
  * Get the Paraclear balance of the given token for the given account.
5
6
  */
@@ -64,12 +65,16 @@ export async function getReceivableAmount(params) {
64
65
  if (amountBn.isNaN()) {
65
66
  throw new Error('Invalid amount');
66
67
  }
68
+ const token = params.config.bridgedTokens[params.token];
69
+ if (token == null) {
70
+ throw new Error(`Token ${params.token} is not supported`);
71
+ }
67
72
  const { socializedLossFactor } = await getSocializedLossFactor({
68
73
  config: params.config,
69
74
  provider: params.provider,
70
75
  });
71
76
  const receivableAmount = amountBn.times(BigNumber(1).minus(socializedLossFactor));
72
- const receivableAmountChainBn = toChainSize(receivableAmount.toString(), params.config.paraclearDecimals);
77
+ const receivableAmountChainBn = toChainSize(receivableAmount.toString(), token.decimals);
73
78
  return {
74
79
  receivableAmount: receivableAmount.toString(),
75
80
  receivableAmountChain: receivableAmountChainBn.toString(),
@@ -95,22 +100,30 @@ export async function withdraw(params) {
95
100
  throw new Error(`Token ${params.token} is not supported`);
96
101
  }
97
102
  const chainAmountBn = toChainSize(params.amount, params.config.paraclearDecimals);
103
+ // ensure unique txn hash on subsequent calls via `intNoise`
104
+ const maxFee = MAX_FEE.plus(intNoise(10000));
98
105
  const result = await params.account.execute([
99
106
  {
100
107
  contractAddress: params.config.paraclearAddress,
101
- entrypoint: 'initiate_withdrawal',
102
- calldata: Starknet.CallData.compile([
103
- token.l2TokenAddress,
104
- chainAmountBn.toString(),
105
- ]),
108
+ entrypoint: 'withdraw',
109
+ calldata: [token.l2TokenAddress, chainAmountBn.toString()],
106
110
  },
107
111
  params.bridgeCall,
108
- ]);
112
+ ], undefined, { maxFee: maxFee.toString() });
109
113
  return { hash: result.transaction_hash };
110
114
  }
111
115
  function fromChainSize(size, decimals) {
112
116
  return new BigNumber(size).div(10 ** decimals);
113
117
  }
114
118
  function toChainSize(size, decimals) {
115
- return new BigNumber(size).times(10 ** decimals);
119
+ return new BigNumber(size)
120
+ .times(10 ** decimals)
121
+ .integerValue(BigNumber.ROUND_FLOOR);
122
+ }
123
+ /**
124
+ * Generates a pseudorandom integer between 0 and `max`.
125
+ * @param max Maximum value for the noise.
126
+ */
127
+ function intNoise(max) {
128
+ return Math.round(Math.random() * max);
116
129
  }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generates a pseudorandom integer between 0 and `max`.
3
+ * @param max Maximum value for the noise.
4
+ */
5
+ export declare function intNoise(max: number): number;
package/dist/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Generates a pseudorandom integer between 0 and `max`.
3
+ * @param max Maximum value for the noise.
4
+ */
5
+ export function intNoise(max) {
6
+ return Math.round(Math.random() * max);
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paradex/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "JavaScript SDK for Paradex",
5
5
  "main": "dist/index.js",
6
6
  "files": [