Skip to content

Commit 085c0e1

Browse files
committed
wip
1 parent fbd4009 commit 085c0e1

File tree

10 files changed

+79
-63
lines changed

10 files changed

+79
-63
lines changed

src/common/wallet/components.tsx

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect } from "react";
1+
import { useCallback, useEffect, useMemo } from "react";
22

33
import { useRouter } from "next/router";
44

@@ -7,7 +7,7 @@ import { IS_CLIENT } from "@/common/constants";
77

88
import { useWalletUserAdapter } from "./adapters";
99
import { useWalletUserMetadataStore } from "./model";
10-
import { isAccountId } from "../lib";
10+
import { isAccountId, useRouteQuery } from "../lib";
1111

1212
type WalletProviderProps = {
1313
children: React.ReactNode;
@@ -16,10 +16,23 @@ type WalletProviderProps = {
1616
const WalletProvider: React.FC<WalletProviderProps> = ({ children }) => {
1717
const router = useRouter();
1818

19+
const { query, setSearchParams } = useRouteQuery();
20+
21+
console.log(query);
22+
1923
const trackedQueryParams = router.query as {
24+
/**
25+
* Backward compatibility for deprecated parameter name
26+
*/
27+
referrerId?: string;
2028
referrerAccountId?: string;
2129
};
2230

31+
const referrerAccountIdUrlParameter = useMemo(
32+
() => trackedQueryParams.referrerAccountId || trackedQueryParams.referrerId,
33+
[trackedQueryParams.referrerAccountId, trackedQueryParams.referrerId],
34+
);
35+
2336
const { registerInit, setAccountState, setError, isReady, isSignedIn, accountId, error } =
2437
useWalletUserAdapter();
2538

@@ -73,20 +86,30 @@ const WalletProvider: React.FC<WalletProviderProps> = ({ children }) => {
7386
};
7487
}, [syncWalletState, isReady, handleChange]);
7588

89+
console.log(referrerAccountIdUrlParameter);
90+
7691
/**
7792
* Updating referrer account id, if detected
7893
*/
7994
useEffect(() => {
8095
if (
8196
isReady &&
8297
isSignedIn &&
83-
isAccountId(trackedQueryParams.referrerAccountId) &&
84-
trackedQueryParams.referrerAccountId !== referrerAccountId &&
85-
trackedQueryParams.referrerAccountId !== accountId
98+
isAccountId(referrerAccountIdUrlParameter) &&
99+
referrerAccountIdUrlParameter !== referrerAccountId &&
100+
referrerAccountIdUrlParameter !== accountId
86101
) {
87-
setReferrerAccountId(trackedQueryParams.referrerAccountId);
102+
setReferrerAccountId(referrerAccountIdUrlParameter);
88103
}
89-
}, [accountId, isReady, isSignedIn, referrerAccountId, setReferrerAccountId, trackedQueryParams]);
104+
}, [
105+
accountId,
106+
isReady,
107+
isSignedIn,
108+
referrerAccountId,
109+
referrerAccountIdUrlParameter,
110+
setReferrerAccountId,
111+
trackedQueryParams,
112+
]);
90113

91114
return children;
92115
};

src/common/wallet/model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type WalletUserDaoRepresentativeParams =
99
| { isDaoRepresentative: true; daoAccountId: AccountId };
1010

1111
export type WalletUserMetadata = {
12-
referrerAccountId?: string;
12+
referrerAccountId?: AccountId;
1313
};
1414

1515
export type WalletUserSession = WalletUserMetadata &
@@ -51,7 +51,7 @@ export type WalletUserSession = WalletUserMetadata &
5151
);
5252

5353
interface WalletUserMetadataState extends WalletUserMetadata {
54-
setReferrerAccountId: (recipientAccountId: string | undefined) => void;
54+
setReferrerAccountId: (accountId: AccountId | undefined) => void;
5555
reset: () => void;
5656
}
5757

src/entities/pot/hooks/forms.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { potContractClient } from "@/common/contracts/core/pot";
99
import { challengeResolveSchema, challengeSchema } from "../models/schemas";
1010
import { ChallengeInputs, ChallengeResolveInputs } from "../models/types";
1111

