Skip to content

Commit

Permalink
Update build process (#30)
Browse files Browse the repository at this point in the history
* Update CHANGELOG

* Bump version. Add esm to main package

* Prettify

* Fix complex argument resolver

* Bump fcl packages

* Update build script

* Refactor files to use a different structure

* Update folder structure

* Update build process

* Update configs

* Add npm-run-all package

* Update path to cli

* Bump version

* Update tests

* Fixed key type issue. Update tests

* Bump alpha version

* Remove module statement in package.json

* Bump alpha version

* Add tests for bad inputs

* Add txId field to resolved transaction

* Bump version

* Add more tests

* Update mainnet access node

* Update package.json

* Update paths

* Bump version

* Fix require in bin

* Fix parser to handle comments properly

* Fix paths. bump version

* Remove comma

* Fix package files

* Add execution limit config

* Fix block comments regexp

* Bump version

* Bump version to release
  • Loading branch information
Maksim Daunarovich authored Nov 1, 2021
1 parent 3564459 commit 41fca98
Show file tree
Hide file tree
Showing 38 changed files with 1,583 additions and 16,540 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 0.1.11 - 2021-10-11
- More fixes to complex argument resolver
- Bundle esm into main package

### 0.1.10 - 2021-10-08
- Improved interactions - return raw values and wait for specific transaction status
- Fix argument resolver

### 0.1.9 - 2021-09-23
- Add parser logic for extracting contract arguments
- Fix argument resolver for complex types
Expand Down
3 changes: 0 additions & 3 deletions babel.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
4 changes: 2 additions & 2 deletions bin/generate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require("esm")(module /*, options*/)("../src/generator/cli").run(process.argv);
import ESModule from "esm"
ESModule("esm")(module /*, options*/)("../generator/src/cli").run(process.argv);
52 changes: 52 additions & 0 deletions dev-test/test/bad-input.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import path from "path";
import { emulator, init } from "flow-js-testing";
import { executeScript, mutate } from "../../src";
import { authorization } from "../utils";

// Increase timeout if your tests failing due to timeout
jest.setTimeout(10000);

describe("optional arguments", () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence");
// You can specify different port to parallelize execution of describe blocks
const port = 8080;
// Setting logging flag to true will pipe emulator output to console
const logging = false;

await init(basePath, { port });
return emulator.start(port, logging);
});

// Stop emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});

it("shall throw - from faulty script", async () => {
const executionResult = await executeScript({
code: `
pub fun main(): Int{
return "bazinga"
}
`,
});
const [result, err] = executionResult;
expect(result).toBe(null)
expect(err).not.toBe(null)
});

