Skip to content

Commit 4ecf7e0

Browse files
committed
include approval action in asset transfer permissions
1 parent d988a9a commit 4ecf7e0

File tree

8 files changed

+110
-24
lines changed

8 files changed

+110
-24
lines changed

src/components/ActionsBuilder/Action/Action.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface ActionViewProps {
2929
call?: Call;
3030
decodedAction?: DecodedAction;
3131
isEditable?: boolean;
32-
onEdit?: (updatedCall: DecodedAction) => void;
32+
onEdit?: (updatedCall: DecodedAction | DecodedAction[]) => void;
3333
onRemove?: (updatedCall: DecodedAction) => void;
3434
}
3535

src/components/ActionsBuilder/Option/Option.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ export const OptionRow: React.FC<OptionRowProps> = ({
4343
} = useSortable({ id: option.id });
4444
const [isActionsModalOpen, setIsActionsModalOpen] = useState(false);
4545

46-
function addAction(action: DecodedAction) {
46+
function addAction(action: DecodedAction | DecodedAction[]) {
47+
const newActions = Array.isArray(action) ? action : [action];
48+
4749
onChange({
4850
...option,
49-
decodedActions: [...option.decodedActions, action],
51+
decodedActions: [...option.decodedActions, ...newActions],
5052
});
5153
}
5254

53-
function updateAction(index: number, action: DecodedAction) {
55+
function updateAction(
56+
index: number,
57+
action: DecodedAction | DecodedAction[]
58+
) {
59+
if (Array.isArray(action)) return;
60+
5461
const updatedActions = option?.decodedActions.map((a, i) =>
5562
index === i ? action : a
5663
);

src/components/ActionsBuilder/SupportedActions/SetPermissions/SetPermissionsEditor.tsx

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { Controller, useForm } from 'react-hook-form';
66
import { FiChevronDown } from 'react-icons/fi';
77

88
import { ParsedDataInterface, TABS } from './types';
9-
import { ANY_FUNC_SIGNATURE, ERC20_TRANSFER_SIGNATURE } from 'utils';
9+
import {
10+
ANY_FUNC_SIGNATURE,
11+
ERC20_TRANSFER_SIGNATURE,
12+
ERC20_APPROVE_SIGNATURE,
13+
} from 'utils';
1014
import { resolveUri } from 'utils/url';
1115
import { ActionEditorProps } from '..';
1216
import { useTokenList } from 'hooks/Guilds/tokens/useTokenList';
@@ -62,6 +66,10 @@ const Permissions: React.FC<ActionEditorProps> = ({
6266
};
6367
}, [decodedCall]);
6468

69+
const isEdit = useMemo(() => {
70+
return !!decodedCall.args?.to || !!decodedCall.args?.functionSignature;
71+
}, []); //eslint-disable-line
72+
6573
const { t } = useTranslation();
6674

6775
const [activeTab, setActiveTab] = useState(parsedData.tab);
@@ -111,30 +119,78 @@ const Permissions: React.FC<ActionEditorProps> = ({
111119
updateFunctionSignature(value);
112120
};
113121

114-
const submitAction = (values: FormValues) => {
115-
const isAssetTransferCall = activeTab === TABS.ASSET_TRANSFER;
122+
const submitAssetTransfer = (values: FormValues) => {
123+
const baseCall = {
124+
...decodedCall,
125+
args: {
126+
...decodedCall.args,
127+
to: values.tokenAddress,
128+
valueAllowed: BigNumber.from(0),
129+
// "from" field set by default previously as guild id
130+
},
131+
optionalProps: {
132+
asset: values.tokenAddress,
133+
tab: TABS.ASSET_TRANSFER,
134+
},
135+
};
136+
const newAssetTransferCall: DecodedCall = {
137+
...baseCall,
138+
args: {
139+
...baseCall.args,
140+
functionSignature: ERC20_TRANSFER_SIGNATURE,
141+
},
142+
optionalProps: {
143+
...baseCall.optionalProps,
144+
functionName: '',
145+
},
146+
};
116147

148+
const newApprovalAssetCall = {
149+
...decodedCall,
150+
args: {
151+
...decodedCall.args,
152+
functionSignature: ERC20_APPROVE_SIGNATURE,
153+
},
154+
optionalProps: {
155+
...baseCall.optionalProps,
156+
functionName: 'approve',
157+
},
158+
};
159+
160+
if (isEdit) {
161+
// in case of edit mode we submit only one action that is being edited
162+
return parsedData.functionSignature === ERC20_APPROVE_SIGNATURE
163+
? onSubmit(newApprovalAssetCall)
164+
: onSubmit(newAssetTransferCall);
165+
}
166+
return onSubmit([newAssetTransferCall, newApprovalAssetCall]);
167+
};
168+
169+
const submitFunctionCall = (values: FormValues) => {
117170
const newCall: DecodedCall = {
118171
...decodedCall,
119172
args: {
120173
...decodedCall.args,
121-
to: isAssetTransferCall ? values.tokenAddress : values.toAddress,
174+
to: values.toAddress,
122175
// native value allowed
123-
valueAllowed: isAssetTransferCall ? BigNumber.from(0) : values.amount,
124-
functionSignature: isAssetTransferCall
125-
? ERC20_TRANSFER_SIGNATURE
126-
: values.functionSignature,
176+
valueAllowed: values.amount,
177+
functionSignature: values.functionSignature,
127178
// "from" field set by default previously as guild id
128179
},
129180
optionalProps: {
130-
functionName: isAssetTransferCall ? '' : values.functionName,
131-
asset: isAssetTransferCall ? values.tokenAddress : '',
181+
functionName: values.functionName,
182+
asset: '',
132183
tab: activeTab,
133184
},
134185
};
135186
onSubmit(newCall);
136187
};
137188

189+
const submitAction = (values: FormValues) => {
190+
const isAssetTransferCall = activeTab === TABS.ASSET_TRANSFER;
191+
(isAssetTransferCall ? submitAssetTransfer : submitFunctionCall)(values);
192+
};
193+
138194
const tabArray = [
139195
{
140196
title: t('assetTransfer'),

src/components/ActionsBuilder/SupportedActions/SetPermissions/SetPermissionsInfoLine.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { Flex } from 'components/primitives/Layout';
99
import { useNetwork } from 'wagmi';
1010
import { useTokenList } from 'hooks/Guilds/tokens/useTokenList';
1111
import { resolveUri } from 'utils/url';
12-
import { shortenAddress } from 'utils';
12+
import {
13+
shortenAddress,
14+
ERC20_TRANSFER_SIGNATURE,
15+
ERC20_APPROVE_SIGNATURE,
16+
} from 'utils';
1317

1418
const SetPermissionsInfoLine: React.FC<ActionViewProps> = ({
1519
decodedCall,
@@ -48,7 +52,15 @@ const SetPermissionsInfoLine: React.FC<ActionViewProps> = ({
4852
<Segment>
4953
<BiCheckShield size={16} />
5054
</Segment>
51-
<Segment>{t('setPermissionsFor')}</Segment>
55+
<Segment>
56+
{t(
57+
parsedData?.functionSignature === ERC20_APPROVE_SIGNATURE
58+
? 'setApprovalPermissionsFor'
59+
: parsedData?.functionSignature === ERC20_TRANSFER_SIGNATURE
60+
? 'setTransferPermissionsFor'
61+
: 'setPermissionsFor'
62+
)}
63+
</Segment>
5264
<Segment>
5365
{currentToken && currentToken.address ? (
5466
<>

src/components/ActionsBuilder/SupportedActions/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface ActionViewProps {
3232

3333
export interface ActionEditorProps extends ActionViewProps {
3434
updateCall?: (updatedCall: DecodedCall) => void;
35-
onSubmit: (decodedCall: DecodedCall) => void;
35+
onSubmit: (decodedCall: DecodedCall | DecodedCall[]) => void;
3636
}
3737

3838
type SupportedActionViews = {

src/components/ActionsModal/ActionsModal.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const ActionModal: React.FC<ActionModalProps> = ({
162162
<Editor
163163
decodedCall={data}
164164
updateCall={setData}
165-
onSubmit={saveSupportedAction}
165+
onSubmit={handleEditorSubmit}
166166
/>
167167
{displaySubmit(selectedAction) && (
168168
<Button
@@ -218,20 +218,29 @@ const ActionModal: React.FC<ActionModalProps> = ({
218218
setSelectedAction(action);
219219
}
220220

221-
function saveSupportedAction(call?: DecodedCall) {
222-
const decodedCall = call ?? data;
223-
221+
function buildAction(decodedCall: DecodedCall): DecodedAction {
222+
if (!decodedCall) return null;
224223
const defaultDecodedAction = defaultValues[decodedCall.callType];
225224

226-
if (!selectedAction || !decodedCall) return;
227-
228225
const decodedAction: DecodedAction = {
229226
id: `action-${Math.random()}`,
230227
decodedCall,
231228
contract: defaultDecodedAction.contract,
232229
};
230+
return decodedAction;
231+
}
233232

233+
function saveSupportedAction(call?: DecodedCall) {
234+
if (!call) return;
235+
const decodedAction = buildAction(call);
234236
onAddAction(decodedAction);
237+
}
238+
239+
function handleEditorSubmit(calls?: DecodedCall[] | DecodedCall) {
240+
if (!calls) return;
241+
onAddAction(
242+
Array.isArray(calls) ? calls.map(buildAction) : buildAction(calls)
243+
);
235244
handleClose();
236245
}
237246

src/components/ActionsModal/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { DecodedAction } from 'components/ActionsBuilder/types';
33
export interface ActionModalProps {
44
isOpen: boolean;
55
setIsOpen: (isOpen: boolean) => void;
6-
onAddAction: (action: DecodedAction) => void;
6+
onAddAction: (action: DecodedAction | DecodedAction[]) => void;
77
action?: DecodedAction;
88
}

src/locales/en/translation.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"functionCalls": "Function Calls",
7070
"permission": "Permission",
7171
"setPermissionsFor": "Set permissions for",
72+
"setApprovalPermissionsFor": "Set approval permissions for",
73+
"setTransferPermissionsFor": "Set transfer permissions for",
7274
"unknownAction": "Unknown Action",
7375
"default": "Default",
7476
"edit": "Edit",

0 commit comments

Comments
 (0)