12-
export const useChallengeForm = ({ potDetail }: { potDetail: Pot; referrerId?: string }) => {
12+
export const useChallengeForm = ({ potDetail }: { potDetail: Pot }) => {
1313
const form = useForm<ChallengeInputs>({
1414
resolver: zodResolver(challengeSchema),
1515
});

src/features/donation/hooks/redirects.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export const useDonationSuccessWalletRedirect = () => {
2222

2323
const transactionHash =
2424
(Array.isArray(transactionHashes) ? transactionHashes.at(-1) : undefined) ??
25-
(typeof transactionHashes === "string" ? transactionHashes : undefined);
25+
(typeof transactionHashes === "string" && transactionHashes.length > 0
26+
? transactionHashes
27+
: undefined);
2628

2729
const isTransactionOutcomeDetected =
2830
transactionHash && Boolean(recipientAccountId ?? potAccountId);

src/features/matching-pool-contribution/components/MatchingPoolContributionModal.tsx

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRouter } from "next/router";
21
import { Form } from "react-hook-form";
32

43
import { Pot } from "@/common/api/indexer";
@@ -15,8 +14,8 @@ import {
1514
Spinner,
1615
Textarea,
1716
} from "@/common/ui/layout/components";
17+
import { useWalletUserSession } from "@/common/wallet";
1818
import { AccountProfileLink } from "@/entities/_shared/account";
19-
import { useGlobalStoreSelector } from "@/store";
2019

2120
import { useMatchingPoolContributionForm } from "../hooks/forms";
2221

@@ -31,25 +30,14 @@ export const MatchingPoolContributionModal: React.FC<MatchingPoolContributionMod
3130
onCloseClick,
3231
potDetail,
3332
}) => {
34-
const { actAsDao, accountId } = useGlobalStoreSelector((state) => state.nav);
33+
const viewer = useWalletUserSession();
3534

36-
const router = useRouter();
37-
const query = router.query as { referrerId?: string };
38-
const referrerId = query.referrerId;
39-
40-
// AccountID (Address)
41-
const asDao = actAsDao.toggle && !!actAsDao.defaultAddress;
42-
43-
// Form settings
4435
const { form, errors, onSubmit, inProgress } = useMatchingPoolContributionForm({
45-
accountId: asDao ? actAsDao.defaultAddress : accountId,
46-
asDao,
4736
potDetail,
48-
referrerId,
4937
});
5038

5139
const hasMinimumAmount = ["0", "1"].includes(potDetail.min_matching_pool_donation_amount);
52-
const yoctoMinimumAmout = yoctoNearToFloat(potDetail.min_matching_pool_donation_amount);
40+
const yoctoMinimumAmount = yoctoNearToFloat(potDetail.min_matching_pool_donation_amount);
5341

5442
// Get Protocol Config
5543
const protocolConfig = useProtocolConfig(potDetail);
@@ -70,7 +58,7 @@ export const MatchingPoolContributionModal: React.FC<MatchingPoolContributionMod
7058
? 0
7159
: (formValues.amountNEAR * potDetail.chef_fee_basis_points) / 10_000 || 0;
7260

73-
const referrerFeeAmountNear = referrerId
61+
const referrerFeeAmountNear = viewer.referrerAccountId
7462
? (formValues.amountNEAR * potDetail.referral_fee_matching_pool_basis_points) / 10_000 || 0
7563
: 0;
7664

@@ -89,7 +77,7 @@ export const MatchingPoolContributionModal: React.FC<MatchingPoolContributionMod
8977
{/*NEAR Input */}
9078
<p className="my-2 break-words text-[16px] font-normal leading-[20px] text-[#525252]">
9179
Enter matching pool contribution amount in NEAR{" "}
92-
{hasMinimumAmount ? "(no minimum)" : `(Min. ${yoctoMinimumAmout})`}
80+
{hasMinimumAmount ? "(no minimum)" : `(Min. ${yoctoMinimumAmount})`}
9381
</p>
9482

9583
{/* Amount NEAR input */}
@@ -183,7 +171,7 @@ export const MatchingPoolContributionModal: React.FC<MatchingPoolContributionMod
183171
)}
184172

