Skip to content

Commit

Permalink
Merge pull request #18 from gnosisguild/fix-condition-edit-bugs
Browse files Browse the repository at this point in the history
app: fix some bugs when editing conditions
  • Loading branch information
jfschwarz authored Aug 12, 2024
2 parents 99c1008 + faf9086 commit 4523133
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const useStyles = makeStyles((theme) => ({
}))

const PlaceholderPerType: Record<ParamNativeType, string> = {
[ParamNativeType.ARRAY]: "[value 1, value 2, ...]",
[ParamNativeType.TUPLE]: "(1,2,3)",
[ParamNativeType.ARRAY]: "[element 0, value 1, ...]",
[ParamNativeType.TUPLE]: "[field 0, field 1, ...]",
[ParamNativeType.BOOLEAN]: "true",
[ParamNativeType.INT]: "-235000000",
[ParamNativeType.UINT]: "235000000",
Expand Down Expand Up @@ -127,14 +127,13 @@ const useDecodedValue = (
) => {
const callbackRef = useRef(onDecodingError)
callbackRef.current = onDecodingError
const paramTypeString = param && param.format("full")
const valueDecoded = paramTypeString ? tryAbiDecode(paramTypeString, value) : value
const valueDecoded = param ? tryAbiDecode(param, value) : value

useEffect(() => {
if (paramTypeString) {
tryAbiDecode(paramTypeString, value, callbackRef.current)
if (param) {
tryAbiDecode(param, value, callbackRef.current)
}
}, [paramTypeString, value])
}, [param, value])

return valueDecoded
}
Expand All @@ -149,10 +148,16 @@ const tryAbiEncode = (param: ethers.utils.ParamType | null, value: string) => {
}
}

const tryAbiDecode = (param: string, value: string, onDecodingError?: (err: Error) => void) => {
const tryAbiDecode = (param: ethers.utils.ParamType, value: string, onDecodingError?: (err: Error) => void) => {
if (!value) return value

const paramTypeString = param.format("full")
const nativeType = getNativeType(param)
try {
return ethers.utils.defaultAbiCoder.decode([param], value).toString()
const decoded = ethers.utils.defaultAbiCoder.decode([paramTypeString], value)[0]
return nativeType === ParamNativeType.ARRAY || nativeType === ParamNativeType.TUPLE
? JSON.stringify(decoded)
: decoded.toString()
} catch (err) {
console.error("Error decoding value", err, { param, value })
if (onDecodingError) onDecodingError(err as Error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ export const TargetFunctionParams = ({ func, funcConditions, disabled, onChange

const parameterMismatch = (func: FunctionFragment, funcConditions: FunctionCondition): boolean =>
func.inputs.length <= funcConditions.params.reduce((acc, param) => (acc < param.index ? param.index : acc), 0)
console.log({
parameterMismatch: typeof func === "string" || parameterMismatch(func, funcConditions),
func: typeof func === "string" ? func : func.name,
funcConditions,
decodingError,
})

return typeof func === "string" || parameterMismatch(func, funcConditions) || decodingError ? (
<>
{indices.map((index) => {
Expand Down
24 changes: 7 additions & 17 deletions packages/app/src/services/rolesModifierContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { FunctionFragment, Interface } from "@ethersproject/abi"
import { _signer } from "../hooks/useWallet"
import { getExplorer } from "../utils/explorer"
import { Network } from "../utils/networks"
import { formatParamValue, isWriteFunction } from "../utils/conditions"
import { isWriteFunction } from "../utils/conditions"

export enum WalletType {
INJECTED = "injected",
Expand Down Expand Up @@ -75,13 +75,11 @@ function getFunctionTransaction(

for (let i = 0; i < paramsLength; i++) {
const param = funcCondition.params.find((param) => param.index === i)
if (param) {
const type = func.inputs[param.index]
const value = ethers.utils.defaultAbiCoder.encode([type], param.value)
if (param && param.condition !== ParamComparison.ONE_OF) {
isParamScoped.push(true)
paramType.push(getParameterTypeInt(param.type))
paramComp.push(getParamComparisonInt(param.condition))
compValue.push(value)
compValue.push(param.value[0])
} else {
isParamScoped.push(false)
paramType.push(0)
Expand Down Expand Up @@ -221,8 +219,6 @@ export const updateRole = async (
const paramLevelTxs: Promise<PopulatedTransaction>[] = Object.entries(paramEventsPerFunction)
.map(([sighash, params]) => {
return params.map((paramCondition) => {
const param = functions[sighash].inputs[paramCondition.index]

if (paramCondition.type === ParameterType.NO_RESTRICTION) {
// unscopeParameter
console.log("[updateRole] unscope parameter", [role.id, target.address, sighash, paramCondition.index])
Expand All @@ -233,16 +229,14 @@ export const updateRole = async (
paramCondition.index,
)
} else if (paramCondition.condition !== ParamComparison.ONE_OF) {
const value = formatParamValue(param, paramCondition.value[0])
const encodedValue = ethers.utils.defaultAbiCoder.encode([param], [value])
console.log("[updateRole] scope parameter", [
role.id,
target.address,
sighash,
paramCondition.index,
getParameterTypeInt(paramCondition.type),
getParamComparisonInt(paramCondition.condition),
encodedValue,
paramCondition.value[0],
])
return rolesModifierContract.populateTransaction.scopeParameter(
role.id,
Expand All @@ -251,28 +245,24 @@ export const updateRole = async (
paramCondition.index,
getParameterTypeInt(paramCondition.type),
getParamComparisonInt(paramCondition.condition),
encodedValue,
paramCondition.value[0],
)
} else {
const encodedValues = paramCondition.value.map((value) => {
return ethers.utils.defaultAbiCoder.encode([param], [formatParamValue(param, value)])
})

console.log("[updateRole] scope parameter as OneOf", [
role.id,
target.address,
sighash,
paramCondition.index,
getParameterTypeInt(paramCondition.type),
encodedValues || [],
paramCondition.value,
])
return rolesModifierContract.populateTransaction.scopeParameterAsOneOf(
role.id,
target.address,
sighash,
paramCondition.index,
getParameterTypeInt(paramCondition.type),
encodedValues || [],
paramCondition.value,
)
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/typings/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface ParamCondition {
index: number
type: ParameterType
condition: ParamComparison
value: string[]
value: string[] // usually a single-element array, multiple values are used only for ParamComparison.ONE_OF
}

export enum ParamComparison {
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/utils/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const getWriteFunctions = (abi: JsonFragment[] | undefined) =>
!abi ? [] : Object.values(new Interface(abi).functions).filter(isWriteFunction)

export function formatParamValue(param: ethers.utils.ParamType, value: string) {
if (getNativeType(param) === ParamNativeType.ARRAY) return JSON.parse(value)
const nativeType = getNativeType(param)
if (nativeType === ParamNativeType.ARRAY || nativeType === ParamNativeType.TUPLE) return JSON.parse(value)
return value
}

0 comments on commit 4523133

Please sign in to comment.