diff --git a/artifacts/ts/Add.ts b/artifacts/ts/Add.ts index ada3ed739..aef679917 100644 --- a/artifacts/ts/Add.ts +++ b/artifacts/ts/Add.ts @@ -161,10 +161,6 @@ class Factory extends ContractFactory { ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as AddTypes.Fields; - } - eventIndex = { Add: 0, Add1: 1, Empty: 2 }; at(address: string): AddInstance { diff --git a/artifacts/ts/DeprecatedNFTTest1.ts b/artifacts/ts/DeprecatedNFTTest1.ts index df48ad617..108dd1f54 100644 --- a/artifacts/ts/DeprecatedNFTTest1.ts +++ b/artifacts/ts/DeprecatedNFTTest1.ts @@ -98,10 +98,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest1Types.Fields; - } - at(address: string): DeprecatedNFTTest1Instance { return new DeprecatedNFTTest1Instance(address); } diff --git a/artifacts/ts/DeprecatedNFTTest2.ts b/artifacts/ts/DeprecatedNFTTest2.ts index 4e2c5b69c..ee5b02d43 100644 --- a/artifacts/ts/DeprecatedNFTTest2.ts +++ b/artifacts/ts/DeprecatedNFTTest2.ts @@ -106,10 +106,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest2Types.Fields; - } - at(address: string): DeprecatedNFTTest2Instance { return new DeprecatedNFTTest2Instance(address); } diff --git a/artifacts/ts/DeprecatedNFTTest3.ts b/artifacts/ts/DeprecatedNFTTest3.ts index b9a2d2b56..b190f5655 100644 --- a/artifacts/ts/DeprecatedNFTTest3.ts +++ b/artifacts/ts/DeprecatedNFTTest3.ts @@ -106,10 +106,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest3Types.Fields; - } - at(address: string): DeprecatedNFTTest3Instance { return new DeprecatedNFTTest3Instance(address); } diff --git a/artifacts/ts/DeprecatedNFTTest4.ts b/artifacts/ts/DeprecatedNFTTest4.ts index 194b1eec2..7a350173c 100644 --- a/artifacts/ts/DeprecatedNFTTest4.ts +++ b/artifacts/ts/DeprecatedNFTTest4.ts @@ -106,10 +106,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest4Types.Fields; - } - at(address: string): DeprecatedNFTTest4Instance { return new DeprecatedNFTTest4Instance(address); } diff --git a/artifacts/ts/DeprecatedNFTTest5.ts b/artifacts/ts/DeprecatedNFTTest5.ts index be092ae2e..f9a0e6588 100644 --- a/artifacts/ts/DeprecatedNFTTest5.ts +++ b/artifacts/ts/DeprecatedNFTTest5.ts @@ -106,10 +106,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest5Types.Fields; - } - at(address: string): DeprecatedNFTTest5Instance { return new DeprecatedNFTTest5Instance(address); } diff --git a/artifacts/ts/DeprecatedNFTTest6.ts b/artifacts/ts/DeprecatedNFTTest6.ts index 82bd832f2..7b789df95 100644 --- a/artifacts/ts/DeprecatedNFTTest6.ts +++ b/artifacts/ts/DeprecatedNFTTest6.ts @@ -106,10 +106,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest6Types.Fields; - } - at(address: string): DeprecatedNFTTest6Instance { return new DeprecatedNFTTest6Instance(address); } diff --git a/artifacts/ts/DeprecatedNFTTest7.ts b/artifacts/ts/DeprecatedNFTTest7.ts index 9a10e2604..873f22736 100644 --- a/artifacts/ts/DeprecatedNFTTest7.ts +++ b/artifacts/ts/DeprecatedNFTTest7.ts @@ -106,10 +106,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as DeprecatedNFTTest7Types.Fields; - } - at(address: string): DeprecatedNFTTest7Instance { return new DeprecatedNFTTest7Instance(address); } diff --git a/artifacts/ts/FakeTokenTest.ts b/artifacts/ts/FakeTokenTest.ts index 330b4146c..f000e13ca 100644 --- a/artifacts/ts/FakeTokenTest.ts +++ b/artifacts/ts/FakeTokenTest.ts @@ -129,10 +129,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as FakeTokenTestTypes.Fields; - } - at(address: string): FakeTokenTestInstance { return new FakeTokenTestInstance(address); } diff --git a/artifacts/ts/Greeter.ts b/artifacts/ts/Greeter.ts index 4ddac5ecf..09686824e 100644 --- a/artifacts/ts/Greeter.ts +++ b/artifacts/ts/Greeter.ts @@ -102,10 +102,6 @@ class Factory extends ContractFactory { ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as GreeterTypes.Fields; - } - at(address: string): GreeterInstance { return new GreeterInstance(address); } diff --git a/artifacts/ts/NFTCollectionTest.ts b/artifacts/ts/NFTCollectionTest.ts index 5adb75080..ff9360edb 100644 --- a/artifacts/ts/NFTCollectionTest.ts +++ b/artifacts/ts/NFTCollectionTest.ts @@ -134,10 +134,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as NFTCollectionTestTypes.Fields; - } - at(address: string): NFTCollectionTestInstance { return new NFTCollectionTestInstance(address); } diff --git a/artifacts/ts/NFTCollectionWithRoyaltyTest.ts b/artifacts/ts/NFTCollectionWithRoyaltyTest.ts index ad6da1d6e..9da7a8aa7 100644 --- a/artifacts/ts/NFTCollectionWithRoyaltyTest.ts +++ b/artifacts/ts/NFTCollectionWithRoyaltyTest.ts @@ -169,10 +169,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as NFTCollectionWithRoyaltyTestTypes.Fields; - } - at(address: string): NFTCollectionWithRoyaltyTestInstance { return new NFTCollectionWithRoyaltyTestInstance(address); } diff --git a/artifacts/ts/NFTTest.ts b/artifacts/ts/NFTTest.ts index 136ba54f1..d3528d851 100644 --- a/artifacts/ts/NFTTest.ts +++ b/artifacts/ts/NFTTest.ts @@ -104,10 +104,6 @@ class Factory extends ContractFactory { ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as NFTTestTypes.Fields; - } - at(address: string): NFTTestInstance { return new NFTTestInstance(address); } diff --git a/artifacts/ts/NFTTestStd.ts b/artifacts/ts/NFTTestStd.ts index f2e8ada97..f830a052e 100644 --- a/artifacts/ts/NFTTestStd.ts +++ b/artifacts/ts/NFTTestStd.ts @@ -115,10 +115,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as NFTTestStdTypes.Fields; - } - at(address: string): NFTTestStdInstance { return new NFTTestStdInstance(address); } diff --git a/artifacts/ts/OwnerOnly.ts b/artifacts/ts/OwnerOnly.ts index 8eee3e485..9565cd062 100644 --- a/artifacts/ts/OwnerOnly.ts +++ b/artifacts/ts/OwnerOnly.ts @@ -97,10 +97,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as OwnerOnlyTypes.Fields; - } - at(address: string): OwnerOnlyInstance { return new OwnerOnlyInstance(address); } diff --git a/artifacts/ts/Sub.ts b/artifacts/ts/Sub.ts index a74622c63..327e4ff87 100644 --- a/artifacts/ts/Sub.ts +++ b/artifacts/ts/Sub.ts @@ -96,10 +96,6 @@ class Factory extends ContractFactory { ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as SubTypes.Fields; - } - eventIndex = { Sub: 0 }; at(address: string): SubInstance { diff --git a/artifacts/ts/TokenTest.ts b/artifacts/ts/TokenTest.ts index de5207156..0df99fba9 100644 --- a/artifacts/ts/TokenTest.ts +++ b/artifacts/ts/TokenTest.ts @@ -124,10 +124,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as TokenTestTypes.Fields; - } - at(address: string): TokenTestInstance { return new TokenTestInstance(address); } diff --git a/artifacts/ts/TokenTestStd.ts b/artifacts/ts/TokenTestStd.ts index 001445215..679e148b8 100644 --- a/artifacts/ts/TokenTestStd.ts +++ b/artifacts/ts/TokenTestStd.ts @@ -132,10 +132,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as TokenTestStdTypes.Fields; - } - at(address: string): TokenTestStdInstance { return new TokenTestStdInstance(address); } diff --git a/artifacts/ts/UserAccount.ts b/artifacts/ts/UserAccount.ts index ab533f474..6e5853a73 100644 --- a/artifacts/ts/UserAccount.ts +++ b/artifacts/ts/UserAccount.ts @@ -118,10 +118,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as UserAccountTypes.Fields; - } - at(address: string): UserAccountInstance { return new UserAccountInstance(address); } diff --git a/artifacts/ts/Warnings.ts b/artifacts/ts/Warnings.ts index 87904815f..615aa8299 100644 --- a/artifacts/ts/Warnings.ts +++ b/artifacts/ts/Warnings.ts @@ -95,10 +95,6 @@ class Factory extends ContractFactory { ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as WarningsTypes.Fields; - } - consts = { C: BigInt("0") }; at(address: string): WarningsInstance { diff --git a/artifacts/ts/WrongNFTTest.ts b/artifacts/ts/WrongNFTTest.ts index 1cada8cb5..d7da1c2e0 100644 --- a/artifacts/ts/WrongNFTTest.ts +++ b/artifacts/ts/WrongNFTTest.ts @@ -107,10 +107,6 @@ class Factory extends ContractFactory< ); } - getInitialFieldsWithDefaultValues() { - return this.contract.getInitialFieldsWithDefaultValues() as WrongNFTTestTypes.Fields; - } - at(address: string): WrongNFTTestInstance { return new WrongNFTTestInstance(address); } diff --git a/packages/cli/src/codegen.ts b/packages/cli/src/codegen.ts index 86552d496..cb5153c43 100644 --- a/packages/cli/src/codegen.ts +++ b/packages/cli/src/codegen.ts @@ -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, 'fields'>` @@ -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))} diff --git a/packages/cli/src/deployment.ts b/packages/cli/src/deployment.ts index b3f723514..951d90756 100644 --- a/packages/cli/src/deployment.ts +++ b/packages/cli/src/deployment.ts @@ -359,7 +359,7 @@ function createDeployer( const deployedContracts: string[] = [] const executedScripts: string[] = [] - const deployContract = async ( + const deployContractInner = async ( contractFactory: ContractFactory, params: DeployContractParams

, taskTag?: string @@ -416,6 +416,24 @@ function createDeployer( return deployResult } + const deployContract = async ( + contractFactory: ContractFactory, + params: DeployContractParams

, + taskTag?: string + ): Promise> => { + return deployContractInner(contractFactory, params, taskTag) + } + + const deployContractTemplate = async ( + contractFactory: ContractFactory, + taskTag?: string + ): Promise> => { + const params: DeployContractParams

= { + initialFields: contractFactory.contract.getInitialFieldsWithDefaultValues() as P + } + return deployContractInner(contractFactory, params, taskTag) + } + const runScript = async

( executableScript: ExecutableScript

, params: ExecuteScriptParams

, @@ -490,6 +508,7 @@ function createDeployer( provider: web3.getCurrentNodeProvider(), account: account, deployContract: deployContract, + deployContractTemplate: deployContractTemplate, runScript: runScript, getDeployContractResult: getDeployContractResult, getRunScriptResult: getRunScriptResult, diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 1148d0044..b2fa8fc6e 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -135,6 +135,11 @@ export interface Deployer { taskTag?: string ): Promise> + deployContractTemplate( + constractFactory: ContractFactory, + taskTag?: string + ): Promise> + runScript

( executableScript: ExecutableScript

, params: ExecuteScriptParams

, diff --git a/packages/web3/src/contract/contract.ts b/packages/web3/src/contract/contract.ts index e94d9260b..1e2f70c93 100644 --- a/packages/web3/src/contract/contract.ts +++ b/packages/web3/src/contract/contract.ts @@ -1010,6 +1010,12 @@ export abstract class ContractFactory> { + 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 { const newAsset = { diff --git a/test/contract.test.ts b/test/contract.test.ts index e96d6341d..18c2a02c5 100644 --- a/test/contract.test.ts +++ b/test/contract.test.ts @@ -37,12 +37,14 @@ import { encodePrimitiveValues, addressVal, byteVecVal, - u256Val + u256Val, + ZERO_ADDRESS, + MINIMAL_CONTRACT_DEPOSIT } from '../packages/web3' import { Contract, Script, getContractIdFromUnsignedTx } from '../packages/web3' import { expectAssertionError, testAddress, randomContractAddress, getSigner, mintToken } from '../packages/web3-test' import { PrivateKeyWallet } from '@alephium/web3-wallet' -import { Greeter } from '../artifacts/ts/Greeter' +import { Greeter, GreeterTypes } from '../artifacts/ts/Greeter' import { GreeterMain, InsertIntoMap, @@ -189,7 +191,7 @@ describe('contract', function () { }) it('should test contract (2)', async () => { - const initialFields = Greeter.getInitialFieldsWithDefaultValues() + const initialFields = Greeter.contract.getInitialFieldsWithDefaultValues() as GreeterTypes.Fields const testResult = await Greeter.tests.greet({ initialFields: { ...initialFields, btcPrice: 1n } }) expect(testResult.returns).toEqual(1n) expect(testResult.contracts[0].codeHash).toEqual(Greeter.contract.codeHash) @@ -233,16 +235,6 @@ describe('contract', function () { expect(event.fields.stdInterfaceIdGuessed).toEqual(undefined) }) - it('should deploy contract with default initial values', async () => { - const initialFields = Greeter.getInitialFieldsWithDefaultValues() - const result = await Greeter.deploy(signer, { initialFields }) - const state = await result.contractInstance.fetchState() - expect(state.fields).toEqual(initialFields) - - const tokenInitialFields = TokenTest.getInitialFieldsWithDefaultValues() - expect(tokenInitialFields).toEqual({ symbol: '', name: '', decimals: 0n, totalSupply: 0n }) - }) - function loadJson(fileName: string) { const filePath = path.resolve(process.cwd() + path.sep + fileName) const rawData = fs.readFileSync(filePath).toString() @@ -438,14 +430,16 @@ describe('contract', function () { it('should test struct', async () => { const initialFields = { - ...UserAccount.getInitialFieldsWithDefaultValues(), + id: '', + address: ZERO_ADDRESS, balances: { totalAmount: 0n, tokens: [ { tokenId: '0011', amount: 0n }, { tokenId: '0022', amount: 0n } ] as [TokenBalance, TokenBalance] - } + }, + name: '' } const result = await UserAccount.deploy(signer, { initialFields }) const state = await result.contractInstance.fetchState() @@ -710,14 +704,16 @@ describe('contract', function () { expect(callResult0.returns.balance).toEqual(10n) const initialFields = { - ...UserAccount.getInitialFieldsWithDefaultValues(), + id: '', + address: ZERO_ADDRESS, balances: { totalAmount: 0n, tokens: [ { tokenId: '0011', amount: 0n }, { tokenId: '0022', amount: 0n } ] as [TokenBalance, TokenBalance] - } + }, + name: '' } const result1 = await UserAccount.deploy(signer, { initialFields }) const userAccount = result1.contractInstance @@ -736,4 +732,33 @@ describe('contract', function () { } ]) }) + + it('should deploy contract template', async () => { + const signer = await getSigner() + const template0 = await Assert.deployTemplate(signer) + const state0 = await template0.contractInstance.fetchState() + expect(state0['fields']).toEqual({}) + expect(BigInt(state0.asset.alphAmount)).toEqual(MINIMAL_CONTRACT_DEPOSIT) + + const template1 = await Add.deployTemplate(signer) + const state1 = await template1.contractInstance.fetchState() + expect(state1.fields).toEqual({ sub: '', result: 0n }) + expect(BigInt(state1.asset.alphAmount)).toEqual(MINIMAL_CONTRACT_DEPOSIT) + + const template2 = await UserAccount.deployTemplate(signer) + const state2 = await template2.contractInstance.fetchState() + expect(state2.fields).toEqual({ + id: '', + address: ZERO_ADDRESS, + balances: { + totalAmount: 0n, + tokens: [ + { tokenId: '', amount: 0n }, + { tokenId: '', amount: 0n } + ] + }, + name: '' + }) + expect(BigInt(state2.asset.alphAmount)).toEqual(MINIMAL_CONTRACT_DEPOSIT) + }) })