185173
{/* Referrer Fee */}
186-
{referrerId && (
174+
{viewer.referrerAccountId && (
187175
<p className="mt-3 flex flex-row items-center break-words text-[14px] font-normal leading-[20px] text-[#292929]">
188176
Referrer fee: {referrerFeeAmountNear} NEAR
189177
</p>
@@ -203,11 +191,16 @@ export const MatchingPoolContributionModal: React.FC<MatchingPoolContributionMod
203191
{inProgress ? (
204192
<Spinner />
205193
) : (
206-
<>
207-
{asDao ? "Create proposal to contribute " : "Contribute "}
208-
{!asDao &&
209-
`${formValues.amountNEAR || 0} ${potDetail.base_currency?.toUpperCase()} to matching pool`}
210-
</>
194+
<span className="inline-flex gap-1">
195+
<span>
196+
{viewer.isDaoRepresentative ? "Create proposal to contribute" : "Contribute"}
197+
</span>
198+
199+
<span>
200+
{`${formValues.amountNEAR || 0} ${potDetail.base_currency?.toUpperCase()}` +
201+
" to matching pool"}
202+
</span>
203+
</span>
211204
)}
212205
</Button>
213206
</div>

src/features/matching-pool-contribution/hooks/forms.ts

+22-25
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@ import { Pot } from "@/common/api/indexer";
88
import { naxiosInstance } from "@/common/blockchains/near-protocol/client";
99
import { FIFTY_TGAS, FULL_TGAS, MIN_PROPOSAL_DEPOSIT_FALLBACK, ONE_TGAS } from "@/common/constants";
1010
import { getDaoPolicy } from "@/common/contracts/sputnik-dao";
11+
import { useWalletUserSession } from "@/common/wallet";
1112

1213
import { MatchingPoolContributionInputs, matchingPoolFundingSchema } from "../model/schemas";
1314

14-
export const useMatchingPoolContributionForm = ({
15-
accountId,
16-
potDetail,
17-
referrerId,
18-
asDao,
19-
}: {
20-
accountId: string;
21-
potDetail: Pot;
22-
referrerId?: string;
23-
asDao: boolean;
24-
}) => {
15+
export const useMatchingPoolContributionForm = ({ potDetail }: { potDetail: Pot }) => {
16+
const viewer = useWalletUserSession();
17+
2518
const form = useForm<MatchingPoolContributionInputs>({
2619
resolver: zodResolver(matchingPoolFundingSchema),
2720
mode: "all",
@@ -34,7 +27,7 @@ export const useMatchingPoolContributionForm = ({
3427
const args = {
3528
message: formData.data.message,
3629
matching_pool: true,
37-
referrer_id: referrerId,
30+
referrer_id: viewer.referrerAccountId,
3831
bypass_protocol_fee: formData.data.bypassProtocolFee,
3932
custom_chef_fee_basis_points: formData.data.bypassChefFee ? 0 : undefined,
4033
};
@@ -68,35 +61,39 @@ export const useMatchingPoolContributionForm = ({
6861
try {
6962
setInProgress(true);
7063

71-
if (asDao) {
72-
// If Dao, get dao policy
73-
const daoPolicy = await getDaoPolicy(accountId);
64+
if (viewer.isDaoRepresentative) {
65+
const daoPolicy = await getDaoPolicy(viewer.daoAccountId);
7466

7567
await naxiosInstance
76-
.contractApi({ contractId: accountId }) // INFO: In this case, the accountId has daoAddress value
68+
.contractApi({ contractId: viewer.daoAccountId })
7769
.call("add_proposal", {
7870
args: daoTransactionArgs,
7971
deposit: daoPolicy?.proposal_bond || MIN_PROPOSAL_DEPOSIT_FALLBACK,
8072
gas: FULL_TGAS,
8173
callbackUrl,
8274
});
8375
} else {
84-
await naxiosInstance
85-
.contractApi({ contractId: potDetail.account }) // INFO: In this case, the accountId is a regular pot account
86-
.call("donate", {
87-
args,
88-
deposit: parseNearAmount(formData.data.amountNEAR.toString()) || "0",
89-
gas: ONE_TGAS.mul(100).toString(),
90-
callbackUrl,
91-
});
76+
await naxiosInstance.contractApi({ contractId: potDetail.account }).call("donate", {
77+
args,
78+
deposit: parseNearAmount(formData.data.amountNEAR.toString()) || "0",
79+
gas: ONE_TGAS.mul(100).toString(),
80+
callbackUrl,
81+
});
9282
}
9383
} catch (e) {
9484
console.error(e);
9585
} finally {
9686
setInProgress(false);
9787
}
9888
},
99-
[accountId, asDao, potDetail, referrerId],
89+
90+
[
91+
potDetail.account,
92+
potDetail.name,
93+
viewer.daoAccountId,
94+
viewer.isDaoRepresentative,
95+
viewer.referrerAccountId,
96+
],
10097
);
10198

10299
return {

src/features/pot-application/hooks/forms.ts

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const usePotApplicationForm = ({
2525
}: {
2626
accountId: string;
2727
potDetail: Pot;
28-
referrerId?: string;
2928
asDao: boolean;
3029
}) => {
3130
const form = useForm<PotApplicationInputs>({

src/features/pot-configuration/hooks/middlewares.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export const usePotDeploymentSuccessMiddleware = () => {
1717

1818
const transactionHash =
1919
(Array.isArray(transactionHashes) ? transactionHashes.at(-1) : undefined) ??
20-
(typeof transactionHashes === "string" ? transactionHashes : undefined);
20+
(typeof transactionHashes === "string" && transactionHashes.length > 0
21+
? transactionHashes
22+
: undefined);
2123

2224
const isTransactionOutcomeDetected = transactionHash !== undefined;
2325

src/layout/pot/components/layout-hero.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const PotLayoutHero: React.FC<PotLayoutHeroProps> = ({
6868
);
6969

7070
const referrerPotLink = viewer.isSignedIn
71-
? window.location.origin + window.location.pathname + `?referrerId=${viewer.accountId}`
71+
? window.location.origin + window.location.pathname + `?referrerAccountId=${viewer.accountId}`
7272
: null;
7373

7474
const [description, linkedDocumentUrl] = useMemo(() => {

src/layout/profile/components/summary.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const Linktree: React.FC<ByAccountId> = ({ accountId }) => {
3636
<CopyToClipboard
3737
text={
3838
window.location.origin +
39-
`${rootPathnames.PROFILE}/${accountId}?referrerId=${viewer.accountId}`
39+
`${rootPathnames.PROFILE}/${accountId}?referrerAccountId=${viewer.accountId}`
4040
}
4141
onCopy={() => {
4242
setCopied(true);

0 commit comments

Comments
 (0)