or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

contract-deployment.mdcontract-factories.mdcontract-instances.mdindex.mdprovider-access.mdsigner-management.md

contract-factories.mddocs/

0

# Contract Factories

1

2

Create ethers ContractFactory instances from project artifacts, ABI/bytecode, or contract names with library linking support.

3

4

## Capabilities

5

6

### Get Contract Factory by Name

7

8

Creates a ContractFactory from a contract name in your Hardhat project.

9

10

```typescript { .api }

11

/**

12

* Create a ContractFactory from contract name

13

* @param name - Contract name or fully qualified name

14

* @param signerOrOptions - Signer or factory options

15

* @returns Promise resolving to ContractFactory instance

16

*/

17

function getContractFactory<A extends any[] = any[], I = ethers.Contract>(

18

name: string,

19

signerOrOptions?: ethers.Signer | FactoryOptions

20

): Promise<ethers.ContractFactory<A, I>>;

21

22

interface FactoryOptions {

23

signer?: ethers.Signer;

24

libraries?: Libraries;

25

}

26

27

interface Libraries {

28

[libraryName: string]: string | ethers.Addressable;

29

}

30

```

31

32

**Usage Examples:**

33

34

```typescript

35

import { network } from "hardhat";

36

37

const { ethers } = await network.connect();

38

39

// Basic factory creation

40

const Counter = await ethers.getContractFactory("Counter");

41

const counter = await Counter.deploy();

42

43

// With specific signer

44

const [, deployer] = await ethers.getSigners();

45

const Counter = await ethers.getContractFactory("Counter", deployer);

46

47

// With library linking

48

const Counter = await ethers.getContractFactory("Counter", {

49

libraries: {

50

SafeMath: "0x1234...abcd",

51

},

52

});

53

54

// Fully qualified name for disambiguation

55

const Counter = await ethers.getContractFactory("contracts/v1/Counter.sol:Counter");

56

```

57

58

### Get Contract Factory by ABI and Bytecode

59

60

Creates a ContractFactory from ABI and bytecode directly.

61

62

```typescript { .api }

63

/**

64

* Create a ContractFactory from ABI and bytecode

65

* @param abi - Contract ABI array or Abi type

66

* @param bytecode - Contract deployment bytecode

67

* @param signer - Signer to use for deployment

68

* @returns Promise resolving to ContractFactory instance

69

*/

70

function getContractFactory<A extends any[] = any[], I = ethers.Contract>(

71

abi: any[] | Abi,

72

bytecode: ethers.BytesLike,

73

signer?: ethers.Signer

74

): Promise<ethers.ContractFactory<A, I>>;

75

76

type Abi = ReadonlyArray<Fragment>;

77

78

interface Fragment {

79

type: string;

80

name?: string;

81

inputs?: any[];

82

outputs?: any[];

83

stateMutability?: string;

84

}

85

86

interface LinkReferences {

87

[sourceName: string]: {

88

[libraryName: string]: Array<{

89

start: number;

90

length: number;

91

}>;

92

};

93

}

94

```

95

96

**Usage Examples:**

97

98

```typescript

99

// With imported ABI and bytecode

100

import counterAbi from "./artifacts/Counter.sol/Counter.json";

101

102

const Counter = await ethers.getContractFactory(

103

counterAbi.abi,

104

counterAbi.bytecode

105

);

106

107

// With specific signer

108

const [, deployer] = await ethers.getSigners();

109

const Counter = await ethers.getContractFactory(

110

counterAbi.abi,

111

counterAbi.bytecode,

112

deployer

113

);

114

```

115

116

### Get Contract Factory from Artifact

117

118

Creates a ContractFactory from a Hardhat artifact object.

119

120

```typescript { .api }

121

/**

122

* Create a ContractFactory from Hardhat artifact

123

* @param artifact - Hardhat artifact containing ABI, bytecode, and metadata

124

* @param signerOrOptions - Signer or factory options

125

* @returns Promise resolving to ContractFactory instance

126

*/

127

function getContractFactoryFromArtifact<A extends any[] = any[], I = ethers.Contract>(

128

artifact: Artifact<Abi>,

129

signerOrOptions?: ethers.Signer | FactoryOptions

130

): Promise<ethers.ContractFactory<A, I>>;

131

132

interface Artifact<T = any> {

133

contractName: string;

134

sourceName: string;

135

abi: T;

136

bytecode: string;

137

deployedBytecode: string;

138

linkReferences: LinkReferences;

139

deployedLinkReferences: LinkReferences;

140

}

141

```

142

143

**Usage Examples:**

144

145

```typescript

146

import { artifacts } from "hardhat";

147

148

// Load artifact and create factory

149

const counterArtifact = await artifacts.readArtifact("Counter");

150

const Counter = await ethers.getContractFactoryFromArtifact(counterArtifact);

151

152

// With library linking

153

const Calculator = await ethers.getContractFactoryFromArtifact(calculatorArtifact, {

154

libraries: {

155

MathLib: mathLibAddress,

156

},

157

});

158

159

// With specific signer

160

const [, deployer] = await ethers.getSigners();

161

const Counter = await ethers.getContractFactoryFromArtifact(counterArtifact, deployer);

162

```

163

164

### Factory Usage Patterns

165

166

Once you have a ContractFactory, you can use standard ethers.js patterns:

167

168

```typescript

169

// Deploy contract

170

const counter = await Counter.deploy(constructorArg1, constructorArg2);

171

172

// Deploy with transaction options

173

const counter = await Counter.deploy(arg1, arg2, {

174

gasPrice: ethers.parseUnits("20", "gwei"),

175

value: ethers.parseEther("1.0"),

176

});

177

178

// Connect to existing contract

179

const existingCounter = Counter.attach("0x1234...abcd");

180

181

// Get deployment transaction

182

const deployTx = Counter.getDeployTransaction(arg1, arg2);

183

```

184

185

### Library Linking with Factories

186

187

For contracts that require library linking, provide library addresses in FactoryOptions:

188

189

```typescript

190

// Deploy libraries first

191

const mathLib = await ethers.deployContract("MathLib");

192

const stringLib = await ethers.deployContract("StringLib");

193

194

// Create factory with linked libraries

195

const Calculator = await ethers.getContractFactory("Calculator", {

196

libraries: {

197

MathLib: mathLib.address,

198

StringLib: stringLib.address,

199

// Can also specify fully qualified names

200

"contracts/libraries/AdvancedMath.sol:AdvancedMath": "0x5678...",

201

},

202

});

203

204

// Deploy with linked libraries

205

const calculator = await Calculator.deploy();

206

```