Skip to content

Commit b747b1a

Browse files
committed
refactor(app/bounty): move bounty on-chain operations into repository
1 parent 01c3c31 commit b747b1a

File tree

7 files changed

+36
-53
lines changed

7 files changed

+36
-53
lines changed

src/app/creator/build/[type]/AppliedModal.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import Loader from '@/components/Loader';
2929
import { Modal } from '@/components/Modal';
3030
import { Confirm } from '@/components/Modal/Confirm';
3131
import { NoData } from '@/components/NoData';
32-
import { writeBountyAbiActions } from '@/constants/abis/bounty';
3332
import { BOUNTY_SUPPORTED_CHAIN } from '@/constants/chain';
3433
import { contracts, payTokens } from '@/constants/contract';
3534
import { useAllowance, useApprove } from '@/hooks/useERC20';
3635
import { parseTokenUnits } from '@/utils/web3';
3736

37+
import { createTask } from '#/domain/bounty/repository';
3838
import { EXPERIENCE_OPTIONS } from '#/lib/user';
3939
import { denyBuilder, approveBuilder } from '#/services/creator';
4040
import { useCreatorBountyBuilders } from '#/services/creator/hooks';
@@ -142,7 +142,7 @@ export function AppliedModal({ open, closeModal, bounty, applyCallback }) {
142142

143143
const write = async () => {
144144
try {
145-
const { hash } = await writeBountyAbiActions.createTask(_contracts.bounty, [
145+
const { hash } = await createTask(_contracts.bounty, [
146146
bounty.task,
147147
currUser?.user_wallet,
148148
payToken.address,

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ import { useNetwork, useWalletClient } from 'wagmi';
2424
import { Button } from '@/components/Button';
2525
import { Modal } from '@/components/Modal';
2626
import { NoData } from '@/components/NoData';
27-
import { withdraw } from '@/constants/bounty';
2827
import { BOUNTY_SUPPORTED_CHAIN } from '@/constants/chain';
2928
import { contracts, payTokens } from '@/constants/contract';
3029
import { currentTime, fromNow } from '@/utils/date';
3130
import { formatTime } from '@/utils/date';
3231
import { parseTokenUnits, signBounty } from '@/utils/web3';
3332

34-
import { requestTermination } from '#/domain/bounty/repository';
33+
import { requestTermination, withdraw } from '#/domain/bounty/repository';
3534
import {
3635
getProgressList,
3736
finishConfirm,

src/shared/constants/abis/bounty.ts renamed to src/domain/bounty/helper/abi.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { abiContractFactory } from '../../utils/abiContractFactory';
18-
19-
export const BountyABI = [
17+
const abi = [
2018
{ inputs: [], stateMutability: 'nonpayable', type: 'constructor' },
2119
{ inputs: [], name: 'AlreadyExists', type: 'error' },
2220
{ inputs: [], name: 'AlreadyWithdrawn', type: 'error' },
@@ -205,6 +203,4 @@ export const BountyABI = [
205203
},
206204
] as const;
207205

208-
export const { readActions: readBountyAbiActions, writeActions: writeBountyAbiActions } = abiContractFactory(BountyABI);
209-
210-
export default BountyABI;
206+
export default abi;

src/domain/bounty/repository.js renamed to src/domain/bounty/repository.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { waitForTransaction } from '@wagmi/core';
18+
1719
import { PAGE_SIZE } from '@/constants/config';
20+
import { contracts, payTokens } from '@/constants/contract';
1821
import { isInteger, merge } from '@/utils';
22+
import { createContractActions } from '@/utils/contract';
1923
import httpClient from '@/utils/http';
24+
import { parseTokenUnits } from '@/utils/web3';
25+
26+
import bountyAbi from './helper/abi';
27+
28+
const { writeActions: { createTask, withdraw: withdrawFromAbi } } = createContractActions(bountyAbi);
2029

2130
function resolveSkipped(page, size = PAGE_SIZE) {
2231
let resolved = Number(page);
@@ -28,7 +37,7 @@ function resolveSkipped(page, size = PAGE_SIZE) {
2837
return (resolved - 1) * size;
2938
}
3039

31-
async function fetchList(params = {}) {
40+
async function fetchList(params = {} as any) {
3241
const { page = 1, sort, ...others } = params;
3342

3443
return httpClient.get('/build/general/bounties', {
@@ -59,13 +68,13 @@ async function fetchBuilderListForCreator(id, params) {
5968
return httpClient.get(`/build/creator/bounties/${id}/builders`, { params });
6069
}
6170

62-
async function fetchPublishedBountyList(params = {}) {
71+
async function fetchPublishedBountyList(params = {} as any) {
6372
const { userId, ...others } = params;
6473

6574
return fetchList({ ...others, team_uid: userId });
6675
}
6776

68-
async function fetchAppliedBountyList(params = {}) {
77+
async function fetchAppliedBountyList(params = {} as any) {
6978
const { userId, sort, ...others } = params;
7079

7180
return httpClient.get(`/build/dashboard/bounties/public/${userId}`, {
@@ -77,9 +86,25 @@ async function requestTermination(id, data) {
7786
return httpClient.post(`/build/creator/bounties/${id}/status/termination/propose`, data);
7887
}
7988

89+
async function withdraw(walletClient, chainId, taskId, amount, deadline, signature) {
90+
try {
91+
const { hash } = await withdrawFromAbi(contracts[chainId].bounty, [
92+
taskId,
93+
parseTokenUnits(amount.toString(), payTokens[chainId].usdt.decimals).toBigInt(),
94+
deadline,
95+
signature,
96+
]);
97+
const wait = await waitForTransaction({ hash });
98+
return { hash, wait };
99+
} catch {
100+
return { hash: 'error', wait: null };
101+
}
102+
}
103+
80104
export {
81105
fetchList, fetchOne, applyOne,
82106
fetchActivityList, fetchBuilderList, fetchBuilderListForCreator,
83107
fetchPublishedBountyList, fetchAppliedBountyList,
84108
requestTermination,
109+
createTask, withdraw,
85110
};

src/domain/bounty/widgets/process-list/AgreeFinishedModal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import { useNetwork, useWalletClient } from 'wagmi';
2020

2121
import { Button } from '@/components/Button';
2222
import { Modal } from '@/components/Modal';
23-
import { withdraw } from '@/constants/bounty';
2423

2524
import {
2625
finishConfirm,
2726
finishDeny,
2827
} from '#/services/bounties';
2928

3029
import { useBountyEnvCheck } from '../../hooks';
30+
import { withdraw } from '../../repository';
3131

3232
function AgreeFinishedModal({open, close, bounty, revalidatePathAction}) {
3333
const [loading, setLoading] = useState(false);

src/domain/bounty/widgets/process-list/AppliedModal.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { useAccount } from 'wagmi'; // useNetwork
2222
import { Modal } from '@/components/Modal';
2323
import { Confirm } from '@/components/Modal/Confirm';
2424
import { NoData } from '@/components/NoData';
25-
import { writeBountyAbiActions } from '@/constants/abis/bounty';
2625
import { BOUNTY_SUPPORTED_CHAIN } from '@/constants/chain';
2726
import { contracts, payTokens } from '@/constants/contract';
2827
import { useAllowance, useApprove } from '@/hooks/useERC20';
@@ -32,7 +31,7 @@ import { parseTokenUnits } from '@/utils/web3';
3231
import { approveBuilder } from '#/services/creator';
3332

3433
import { useBountyEnvCheck } from '../../hooks';
35-
import { fetchBuilderListForCreator } from '../../repository';
34+
import { fetchBuilderListForCreator, createTask } from '../../repository';
3635
import AppliedBuilderListView from '../../views/applied-builder-list';
3736

3837
function AppliedModal({ open, closeModal, bounty, revalidatePathAction }) {
@@ -77,7 +76,7 @@ function AppliedModal({ open, closeModal, bounty, revalidatePathAction }) {
7776

7877
const write = useCallback(async() => {
7978
try {
80-
const { hash } = await writeBountyAbiActions.createTask(_contracts.bounty, [
79+
const { hash } = await createTask(_contracts.bounty, [
8180
bounty.task,
8281
currUser?.user_wallet,
8382
payToken.address,

src/shared/constants/bounty.ts

-36
This file was deleted.

0 commit comments

Comments
 (0)