-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from WatchItDev/app/refactor/quick-trasnsfer-…
…amount feat: adapt new protocol contracts
- Loading branch information
Showing
19 changed files
with
236 additions
and
23,433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { Address } from 'viem'; | ||
import { publicClient } from '@src/clients/viem/publicClient'; | ||
import AccessAggAbi from '@src/config/abi/AccessAgg.json'; | ||
import { GLOBAL_CONSTANTS } from '@src/config-global.ts'; | ||
|
||
interface ActiveLicensesError { | ||
message: string; | ||
code?: number; | ||
[key: string]: any; | ||
} | ||
|
||
interface PolicyTerms { | ||
amount: string; | ||
currency: string; | ||
rateBasis: number; | ||
uri: string; | ||
} | ||
|
||
interface Policy { | ||
policy: string; | ||
terms: PolicyTerms; | ||
} | ||
|
||
interface UseGetActiveLicensesHook { | ||
activeLicenses: any[]; | ||
loading: boolean; | ||
error?: ActiveLicensesError | null; | ||
refetch: () => void; | ||
} | ||
|
||
export const useGetActiveLicenses = ( | ||
recipient: Address, holder?: Address | ||
): UseGetActiveLicensesHook => { | ||
const [activeLicenses, setActiveLicenses] = useState<Policy[]>([]); | ||
const [loading, setLoading] = useState<boolean>(true); | ||
const [error, setError] = useState<ActiveLicensesError | null>(null); | ||
|
||
const fetchHolderPolicies = useCallback(async () => { | ||
// Validate that holder exists | ||
if (!holder) { | ||
setActiveLicenses([]); | ||
setLoading(false); | ||
setError({ message: 'Holder address is missing.' }); | ||
return; | ||
} | ||
|
||
setLoading(true); | ||
|
||
try { | ||
console.log('getActiveLicenses') | ||
console.log(GLOBAL_CONSTANTS.ACCESS_AGG_ADDRESS) | ||
// Call the contract method | ||
const licenses: any = (await publicClient.readContract({ | ||
address: GLOBAL_CONSTANTS.ACCESS_AGG_ADDRESS, | ||
abi: AccessAggAbi.abi, | ||
functionName: 'getActiveLicenses', | ||
args: [recipient, holder], | ||
})) as Policy[]; | ||
|
||
console.log('getActiveLicenses') | ||
console.log(licenses) | ||
console.log(GLOBAL_CONSTANTS.ACCESS_AGG_ADDRESS) | ||
|
||
// Store the response in state | ||
setActiveLicenses(licenses); | ||
setError(null); | ||
} catch (err: any) { | ||
console.error('Error fetching active licenses:', err); | ||
setActiveLicenses([]); | ||
setError({ message: err?.message || 'Error occurred while fetching active licenses.' }); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [holder]); | ||
|
||
useEffect(() => { | ||
fetchHolderPolicies(); | ||
}, [fetchHolderPolicies]); | ||
|
||
// Allows to refetch the data on demand | ||
const refetch = useCallback(() => { | ||
fetchHolderPolicies(); | ||
}, [fetchHolderPolicies]); | ||
|
||
return { | ||
activeLicenses, | ||
loading, | ||
error, | ||
refetch, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { Address } from 'viem'; | ||
import { publicClient } from '@src/clients/viem/publicClient'; | ||
import SubscriptionCampaignAbi from '@src/config/abi/SubscriptionCampaign.json'; | ||
import { GLOBAL_CONSTANTS } from '@src/config-global.ts'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
interface IsActiveCampaignError { | ||
message: string; | ||
code?: number; | ||
[key: string]: any; | ||
} | ||
|
||
interface UseIsActiveCampaignHook { | ||
isActive?: boolean; | ||
loading: boolean; | ||
error?: IsActiveCampaignError | null; | ||
refetch: () => void; | ||
} | ||
|
||
export const useIsActiveCampaign = (ownerAddress?: Address): UseIsActiveCampaignHook => { | ||
const sessionData = useSelector((state: any) => state.auth.session); | ||
const userAddress = sessionData?.profile?.ownedBy?.address as Address | undefined; | ||
|
||
const [isActive, setIsActive] = useState<boolean | undefined>(undefined); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<IsActiveCampaignError | null>(null); | ||
|
||
const fetchIsActive = useCallback(async () => { | ||
if (!userAddress || !ownerAddress) { | ||
setLoading(false); | ||
setError({ message: 'User address or owner address is missing.' }); | ||
return; | ||
} | ||
|
||
setLoading(true); | ||
try { | ||
const activeData: any = await publicClient.readContract({ | ||
address: GLOBAL_CONSTANTS.SUBSCRIPTION_CAMPAIGN_ADDRESS, | ||
abi: SubscriptionCampaignAbi.abi, | ||
functionName: 'isActiveCampaign', | ||
args: [ownerAddress, GLOBAL_CONSTANTS.ACCESS_WORKFLOW_ADDRESS], | ||
}); | ||
|
||
console.log('is active campaign') | ||
console.log(activeData) | ||
|
||
const access = Boolean(activeData?.[0]); | ||
setIsActive(access); | ||
setError(null); | ||
} catch (err: any) { | ||
console.error('Error checking access:', err); | ||
setIsActive(undefined); | ||
setError({ message: err?.message || 'An error occurred' }); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [userAddress, ownerAddress]); | ||
|
||
useEffect(() => { | ||
fetchIsActive(); | ||
}, [fetchIsActive]); | ||
|
||
const refetch = useCallback(() => { | ||
fetchIsActive(); | ||
}, [fetchIsActive]); | ||
|
||
if (!userAddress) { | ||
return { | ||
isActive: false, | ||
loading: false, | ||
error: null, | ||
refetch: () => {}, | ||
}; | ||
} | ||
|
||
return { | ||
isActive, | ||
loading, | ||
error, | ||
refetch, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.