Skip to content

Commit b6fb035

Browse files
authored
Merge pull request #325 from MiltonTulli/feature/approve-asset-transfer
Add approve to asset transfer permissions
2 parents b795a92 + a6a3c24 commit b6fb035

File tree

13 files changed

+164
-74
lines changed

13 files changed

+164
-74
lines changed

src/components/ActionsBuilder/Action/Action.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ export const ActionRow: React.FC<ActionViewProps> = ({
179179
isOpen={isEditActionModalOpen}
180180
setIsOpen={setIsEditActionModalOpen}
181181
action={decodedAction}
182-
onAddAction={onEdit}
182+
onAddActions={null}
183+
onEditAction={onEdit}
184+
isEditing={true}
183185
/>
184186
)}
185187
</CardWrapperWithMargin>

src/components/ActionsBuilder/Option/Option.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ 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 addActions(actions: DecodedAction[]) {
4747
onChange({
4848
...option,
49-
decodedActions: [...option.decodedActions, action],
49+
decodedActions: [...option.decodedActions, ...actions],
5050
});
5151
}
5252

53-
function updateAction(index: number, action: DecodedAction) {
54-
const updatedActions = option?.decodedActions.map((a, i) =>
55-
index === i ? action : a
53+
function updateAction(action: DecodedAction) {
54+
const updatedActions = option?.decodedActions.map(a =>
55+
a.id === action.id ? action : a
5656
);
5757
onChange({ ...option, decodedActions: updatedActions });
5858
}
@@ -118,7 +118,7 @@ export const OptionRow: React.FC<OptionRowProps> = ({
118118
key={index}
119119
isEditable={true}
120120
decodedAction={action}
121-
onEdit={updatedAction => updateAction(index, updatedAction)}
121+
onEdit={updateAction}
122122
onRemove={targetAction => removeAction(targetAction)}
123123
/>
124124
);
@@ -137,10 +137,12 @@ export const OptionRow: React.FC<OptionRowProps> = ({
137137
<ActionModal
138138
isOpen={isActionsModalOpen}
139139
setIsOpen={setIsActionsModalOpen}
140-
onAddAction={action => {
141-
addAction(action);
140+
onAddActions={action => {
141+
addActions(action);
142142
setIsActionsModalOpen(false);
143143
}}
144+
onEditAction={updateAction}
145+
isEditing={false}
144146
/>
145147
</OptionWrapper>
146148
);

src/components/ActionsBuilder/SupportedActions/ERC20Transfer/ERC20TransferEditor.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,30 @@ const ERC20TransferEditor: React.FC<ActionEditorProps> = ({
9191
if (values.token.type === TokenType.ERC20) {
9292
const ERC20Contract = new utils.Interface(ERC20.abi);
9393

94-
onSubmit({
95-
...decodedCall,
96-
callType: SupportedAction.ERC20_TRANSFER,
97-
to: values.token.address,
98-
value: BigNumber.from(0),
99-
function: ERC20Contract.getFunction('transfer'),
100-
args: {
101-
_value: values.amount,
102-
_to: values.recipientAddress,
94+
onSubmit([
95+
{
96+
...decodedCall,
97+
callType: SupportedAction.ERC20_TRANSFER,
98+
to: values.token.address,
99+
value: BigNumber.from(0),
100+
function: ERC20Contract.getFunction('transfer'),
101+
args: {
102+
_value: values.amount,
103+
_to: values.recipientAddress,
104+
},
103105
},
104-
});
106+
]);
105107
} else {
106-
onSubmit({
107-
...decodedCall,
108-
callType: SupportedAction.NATIVE_TRANSFER,
109-
to: values.recipientAddress,
110-
value: values.amount,
111-
function: null,
112-
args: null,
113-
});
108+
onSubmit([
109+
{
110+
...decodedCall,
111+
callType: SupportedAction.NATIVE_TRANSFER,
112+
to: values.recipientAddress,
113+
value: values.amount,
114+
function: null,
115+
args: null,
116+
},
117+
]);
114118
}
115119
};
116120

src/components/ActionsBuilder/SupportedActions/RepMint/RepMintEditor.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ export const Mint: React.FC<ActionEditorProps> = ({
6868
};
6969

7070
const submitAction = (values: RepMintFormValues) => {
71-
onSubmit({
72-
...decodedCall,
73-
args: {
74-
...decodedCall.args,
75-
to: values.recipient,
76-
amount: ethers.utils.parseUnits(repAmount.toString()),
71+
onSubmit([
72+
{
73+
...decodedCall,
74+
args: {
75+
...decodedCall.args,
76+
to: values.recipient,
77+
amount: ethers.utils.parseUnits(repAmount.toString()),
78+
},
7779
},
78-
});
80+
]);
7981
};
8082

8183
return (

src/components/ActionsBuilder/SupportedActions/SetGuildConfig/SetGuildConfigEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const SetGuildConfigEditor: FC<ActionEditorProps> = ({
197197
});
198198

199199
if (Object.keys(updatedValues).length > 0) {
200-
return onSubmit(call);
200+
return onSubmit([call]);
201201
}
202202
return setNoValueUpdatedError(true);
203203
};

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

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ParsedDataInterface, TABS } from './types';
99
import {
1010
ANY_FUNC_SIGNATURE,
1111
ERC20_TRANSFER_SIGNATURE,
12+
ERC20_APPROVE_SIGNATURE,
1213
preventEmptyString,
1314
} from 'utils';
1415
import { resolveUri } from 'utils/url';
@@ -49,6 +50,7 @@ interface FormValues {
4950
const Permissions: React.FC<ActionEditorProps> = ({
5051
decodedCall,
5152
onSubmit,
53+
isEdit,
5254
}) => {
5355
const parsedData = useMemo<ParsedDataInterface>(() => {
5456
if (!decodedCall) return null;
@@ -115,28 +117,76 @@ const Permissions: React.FC<ActionEditorProps> = ({
115117
updateFunctionSignature(value);
116118
};
117119

118-
const submitAction = (values: FormValues) => {
119-
const isAssetTransferCall = activeTab === TABS.ASSET_TRANSFER;
120+
const submitAssetTransfer = (values: FormValues) => {
121+
const baseCall = {
122+
...decodedCall,
123+
args: {
124+
...decodedCall.args,
125+
to: values.tokenAddress,
126+
valueAllowed: BigNumber.from(0),
127+
// "from" field set by default previously as guild id
128+
},
129+
optionalProps: {
130+
asset: values.tokenAddress,
131+
tab: TABS.ASSET_TRANSFER,
132+
},
133+
};
134+
const newAssetTransferCall: DecodedCall = {
135+
...baseCall,
136+
args: {
137+
...baseCall.args,
138+
functionSignature: ERC20_TRANSFER_SIGNATURE,
139+
},
140+
optionalProps: {
141+
...baseCall.optionalProps,
142+
functionName: '',
143+
},
144+
};
145+
146+
const newApprovalAssetCall = {
147+
...baseCall,
148+
args: {
149+
...baseCall.args,
150+
functionSignature: ERC20_APPROVE_SIGNATURE,
151+
},
152+
optionalProps: {
153+
...baseCall.optionalProps,
154+
functionName: 'approve(address,uint256)',
155+
},
156+
};
120157

158+
if (isEdit) {
159+
// in case of edit mode we submit only one action that is being edited
160+
return parsedData?.functionSignature === ERC20_APPROVE_SIGNATURE
161+
? onSubmit([newApprovalAssetCall])
162+
: onSubmit([newAssetTransferCall]);
163+
}
164+
return onSubmit([newAssetTransferCall, newApprovalAssetCall]);
165+
};
166+
167+
const submitFunctionCall = (values: FormValues) => {
121168
const newCall: DecodedCall = {
122169
...decodedCall,
123170
args: {
124171
...decodedCall.args,
125-
to: isAssetTransferCall ? values.tokenAddress : values.toAddress,
172+
to: values.toAddress,
126173
// native value allowed
127-
valueAllowed: isAssetTransferCall ? BigNumber.from(0) : values.amount,
128-
functionSignature: isAssetTransferCall
129-
? ERC20_TRANSFER_SIGNATURE
130-
: values.functionSignature,
174+
valueAllowed: values.amount,
175+
functionSignature: values.functionSignature,
131176
// "from" field set by default previously as guild id
132177
},
133178
optionalProps: {
134-
functionName: isAssetTransferCall ? '' : values.functionName,
135-
asset: isAssetTransferCall ? values.tokenAddress : '',
179+
functionName: values.functionName,
180+
asset: '',
136181
tab: activeTab,
137182
},
138183
};
139-
onSubmit(newCall);
184+
onSubmit([newCall]);
185+
};
186+
187+
const submitAction = (values: FormValues) => {
188+
const isAssetTransferCall = activeTab === TABS.ASSET_TRANSFER;
189+
(isAssetTransferCall ? submitAssetTransfer : submitFunctionCall)(values);
140190
};
141191

142192
const tabArray = [

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/UpdateENSContent/UpdateENSContentEditor.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,22 @@ const UpdateENSContentEditor: React.FC<ActionEditorProps> = ({
5656
const { nameHash } = convertToNameHash(fullEnsName);
5757
const { contentHash } = convertToContentHash(values.ipfsHash);
5858

59-
onSubmit({
60-
...decodedCall,
61-
to: resolver?.address,
62-
args: {
63-
...decodedCall.args,
64-
node: nameHash,
65-
hash: contentHash,
59+
onSubmit([
60+
{
61+
...decodedCall,
62+
to: resolver?.address,
63+
args: {
64+
...decodedCall.args,
65+
node: nameHash,
66+
hash: contentHash,
67+
},
68+
optionalProps: {
69+
...decodedCall.optionalProps,
70+
ensName: values.ensName,
71+
ipfsHash: values.ipfsHash,
72+
},
6673
},
67-
optionalProps: {
68-
...decodedCall.optionalProps,
69-
ensName: values.ensName,
70-
ipfsHash: values.ipfsHash,
71-
},
72-
});
74+
]);
7375
};
7476

7577
if (chain.id === LOCALHOST_ID)

src/components/ActionsBuilder/SupportedActions/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export interface ActionViewProps {
3535

3636
export interface ActionEditorProps extends ActionViewProps {
3737
updateCall?: (updatedCall: DecodedCall) => void;
38-
onSubmit: (decodedCall: DecodedCall) => void;
38+
onSubmit: (decodedCall: DecodedCall[]) => void;
39+
isEdit?: boolean;
3940
}
4041

4142
type SupportedActionViews = {

src/components/ActionsModal/ActionsModal.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('ActionsModal', () => {
4545
props = {
4646
isOpen: false,
4747
setIsOpen: jest.fn(),
48-
onAddAction: jest.fn(), // (action: DecodedAction) => void;
48+
onAddActions: jest.fn(), // (actions: DecodedAction[]) => void;
4949
};
5050
});
5151

0 commit comments

Comments
 (0)