Skip to content

Commit

Permalink
refactor!: use sdk provided filesystem util, remove aeproject util im…
Browse files Browse the repository at this point in the history
…plementation
  • Loading branch information
thepiwo committed Jul 31, 2023
1 parent 7590118 commit a661796
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 52 deletions.
5 changes: 3 additions & 2 deletions docs/cli/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Helper and utilities for AEproject use, e.g. prefunded wallets, network definiti

```js
const { networks, utils, wallets } = require('@aeternity/aeproject');
const { getFileSystem } = require("@aeternity/aepp-sdk");
```

Read [AEproject Library](../lib.md) for a more detailed explanation about the usage of these imports.
Expand All @@ -42,7 +43,7 @@ aeSdk = await utils.getSdk();

Get the filesystem definition for (custom) `includes` of the given contract:
```js
const filesystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
const fileSystem = await getFileSystem(EXAMPLE_CONTRACT_SOURCE);
```

Read the contract source from the filesystem:
Expand All @@ -52,7 +53,7 @@ const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);

Initialize the contract instance:
```js
contract = await aeSdk.initializeContract({ sourceCode, filesystem });
contract = await aeSdk.initializeContract({ sourceCode, fileSystem });
```

Deploy the contract:
Expand Down
5 changes: 0 additions & 5 deletions docs/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ utils.getContractContent(contractPath);
```
Read the contract source from given path, just a wrapper for `fs.readFileSync` using `utf-8` encoding.

```javascript
utils.getFilesystem(contractPath);
```
Add the required filesystem imports for contract from given path, excluding the Sophia provided library imports.

```javascript
utils.get(url);
```
Expand Down
3 changes: 2 additions & 1 deletion src/init/update-artifacts/test/exampleTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { assert } = require('chai');
const { utils } = require('@aeternity/aeproject');
const { getFileSystem } = require("@aeternity/aepp-sdk");

const EXAMPLE_CONTRACT_SOURCE = './contracts/ExampleContract.aes';

Expand All @@ -11,7 +12,7 @@ describe('ExampleContract', () => {
aeSdk = await utils.getSdk();

// a filesystem object must be passed to the compiler if the contract uses custom includes
const fileSystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
const fileSystem = await getFileSystem(EXAMPLE_CONTRACT_SOURCE);

// get content of contract
const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);
Expand Down
45 changes: 1 addition & 44 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
import fs from 'fs';
import path from 'path';

import {
AeSdk, MemoryAccount, Node, CompilerHttp,
AeSdk, MemoryAccount, Node, CompilerHttp, getFileSystem,
} from '@aeternity/aepp-sdk';
import * as networks from './networks.json';
import wallets from './wallets.json';
import { get } from '../utils/utils';

export const getContractContent = (contractPath) => fs.readFileSync(contractPath, 'utf8');

export const getFilesystem = (contractPath) => {
const defaultIncludes = [
'List.aes', 'Option.aes', 'String.aes',
'Func.aes', 'Pair.aes', 'Triple.aes',
'BLS12_381.aes', 'Frac.aes', 'Set.aes',
'Bitwise.aes',
];

const rgx = /^include\s+"([\w/.-]+)"/gim;
const rgxIncludePath = /"([\w/.-]+)"/i;
const rgxMainPath = /.*\//g;

const contractContent = getContractContent(contractPath);
const filesystem = {};

const rootIncludes = contractContent.match(rgx);
if (!rootIncludes) return filesystem;
const contractPathMatch = rgxMainPath.exec(contractPath);

// eslint-disable-next-line no-restricted-syntax
for (const rootInclude of rootIncludes) {
const includeRelativePath = rgxIncludePath.exec(rootInclude);

// eslint-disable-next-line no-continue
if (defaultIncludes.includes(includeRelativePath[1])) continue;

// eslint-disable-next-line no-console
console.log(`==> Adding include to filesystem: ${includeRelativePath[1]}`);
const includePath = path.resolve(`${contractPathMatch[0]}/${includeRelativePath[1]}`);

try {
filesystem[includeRelativePath[1]] = fs.readFileSync(includePath, 'utf-8');
} catch (error) {
throw Error(`File to include '${includeRelativePath[1]}' not found.`);
}

Object.assign(filesystem, getFilesystem(includePath));
}

return filesystem;
};

export const getDefaultAccounts = () => wallets.map((keypair) => new MemoryAccount(keypair.secretKey));

export const getSdk = () => {
Expand Down

0 comments on commit a661796

Please sign in to comment.