Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 072dc23

Browse files
author
Maksim Daunarovich
authored
Add automatic resolvers for imports and arguments (#33)
* Prettify * Add auto resolver for arguments * Update flow-cadut version * Remove console.log. Add new tests for arguments * Bump version * Add tests around import resolver * Export resolveImports method * Implement resolveImports method * Use babel-jest for tests * Update interactions to use importResolver * Pass service tag to prevent infinite loop * Refactor code around service interactions * Exclude dev tests * Deploy to service account by default * Update import test and assertions * Remove unused import * Add new line at the eof * Bring back slash * Export getServiceAddress method from index. * Prettify * Fix docstrings for resolveImports method
1 parent 569f63e commit 072dc23

14 files changed

+1519
-1044
lines changed

babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
3+
};

dev-test/imports.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from "path";
2+
import { emulator, init, deployContract, resolveImports, getAccountAddress } from "../src";
3+
import { defaultsByName } from "../src/file";
4+
import { getServiceAddress } from "../src/manager";
5+
6+
jest.setTimeout(10000);
7+
8+
const emptyContract = (name) =>
9+
`pub contract ${name}{
10+
init(){}
11+
}
12+
`;
13+
14+
describe("import resolver", () => {
15+
// Instantiate emulator and path to Cadence files
16+
beforeEach(async () => {
17+
const basePath = path.resolve(__dirname, "./cadence");
18+
const port = 8080;
19+
await init(basePath, port);
20+
return emulator.start(port, false);
21+
});
22+
23+
// Stop emulator, so it could be restarted
24+
afterEach(async () => {
25+
return emulator.stop();
26+
});
27+
28+
test("use imports", async () => {
29+
await deployContract({ code: emptyContract("First"), name: "First" });
30+
await deployContract({ code: emptyContract("Second"), name: "Second" });
31+
32+
const code = `
33+
import First from 0xFIRST
34+
import Second from 0xSECOND
35+
36+
import FungibleToken from 0xFUNGIBLETOKEN
37+
import FlowToken from 0xFLOWTOKEN
38+
39+
pub fun main(){}
40+
`;
41+
42+
const addressMap = await resolveImports(code);
43+
const Registry = await getServiceAddress();
44+
expect(addressMap["First"]).toBe(Registry);
45+
expect(addressMap["Second"]).toBe(Registry);
46+
expect(addressMap["FungibleToken"]).toBe(defaultsByName.FungibleToken);
47+
expect(addressMap["FlowToken"]).toBe(defaultsByName.FlowToken);
48+
});
49+
});

dev-test/interaction.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "path";
2+
import * as types from "@onflow/types"
23
import {
34
emulator,
45
init,
@@ -57,6 +58,52 @@ describe("interactions - sendTransaction", () => {
5758
return sendTransaction({ code });
5859
});
5960
});
61+
62+
test("sendTransaction - argument mapper - basic", async () => {
63+
await shallPass(async () => {
64+
const code = `
65+
transaction(a: Int){
66+
prepare(signer: AuthAccount){
67+
log(signer.address)
68+
}
69+
}
70+
`;
71+
const args = [
72+
[42, types.Int]
73+
]
74+
return sendTransaction({ code, args });
75+
});
76+
});
77+
test("sendTransaction - argument mapper - multiple", async () => {
78+
await shallPass(async () => {
79+
const code = `
80+
transaction(a: Int, b: Int, name: String){
81+
prepare(signer: AuthAccount){
82+
log(signer.address)
83+
}
84+
}
85+
`;
86+
const args = [
87+
[42, 1337, types.Int],
88+
["Hello, Cadence", types.String]
89+
]
90+
return sendTransaction({ code, args });
91+
});
92+
});
93+
test("sendTransaction - argument mapper - automatic", async () => {
94+
await shallPass(async () => {
95+
const code = `
96+
transaction(a: Int, b: Int, name: String){
97+
prepare(signer: AuthAccount){
98+
log(signer.address)
99+
}
100+
}
101+
`;
102+
const args = [ 42, 1337, "Hello, Cadence"]
103+
104+
return sendTransaction({ code, args });
105+
});
106+
});
60107
});
61108
describe("interactions - executeScript", () => {
62109
// Instantiate emulator and path to Cadence files

0 commit comments

Comments
 (0)