Skip to content

Commit f89277c

Browse files
dkent600arseneeth
andauthored
transform GenericScheme array item values just like field values (#2289)
* add BalancerPoolManager.json * upd BalancerPoolManager.json * add BalancerPoolManager.json to index.ts * fix change address to lower case * update BalancerPoolManager.json * fix lint * transform array values like regular field values * Revert changes to json * back out spaces changes * comment Co-authored-by: arsenyjin <[email protected]>
1 parent 35a43d6 commit f89277c

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/genericSchemeRegistry/index.ts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,40 +96,56 @@ export class ActionField {
9696
this.placeholder = options.placeholder;
9797
this.transformation = options.transformation;
9898
}
99-
/**
100-
* the value to pass to the contract call (as calculated from the user's input data)
101-
* @return [description]
102-
*/
103-
public callValue(userValue: string|string[]) {
104-
if (Array.isArray(userValue)) {
105-
userValue = userValue.map((val: string) => Object.prototype.hasOwnProperty.call(val, "trim") ? val.trim() : val);
106-
} else if (Object.prototype.hasOwnProperty.call(userValue, "trim")) {
107-
userValue = userValue.trim();
99+
100+
private transformValue(value: string, web3: any): string | boolean {
101+
if (Object.prototype.hasOwnProperty.call(value, "trim")) {
102+
value = (value as string).trim();
108103
}
109104

110105
if (this.type === "bool") {
111-
return parseInt(userValue as string, 10) === 1;
106+
return parseInt(value as string, 10) === 1;
112107
}
113-
114-
if (this.decimals) {
115-
return (new BN(userValue as string).mul(new BN(10).pow(new BN(this.decimals)))).toString();
108+
/**
109+
* Note that if this is an array item, the field's one `decimals` value applies
110+
* to all items in the array. Same for `transformation`.
111+
*/
112+
else if (this.decimals) {
113+
return (new BN(value as string).mul(new BN(10).pow(new BN(this.decimals)))).toString();
114+
}
115+
else {
116+
switch (this.transformation) {
117+
case "namehash": {
118+
return namehash.hash(value);
119+
}
120+
case "keccak256": {
121+
return web3.utils.keccak256(value);
122+
}
123+
case "toWei": {
124+
return web3.utils.toWei(value.toString(), "ether").toString();
125+
}
126+
default:
127+
return value;
128+
}
116129
}
130+
}
131+
132+
/**
133+
* the value to pass to the contract call (as calculated from the user's input data)
134+
* @return [description]
135+
*/
136+
public callValue(userValue: string | Array<string>): string | boolean | Array<string | boolean> {
137+
let returnValue: string | boolean | Array<string | boolean> = userValue;
117138

118139
const web3 = new Web3();
119140

120-
switch (this.transformation) {
121-
case "namehash": {
122-
return namehash.hash(userValue);
123-
}
124-
case "keccak256": {
125-
return web3.utils.keccak256(userValue);
126-
}
127-
case "toWei": {
128-
return web3.utils.toWei(userValue.toString(), "ether").toString();
129-
}
141+
if (Array.isArray(returnValue)) {
142+
returnValue = (returnValue as Array<string>).map((val: string): string | boolean => {
143+
return this.transformValue(val, web3);
144+
});
145+
} else {
146+
returnValue = this.transformValue(returnValue, web3);
130147
}
131-
132-
return userValue;
148+
return returnValue;
133149
}
134150
}
135151

0 commit comments

Comments
 (0)