Skip to content

Commit

Permalink
Merge pull request #403 from WatchItDev/app/refactor/quick-trasnsfer-…
Browse files Browse the repository at this point in the history
…amount

feat: adapt new protocol contracts
  • Loading branch information
geolffreym authored Jan 10, 2025
2 parents aa3c752 + 08888eb commit 992ed1f
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 23,433 deletions.
2 changes: 2 additions & 0 deletions src/config-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const GLOBAL_CONSTANTS = {
process.env.VITE_POLICIES_AGG_ADDRESS || import.meta.env.VITE_POLICIES_AGG_ADDRESS || '',
LEDGER_VAULT_ADDRESS:
process.env.VITE_LEDGER_VAULT_ADDRESS || import.meta.env.VITE_LEDGER_VAULT_ADDRESS || '',
SUBSCRIPTION_CAMPAIGN_ADDRESS:
process.env.VITE_SUBSCRIPTION_CAMPAIGN_ADDRESS || import.meta.env.VITE_SUBSCRIPTION_CAMPAIGN_ADDRESS || '',
OKLINK_API_KEY: process.env.VITE_OKLINK_API_KEY || import.meta.env.VITE_OKLINK_API_KEY || '',
RIGHT_POLICY_AUTHORIZER:
process.env.VITE_RIGHT_POLICY_AUTHORIZER || import.meta.env.VITE_RIGHT_POLICY_AUTHORIZER || '',
Expand Down
1,916 changes: 1 addition & 1,915 deletions src/config/abi/AccessAgg.json

Large diffs are not rendered by default.

3,115 changes: 1 addition & 3,114 deletions src/config/abi/AccessWorkflow.json

Large diffs are not rendered by default.

5,327 changes: 1 addition & 5,326 deletions src/config/abi/LedgerVault.json

Large diffs are not rendered by default.

3,862 changes: 1 addition & 3,861 deletions src/config/abi/PoliciesAgg.json

Large diffs are not rendered by default.

4,246 changes: 1 addition & 4,245 deletions src/config/abi/RightsPolicyAuthorizer.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/config/abi/SubscriptionCampaign.json

Large diffs are not rendered by default.

4,927 changes: 1 addition & 4,926 deletions src/config/abi/SubscriptionPolicy.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/hooks/use-authorize-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export const useAuthorizePolicy = (): useAuthorizePolicyHook => {
setData(receipt);
setLoading(false);
} catch (err: any) {
console.log('AUTHORIZE: ',err);
console.log('err')
console.log(err)
setError(ERRORS.UNKNOWN_ERROR);
setLoading(false);
}
Expand Down
92 changes: 92 additions & 0 deletions src/hooks/use-get-active-licenses.ts
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,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface UseGetAuthorizedHolderPoliciesHook {
* Custom hook that fetches the authorized policies for a given holder.
* @param holder Address of the holder.
*/
export const useGetAuthorizedHolderPolicies = (
export const useGetPoliciesTerms = (
holder: Address | undefined
): UseGetAuthorizedHolderPoliciesHook => {
const [authorizedHolderPolicies, setAuthorizedHolderPolicies] = useState<Policy[]>([]);
Expand All @@ -56,15 +56,18 @@ export const useGetAuthorizedHolderPolicies = (
const policies: any = (await publicClient.readContract({
address: GLOBAL_CONSTANTS.POLICIES_AGG_ADDRESS,
abi: PoliciesAggAbi.abi,
functionName: 'getHolderWidePolicies',
functionName: 'getPoliciesTerms',
args: [holder],
})) as Policy[];

console.log('getPoliciesTerms')
console.log(policies)

// Store the response in state
setAuthorizedHolderPolicies(policies);
setError(null);
} catch (err: any) {
console.error('Error fetching holder-wide policies:', err);
console.error('Error fetching policies terms:', err);
setAuthorizedHolderPolicies([]);
setError({ message: err?.message || 'Error occurred while fetching authorized policies.' });
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { useState, useEffect, useCallback } from 'react';
import { Address } from 'viem';
import { publicClient } from '@src/clients/viem/publicClient';
import { GLOBAL_CONSTANTS } from '@src/config-global';
import SubscriptionPolicyAbi from '@src/config/abi/SubscriptionPolicy.json';
import { useSelector } from 'react-redux';
import { useGetActiveLicenses } from '@src/hooks/use-get-active-licenses';

interface AttestationError {
message: string;
code?: number;
[key: string]: any;
}

interface UseGetAttestationHook {
interface UseGetPolicyAttestationHook {
attestation?: string;
loading: boolean;
fetching: boolean;
Expand All @@ -20,61 +18,71 @@ interface UseGetAttestationHook {
}

/**
* Custom hook to get the attestation of a subscription.
* Custom hook to get the attestation for a specific policy.
* @param policy The address of the policy to check.
* @param recipient The address of the recipient of subscription.
* @param holder The address of the holder of subscription.
* @returns An object containing the attestation data, loading state, error, and a refetch function.
*/
export function useGetAttestation(recipient: Address, holder?: Address): UseGetAttestationHook {
export function useGetPolicyAttestation(policy: Address, recipient: Address, holder?: Address): UseGetPolicyAttestationHook {
const sessionData = useSelector((state: any) => state.auth.session);
const userAddress = sessionData?.profile?.ownedBy?.address as Address | undefined;

const [attestation, setAttestation] = useState<string | undefined>(undefined);
const [loading, setLoading] = useState(true);
const [fetching, setFetching] = useState(true);
const [error, setError] = useState<AttestationError | null>(null);
const { activeLicenses, loading: licensesLoading, refetch: refetchLicenses } = useGetActiveLicenses(recipient, holder);

const fetchAttestation = useCallback(async () => {
const fetchAttestation = useCallback(() => {
setFetching(true);

if (!policy || licensesLoading) {
setFetching(false);
setLoading(false);
return;
}

try {
const attestationData: unknown = await publicClient.readContract({
address: GLOBAL_CONSTANTS.SUBSCRIPTION_POLICY_ADDRESS,
abi: SubscriptionPolicyAbi.abi,
functionName: 'getAttestation',
args: [recipient, holder ?? (userAddress as Address)],
});
const matchedLicense = activeLicenses.find(
(license) => license.policy.toLowerCase() === policy.toLowerCase()
);

const attestationStr = attestationData ? String(attestationData) : undefined;
setAttestation(attestationStr);
setError(null);
if (matchedLicense) {
setAttestation(matchedLicense.license);
setError(null);
} else {
setAttestation(undefined);
setError({ message: 'No matching license found for the specified policy.' });
}
} catch (err: any) {
console.error('Error fetching attestation:', err);
console.error('Error fetching license:', err);
setError({ message: err.message || 'An error occurred' });
setAttestation(undefined);
} finally {
setLoading(false);
setFetching(false);
}
}, [recipient, holder, userAddress]);
}, [policy, activeLicenses, licensesLoading]);

useEffect(() => {
if (!recipient || !(holder ?? userAddress)) {
if (!policy || !recipient || !(holder ?? userAddress)) {
setLoading(false);
setFetching(false);
setError({ message: 'Recipient or holder address is missing.' });
setError({ message: 'Policy, recipient, or holder address is missing.' });
return;
}

fetchAttestation();
}, [recipient, holder, userAddress, fetchAttestation]);
}, [policy, recipient, holder, userAddress, fetchAttestation]);

const refetch = useCallback(() => {
setFetching(true);
refetchLicenses();
fetchAttestation();
}, [fetchAttestation]);
}, [refetchLicenses, fetchAttestation]);

return {
attestation,
loading,
loading: licensesLoading || loading,
fetching,
error,
refetch,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use-get-policy-terms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect, useCallback } from 'react';
import { Address } from 'viem';
import { useGetAuthorizedHolderPolicies } from './use-get-authorized-holder-policies';
import { useGetPoliciesTerms } from './use-get-policies-terms.ts';

interface HasAccessError {
message: string;
Expand Down Expand Up @@ -42,7 +42,7 @@ export const useGetPolicyTerms = (
loading: loadingPolicies,
error: errorPolicies,
refetch: refetchPolicies,
} = useGetAuthorizedHolderPolicies(holderAddress);
} = useGetPoliciesTerms(holderAddress);

/**
* Attempt to find `policyAddress` in the array of `authorizedHolderPolicies`
Expand Down
8 changes: 3 additions & 5 deletions src/hooks/use-has-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ export const useHasAccess = (ownerAddress?: Address): UseHasAccessHook => {

setFetching(true);
try {
const accessData: unknown = await publicClient.readContract({
const accessData: any = await publicClient.readContract({
address: GLOBAL_CONSTANTS.ACCESS_AGG_ADDRESS,
abi: AccessAggAbi.abi,
functionName: 'isAccessAllowedByHolder',
functionName: 'isAccessAllowed',
args: [userAddress, ownerAddress],
});

console.log('isAccessAllowedByHolder', accessData);

const access = Boolean(accessData);
const access = Boolean(accessData?.[0]);
setHasAccess(access);
setError(null);
} catch (err: any) {
Expand Down
83 changes: 83 additions & 0 deletions src/hooks/use-is-active-campaign.ts
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,
};
};
4 changes: 2 additions & 2 deletions src/hooks/use-is-policy-authorized.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect, useCallback } from 'react';
import { Address } from 'viem';
import { useSelector } from 'react-redux';
import { useGetAuthorizedHolderPolicies } from './use-get-authorized-holder-policies.ts';
import { useGetPoliciesTerms } from './use-get-policies-terms.ts';

interface HasAccessError {
message: string;
Expand Down Expand Up @@ -40,7 +40,7 @@ export const useIsPolicyAuthorized = (
loading: loadingPolicies,
error: errorPolicies,
refetch: refetchPolicies,
} = useGetAuthorizedHolderPolicies(holder ?? userAddress);
} = useGetPoliciesTerms(holder ?? userAddress);

/**
* Checks if the given policy exists in the holder's authorized policies.
Expand Down
Loading

0 comments on commit 992ed1f

Please sign in to comment.