Skip to content

Commit de33841

Browse files
committed
fix: allow overriding default import map
1 parent 816ae53 commit de33841

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/imports.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ export const resolveImports = async code => {
4545
const addressMap = {}
4646
const importList = extractImports(fixShorthandImports(code))
4747
for (const key in importList) {
48-
if (defaultsByName[key]) {
48+
if (
49+
key !== "FlowManager" &&
50+
importList[key] &&
51+
importList[key].toLowerCase().startsWith("0x") &&
52+
importList[key].length === 18
53+
) {
54+
addressMap[key] = importList[key]
55+
} else if (
56+
defaultsByName[key] &&
57+
!(importList[key] && importList[key] === '"DYNAMIC"')
58+
) {
4959
addressMap[key] = defaultsByName[key]
5060
} else {
5161
const address = await getContractAddress(key)

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {
2323
getScriptCode,
2424
getContractCode,
2525
getTransactionCode,
26+
defaultsByName,
2627
} from "./file"
2728
export {sendTransaction, executeScript} from "./interaction"
2829
export {getFlowBalance, mintFlow} from "./flow-token"

test/integration/imports.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
deployContract,
66
resolveImports,
77
getServiceAddress,
8+
getAccountAddress,
89
} from "../../src"
9-
import {defaultsByName} from "../../src/file"
10+
import {defaultsByName} from "../../src"
1011
import {DEFAULT_TEST_TIMEOUT} from "../util/timeout.const"
1112
import {fixShorthandImports} from "../../src/imports"
1213

@@ -32,11 +33,13 @@ describe("import resolver", () => {
3233
})
3334

3435
test("use imports", async () => {
36+
const Dynamic = await getAccountAddress("Dynamic")
3537
await deployContract({code: emptyContract("First"), name: "First"})
3638
await deployContract({code: emptyContract("Second"), name: "Second"})
3739
await deployContract({code: emptyContract("Third"), name: "Third"})
3840
await deployContract({code: emptyContract("A"), name: "A"})
3941
await deployContract({code: emptyContract("B"), name: "B"})
42+
await deployContract({code: emptyContract("Dynamo"), name: "Dynamo", to: Dynamic})
4043

4144
const code = `
4245
import First from 0xFIRST
@@ -47,6 +50,10 @@ describe("import resolver", () => {
4750
import FungibleToken from 0xFUNGIBLETOKEN
4851
import FlowToken from 0xFLOWTOKEN
4952
53+
import Dynamo from "DYNAMIC"
54+
import Direct from 0x0123456789012345
55+
import FlowFees from 0x0123456789012345
56+
5057
access(all) fun main(){}
5158
`
5259

@@ -64,5 +71,9 @@ describe("import resolver", () => {
6471
expect(B).toBe(Registry)
6572
expect(FungibleToken).toBe(defaultsByName.FungibleToken)
6673
expect(FlowToken).toBe(defaultsByName.FlowToken)
74+
const {Dynamo, Direct, FlowFees} = addressMap
75+
expect(Dynamo).toBe(Dynamic)
76+
expect(Direct).toBe('0x0123456789012345')
77+
expect(FlowFees).toBe('0x0123456789012345')
6778
})
6879
})

0 commit comments

Comments
 (0)