Skip to content

Refactored fcl-core interaction-template-utils folder files to TypeScript #2474

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

Open
wants to merge 5 commits into
base: feature/typescript-fcl-core
Choose a base branch
from
Open
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {invariant} from "@onflow/util-invariant"
import type {InteractionTemplate100} from "../interaction-template"

export interface DeriveCadenceByNetwork100Params {
network: string
template: InteractionTemplate100
}

/**
* @description Fills import addresses in Cadence for network
*
* @param {DeriveCadenceByNetwork100Params} params
* @param {string} params.network Network to derive Cadence for
* @param {InteractionTemplate100} params.template Interaction Template to derive Cadence from
* @returns {Promise<string>} Promise that resolves with the derived Cadence code
*/
export async function deriveCadenceByNetwork100({
network,
template,
}: DeriveCadenceByNetwork100Params): Promise<string> {
invariant(
template.f_version === "1.0.0",
"deriveCadenceByNetwork100({ template }) -- template must be version 1.0.0"
)

const networkDependencies: [string, string][] = Object.keys(
template?.data?.dependencies
).map((dependencyPlaceholder: string): [string, string] => {
const dependencyNetworkContracts = Object.values(
template?.data?.dependencies?.[dependencyPlaceholder]
)

invariant(
dependencyNetworkContracts !== undefined,
`deriveCadenceByNetwork100 -- Could not find contracts for dependency placeholder: ${dependencyPlaceholder}`
)

invariant(
dependencyNetworkContracts.length > 0,
`deriveCadenceByNetwork100 -- Could not find contracts for dependency placeholder: ${dependencyPlaceholder}`
)

const dependencyContract = dependencyNetworkContracts[0]
const dependencyContractForNetwork = dependencyContract?.[network]

invariant(
dependencyContractForNetwork as any,
`deriveCadenceByNetwork100 -- Could not find ${network} network information for dependency: ${dependencyPlaceholder}`
)

return [dependencyPlaceholder, dependencyContractForNetwork?.address]
})

return networkDependencies.reduce(
(cadence: string, [placeholder, address]: [string, string]) => {
const regex = new RegExp("(\\b" + placeholder + "\\b)", "g")
return cadence.replace(regex, address)
},
template.data.cadence
)
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import {invariant} from "@onflow/util-invariant"
import {replaceStringImports} from "../utils/replace-string-imports.js"
import {replaceStringImports} from "../utils/replace-string-imports"
import type {InteractionTemplate110} from "../interaction-template"

export interface DeriveCadenceByNetwork110Params {
network: string
template: InteractionTemplate110
}

/**
* @description Fills import addresses in Cadence for network
*
* @param {object} params
* @param {string} params.network - Network to derive Cadence for
* @param {object} params.template - Interaction Template to derive Cadence from
* @returns {Promise<string>} - Promise that resolves with the derived Cadence code
* @param {DeriveCadenceByNetwork110Params} params
* @param {string} params.network Network to derive Cadence for
* @param {InteractionTemplate110} params.template Interaction Template to derive Cadence from
* @returns {Promise<string>} Promise that resolves with the derived Cadence code
*/
export async function deriveCadenceByNetwork110({network, template}) {
export async function deriveCadenceByNetwork110({
network,
template,
}: DeriveCadenceByNetwork110Params): Promise<string> {
invariant(
template.f_version === "1.1.0",
"deriveCadenceByNetwork110({ template }) -- template must be version 1.0.0"
"deriveCadenceByNetwork110({ template }) -- template must be version 1.1.0"
)

// get network dependencies from template dependencies, use new string import format
const networkDependencies = {}
const networkDependencies: Record<string, string> = {}

template?.data?.dependencies.forEach(dependency => {
dependency.contracts.forEach(contract => {
Expand Down Expand Up @@ -46,7 +56,7 @@ export async function deriveCadenceByNetwork110({network, template}) {
)

invariant(
template?.data?.cadence?.body,
template?.data?.cadence?.body as any,
`no cadence found -- Could not replace import dependencies: ${networkDependencies}`
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {deriveCadenceByNetwork} from "./derive-cadence-by-network.js"
import {deriveCadenceByNetwork} from "./derive-cadence-by-network"

describe("Derive cadence by network 1.0.0", () => {
const template = {
const template: any = {
f_type: "InteractionTemplate",
f_version: "1.0.0",
id: "abc123",
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("Derive cadence by network 1.0.0", () => {
})

describe("Derive cadence by network 1.1.0", () => {
const template11 = {
const template11: any = {
f_type: "InteractionTemplate",
f_version: "1.1.0",
id: "3a99af243b85f3f6af28304af2ed53a37fb913782b3efc483e6f0162a47720a0",
Expand Down Expand Up @@ -207,7 +207,7 @@ describe("Derive cadence by network 1.1.0", () => {
await expect(() =>
deriveCadenceByNetwork({
network: "mainnet",
template: {f_type: "InteractionTemplate", f_version: "0.0.0"},
template: {f_type: "InteractionTemplate", f_version: "0.0.0"} as any,
})
).rejects.toThrow(Error)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import {invariant} from "@onflow/util-invariant"
import {deriveCadenceByNetwork100} from "./derive-cadence-by-network-1.0.0.js"
import {deriveCadenceByNetwork110} from "./derive-cadence-by-network-1.1.0.js"
import {deriveCadenceByNetwork100} from "./derive-cadence-by-network-1.0.0"
import {deriveCadenceByNetwork110} from "./derive-cadence-by-network-1.1.0"
import type {InteractionTemplate} from "../interaction-template"

export interface DeriveCadenceByNetworkParams {
network: string
template: InteractionTemplate
}

/**
* @description Fills import addresses in Cadence for network
*
* @param {object} params
* @param {string} params.network - Network to derive Cadence for
* @param {object} params.template - Interaction Template to derive Cadence from
* @returns {Promise<string>} - Promise that resolves with the derived Cadence code
* @param {DeriveCadenceByNetworkParams} params
* @param {string} params.network Network to derive Cadence for
* @param {InteractionTemplate} params.template Interaction Template to derive Cadence from
* @returns {Promise<string>} Promise that resolves with the derived Cadence code
*/
export async function deriveCadenceByNetwork({network, template}) {
export async function deriveCadenceByNetwork({
network,
template,
}: DeriveCadenceByNetworkParams): Promise<string> {
invariant(
network != undefined,
"deriveCadenceByNetwork({ network }) -- network must be defined"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import {invariant, send, getAccount, config, decode} from "@onflow/sdk"
import {genHash} from "../utils/hash.js"
import {findImports} from "../utils/find-imports.js"
import {generateImport} from "../utils/generate-import.js"
import {genHash} from "../utils/hash"
import {findImports} from "../utils/find-imports"
import {generateImport} from "../utils/generate-import"

export interface GenerateDependencyPin100Params {
address: string
contractName: string
}

/**
* @description Produces a dependency pin for a contract at current state of chain
* @param {object} params
* @param {string} params.address - The address of the account containing the contract
* @param {string} params.contractName - The name of the contract
* @param {object} opts - Options to pass to the interaction
* @returns {Promise<string>} - The dependency pin
* @param {GenerateDependencyPin100Params} params
* @param {string} params.address The address of the account containing the contract
* @param {string} params.contractName The name of the contract
* @param {object} opts Options to pass to the interaction
* @returns {Promise<string>} The dependency pin
*/
export async function generateDependencyPin100(
{address, contractName},
opts = {}
) {
{address, contractName}: GenerateDependencyPin100Params,
opts: any = {}
): Promise<string> {
invariant(
address != undefined,
"generateDependencyPin({ address }) -- address must be defined"
Expand All @@ -32,7 +37,7 @@ export async function generateDependencyPin100(
"generateDependencyPin({ contractName }) -- contractName must be a string"
)

const horizon = [generateImport({contractName, address})]
const horizon: any = [generateImport({contractName, address})]

for (const horizonImport of horizon) {
const account = await send(
Expand All @@ -56,7 +61,7 @@ export async function generateDependencyPin100(
horizon.push(...contractImports)
}

const contractHashes = horizon.map(iport => genHash(iport.contract))
const contractHashes = horizon.map((iport: any) => genHash(iport.contract))

const contractHashesJoined = contractHashes.join("")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import {invariant, send, getAccount, config, decode} from "@onflow/sdk"
import {genHash} from "../utils/hash.js"
import {findImports} from "../utils/find-imports.js"
import {generateImport} from "../utils/generate-import.js"
import {genHash} from "../utils/hash"
import {findImports} from "../utils/find-imports"
import {generateImport} from "../utils/generate-import"

export interface GenerateDependencyPin110Params {
address: string
contractName: string
blockHeight?: number
}

/**
* @description Produces a dependency pin for a contract at current state of chain
* @param {object} params
* @param {string} params.address - The address of the account containing the contract
* @param {string} params.contractName - The name of the contract
* @param {object} opts - Options to pass to the interaction
* @returns {Promise<string>} - The dependency pin
* @param {GenerateDependencyPin110Params} params
* @param {string} params.address The address of the account containing the contract
* @param {string} params.contractName The name of the contract
* @param {number} params.blockHeight The block height to generate the dependency pin at
* @param {object} opts Options to pass to the interaction
* @returns {Promise<string>} The dependency pin
*/
export async function generateDependencyPin110(
{address, contractName},
opts = {}
) {
{address, contractName}: GenerateDependencyPin110Params,
opts: any = {}
): Promise<string> {
invariant(
address != undefined,
"generateDependencyPin({ address }) -- address must be defined"
Expand All @@ -32,7 +39,7 @@ export async function generateDependencyPin110(
"generateDependencyPin({ contractName }) -- contractName must be a string"
)

const horizon = [generateImport({contractName, address})]
const horizon: any = [generateImport({contractName, address})]

for (const horizonImport of horizon) {
const account = await send(
Expand All @@ -56,7 +63,7 @@ export async function generateDependencyPin110(
horizon.push(...contractImports)
}

const contractPinSelfHashesPromises = horizon.map(iport =>
const contractPinSelfHashesPromises = horizon.map((iport: any) =>
genHash(iport.contract)
)
// genHash returns a promise, so we need to await the results of all the promises
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
generateDependencyPin110,
generateDependencySelfPin,
} from "./generate-dependency-pin-1.1.0.js"
import {config} from "@onflow/config"
import {generateDependencyPin110} from "./generate-dependency-pin-1.1.0"

const returnedAccount = {
address: "0xf233dcee88fe0abe",
Expand Down Expand Up @@ -278,12 +275,14 @@ jest.mock("@onflow/sdk", () => ({
}))

describe("1.1.0, generate dependency pin", () => {
let warnSpy: jest.SpyInstance

beforeAll(() => {
jest.spyOn(console, "warn").mockImplementation(() => {})
warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {})
})

afterAll(() => {
console.warn.mockRestore()
warnSpy.mockRestore()
})

test("v1.1.0, get dependency pin", async () => {
Expand Down
Loading