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

Update FCL to v1.10.1 & Fix CI #231

Merged
merged 10 commits into from
May 22, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/two-news-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/flow-js-testing": patch
---

Update @onflow/fcl to v1.10.1
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Get Flow CLI version
id: testbed
run: |
echo "flow-version=$(echo | flow-c1 version --output json | grep 'Version:' | grep -oEi '(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?')" >> $GITHUB_OUTPUT
echo "flow-version=$(flow-c1 version --output=json | jq -r '.version')" >> $GITHUB_OUTPUT
echo "package-version=$(grep version package.json | sed 's/.*"version": "\(.*\)".*/\1/')" >> $GITHUB_OUTPUT
echo "fcl-version=$(grep 'fcl":' package.json | sed 's/.*"@onflow\/fcl": "\(.*\)".*/\1/')" >> $GITHUB_OUTPUT

Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
6,004 changes: 3,894 additions & 2,110 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"build": "microbundle --no-compress",
"generate-code": "echo manually edited, do not run // node_modules/.bin/flow-generate -i ./cadence -o ./src/generated",
"generate-code": "node_modules/.bin/flow-generate -i ./cadence -o ./src/generated",
"lint": "eslint -c .eslintrc.js src",
"check-headers": "sh ./check-headers.sh",
"prettify": "prettier --write ./src",
Expand Down Expand Up @@ -55,24 +55,25 @@
]
},
"dependencies": {
"@onflow/fcl": "^1.3.2",
"@onflow/fcl": "1.3.2",
"@onflow/fcl-config": "^0.0.1",
"@onflow/flow-cadut": "^0.3.0-stable-cadence.0",
"@onflow/types": "^1.0.5",
"@onflow/flow-cadut": "^0.3.0-stable-cadence.1",
"elliptic": "^6.5.4",
"esm": "^3.2.25",
"jest-environment-uint8array": "^1.0.0",
"js-sha256": "^0.9.0",
"js-sha3": "^0.8.0",
"rimraf": "^3.0.2",
"rlp": "^2.2.6",
"semver": "^7.6.2",
"yargs": "^17.0.1"
},
"devDependencies": {
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.14.5",
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.0",
"@onflow/flow-cadut-generator": "^0.1.1-stable-cadence.0",
"babel-jest": "^27.0.2",
"eslint": "^7.24.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
26 changes: 20 additions & 6 deletions src/emulator/emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import {send, build, getBlock, decode, config} from "@onflow/fcl"
import {Logger, LOGGER_LEVELS} from "./logger"
import {getAvailablePorts, getFlowVersion} from "../utils"
import {satisfies} from "semver"

const {spawn} = require("child_process")

const SUPPORTED_FLOW_CLI_VERSIONS = ">=2.0.0"
const SUPPORTED_PRE_RELEASE_MATCHER = "cadence-v1.0.0-preview"

const DEFAULT_HTTP_PORT = 8080
const DEFAULT_GRPC_PORT = 3569

Expand Down Expand Up @@ -79,13 +83,26 @@
* @returns Promise<*>
*/
async start(options = {}) {
const {flags, logging = false, signatureCheck = false, execName} = options
if (execName) this.execName = execName

// Get version of CLI
const flowVersion = await getFlowVersion()
if (flowVersion.major < 1) {
const flowVersion = await getFlowVersion(this.execName)
const satisfiesVersion = satisfies(
flowVersion.raw,
SUPPORTED_FLOW_CLI_VERSIONS,
{
includePrerelease: true,
}
)
const satisfiesPreRelease = flowVersion.raw.includes(
SUPPORTED_PRE_RELEASE_MATCHER
)
if (!satisfiesVersion && !satisfiesPreRelease) {
throw new Error(
`Flow CLI version ${flowVersion.major}.${flowVersion.minor}.${flowVersion.patch} is not supported. Please install version 1.0.0 or higher.`
`Unsupported Flow CLI version: ${flowVersion.raw}. Supported versions: ${SUPPORTED_FLOW_CLI_VERSIONS} or pre-releases tagged with ${SUPPORTED_PRE_RELEASE_MATCHER}`
)
}

Check warning on line 105 in src/emulator/emulator.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

// populate emulator ports with available ports
const ports = await getAvailablePorts(4)
Expand All @@ -108,9 +125,6 @@
this.grpcPort = DEFAULT_GRPC_PORT + offset
}

const {flags, logging = false, signatureCheck = false, execName} = options
if (execName) this.execName = execName

// config access node
config().put("accessNode.api", `http://localhost:${this.restPort}`)

Expand Down
70 changes: 2 additions & 68 deletions src/generated/contracts/FlowManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
getEnvironment,
replaceImportAddresses,
reportMissingImports,
// deployContract, // TODO broken, using our own version
sendTransaction,
deployContract,
} from '@onflow/flow-cadut'

export const CODE = `
Expand Down Expand Up @@ -161,78 +160,13 @@ export const FlowManagerTemplate = async (addressMap = {}) => {
return replaceImportAddresses(CODE, fullMap);
};

// TODO copied from cadut and patched for Cadence 1.0
const deployContract = async props => {
const {
name,
to,
payer,
proposer,
code: contractCode,
update = false,
processed = false,
addressMap = {},
} = props

// Update imprort statement with addresses from addressMap
const ixContractCode = processed
? contractCode
: replaceImportAddresses(contractCode, addressMap)

// TODO: Implement arguments for "init" method
const template = update ? `
transaction(name: String, code: String) {
prepare(acct: auth(AddContract) &Account) {
let decoded = code.decodeHex()

acct.contracts.add(
name: name,
code: decoded,
)
}
}
` : `
transaction(name: String, code: String){
prepare(acct: auth(AddContract, UpdateContract) &Account) {
let decoded = code.decodeHex()

if acct.contracts.get(name: name) == nil {
acct.contracts.add(name: name, code: decoded)
} else {
acct.contracts.update(name: name, code: decoded)
}
}
}
`

const hexedCode = Buffer.from(ixContractCode, "utf8").toString("hex")
const args = [name, hexedCode]
// Set roles
let ixProposer = to
let ixPayer = to
let ixSigners = [to]

if (payer) {
ixPayer = payer
ixProposer = proposer || payer
}

return await sendTransaction({
payer: ixPayer,
proposer: ixProposer,
signers: ixSigners,
code: template,
args,
});
}

/**
* Deploys FlowManager transaction to the network
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
* @param Array<*> args - list of arguments
* param Array<string> - list of signers
*/
export const deployFlowManager = async (props) => {
export const deployFlowManager = async (props = {}) => {
const { addressMap = {} } = props;
const code = await FlowManagerTemplate(addressMap);
const name = "FlowManager"
Expand Down
12 changes: 1 addition & 11 deletions src/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,6 @@ export const sendTransaction = async (...props) => {
return [result, err, logs]
}

// TODO helper until flow-cadut is updated
const replaceAccessAllInScript = code => {
const scriptMatcher = /(pub|access\s*\(\s*all\s*\))\s+fun\s+main\s*/gimu
if (scriptMatcher.test(code)) {
return code.replace(scriptMatcher, "pub fun main")
}
return code
}

/**
* Sends script code for execution. Returns decoded value
* @param {Object} props
Expand All @@ -188,12 +179,11 @@ export const executeScript = async (...props) => {
try {
const extractor = extractParameters("script")
const {code, args, limit} = await extractor(_props)
const patchedCode = replaceAccessAllInScript(code)

const ix = [fcl.script(code), fcl.limit(limit)]
// add arguments if any
if (args) {
const resolvedArgs = await resolveArguments(args, patchedCode)
const resolvedArgs = await resolveArguments(args, code)
ix.push(fcl.args(resolvedArgs))
}
const response = await fcl.send(ix)
Expand Down
25 changes: 15 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import {config, withPrefix} from "@onflow/fcl"
import {exec} from "child_process"
import {createServer} from "net"

const FLOW_VERSION_REGEX = /v((\d+)\.(\d+)\.(\d+))/
import * as semver from "semver"

export const isObject = arg => typeof arg === "object" && arg !== null
export const isString = obj => typeof obj === "string" || obj instanceof String
Expand All @@ -40,20 +39,26 @@
})
}

export async function getFlowVersion() {
/**
* Get the Flow CLI version
* @param {string} flowCommand - the Flow CLI command name
* @returns {Promise<import("semver").SemVer>}
*/
export async function getFlowVersion(flowCommand = "flow") {
return new Promise((resolve, reject) => {
exec("flow version", (error, stdout) => {
exec(`${flowCommand} version --output=json`, (error, stdout) => {
if (error) {
reject(
"Could not determine Flow CLI version, please make sure it is installed and available in your PATH"
)
} else {
const version = FLOW_VERSION_REGEX.exec(stdout).slice(2, 5)
resolve({
major: parseInt(version[0]),
minor: parseInt(version[1]),
patch: parseInt(version[2]),
})
const versionStr = JSON.parse(stdout).version
const version = semver.parse(versionStr)
if (!version) {
reject(`Invalid Flow CLI version string: ${versionStr}`)

Check warning on line 58 in src/utils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 59 in src/utils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

resolve(version)
}

Check warning on line 62 in src/utils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/integration/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ beforeEach(async () => {
})

afterEach(async () => {
emulator.stop()
return emulator.stop()
})

it("createAccount - should work with name and resolves to correct getAccountAddress", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ beforeEach(async () => {
})

afterEach(async () => {
emulator.stop()
await emulator.stop()
})

describe("Storage Inspection", () => {
Expand Down
Loading