-
-
Notifications
You must be signed in to change notification settings - Fork 252
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
feat(unlock-app): Support ENS in checkout builder for Referrer field #14834
base: master
Are you sure you want to change the base?
Changes from 3 commits
46395bc
344f189
8e2b3ca
4781dbd
ee0bb6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { PaywallConfigType } from '@unlock-protocol/core' | ||
|
||
import { getReferrers } from '~/utils/checkoutLockUtils' | ||
|
||
export const useGetReferrers = ( | ||
recipients: string[], | ||
paywallConfig: PaywallConfigType, | ||
lockAddress?: string | ||
) => { | ||
return useQuery({ | ||
queryKey: ['getReferrers', paywallConfig, lockAddress, recipients], | ||
queryFn: async () => { | ||
return getReferrers(recipients, paywallConfig, lockAddress) | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,14 @@ | |
// type so that it at least includes as optional all possible | ||
// properties on a lock. These are all compatible with RawLock insofar | ||
|
||
import { PaywallConfigType } from '@unlock-protocol/core' | ||
import { isEns } from '@unlock-protocol/ui' | ||
|
||
import { Lock } from '~/unlockTypes' | ||
import { isAccount } from '../utils/checkoutValidators' | ||
import { locksmith } from '~/config/locksmith' | ||
import { getCurrencySymbol } from './currency' | ||
import { PaywallConfigType } from '@unlock-protocol/core' | ||
import { onResolveName } from './resolvers' | ||
|
||
// as they only extend it with properties that may be undefined. | ||
interface LockKeysAvailableLock { | ||
|
@@ -145,29 +148,61 @@ export const inClaimDisallowList = (address: string) => { | |
return claimDisallowList.indexOf(address) > -1 | ||
} | ||
|
||
/** | ||
* Helper function that returns a valid referrer address | ||
* @param recipient | ||
* @param paywallConfig | ||
* @param lockAddress | ||
* @returns | ||
*/ | ||
export const getReferrer = ( | ||
recipient: string, | ||
export const shouldReferrerResolveForENS = ( | ||
paywallConfig?: PaywallConfigType, | ||
lockAddress?: string | ||
): string => { | ||
) => { | ||
if (paywallConfig) { | ||
if ( | ||
lockAddress && | ||
paywallConfig.locks[lockAddress] && | ||
isAccount(paywallConfig.locks[lockAddress].referrer) | ||
) { | ||
return paywallConfig.locks[lockAddress].referrer! | ||
return false | ||
} | ||
if (paywallConfig.referrer && isAccount(paywallConfig.referrer)) { | ||
return paywallConfig.referrer | ||
if (paywallConfig.referrer && isEns(paywallConfig.referrer)) { | ||
return true | ||
} | ||
} | ||
return recipient | ||
return false | ||
} | ||
|
||
export const getReferrers = async ( | ||
recipients: string[], | ||
paywallConfig?: PaywallConfigType, | ||
lockAddress?: string | ||
): Promise<string[]> => { | ||
const isReferrerAddressEns = shouldReferrerResolveForENS( | ||
paywallConfig, | ||
lockAddress | ||
) | ||
if (isReferrerAddressEns) { | ||
if (isReferrerAddressEns && paywallConfig && paywallConfig.referrer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason to change this to be here is that if the referrer is an ENS it will be the same for all. There is no reason to call this for all recipients since that will unnecessarily make quite a few ENS calls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This codepath will be executed for any existing ENS addresses on config before this change goes live. since it will be formatted to address from here on out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure the referrer should ever be an ENS. You are resolving the ENS in the settings UI and the config only receives the actual address, which, IMO is good! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The issue states to handle this scenario, hence I added it. Let me know if my assumption here is wrong. If we don't have to resolve an ENS from config all we need is small change of using |
||
try { | ||
// paywallConfig.referrer is always a string if isReferrerAddressEns is true | ||
const response = await onResolveName(paywallConfig.referrer) | ||
if (response && response.address && response.address !== null) { | ||
return recipients.map(() => response.address as string) | ||
} | ||
} catch (e) { | ||
console.log('Error resolving referrer ENS', e) | ||
} | ||
} | ||
} | ||
|
||
return recipients.map((recipient) => { | ||
if (paywallConfig) { | ||
if ( | ||
lockAddress && | ||
paywallConfig.locks[lockAddress] && | ||
isAccount(paywallConfig.locks[lockAddress].referrer) | ||
) { | ||
return paywallConfig.locks[lockAddress].referrer! | ||
} | ||
if (paywallConfig.referrer && isAccount(paywallConfig.referrer)) { | ||
return paywallConfig.referrer | ||
} | ||
} | ||
return recipient | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we need a hook for this and we could not call
getReferrers
when we need ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(you actually do that in several places and that's correct!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since
getReferrers
is not sync and it does make rpc calls, I am using hook. react query caches it based on the inputs to avoid making extra calls on re-renders causing issues.