Skip to content

Commit

Permalink
fix(contract): add missed sourceCodePath cases
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 5, 2023
1 parent c3a5983 commit 5be4cd9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
35 changes: 28 additions & 7 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,19 @@ class Contract<M extends ContractMethodsBase> {
*/
async $compile(): Promise<Encoded.ContractBytearray> {
if (this.$options.bytecode != null) return this.$options.bytecode;
if (this.$options.sourceCode == null) throw new IllegalArgumentError('Can\'t compile without source code');
if (this.$options.onCompiler == null) throw new IllegalArgumentError('Can\'t compile without compiler');
this.$options.bytecode = (await this.$options.onCompiler
.compileBySourceCode(this.$options.sourceCode, this.$options.fileSystem)
).bytecode;
if (this.$options.sourceCode != null) {
const { bytecode } = await this.$options.onCompiler

Check warning on line 137 in src/contract/Contract.ts

View check run for this annotation

Codecov / codecov/patch

src/contract/Contract.ts#L137

Added line #L137 was not covered by tests
.compileBySourceCode(this.$options.sourceCode, this.$options.fileSystem);
this.$options.bytecode = bytecode;

Check warning on line 139 in src/contract/Contract.ts

View check run for this annotation

Codecov / codecov/patch

src/contract/Contract.ts#L139

Added line #L139 was not covered by tests
}
if (this.$options.sourceCodePath != null) {
const { bytecode } = await this.$options.onCompiler.compile(this.$options.sourceCodePath);
this.$options.bytecode = bytecode;
}
if (this.$options.bytecode == null) {
throw new IllegalArgumentError('Can\'t compile without sourceCode and sourceCodePath');

Check warning on line 146 in src/contract/Contract.ts

View check run for this annotation

Codecov / codecov/patch

src/contract/Contract.ts#L146

Added line #L146 was not covered by tests
}
return this.$options.bytecode;
}

Expand Down Expand Up @@ -478,7 +486,7 @@ class Contract<M extends ContractMethodsBase> {
) as Encoded.ContractAddress;
}

if (address == null && sourceCode == null && bytecode == null) {
if (address == null && sourceCode == null && sourceCodePath == null && bytecode == null) {
throw new MissingContractAddressError('Can\'t create instance by ACI without address');
}

Expand All @@ -495,12 +503,25 @@ class Contract<M extends ContractMethodsBase> {
else if (sourceCode != null) {
if (onCompiler == null) throw new IllegalArgumentError('Can\'t validate bytecode without compiler');
isValid = await onCompiler.validateBySourceCode(onChanBytecode, sourceCode, fileSystem);
} else if (sourceCodePath != null) {
if (onCompiler == null) throw new IllegalArgumentError('Can\'t validate bytecode without compiler');
isValid = await onCompiler.validate(onChanBytecode, sourceCodePath);

Check warning on line 508 in src/contract/Contract.ts

View check run for this annotation

Codecov / codecov/patch

src/contract/Contract.ts#L508

Added line #L508 was not covered by tests
}
if (!isValid) {
throw new BytecodeMismatchError((sourceCode ?? sourceCodePath) != null ? 'source code' : 'bytecode');
}
if (!isValid) throw new BytecodeMismatchError(sourceCode != null ? 'source code' : 'bytecode');
}

return new ContractWithMethods<M>({
onCompiler, onNode, sourceCode, bytecode, aci, address, fileSystem, ...otherOptions,
onCompiler,
onNode,
sourceCode,
sourceCodePath,
bytecode,
aci,
address,
fileSystem,
...otherOptions,
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/contract/compiler/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export type Aci = Array<{
name: string;
event?: any;
functions: FunctionAci[];
kind: string; // known values: 'contract_main'
payable: boolean;
typedefs: any[];
};
}>;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ export class MissingContractAddressError extends ContractError {
*/
export class MissingContractDefError extends ContractError {
constructor() {
super('Either ACI or source code is required');
super('Either ACI or sourceCode or sourceCodePath is required');
this.name = 'MissingContractDefError';
}
}
Expand Down
27 changes: 26 additions & 1 deletion test/integration/contract-aci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ describe('Contract instance', () => {
expect(testContract.$options.bytecode).to.satisfy((b: string) => b.startsWith('cb_'));
});

it('compiles contract by sourceCodePath', async () => {
const ctr = await aeSdk.initializeContract({
aci: [{
contract: {
functions: [{
arguments: [{ name: 'x', type: 'int' }],
name: 'increment',
payable: false,
returns: 'int',
stateful: false,
},
],
kind: 'contract_main',
name: 'Increment',
payable: false,
typedefs: [],
},
}],
sourceCodePath: './test/integration/contracts/Increment.aes',
});
expect(ctr.$options.bytecode).to.equal(undefined);
expect(await ctr.$compile()).to.satisfy((b: string) => b.startsWith('cb_'));
});

it('fails on calling without deployment', () => expect(testContract.intFn(2))
.to.be.rejectedWith(MissingContractAddressError, 'Can\'t dry-run contract without address'));

Expand Down Expand Up @@ -275,7 +299,8 @@ describe('Contract instance', () => {

it('generates by bytecode and aci', async () => aeSdk.initializeContract({ bytecode: testContractBytecode, aci: testContractAci }));

it('fails on generation without arguments', () => expect(aeSdk.initializeContract()).to.be.rejectedWith(MissingContractDefError, 'Either ACI or source code is required'));
it('fails on generation without arguments', () => expect(aeSdk.initializeContract())
.to.be.rejectedWith(MissingContractDefError, 'Either ACI or sourceCode or sourceCodePath is required'));

it('calls by aci', async () => {
const contract = await aeSdk.initializeContract<TestContractApi>(
Expand Down

0 comments on commit 5be4cd9

Please sign in to comment.