Skip to content

Commit 6747313

Browse files
authored
Merge pull request #1589 from AmbireTech/fix/humanizer
Humanizer bug fixes
2 parents a875df2 + d8a7fb9 commit 6747313

File tree

5 files changed

+374
-216
lines changed

5 files changed

+374
-216
lines changed

src/components/common/TxnPreview/TxnPreview.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,23 @@ export default function TxnPreview({
9292
<div className={styles.summary}>
9393
{extendedSummary.map((entry) => {
9494
// If entry is extended summary parse it
95-
if (Array.isArray(entry)) {
96-
return entry.map((item, i) => (
97-
<ExtendedSummaryItem
98-
key={`item-${i}`}
99-
item={item}
100-
i={i}
101-
networkDetails={networkDetails}
102-
feeAssets={feeAssets}
103-
/>
104-
))
105-
}
106-
if (typeof entry !== 'string') {
107-
return `operation with address ${txn[0]} (unable to parse)`
108-
}
109-
return entry
95+
return <div className={styles.subSummary}>
96+
{
97+
Array.isArray(entry) ?
98+
entry.map((item, i) => (
99+
<ExtendedSummaryItem
100+
key={`item-${i}`}
101+
item={item}
102+
i={i}
103+
networkDetails={networkDetails}
104+
feeAssets={feeAssets}
105+
/>
106+
))
107+
: typeof entry !== 'string' ?
108+
`operation with address ${txn[0]} (unable to parse)`
109+
: entry
110+
}
111+
</div>
110112
})}
111113
</div>
112114
{onDismiss ? (

src/components/common/TxnPreview/TxnPreview.module.scss

+9-5
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ $extended-summary-height: 2.25rem;
6666

6767
.summary {
6868
width: 100%;
69-
display: flex;
70-
align-items: center;
71-
column-gap: 0.625rem;
72-
row-gap: 0.5rem;
73-
flex-wrap: wrap;
7469
word-break: break-word;
7570
font-size: 0.875rem;
71+
row-gap: 0.5rem;
72+
}
73+
.subSummary{
74+
align-items: center;
75+
display: flex;
76+
flex-wrap: wrap;
77+
}
78+
.subSummary > *{
79+
margin-right: 0.625rem;
7680
}
7781
}
7882

src/lib/humanReadableTransactions.js

+90-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { formatUnits } from 'ethers/lib/utils'
22
import { constants } from 'ethers'
33
import networks from 'consts/networks'
44
import humanizers from './humanizers'
5+
import { isZeroAddress } from 'ethereumjs-util'
56

67
// address (lowercase) => name
78
let knownAliases = {}
@@ -10,6 +11,93 @@ const knownTokens = {}
1011
// address (lowercase) => name
1112
const knownAddressNames = {}
1213

14+
function parseActions(actions){
15+
const result = []
16+
for(let i=0;i<actions.length;i++){
17+
18+
const notLast = i < actions.length-1
19+
if(!notLast){
20+
result.push(actions[i])
21+
continue
22+
}
23+
24+
if(
25+
// are valid [obj]
26+
actions[i].length>=4 &&
27+
actions[i+1].length>=2 &&
28+
Array.isArray(actions[i]) &&
29+
Array.isArray(actions[i+1]) &&
30+
// are actual swap and unwrap
31+
typeof actions[i][0] === 'string' &&
32+
actions[i][0].startsWith('Swap') &&
33+
actions[i][3].type==='token' &&
34+
// isWrappedAsset(actions[i][3].address) &&
35+
typeof actions[i+1][3] === 'string' &&
36+
actions[i+1][0].startsWith('Unwrap') &&
37+
actions[i+1][1].type==='token' &&
38+
// have proper values and addresses
39+
actions[i][3].amount === actions[i+1][1].amount &&
40+
isZeroAddress(actions[i+1][1].address)
41+
){
42+
// swap x for at least y
43+
result.push(["Swap", actions[i][1], actions[i][2],actions[i+1][1]])
44+
// skip next ccall, since two were merged
45+
i++
46+
continue
47+
}
48+
49+
if(
50+
// are valid [obj]
51+
actions[i].length>=2 &&
52+
actions[i+1].length>=4 &&
53+
Array.isArray(actions[i]) &&
54+
Array.isArray(actions[i+1]) &&
55+
// are actual Wrap and Swap
56+
typeof actions[i][0] === 'string' &&
57+
actions[i][0].startsWith('Wrap') &&
58+
actions[i][1].type==='token' &&
59+
typeof actions[i+1][0] === 'string' &&
60+
actions[i+1][0].startsWith('Swap') &&
61+
actions[i+1][3].type==='token' &&
62+
// have proper values and addresses
63+
actions[i+1][1].amount === actions[i][1].amount &&
64+
isZeroAddress(actions[i][1].address)
65+
){
66+
// swap x for at least y
67+
result.push(["Swap", actions[i][1], actions[i+1][2],actions[i+1][3]])
68+
// skip next ccall, since two were merged
69+
i++
70+
continue
71+
}
72+
73+
74+
75+
if(
76+
// are valid [obj]
77+
actions[i].length==2 &&
78+
actions[i+1].length==2 &&
79+
Array.isArray(actions[i]) &&
80+
Array.isArray(actions[i+1]) &&
81+
// are actual Unwrap and Sweep
82+
typeof actions[i][0] === 'string' &&
83+
actions[i][0].startsWith('Unwrap') &&
84+
actions[i][1].type==='token' &&
85+
typeof actions[i+1][0] === 'string' &&
86+
actions[i+1][0].startsWith('Sweep') &&
87+
actions[i+1][1].type==='token'
88+
){
89+
result.push(["Remove liquidity and withdraw", actions[i][1], "and", actions[i+1][1]])
90+
// skip next ccall, since two were merged
91+
i++
92+
continue
93+
}
94+
95+
result.push(actions[i])
96+
continue
97+
}
98+
return result
99+
}
100+
13101
export const formatNativeTokenAddress = (address) =>
14102
address.toLowerCase() === `0x${'e'.repeat(40)}` ? `0x${'0'.repeat(40)}` : address.toLowerCase()
15103

@@ -82,7 +170,8 @@ export function getTransactionSummary(
82170

83171
if (humanizer) {
84172
try {
85-
const actions = humanizer({ to, value, data, from: accountAddr }, network, opts)
173+
let actions = humanizer({ to, value, data, from: accountAddr }, network, opts)
174+
actions = parseActions(actions)
86175
return opts.extended === true ? actions : actions.join(', ')
87176
} catch (e) {
88177
callSummary = opts.extended

0 commit comments

Comments
 (0)