Skip to content

Commit

Permalink
Merge pull request #402 from alephium/deploy-contract-template
Browse files Browse the repository at this point in the history
Add method for deploying contract template
  • Loading branch information
Lbqds authored Jul 29, 2024
2 parents 1126c53 + 6d19405 commit a878059
Show file tree
Hide file tree
Showing 26 changed files with 73 additions and 115 deletions.
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 @@ -359,7 +359,7 @@ function createDeployer<Settings = unknown>(
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 @@ function createDeployer<Settings = unknown>(
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 @@ function createDeployer<Settings = unknown>(
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

0 comments on commit a878059

Please sign in to comment.