Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow overriding default import map #230

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cadence/transactions/register-contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion src/generated/transactions/registerContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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')
})
})
Loading