Skip to content

Commit fb51ad5

Browse files
authored
feat: call blockchain ABIs as object's methods
Merge commits below into one: * feat: wip abiContractFactory * refactor: bountyAbiContract * chore: rename nftAbiContract * refactor: erc20ABI
1 parent 754de53 commit fb51ad5

File tree

15 files changed

+249
-130
lines changed

15 files changed

+249
-130
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"@tiptap-extend/columns": "2.1.6",
5454
"@vercel/speed-insights": "^1.0.12",
5555
"@wagmi/chains": "^0.2.18",
56+
"@wagmi/core": "1.4.13",
57+
"abitype": "0.9.8",
5658
"animate.css": "^4.1.1",
5759
"aos": "^2.3.4",
5860
"bignumber.js": "^9.1.1",

src/app/creator/build/[type]/AppliedModal.js renamed to src/app/creator/build/[type]/AppliedModal.tsx

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import { useConnectModal } from '@rainbow-me/rainbowkit';
18-
import { writeContract } from '@wagmi/core';
1918
// import { formatTime } from '@/utils/date'
2019
import clsx from 'clsx';
2120
import Image from 'next/image';
@@ -30,7 +29,7 @@ import Loader from '@/components/Loader';
3029
import { Modal } from '@/components/Modal';
3130
import { Confirm } from '@/components/Modal/Confirm';
3231
import { NoData } from '@/components/NoData';
33-
import { BountyABI } from '@/constants/abis/bounty';
32+
import { writeBountyAbiActions } from '@/constants/abis/bounty';
3433
import { BOUNTY_SUPPORTED_CHAIN } from '@/constants/chain';
3534
import { contracts, payTokens } from '@/constants/contract';
3635
import { useAllowance, useApprove } from '@/hooks/useERC20';
@@ -39,10 +38,7 @@ import { parseTokenUnits } from '@/utils/web3';
3938
import { EXPERIENCE_OPTIONS } from '#/lib/user';
4039
import { denyBuilder, approveBuilder } from '#/services/creator';
4140
import { useCreatorBountyBuilders } from '#/services/creator/hooks';
42-
import {
43-
useMediaUrl,
44-
useConfig,
45-
} from '#/state/application/hooks';
41+
import { useMediaUrl, useConfig } from '#/state/application/hooks';
4642

4743
import { CommentsModal } from './CommentsModal';
4844

@@ -66,18 +62,10 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
6662
bid: undefined,
6763
bountyId: undefined,
6864
});
69-
const [currUser, setCurrUser] = useState();
65+
const [currUser, setCurrUser] = useState<any>();
7066

71-
const { allowance } = useAllowance(
72-
payToken.address,
73-
_contracts.bounty,
74-
address
75-
);
76-
const { approveAsync } = useApprove(
77-
payToken.address,
78-
_contracts.bounty,
79-
address
80-
);
67+
const { allowance } = useAllowance(payToken.address, _contracts.bounty, address);
68+
const { approveAsync } = useApprove(payToken.address, _contracts.bounty, address);
8169

