-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aztec-js): Return contract instance when awaiting deploy tx (#1360)
Overrides the `send` method of `DeployMethod` so it returns a `DeploySentTx`. This tx can be `wait`ed to return a receipt with the contract instance, and also provides a `deployed` method that returns just the contract. The wallet used for the instance can be set when creating the `DeployMethod` or passed as an argument to `wait`/`deployed`. Typed contracts return a typed version of DeployMethod that returns a typed contract instance.
- Loading branch information
1 parent
7ec6b32
commit e9c945c
Showing
26 changed files
with
167 additions
and
113 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
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
55 changes: 55 additions & 0 deletions
55
yarn-project/aztec.js/src/contract_deployer/deploy_sent_tx.ts
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,55 @@ | ||
import { FieldsOf } from '@aztec/circuits.js'; | ||
import { ContractAbi } from '@aztec/foundation/abi'; | ||
import { TxHash, TxReceipt } from '@aztec/types'; | ||
|
||
import { AztecAddress, AztecRPC, Contract, ContractBase, SentTx, WaitOpts, Wallet } from '../index.js'; | ||
|
||
/** Options related to waiting for a deployment tx. */ | ||
export type DeployedWaitOpts = WaitOpts & { | ||
/** Wallet to use for creating a contract instance. Uses the one set in the deployer constructor if not set. */ | ||
wallet?: Wallet; | ||
}; | ||
|
||
/** Extends a transaction receipt with a contract instance that represents the newly deployed contract. */ | ||
export type DeployTxReceipt<TContract extends ContractBase = Contract> = FieldsOf<TxReceipt> & { | ||
/** Instance of the newly deployed contract. */ | ||
contract: TContract; | ||
}; | ||
|
||
/** | ||
* A contract deployment transaction sent to the network, extending SentTx with methods to create a contract instance. | ||
*/ | ||
export class DeploySentTx<TContract extends ContractBase = Contract> extends SentTx { | ||
constructor(private abi: ContractAbi, wallet: AztecRPC | Wallet, txHashPromise: Promise<TxHash>) { | ||
super(wallet, txHashPromise); | ||
} | ||
|
||
/** | ||
* Awaits for the tx to be mined and returns the contract instance. Throws if tx is not mined. | ||
* @param opts - Options for configuring the waiting for the tx to be mined. | ||
* @returns The deployed contract instance. | ||
*/ | ||
public async deployed(opts?: DeployedWaitOpts): Promise<TContract> { | ||
const receipt = await this.wait(opts); | ||
return receipt.contract; | ||
} | ||
|
||
/** | ||
* Awaits for the tx to be mined and returns the receipt along with a contract instance. Throws if tx is not mined. | ||
* @param opts - Options for configuring the waiting for the tx to be mined. | ||
* @returns The transaction receipt with the deployed contract instance. | ||
*/ | ||
public async wait(opts?: DeployedWaitOpts): Promise<DeployTxReceipt<TContract>> { | ||
const receipt = await super.wait(opts); | ||
const contract = await this.getContractInstance(opts?.wallet, receipt.contractAddress); | ||
return { ...receipt, contract }; | ||
} | ||
|
||
protected getContractInstance(wallet?: Wallet, address?: AztecAddress): Promise<TContract> { | ||
const isWallet = (rpc: AztecRPC | Wallet): rpc is Wallet => !!(rpc as Wallet).createAuthenticatedTxRequest; | ||
const contractWallet = wallet ?? (isWallet(this.arc) && this.arc); | ||
if (!contractWallet) throw new Error(`A wallet is required for creating a contract instance`); | ||
if (!address) throw new Error(`Contract address is missing from transaction receipt`); | ||
return Contract.create(address, this.abi, contractWallet) as Promise<TContract>; | ||
} | ||
} |
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
Oops, something went wrong.