From 3f3d8d3c4b6a3e15719e4542db06cd0afb758b0c Mon Sep 17 00:00:00 2001 From: NtTestAlert <50535142+NtTestAlert@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:36:00 +0200 Subject: [PATCH 1/3] fix short-hand-import parser --- src/imports.js | 2 +- test/integration/imports.test.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/imports.js b/src/imports.js index 998941b0..568dd1a5 100644 --- a/src/imports.js +++ b/src/imports.js @@ -29,7 +29,7 @@ const importRegex = new RegExp( export const fixShorthandImports = code => { return code.replaceAll(importRegex, found => { if (found.indexOf(" from") !== -1) return found - const whatMatch = found.matchAll(/"?([^"\s]+)"?,?\s*?/giu) + const whatMatch = found.matchAll(/"([^"\s]+)"\s*,?\s*?/giu) return [...whatMatch] .map(what => `import ${what[1]} from "./${what[1]}.cdc"`) .join("\n") diff --git a/test/integration/imports.test.js b/test/integration/imports.test.js index ef3471b5..435581b6 100644 --- a/test/integration/imports.test.js +++ b/test/integration/imports.test.js @@ -8,6 +8,7 @@ import { } from "../../src" import {defaultsByName} from "../../src/file" import {DEFAULT_TEST_TIMEOUT} from "../util/timeout.const" +import {fixShorthandImports} from "../../src/imports" jest.setTimeout(DEFAULT_TEST_TIMEOUT) @@ -49,6 +50,9 @@ describe("import resolver", () => { access(all) fun main(){} ` + const testFixed = fixShorthandImports(code) + expect(testFixed.includes("import.cdc")).toBe(false) + const addressMap = await resolveImports(code) const Registry = await getServiceAddress() From de338416fd3c2f076086c68e6fae461f36dfc748 Mon Sep 17 00:00:00 2001 From: NtTestAlert <50535142+NtTestAlert@users.noreply.github.com> Date: Fri, 17 May 2024 15:17:14 +0200 Subject: [PATCH 2/3] fix: allow overriding default import map --- src/imports.js | 12 +++++++++++- src/index.js | 1 + test/integration/imports.test.js | 13 ++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/imports.js b/src/imports.js index 568dd1a5..021037b0 100644 --- a/src/imports.js +++ b/src/imports.js @@ -45,7 +45,17 @@ export const resolveImports = async code => { const addressMap = {} const importList = extractImports(fixShorthandImports(code)) for (const key in importList) { - if (defaultsByName[key]) { + if ( + key !== "FlowManager" && + importList[key] && + importList[key].toLowerCase().startsWith("0x") && + importList[key].length === 18 + ) { + addressMap[key] = importList[key] + } else if ( + defaultsByName[key] && + !(importList[key] && importList[key] === '"DYNAMIC"') + ) { addressMap[key] = defaultsByName[key] } else { const address = await getContractAddress(key) diff --git a/src/index.js b/src/index.js index 2eb7257a..18480e77 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,7 @@ export { getScriptCode, getContractCode, getTransactionCode, + defaultsByName, } from "./file" export {sendTransaction, executeScript} from "./interaction" export {getFlowBalance, mintFlow} from "./flow-token" diff --git a/test/integration/imports.test.js b/test/integration/imports.test.js index 435581b6..0bfe2217 100644 --- a/test/integration/imports.test.js +++ b/test/integration/imports.test.js @@ -5,8 +5,9 @@ import { deployContract, resolveImports, getServiceAddress, + getAccountAddress, } from "../../src" -import {defaultsByName} from "../../src/file" +import {defaultsByName} from "../../src" import {DEFAULT_TEST_TIMEOUT} from "../util/timeout.const" import {fixShorthandImports} from "../../src/imports" @@ -32,11 +33,13 @@ describe("import resolver", () => { }) test("use imports", async () => { + const Dynamic = await getAccountAddress("Dynamic") await deployContract({code: emptyContract("First"), name: "First"}) await deployContract({code: emptyContract("Second"), name: "Second"}) await deployContract({code: emptyContract("Third"), name: "Third"}) await deployContract({code: emptyContract("A"), name: "A"}) await deployContract({code: emptyContract("B"), name: "B"}) + await deployContract({code: emptyContract("Dynamo"), name: "Dynamo", to: Dynamic}) const code = ` import First from 0xFIRST @@ -47,6 +50,10 @@ describe("import resolver", () => { import FungibleToken from 0xFUNGIBLETOKEN import FlowToken from 0xFLOWTOKEN + import Dynamo from "DYNAMIC" + import Direct from 0x0123456789012345 + import FlowFees from 0x0123456789012345 + access(all) fun main(){} ` @@ -64,5 +71,9 @@ describe("import resolver", () => { expect(B).toBe(Registry) expect(FungibleToken).toBe(defaultsByName.FungibleToken) expect(FlowToken).toBe(defaultsByName.FlowToken) + const {Dynamo, Direct, FlowFees} = addressMap + expect(Dynamo).toBe(Dynamic) + expect(Direct).toBe('0x0123456789012345') + expect(FlowFees).toBe('0x0123456789012345') }) }) From a15082e66b5f9d3896b6011b0cd9a0490626eeb4 Mon Sep 17 00:00:00 2001 From: NtTestAlert <50535142+NtTestAlert@users.noreply.github.com> Date: Fri, 17 May 2024 16:58:09 +0200 Subject: [PATCH 3/3] fix: fix register-contract.cdc --- cadence/transactions/register-contract.cdc | 2 +- src/generated/transactions/registerContract.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cadence/transactions/register-contract.cdc b/cadence/transactions/register-contract.cdc index 74fa7cc8..ac639b5d 100644 --- a/cadence/transactions/register-contract.cdc +++ b/cadence/transactions/register-contract.cdc @@ -3,7 +3,7 @@ import FlowManager from 0x01 transaction(name: String, address: Address) { prepare(signer: auth(BorrowValue) &Account){ let linkPath = FlowManager.contractManagerPath - let contractManager = getAccount(manager).capabilities.borrow<&FlowManager.Mapper>(linkPath)! + let contractManager = signer.capabilities.borrow<&FlowManager.Mapper>(linkPath)! contractManager.setAddress(name, address: address) } } diff --git a/src/generated/transactions/registerContract.js b/src/generated/transactions/registerContract.js index d4672faf..726b77ea 100644 --- a/src/generated/transactions/registerContract.js +++ b/src/generated/transactions/registerContract.js @@ -14,7 +14,7 @@ import FlowManager from 0x01 transaction(name: String, address: Address) { prepare(signer: auth(BorrowValue) &Account){ let linkPath = FlowManager.contractManagerPath - let contractManager = getAccount(manager).capabilities.borrow<&FlowManager.Mapper>(linkPath)! + let contractManager = signer.capabilities.borrow<&FlowManager.Mapper>(linkPath)! contractManager.setAddress(name, address: address) } }