it("shall throw - transaction", async ()=>{
const cadence = `
transaction{
prepare(singer: AuthAccount){
log("all cool")
}
}
`;
const payer = (()=>{})();
const [txResult, err] = await mutate({ cadence, payer });
expect(txResult).toBe(null)
expect(err).not.toBe(null)
})
});
129 changes: 126 additions & 3 deletions dev-test/test/complex-args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,45 @@ describe("arguments - scripts", () => {
}
`;

const data = [{ 0: 1, 1: 1 }, 1];
const dict = { 0: 1, 1: 1 };
const key = 1;
const data = [dict, key];

const args = () => mapValuesToCode(cadence, data);
const result = await query({ cadence, args });
console.log({ result });
expect(result).toBe(dict[key])
});

test("{String: UInt32} dictionary", async () => {
const cadence = `
pub fun main(data: {String: UInt32}, key: String): UInt32?{
return data[key]
}
`;

const dict = { cadence: 42, test: 1337 };
const key = "test";
const data = [dict, key];

const args = () => mapValuesToCode(cadence, data);
const result = await query({ cadence, args });
expect(result).toBe(dict[key])
});

test("{String: String} dictionary", async () => {
const cadence = `
pub fun main(data: {String: String}, key: String): String?{
return data[key]
}
`;

const dict = { cadence: "rules!" };
const key = "cadence";
const data = [dict, key];

const args = () => mapValuesToCode(cadence, data);
const result = await query({ cadence, args });
expect(result).toBe(dict[key])
});

test("array of dictionaries", async () => {
Expand All @@ -58,6 +92,95 @@ describe("arguments - scripts", () => {
const args = () => mapValuesToCode(cadence, [meta]);

const result = await query({ cadence, args });
console.log({ result });
expect(result.name).toBe(meta[0].name)
expect(result.powerLevel).toBe(meta[0].powerLevel)
});

test("dictionary of array", async () => {
const cadence = `
pub fun main(data: {String: [UInt64]}): {String: [UInt64]} {
return data
}
`;

const data = {
Starly: [1, 3, 3, 7],
TopShot: [42],
};

const args = () => mapValuesToCode(cadence, [data]);

const result = await query({ cadence, args });
expect(result.Starly.length).toBe(data.Starly.length)
expect(result.TopShot.length).toBe(data.TopShot.length)
});

test("dictionary of array - Address to [UInt64]", async () => {
const cadence = `
pub fun main(data: {Address: [UInt64]}): {Address: [UInt64]} {
return data
}
`;

const First = "0x0000000000000001"
const Second = "0x0000000000000002"

const data = {
[First]: [1, 3, 3, 7],
[Second]: [42],
};

const args = () => mapValuesToCode(cadence, [data]);

const result = await query({ cadence, args });
expect(result[First].length).toBe(data[First].length)
expect(result[Second].length).toBe(data[Second].length)
});

test("dictionary of array - Address to [UInt64] and another array", async () => {
const cadence = `
pub fun main(recipients:[Address], data: {Address: [UInt64]}): {Address: [UInt64]} {
log(recipients)
return data
}
`;

const First = "0x0000000000000001"
const Second = "0x0000000000000002"

const recipients = [First, Second]

const data = {
[First]: [1, 3, 3, 7],
[Second]: [42],
};

const args = () => mapValuesToCode(cadence, [recipients, data]);

const result = await query({ cadence, args });
expect(result[First].length).toBe(data[First].length)
expect(result[Second].length).toBe(data[Second].length)
});

test("dictionary of dictionaries of arrays", async () => {
const cadence = `
pub fun main(data: {Address: {String: [UInt64]}}): {Address: {String: [UInt64]}} {
return data
}
`;

const user = "0x0000000000000001"
const data = {
[user]: {
Starly: [1, 3, 3, 7],
TopShot: [42],
},
};

const args = () => mapValuesToCode(cadence, [data]);

const result = await query({ cadence, args });
expect(result[user].Starly.length).toBe(data[user].Starly.length)
expect(result[user].TopShot.length).toBe(data[user].TopShot.length)
});
});
27 changes: 25 additions & 2 deletions dev-test/test/interaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from "path";
import { query, mutate } from "../../src";
import { emulator, init } from "flow-js-testing";
import { authorization } from "../utils";
import { config } from "@onflow/fcl";

// Increase timeout if your tests failing due to timeout
jest.setTimeout(10000);
Expand All @@ -13,7 +12,7 @@ describe("arguments - scripts", () => {
// You can specify different port to parallelize execution of describe blocks
const port = 8080;
// Setting logging flag to true will pipe emulator output to console
const logging = false;
const logging = true;

await init(basePath, { port, logging });
return emulator.start(port);
Expand Down Expand Up @@ -58,4 +57,28 @@ describe("arguments - scripts", () => {
const [txId, err] = await mutate({ cadence, payer, wait: null });
console.log({ txId, err });
});

it("shall properly process Address: [UInt64] array", async ()=>{
const cadence = `
pub fun main(recipients: [Address], rewards: {Address: [UInt64]}) : Int{
for address in recipients {
if (rewards[address] != nil) {
let userRewards = rewards[address] as! [UInt64]
log(userRewards)
}
}
}
return 42
`
const args = [
["0x01", "0x02"],
{
"0x01": [1,2,3,4],
"0x02": [3,4,5,6]
}
]

const [result,err] = await query({ cadence, args });
console.log({result, err})
})
});
2 changes: 1 addition & 1 deletion dev-test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ec as EC } from "elliptic";
import * as fcl from "@onflow/fcl";
import { config, sansPrefix, send, tx, withPrefix } from "@onflow/fcl";
import { SHA3 } from "sha3";
import { mapValuesToCode } from "../src";
import { mapValuesToCode } from "../generator/src";
const ec = new EC("p256");

const hashMsgHex = (msgHex) => {
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/contracts/Basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
replaceImportAddresses,
reportMissingImports,
deployContract,
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
pub contract Basic{
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/scripts/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
import Basic from 0x1
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/scripts/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
pub fun main(message: String): Int{
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/scripts/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
pub fun main(metadata: {String:String}): String{
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/scripts/panic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
pub fun main(){
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/transactions/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
sendTransaction
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
transaction{
Expand Down
2 changes: 1 addition & 1 deletion generator-test/src/generated/transactions/panic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
sendTransaction
} from '../../../../src'
} from '../../../../generator/src'

export const CODE = `
transaction{
Expand Down
22 changes: 22 additions & 0 deletions generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "flow-cadut-generator",
"amdName": "flowCadutGenerator",
"version": "0.0.1",
"private": true,
"author": "Maksim Daunarovich",
"license": "Apache-2.0",
"main": "dist/generator.js",
"module": "dist/generator.module.js",
"umd:main": "dist/generator.umd.js",
"source": "src/index.js",
"scripts": {
"build": "microbundle",
"dev": "microbundle watch"
},
"peerDependencies": {
"flow-cadut": "^0.1.10"
},
"mangle": {
"regex": "^_"
}
}
2 changes: 1 addition & 1 deletion src/generator/cli.js → generator/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import fs from "fs";

import { processFolder, processGitRepo } from "./processor";
import "../templates";
import "./templates";

// Initially we will support only GitHub repos
// TODO: support other urls. List can be found here:
Expand Down
2 changes: 1 addition & 1 deletion src/generator/file.js → generator/src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import fs from "fs";
import { resolve, dirname } from "path";
import prettier from "prettier";
import parserBabel from "prettier/parser-babel";
import { underscoreToCamelCase } from "../strings";
import { underscoreToCamelCase } from "../../src/strings";

/**
* Syntax sugar for file reading
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/generator/processor.js → generator/src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { resolve } from "path";
import Handlebars from "handlebars";
import simpleGit from "simple-git";

import { getSplitCharacter, trimAndSplit, underscoreToCamelCase } from "../strings";
import { getSplitCharacter, trimAndSplit, underscoreToCamelCase } from "../../src/strings";
import { generateExports, getFilesList, readFile, writeFile } from "./file";
import { getTemplateInfo, CONTRACT, SCRIPT, TRANSACTION, extractSigners } from "../parser";
import { getTemplateInfo, CONTRACT, SCRIPT, TRANSACTION, extractSigners } from "../../src/parser";

const getFetchUrl = (input) => {
// eslint-disable-next-line no-useless-escape
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 41fca98

Please sign in to comment.