8270
const rolesOpts = useMemo(() => {
8371
return allOpts?.roles?.map(i => ({
@@ -106,10 +94,7 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
10694
skip: 0,
10795
take: 20,
10896
});
109-
const { loading, list, hasNextPage, doSetList } = useCreatorBountyBuilders(
110-
listParams,
111-
open
112-
);
97+
const { loading, list, hasNextPage, doSetList } = useCreatorBountyBuilders(listParams, open);
11398

11499
const [sentryRef] = useInfiniteScroll({
115100
loading,
@@ -123,13 +108,13 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
123108
}
124109

125110
const successCallback = useCallback(
126-
(res, bid, status) => {
111+
(res: any, bid: any, status: any) => {
127112
if (res.code === 200) {
128113
const _list = [...list];
129-
const curr = _list.find(f => f.id === bid);
114+
const curr: any = _list.find((f: any) => f.id === bid);
130115
if (curr) {
131116
curr.status = status;
132-
const fixedList = _list.map(m => {
117+
const fixedList = _list.map((m: any) => {
133118
return m.id === bid ? { ...curr } : m;
134119
});
135120
doSetList(fixedList);
@@ -143,7 +128,7 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
143128
toast.error(res.message);
144129
}
145130
},
146-
[doSetList, list, applyCallback]
131+
[doSetList, list, applyCallback],
147132
);
148133

149134
const decline = async (bountyId, bid) => {
@@ -157,28 +142,19 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
157142

158143
const write = async () => {
159144
try {
160-
const { hash } = await writeContract({
161-
address: _contracts.bounty,
162-
abi: BountyABI,
163-
functionName: 'createTask',
164-
args: [
165-
bounty.task,
166-
currUser?.user_wallet,
167-
payToken.address,
168-
parseTokenUnits((bounty.amount / 100).toString(), payToken.decimals),
169-
],
170-
});
145+
const { hash } = await writeBountyAbiActions.createTask(_contracts.bounty, [
146+
bounty.task,
147+
currUser?.user_wallet,
148+
payToken.address,
149+
parseTokenUnits((bounty.amount / 100).toString(), payToken.decimals).toBigInt(),
150+
]);
171151

172152
if (approveConfirmIds.bountyId && approveConfirmIds.bid) {
173-
const res = await approveBuilder(
174-
approveConfirmIds.bountyId,
175-
approveConfirmIds.bid,
176-
hash
177-
);
153+
const res = await approveBuilder(approveConfirmIds.bountyId, approveConfirmIds.bid, hash);
178154
successCallback(res, approveConfirmIds.bid, 6);
179155
}
180156
setApproveConfirmLoading(false);
181-
} catch (err) {
157+
} catch (err: any) {
182158
setApproveConfirmLoading(false);
183159
console.log(err);
184160
toast.error(err);
@@ -192,13 +168,13 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
192168

193169
const approveConfirm = async () => {
194170
if (!isConnected) {
195-
openConnectModal();
171+
openConnectModal?.();
196172
return;
197173
}
198174
setApproveConfirmLoading(true);
199175
if (
200-
Number(allowance.toString()) < Number(parseTokenUnits((bounty.amount / 100).toString(), payToken.decimals).toString())
201-
&&
176+
Number(allowance.toString()) <
177+
Number(parseTokenUnits((bounty.amount / 100).toString(), payToken.decimals).toString()) &&
202178
approveAsync
203179
) {
204180
await approveAsync();
@@ -209,12 +185,7 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
209185
};
210186

211187
return (
212-
<Modal
213-
isOpen={open}
214-
title={'Build applied'}
215-
closeModal={closeModal}
216-
big={true}
217-
>
188+
<Modal isOpen={open} title={'Build applied'} closeModal={closeModal} big={true}>
218189
<div className="w-full bg-white">
219190
<ul
220191
className={clsx('grid h-10 items-center font-bold text-xs', {
@@ -253,12 +224,7 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
253224
<p>{i.builder_user.user_nick_name}</p>
254225
</li>
255226
<li className="text-center">
256-
<p>
257-
{
258-
rolesOpts?.find(f => f.key === i.builder_user.user_roles)
259-
?.name
260-
}
261-
</p>
227+
<p>{rolesOpts?.find(f => f.key === i.builder_user.user_roles)?.name}</p>
262228

263229
<p>
264230
{i.builder_user.user_skills.map(
@@ -268,28 +234,20 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
268234
{skillOpts?.find(f => f.key === s)?.name}
269235
{k + 1 < i.builder_user.user_skills.length && ', '}{' '}
270236
</span>
271-
)
237+
),
272238
)}
273239
{/* */}
274240
</p>
275241
</li>
276242
<li className="text-center">
277-
{
278-
EXPERIENCE_OPTIONS.find(
279-
f => f.key === i.builder_user.user_experience
280-
)?.name
281-
}
243+
{EXPERIENCE_OPTIONS.find(f => f.key === i.builder_user.user_experience)?.name}
282244
</li>
283245
<li className="text-center">
284246
<div
285247
className="truncate hover:underline cursor-pointer"
286-
onClick={() =>
287-
window.open(mediaUrl + i.builder_user.user_resume)
288-
}
248+
onClick={() => window.open(mediaUrl + i.builder_user.user_resume)}
289249
>
290-
{i.builder_user.user_resume
291-
.split('resume/')[1]
292-
?.split('-')[1] || ''}
250+
{i.builder_user.user_resume.split('resume/')[1]?.split('-')[1] || ''}
293251
</div>
294252
</li>
295253
{/* <li className="text-center">
@@ -356,11 +314,7 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
356314

357315
{(loading || hasNextPage) && <div ref={sentryRef} />}
358316
</div>
359-
<CommentsModal
360-
comment={userComment}
361-
open={commentsModalOpen}
362-
closeModal={() => setCommentsModalOpen(false)}
363-
/>
317+
<CommentsModal comment={userComment} open={commentsModalOpen} closeModal={() => setCommentsModalOpen(false)} />
364318
<Confirm
365319
loading={approveConfirmLoading}
366320
confirmEvt={approveConfirm}

src/app/creator/build/[type]/ManageModal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import { useMediaUrl } from '#/state/application/hooks';
4444
import { AddProgressModal } from './AddProgressModal';
4545

4646
// import { writeContract, prepareWriteContract } from '@wagmi/core'
47-
// import { BountyABI } from '@/constants/abis/bounty'
4847

4948
export function ManageModal({
5049
open,

src/domain/bounty/widgets/process-list/AppliedModal.js renamed to src/domain/bounty/widgets/process-list/AppliedModal.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { writeContract } from '@wagmi/core';
1817
import clsx from 'clsx';
1918
import { useState, useCallback } from 'react';
2019
import { toast } from 'react-toastify';
@@ -23,14 +22,14 @@ import { useAccount } from 'wagmi'; // useNetwork
2322
import { Modal } from '@/components/Modal';
2423
import { Confirm } from '@/components/Modal/Confirm';
2524
import { NoData } from '@/components/NoData';
26-
import { BountyABI } from '@/constants/abis/bounty';
2725
import { BOUNTY_SUPPORTED_CHAIN } from '@/constants/chain';
2826
import { contracts, payTokens } from '@/constants/contract';
2927
import { useAllowance, useApprove } from '@/hooks/useERC20';
3028
import useUpToDate from '@/hooks/useUpToDate';
3129
import { parseTokenUnits } from '@/utils/web3';
3230

3331
import { approveBuilder } from '#/services/creator';
32+
import { writeBountyAbiActions } from '#/shared/constants/abis/bounty';
3433

3534
import { useBountyEnvCheck } from '../../hooks';
3635
import { fetchBuilderListForCreator } from '../../repository';
@@ -50,7 +49,7 @@ function AppliedModal({ open, closeModal, bounty, revalidatePathAction }) {
5049
bid: undefined,
5150
bountyId: undefined,
5251
});
53-
const [currUser, setCurrUser] = useState();
52+
const [currUser, setCurrUser] = useState<any>();
5453

5554
const { allowance } = useAllowance(
5655
payToken.address,
@@ -78,18 +77,12 @@ function AppliedModal({ open, closeModal, bounty, revalidatePathAction }) {
7877

7978
const write = useCallback(async() => {
8079
try {
81-
const { hash } = await writeContract({
82-
address: _contracts.bounty,
83-
abi: BountyABI,
84-
functionName: 'createTask',
85-
args: [
86-
bounty.task,
87-
currUser?.user_wallet,
88-
payToken.address,
89-
parseTokenUnits((bounty.amount / 100).toString(), payToken.decimals),
90-
],
91-
});
92-
// console.log(hash)
80+
const { hash } = await writeBountyAbiActions.createTask(_contracts.bounty, [
81+
bounty.task,
82+
currUser?.user_wallet,
83+
payToken.address,
84+
parseTokenUnits((bounty.amount / 100).toString(), payToken.decimals).toBigInt(),
85+
]);
9386

9487
if (approveConfirmIds.bountyId && approveConfirmIds.bid) {
9588
const res = await approveBuilder(
@@ -106,7 +99,7 @@ function AppliedModal({ open, closeModal, bounty, revalidatePathAction }) {
10699
}
107100
}
108101
setApproveConfirmLoading(false);
109-
} catch (err) {
102+
} catch (err: any) {
110103
console.log(err);
111104
setApproveConfirmLoading(false);
112105
toast.error(err.message);
@@ -126,7 +119,7 @@ function AppliedModal({ open, closeModal, bounty, revalidatePathAction }) {
126119
} else {
127120
write();
128121
}
129-
} catch (error) {
122+
} catch (error: any) {
130123
setApproveConfirmLoading(false);
131124
toast.error(error.message);
132125
}

src/domain/reputation/helper/nftAbi.js renamed to src/domain/reputation/helper/nftAbi.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { abiContractFactory } from '#/shared/utils/abiContractFactory';
18+
1719
const abi = [
1820
{
1921
inputs: [],
@@ -783,6 +785,8 @@ const abi = [
783785
stateMutability: 'view',
784786
type: 'function',
785787
},
786-
];
788+
] as const;
789+
790+
export const { readActions: readNftAbiActions, writeActions: writeNftAbiActions } = abiContractFactory(abi);
787791

788792
export default abi;

0 commit comments

Comments
 (0)