@rabby-wallet/rabby-action 0.1.14 → 0.1.15-beta.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.
@@ -8,10 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import PQueue from 'p-queue';
11
+ import BigNumber from 'bignumber.js';
11
12
  import { waitQueueFinished } from '../../utils/waitQueueFinished';
13
+ const BALANCE_OF_SELECTOR = '0x70a08231';
12
14
  export const fetchDataApproveToken = (options, likeAction) => __awaiter(void 0, void 0, void 0, function* () {
13
15
  const queue = new PQueue();
14
- const { sender, apiProvider, chainId, actionData } = options;
16
+ const { sender, apiProvider, chainId, actionData, walletProvider } = options;
15
17
  const action = likeAction || actionData.approveToken;
16
18
  if (!action || !chainId) {
17
19
  return {};
@@ -49,16 +51,37 @@ export const fetchDataApproveToken = (options, likeAction) => __awaiter(void 0,
49
51
  }));
50
52
  }
51
53
  }));
52
- if (token) {
53
- queue.add(() => __awaiter(void 0, void 0, void 0, function* () {
54
- const t = yield apiProvider.getToken(sender, chainId, token.id);
55
- result.token = t;
56
- }));
57
- }
54
+ const tokenTask = token
55
+ ? queue.add(() => __awaiter(void 0, void 0, void 0, function* () {
56
+ if (!walletProvider.ethRpc) {
57
+ const tokenData = yield apiProvider.getToken(sender, chainId, token.id);
58
+ if (tokenData) {
59
+ result.token = tokenData;
60
+ }
61
+ return;
62
+ }
63
+ const data = `${BALANCE_OF_SELECTOR}${sender
64
+ .toLowerCase()
65
+ .replace(/^0x/, '')
66
+ .padStart(64, '0')}`;
67
+ const rawAmountHex = yield walletProvider.ethRpc({
68
+ method: 'eth_call',
69
+ params: [{ to: token.id, data }, 'latest'],
70
+ }, chainId);
71
+ if (typeof rawAmountHex !== 'string' ||
72
+ !/^0x[0-9a-f]+$/i.test(rawAmountHex)) {
73
+ throw new Error('Invalid token balance returned by ethRpc');
74
+ }
75
+ const rawAmount = new BigNumber(rawAmountHex.slice(2), 16);
76
+ result.token = Object.assign(Object.assign({}, token), { amount: rawAmount
77
+ .div(new BigNumber(10).pow(token.decimals))
78
+ .toNumber(), raw_amount: rawAmount.toFixed(0), raw_amount_hex_str: `0x${rawAmount.toString(16)}` });
79
+ }))
80
+ : Promise.resolve();
58
81
  queue.add(() => __awaiter(void 0, void 0, void 0, function* () {
59
82
  const hasInteraction = yield apiProvider.hasInteraction(sender, chainId, spender);
60
83
  result.hasInteraction = hasInteraction.has_interaction;
61
84
  }));
62
- yield waitQueueFinished(queue);
85
+ yield Promise.all([waitQueueFinished(queue), tokenTask]);
63
86
  return result;
64
87
  });
@@ -62,7 +62,15 @@ export interface TransactionGroup {
62
62
  isSubmitFailed?: boolean;
63
63
  $ctx?: any;
64
64
  }
65
+ type EthRpc = (data: {
66
+ method: 'eth_call';
67
+ params: [{
68
+ to: string;
69
+ data: string;
70
+ }, 'latest'];
71
+ }, chainId: string) => Promise<string>;
65
72
  export type WalletProvider = {
73
+ ethRpc?: EthRpc;
66
74
  hasAddress: (address: string) => Promise<boolean>;
67
75
  hasPrivateKeyInWallet: (address: string) => Promise<string>;
68
76
  getWhitelist: () => Promise<string[]>;
@@ -77,3 +85,4 @@ export type WalletProvider = {
77
85
  }) => Chain | null | undefined;
78
86
  ALIAS_ADDRESS: Record<string, string>;
79
87
  };
88
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/rabby-action",
3
- "version": "0.1.14",
3
+ "version": "0.1.15-beta.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [