@exodus/ethereum-api 2.19.0 → 2.20.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/ethereum-api",
3
- "version": "2.19.0",
3
+ "version": "2.20.0",
4
4
  "description": "Ethereum Api",
5
5
  "main": "src/index.js",
6
6
  "author": "Exodus Movement, Inc.",
@@ -29,5 +29,5 @@
29
29
  "@exodus/assets-base": "^8.0.136",
30
30
  "@exodus/models": "^8.7.2"
31
31
  },
32
- "gitHead": "de6258fcd05409586558bbffff54c2c2a3c065b1"
32
+ "gitHead": "f6ffc7e8100b7b1b85d2c7b30cce4aa2b251cab3"
33
33
  }
@@ -134,13 +134,7 @@ export const getERC20Params = async ({
134
134
  })
135
135
  )
136
136
 
137
- const response = paramNames.reduce(
138
- (accumulatedObj, paramName, index) => ({
139
- ...accumulatedObj,
140
- [paramName]: paramValues[index],
141
- }),
142
- {}
143
- )
137
+ const response = Object.fromEntries(paramNames.map((paramName, index) => ([paramName, paramValues[index]])))
144
138
  erc20ParamsCache[cacheKey] = response
145
139
 
146
140
  return response
@@ -7,18 +7,40 @@ const ethDecimals = assets.ethereum.units.ETH
7
7
 
8
8
  const ethHexToInt = (hexValue) => parseInt(hexValue, '16')
9
9
 
10
- async function getDecimals(type, assetContractAddress = null) {
11
- if (type === 'erc20' && assetContractAddress) {
12
- const { decimals } = await getERC20Params({
13
- address: assetContractAddress,
14
- assetName: 'ethereum',
15
- paramNames: ['decimals'],
16
- })
17
-
18
- return decimals
10
+ async function getDecimalsFromSimulatedTx(internalTransactions, balanceChanges) {
11
+ const decimals = {}
12
+
13
+ if (internalTransactions && Array.isArray(internalTransactions)) {
14
+ for (const transaction of internalTransactions) {
15
+ const { contractCall } = transaction
16
+
17
+ if (!contractCall) continue
18
+
19
+ if (contractCall.methodName === 'transfer' || contractCall.methodName === 'transferFrom') {
20
+ decimals[contractCall.contractAlias] = contractCall.contractDecimals
21
+ }
22
+ }
19
23
  }
20
24
 
21
- return ethDecimals
25
+ for (const balanceChange of balanceChanges) {
26
+ const { asset } = balanceChange
27
+
28
+ if (typeof decimals[asset.symbol] === 'number') {
29
+ continue
30
+ }
31
+
32
+ if (asset.type === 'erc20' && asset.contractAddress) {
33
+ const { decimals: assetDecimal } = await getERC20Params({
34
+ address: asset.contractAddress,
35
+ assetName: 'ethereum',
36
+ paramNames: ['decimals'],
37
+ })
38
+
39
+ decimals[asset.symbol] = assetDecimal
40
+ }
41
+ }
42
+
43
+ return decimals
22
44
  }
23
45
 
24
46
  export async function simulateAndRetrieveSideEffects(transaction) {
@@ -41,31 +63,29 @@ export async function simulateAndRetrieveSideEffects(transaction) {
41
63
  const simulatedTx = await fetchTxPreview(blocknativeTxObject)
42
64
 
43
65
  const [simulatedBalanceChanges] = simulatedTx.netBalanceChanges
66
+ const [internalTransactions] = simulatedTx.internalTransactions
44
67
 
45
68
  const [sender] =
46
69
  simulatedBalanceChanges &&
47
70
  simulatedBalanceChanges.filter(({ address }) => address === transaction.from)
48
71
 
49
72
  if (sender) {
73
+ const decimals = await getDecimalsFromSimulatedTx(internalTransactions, sender.balanceChanges)
74
+
50
75
  for (const balanceChange of sender.balanceChanges) {
51
76
  const { delta, asset } = balanceChange
52
77
 
53
- const decimal = await getDecimals(asset.type, asset.contractAddress)
78
+ const account = {
79
+ symbol: asset.symbol,
80
+ balance: delta,
81
+ assetType: asset.type,
82
+ decimal: decimals[asset.symbol],
83
+ }
54
84
 
55
85
  if (delta.startsWith('-')) {
56
- willSend.push({
57
- symbol: asset.symbol,
58
- balance: delta.slice(1),
59
- assetType: asset.type,
60
- decimal,
61
- })
86
+ willSend.push(account)
62
87
  } else {
63
- willReceive.push({
64
- symbol: asset.symbol,
65
- balance: delta,
66
- assetType: asset.type,
67
- decimal,
68
- })
88
+ willReceive.push(account)
69
89
  }
70
90
  }
71
91
  }
@@ -73,13 +93,13 @@ export async function simulateAndRetrieveSideEffects(transaction) {
73
93
  if (!willSend.length) {
74
94
  willSend.push({
75
95
  symbol: 'ETH',
76
- balance: ethHexToInt(transaction.value).toString(),
96
+ balance: `-${ethHexToInt(transaction.value)}`,
77
97
  assetType: 'ether',
78
98
  decimal: ethDecimals,
79
99
  })
80
100
  }
81
- } catch (_) {
82
- throw new Error('Simulation of Ethereum transaction failed.')
101
+ } catch (err) {
102
+ throw new Error(err)
83
103
  }
84
104
 
85
105
  return { willSend, willReceive }