Skip to content

Commit

Permalink
fix: allow overriding default import map
Browse files Browse the repository at this point in the history
  • Loading branch information
NtTestAlert committed May 17, 2024
1 parent 816ae53 commit e0bec29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
getScriptCode,
getContractCode,
getTransactionCode,
defaultsByName,
} from "./file"
export {sendTransaction, executeScript} from "./interaction"
export {getFlowBalance, mintFlow} from "./flow-token"
Expand Down
13 changes: 12 additions & 1 deletion test/integration/imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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(){}
`

Expand All @@ -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')
})
})

0 comments on commit e0bec29

Please sign in to comment.