Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for deploying contract template #402

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions artifacts/ts/Add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ class Factory extends ContractFactory<AddInstance, AddTypes.Fields> {
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as AddTypes.Fields;
}

eventIndex = { Add: 0, Add1: 1, Empty: 2 };

at(address: string): AddInstance {
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest1Types.Fields;
}

at(address: string): DeprecatedNFTTest1Instance {
return new DeprecatedNFTTest1Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest2Types.Fields;
}

at(address: string): DeprecatedNFTTest2Instance {
return new DeprecatedNFTTest2Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest3Types.Fields;
}

at(address: string): DeprecatedNFTTest3Instance {
return new DeprecatedNFTTest3Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest4Types.Fields;
}

at(address: string): DeprecatedNFTTest4Instance {
return new DeprecatedNFTTest4Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest5Types.Fields;
}

at(address: string): DeprecatedNFTTest5Instance {
return new DeprecatedNFTTest5Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest6Types.Fields;
}

at(address: string): DeprecatedNFTTest6Instance {
return new DeprecatedNFTTest6Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/DeprecatedNFTTest7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest7Types.Fields;
}

at(address: string): DeprecatedNFTTest7Instance {
return new DeprecatedNFTTest7Instance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/FakeTokenTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as FakeTokenTestTypes.Fields;
}

at(address: string): FakeTokenTestInstance {
return new FakeTokenTestInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/Greeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ class Factory extends ContractFactory<GreeterInstance, GreeterTypes.Fields> {
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as GreeterTypes.Fields;
}

at(address: string): GreeterInstance {
return new GreeterInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/NFTCollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as NFTCollectionTestTypes.Fields;
}

at(address: string): NFTCollectionTestInstance {
return new NFTCollectionTestInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/NFTCollectionWithRoyaltyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as NFTCollectionWithRoyaltyTestTypes.Fields;
}

at(address: string): NFTCollectionWithRoyaltyTestInstance {
return new NFTCollectionWithRoyaltyTestInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/NFTTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ class Factory extends ContractFactory<NFTTestInstance, NFTTestTypes.Fields> {
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as NFTTestTypes.Fields;
}

at(address: string): NFTTestInstance {
return new NFTTestInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/NFTTestStd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as NFTTestStdTypes.Fields;
}

at(address: string): NFTTestStdInstance {
return new NFTTestStdInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/OwnerOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as OwnerOnlyTypes.Fields;
}

at(address: string): OwnerOnlyInstance {
return new OwnerOnlyInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/Sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ class Factory extends ContractFactory<SubInstance, SubTypes.Fields> {
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as SubTypes.Fields;
}

eventIndex = { Sub: 0 };

at(address: string): SubInstance {
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/TokenTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as TokenTestTypes.Fields;
}

at(address: string): TokenTestInstance {
return new TokenTestInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/TokenTestStd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as TokenTestStdTypes.Fields;
}

at(address: string): TokenTestStdInstance {
return new TokenTestStdInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/UserAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as UserAccountTypes.Fields;
}

at(address: string): UserAccountInstance {
return new UserAccountInstance(address);
}
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/Warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ class Factory extends ContractFactory<WarningsInstance, WarningsTypes.Fields> {
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as WarningsTypes.Fields;
}

consts = { C: BigInt("0") };

at(address: string): WarningsInstance {
Expand Down
4 changes: 0 additions & 4 deletions artifacts/ts/WrongNFTTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as WrongNFTTestTypes.Fields;
}

at(address: string): WrongNFTTestInstance {
return new WrongNFTTestInstance(address);
}
Expand Down
13 changes: 0 additions & 13 deletions packages/cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,6 @@ function genEncodeFieldsFunc(contract: Contract): string {
`
}

function genGetInitialFieldsWithDefaultValues(contract: Contract): string {
const fieldsSig = getContractFields(contract)
if (fieldsSig.names.length === 0) {
return ''
}
return `
getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as ${contract.name}Types.Fields
}
`
}

function genContractStateType(contract: Contract): string {
if (contract.fieldsSig.names.length === 0) {
return `export type State = Omit<ContractState<any>, 'fields'>`
Expand Down Expand Up @@ -526,7 +514,6 @@ function genContract(contract: Contract, artifactRelativePath: string): string {

class Factory extends ContractFactory<${contract.name}Instance, ${contractFieldType(contract.name, fieldsSig)}> {
${genEncodeFieldsFunc(contract)}
${genGetInitialFieldsWithDefaultValues(contract)}
${genEventIndex(contract)}
${genLocalConsts(contract)}
${genAttach(getInstanceName(contract))}
Expand Down
21 changes: 20 additions & 1 deletion packages/cli/src/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
}

getInstance<I extends ContractInstance>(
contract: ContractFactory<I, any>,

Check warning on line 176 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
group?: number,
taskId?: string
): I | undefined {
Expand Down Expand Up @@ -224,7 +224,7 @@
return this.contracts.size === 0 && this.scripts.size === 0 && this.migrations.size === 0
}

marshal(): any {

Check warning on line 227 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
return {
deployerAddress: this.deployerAddress,
contracts: Object.fromEntries(this.contracts),
Expand All @@ -233,7 +233,7 @@
}
}

static unmarshal(json: any): DeploymentsPerAddress {

Check warning on line 236 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
const deployerAddress = json.deployerAddress as string
const contracts = new Map(Object.entries<DeployContractExecutionResult>(json.contracts))
const scripts = new Map(Object.entries<RunScriptResult>(json.scripts))
Expand Down Expand Up @@ -294,7 +294,7 @@
return true
}
// previous !== undefined if retry is false
return previous!.issueTokenAmount !== issueTokenAmount

Check warning on line 297 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Forbidden non-null assertion
}

async function needToRunScript(
Expand Down Expand Up @@ -359,7 +359,7 @@
const deployedContracts: string[] = []
const executedScripts: string[] = []

const deployContract = async <T extends ContractInstance, P extends Fields>(
const deployContractInner = async <T extends ContractInstance, P extends Fields>(
contractFactory: ContractFactory<T, P>,
params: DeployContractParams<P>,
taskTag?: string
Expand Down Expand Up @@ -416,6 +416,24 @@
return deployResult
}

const deployContract = async <T extends ContractInstance, P extends Fields>(
contractFactory: ContractFactory<T, P>,
params: DeployContractParams<P>,
taskTag?: string
): Promise<DeployContractResult<T>> => {
return deployContractInner(contractFactory, params, taskTag)
}

const deployContractTemplate = async <T extends ContractInstance, P extends Fields>(
contractFactory: ContractFactory<T, P>,
taskTag?: string
): Promise<DeployContractResult<T>> => {
const params: DeployContractParams<P> = {
initialFields: contractFactory.contract.getInitialFieldsWithDefaultValues() as P
}
return deployContractInner(contractFactory, params, taskTag)
}

const runScript = async <P extends Fields>(
executableScript: ExecutableScript<P>,
params: ExecuteScriptParams<P>,
Expand Down Expand Up @@ -490,6 +508,7 @@
provider: web3.getCurrentNodeProvider(),
account: account,
deployContract: deployContract,
deployContractTemplate: deployContractTemplate,
runScript: runScript,
getDeployContractResult: getDeployContractResult,
getRunScriptResult: getRunScriptResult,
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ export interface Deployer {
taskTag?: string
): Promise<DeployContractResult<T>>

deployContractTemplate<T extends ContractInstance, P extends Fields>(
constractFactory: ContractFactory<T, P>,
taskTag?: string
): Promise<DeployContractResult<T>>

runScript<P extends Fields>(
executableScript: ExecutableScript<P>,
params: ExecuteScriptParams<P>,
Expand Down
6 changes: 6 additions & 0 deletions packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,12 @@ export abstract class ContractFactory<I extends ContractInstance, F extends Fiel
}
}

async deployTemplate(signer: SignerProvider): Promise<DeployContractResult<I>> {
return this.deploy(signer, {
initialFields: this.contract.getInitialFieldsWithDefaultValues() as F
})
}

// This is used for testing contract functions
stateForTest(initFields: F, asset?: Asset, address?: string): ContractState<F> {
const newAsset = {
Expand Down
Loading
Loading