Skip to content

Commit

Permalink
feat(contract): includes support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Oct 20, 2022
1 parent 032040d commit 818f375
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 13 deletions.
83 changes: 83 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"@aeternity/aepp-sdk": "^12.1.2",
"@aeternity/aeproject": "^4.1.5",
"bignumber.js": "^9.1.0",
"commander": "^9.3.0",
"env-paths": "^2.2.1",
Expand Down
30 changes: 19 additions & 11 deletions src/actions/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,48 @@
import fs from 'fs-extra';
import path from 'path';
import { encode } from '@aeternity/aepp-sdk';
import { utils } from '@aeternity/aeproject';
import { initSdk, initSdkByWalletFile } from '../utils/cli';
import { print, printTransaction, printUnderscored } from '../utils/print';
import CliError from '../utils/CliError';

const resolve = (filename) => path.resolve(process.cwd(), filename);

// ## Function which compile your `source` code
export async function compile(filename, options) {
const sdk = await initSdk(options);
const { bytecode } = await sdk.compilerApi.compileContract({
code: (await fs.readFile(resolve(filename))).toString(), options: {},
});
if (options.json) print({ bytecode });
else print(`Contract bytecode: ${bytecode}`);
}

async function getContractParams({
descrPath, contractAddress, contractSource, contractBytecode, contractAci,
}, { dummySource, descrMayNotExist } = {}) {
let descriptor = {};
if (descrPath && (!descrMayNotExist || await fs.exists(resolve(descrPath)))) {
descriptor = await fs.readJson(resolve(descrPath));
}
if (contractSource) {
const contractSourcePath = resolve(contractSource);
descriptor.source = (await fs.readFile(contractSourcePath)).toString();
// TODO: remove after fixing https://github.com/aeternity/aeproject/issues/435
const originalConsoleLog = console.log;
console.log = () => {};
descriptor.fileSystem = utils.getFilesystem(contractSourcePath);
console.log = originalConsoleLog;
}
return {
contractAddress: contractAddress ?? descriptor.address,
// TODO: either remove calldata methods in cli or reconsider getContractInstance requirements
...dummySource && { source: 'invalid-source' },
...descriptor,
...contractSource && { source: (await fs.readFile(resolve(contractSource))).toString() },
...contractBytecode && { bytecode: encode(await fs.readFile(resolve(contractBytecode)), 'cb') },
...contractAci && { aci: await fs.readJson(resolve(contractAci)) },
};
}

// ## Function which compile your `source` code
export async function compile(contractSource, options) {
const sdk = await initSdk(options);
const contract = await sdk.getContractInstance(await getContractParams({ contractSource }));
const bytecode = await contract.compile();
if (options.json) print({ bytecode });
else print(`Contract bytecode: ${bytecode}`);
}

export async function encodeCalldata(fn, args, options) {
const sdk = await initSdk(options);
const contract = await sdk.getContractInstance(await getContractParams(options, { dummySource: true }));
Expand Down
15 changes: 13 additions & 2 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ import CliError from '../src/utils/CliError';

const executeContract = (args) => executeProgram(contractProgram, args);

const testLibSource = `
namespace TestLib =
function sum(x: int, y: int) : int = x + y
`;

const testContractSource = `
@compiler >= 6
@compiler < 7
include "testLib.aes"
contract Identity =
record state = { z: int }
entrypoint init(_z: int) = { z = _z }
entrypoint test(x : int, y: int) = x + y + state.z
entrypoint test(x : int, y: int) = TestLib.sum(x, TestLib.sum(y, state.z))
entrypoint getMap(): map(int, int) = {[1] = 2, [3] = 4}
`;

Expand All @@ -49,10 +56,14 @@ describe('Contract Module', function contractTests() {

before(async () => {
await fs.outputFile(contractSourceFile, testContractSource);
await fs.outputFile('test-artifacts/testLib.aes', testLibSource);
sdk = await getSdk();
await fs.outputJson(
contractAciFile,
await sdk.compilerApi.generateACI({ code: testContractSource, options: {} }),
await sdk.compilerApi.generateACI({
code: testContractSource,
options: { fileSystem: { 'testLib.aes': testLibSource } },
}),
);
});

Expand Down

0 comments on commit 818f375

Please sign in to comment.