Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: staking with incorrect refs – rework getRefferalAddress #600

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions features/stake/stake-form/use-stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
throw new MockLimitReachedError('Stake limit reached');
}

const referralAddress = referral
? await getRefferalAddress(referral, stake.core.rpcProvider)
: config.STAKE_FALLBACK_REFERRAL_ADDRESS;
const referralAddress = await getRefferalAddress(
referral,
stake.core.rpcProvider,
);

const onStakeTxConfirmed = async () => {
const [, balance] = await Promise.all([
Expand Down
37 changes: 27 additions & 10 deletions features/stake/stake-form/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { PublicClient, isAddress } from 'viem';
import { getEnsAddress } from 'viem/ens';
import { getEnsAddress, normalize } from 'viem/ens';
import { config } from 'config';

export const getRefferalAddress = async (
input: string,
input: string | null,
provider: PublicClient,
): Promise<string> => {
try {
if (isAddress(input)) return input;
const address = await getEnsAddress(provider, { name: input });
if (address) return address.toString();
return input;
const fallback = config.STAKE_FALLBACK_REFERRAL_ADDRESS;

if (input) {
// The address is Ethereum address
if (isAddress(input)) return input;

// Not all ENS names end with .eth, so we can't detect ENS names easily.
// The address is a http[s] link, return fallback instead
if (/^https?:\/\//.test(input)) return fallback;
// Filter out *.lido.fi referrals, e.g. ref from blog.lido.fi
// Assuming, that no one uses the 'lido.fi' ENS name
if (input.endsWith('lido.fi')) return fallback;

// Trying to get ENS address takes some time, so it is called after another checks
const ensAddress = await getEnsAddress(provider, {
name: normalize(input),
});
if (ensAddress) return ensAddress.toString();
}

// the provided 'input' is not an Ethereum address, nor a ENS address, returning the fallback
return fallback;
} catch {
// noop
// something went wrong during getting the address
throw new ReferralAddressError();
}

// if code gets here, ref is invalid and we need to throw error
throw new ReferralAddressError();
};

export class ReferralAddressError extends Error {
Expand Down
Loading