Skip to content

Commit

Permalink
feat: use the new registry and contract reference model
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Sep 25, 2022
1 parent 2b4cdeb commit 733b4aa
Show file tree
Hide file tree
Showing 179 changed files with 815 additions and 17,947 deletions.
39 changes: 9 additions & 30 deletions packages/metatx/src/meta-contract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Provider } from '@ethersproject/providers';
import { Environment } from '@flair-sdk/common';
import {
ContractFqn,
ContractVersion,
LATEST_VERSION,
loadContract,
} from '@flair-sdk/registry';
import { ContractFqn } from '@flair-sdk/registry';
import {
BaseContract,
Contract,
Expand Down Expand Up @@ -46,26 +41,11 @@ export class MetaContract<
constructor(
metaTransactionsClient: MetaTransactionsClient,
chainId: number,
contractFqn: ContractFqn,
contractVersion: ContractVersion = LATEST_VERSION,
addressOrName?: string,
addressOrName: string,
abi: any,
signerOrProvider?: Signer | Provider,
) {
const contractDefinition = loadContract(contractFqn, contractVersion);
const contractAddressOrName =
addressOrName || contractDefinition?.address?.[String(chainId)];

if (!contractAddressOrName) {
throw new Error(
`Could not determine contract address from constructor (${addressOrName}) nor from definition (${contractDefinition?.address})`,
);
}

super(
contractAddressOrName,
contractDefinition?.artifact.abi || [],
signerOrProvider,
);
super(addressOrName, abi, signerOrProvider);

this.metaTransaction = {};
Object.keys(this.interface.functions).forEach((signature) => {
Expand Down Expand Up @@ -95,23 +75,22 @@ export class MetaContract<
env?: Environment;
chainId: number;
flairClientId: string;
contractFqn: ContractFqn;
contractVersion?: ContractVersion;
addressOrName?: string;
forwarder: string;
addressOrName: string;
abi: any;
signerOrProvider?: Signer | Provider;
}): T {
const metaTxClient = new MetaTransactionsClient({
env: config.env || Environment.PROD,
chainId: config.chainId,
flairClientId: config.flairClientId,
forwarder: config.forwarder,
});

return new MetaContract(
metaTxClient,
config.chainId,
config.contractFqn,
config.contractVersion,
config.addressOrName,
config.abi,
config.signerOrProvider,
) as unknown as T;
}
Expand Down
46 changes: 7 additions & 39 deletions packages/metatx/src/meta-transactions.client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { Environment } from '@flair-sdk/common';
import {
ContractVersion,
LATEST_VERSION,
loadContract,
} from '@flair-sdk/registry';
import axios from 'axios';
import { Signer } from 'ethers';
import { Required } from 'utility-types';
Expand All @@ -14,49 +9,22 @@ import { generateRandomUint256 } from './random-uint256';
import { MetaTransactionSignedData } from './types';
import { MetaTransaction } from './types/meta-transaction';

type Config =
| {
env?: Environment;
chainId: number;
contractVersion?: ContractVersion;
forwarder?: string;
flairClientId: string;
defaults?: Partial<MetaTransaction>;
}
| {
env?: Environment;
chainId?: number;
contractVersion?: ContractVersion;
forwarder: string;
flairClientId: string;
defaults?: Partial<MetaTransaction>;
};
type Config = {
env?: Environment;
forwarder: string;
flairClientId: string;
defaults?: Partial<MetaTransaction>;
};

export class MetaTransactionsClient {
public forwarder: string;

constructor(private readonly config: Config) {
this.forwarder = config.forwarder as string;

if (!config.forwarder && config.chainId) {
try {
const forwarderDefinition = loadContract(
'common/UnorderedForwarder',
LATEST_VERSION,
);
this.forwarder = forwarderDefinition?.address?.[
String(config.chainId)
] as string;
} catch (e) {
console.warn(
`Could not load contract (chain ${config.chainId}) to determine forwarder: ${e}`,
);
}
}

if (!this.forwarder) {
throw new Error(
`Could not determine meta transactions forwarder address, please either specify chainId (given ${config.chainId}) or forwarder address (given ${config.forwarder})`,
`Could not determine meta transactions forwarder address, please specify forwarder address (given ${config.forwarder})`,
);
}
}
Expand Down
23 changes: 4 additions & 19 deletions packages/metatx/src/meta-transactions.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { Environment } from '@flair-sdk/common';
import {
ContractFqn,
ContractVersion,
FlairContract,
} from '@flair-sdk/registry';
import {
Contract,
Contract as EthersContract,
Expand Down Expand Up @@ -80,35 +75,25 @@ export const applyMetaTransactions = <T extends EthersContract>(
return castedContract;
};

export const createFlairContractWithMetaTransactions = <
export const augmentContractWithMetaTransactions = <
T extends EthersContract,
>(config: {
env?: Environment;
chainId: number;
flairClientId: string;
contractFqn: ContractFqn;
contractVersion?: ContractVersion;
contract: T;
forwarder: string;
addressOrName?: string;
signer?: Signer;
forwarder?: string;
}): MetaTransactionsAugmentedContract<T> => {
const metaTxClient = new MetaTransactionsClient({
env: config.env || Environment.PROD,
chainId: config.chainId,
flairClientId: config.flairClientId,
forwarder: config.forwarder,
});

const contract = new FlairContract(
config.chainId,
config.contractFqn,
config.signer,
config.contractVersion,
config.addressOrName,
);

const augmentedContract = applyMetaTransactions<T>(
contract as unknown as T, // TODO fix this to get proper typing!
config.contract,
config.chainId,
metaTxClient,
);
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/common/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export * from './useAddressOfSigner';
export * from './useChainId';
export * from './useChainInfo';
export * from './useContractAbi';
export * from './useContractDefinition';
export * from './useContractManifest';
export * from './useContractRead';
export * from './useContractWriteAndWait';
export * from './useFeatureRead';
Expand All @@ -14,7 +14,6 @@ export * from './useFunctionFeature';
export * from './useHasAnyOfFeatures';
export * from './useNormalizedFunctionCall';
export * from './useSmartContract';
export * from './useSupportsInterface';
export * from './useWaitForTransaction';

// web2
Expand Down
27 changes: 13 additions & 14 deletions packages/react/src/common/hooks/useContractAbi.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import {
ContractFqn,
ContractVersion,
loadContract,
ContractReference,
findContractByReference,
} from '@flair-sdk/registry';
import { useMemo } from 'react';

export type Config = {
contractVersion?: ContractVersion;
contractFqn: ContractFqn;
contractReference: ContractReference;
};

export const useContractAbi = ({
contractVersion = 'v1',
contractFqn,
}: Config) => {
const contract = useMemo(
() => loadContract(contractFqn, contractVersion),
[contractFqn, contractVersion],
);
export const useContractAbi = ({ contractReference }: Config) => {
const contract = useMemo(() => {
try {
return findContractByReference(contractReference);
} catch (error) {
console.warn(`Failed to find contract manifest for ${contractReference}`);
return undefined;
}
}, [contractReference]);

return contract?.artifact.abi || [];
return contract?.artifact?.abi;
};
35 changes: 0 additions & 35 deletions packages/react/src/common/hooks/useContractDefinition.ts

This file was deleted.

30 changes: 30 additions & 0 deletions packages/react/src/common/hooks/useContractManifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
ContractManifest,
ContractReference,
findContractByReference,
} from '@flair-sdk/registry';
import { useEffect, useState } from 'react';

type Config = {
contractReference?: ContractReference;
};

export const useContractManifest = ({ contractReference }: Config) => {
const [manifest, setManifest] = useState<ContractManifest>();

useEffect(() => {
if (!contractReference) {
setManifest(undefined);
return;
}

try {
const manifest = findContractByReference(contractReference);
setManifest(manifest);
} catch (error) {
setManifest(undefined);
}
}, [contractReference]);

return manifest;
};
Loading

0 comments on commit 733b4aa

Please sign in to comment.