Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Make block/timestamp offset utilities work in contracts & deprecate b…
Browse files Browse the repository at this point in the history
…uiltInMethods transformer (#165)
  • Loading branch information
jribbink authored Aug 26, 2022
1 parent 8d95ca3 commit 4ac3741
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 247 deletions.
9 changes: 9 additions & 0 deletions .changeset/red-vans-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@onflow/flow-js-testing": patch
---

Block & timestamp offsets (e.g. `setBlockOffset`/`setTimestampOffset` now work in contracts. As well, `deployContract` & `deployContractByName` have the option of [accepting code transformers](/docs/api.md#deploycontractprops) like scripts/transactions.

Additionally, passing the `builtInMethods` code transformer is now deprecated for scripts & transactions which require usage of block/timestamp offsets as transformer is applied by default internally by Flow JS Testing.

[See more here](/TRANSITIONS.md#0002-depreaction-of-builtinmethods-code-transformer)
11 changes: 11 additions & 0 deletions TRANSITIONS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Transitions

## 0002 Depreaction of `builtInMethods` code transformer

- **Date:** Aug 8 2022
- **Type:** Depreaction of `builtInMethods` code transformer

The `builtInMethods` code transformer is now deprecated and will not be exported by the Flow JS Testing library in future versions. It is now applied by default to all cadence code processed by the Flow JS Testing library and passing this transformer manually is redundant and uncessary.

It was previously used to replace segments of cadence code related to `setBlockOffset`/`setTimestampOffset` utilties, but its implementation has now been handled internally by the Flow JS Testing library.

Please remove this transformer from all your existing `sendTransaction` & `executeScript` calls if you were using any block or timestamp offset utilities.

## 0001 Deprecate `emulator.start()` port argument

- **Date:** Jun 28 2022
Expand Down
128 changes: 91 additions & 37 deletions dev-test/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
setBlockOffset,
getTimestampOffset,
setTimestampOffset,
deployContract,
} from "../src"
import {extractParameters} from "../src/interaction"
import {
Expand All @@ -21,6 +22,7 @@ import {
} from "../src/transformers"
import {getManagerAddress} from "../src/manager"
import * as manager from "../src/manager"
import {query} from "@onflow/fcl"

// We need to set timeout for a higher number, cause some transactions might take up some time
jest.setTimeout(10000)
Expand Down Expand Up @@ -50,39 +52,66 @@ describe("block height offset", () => {

const offset = 42
await shallPass(sendTransaction("set-block-offset", [manager], [offset]))

const [newOffset] = await executeScript("get-block-offset")
expect(newOffset).toBe(String(offset))
})

it("should read offset with utility method", async () => {
// CadUt version of sending transactions and execution scripts don't have
// import resolver built in, so we need to provide addressMap to it
const FlowManager = await getManagerAddress()
const addressMap = {FlowManager}

const [offSet] = await getBlockOffset({addressMap})

expect(offSet).toBe("0")
const [offset] = await getBlockOffset()
expect(offset).toBe("0")
})

it("should update offset with utility method", async () => {
// CadUt version of sending transactions and execution scripts don't have
// import resolver built in, so we need to provide addressMap to it
const FlowManager = await getManagerAddress()
const addressMap = {FlowManager}

const [oldOffset] = await getBlockOffset({addressMap})

const [oldOffset] = await getBlockOffset()
expect(oldOffset).toBe("0")

const offset = 42
await shallPass(setBlockOffset(offset))

const [txResult] = await setBlockOffset(offset)
expect(txResult.errorMessage).toBe("")
const [newOffset] = await getBlockOffset()
expect(newOffset).toBe(String(offset))
})

const [newOffset] = await getBlockOffset({addressMap})
it("should update offset in contract", async () => {
await shallPass(
deployContract({
code: `
pub contract BlockTest {
pub fun currentHeight(): UInt64 {
return getCurrentBlock().height
}
init() {}
}
`,
})
)

expect(newOffset).toBe(String(offset))
const offset = 42
await shallPass(manager.setBlockOffset(offset))

const realBlock = await query({
cadence: `
pub fun main(): UInt64 {
return getCurrentBlock().height
}
`,
})

const [currentBlock] = await shallResolve(
executeScript({
code: `
import BlockTest from 0x01
pub fun main(): UInt64 {
return BlockTest.currentHeight()
}
`,
})
)

// Expect 1 higher than initial block height + offset due to sealed TX @ manager.setBlockOffset
expect(Number(currentBlock)).toBe(Number(realBlock) + offset)
})
})

Expand Down Expand Up @@ -148,34 +177,59 @@ describe("timestamp offset", () => {
})

it("should read offset with utility method", async () => {
// CadUt version of sending transactions and execution scripts don't have
// import resolver built in, so we need to provide addressMap to it
const FlowManager = await getManagerAddress()
const addressMap = {FlowManager}

const [offSet] = await getTimestampOffset({addressMap})

const [offSet] = await getTimestampOffset()
expect(offSet).toBe("0.00000000")
})

it("should update offset with utility method", async () => {
// CadUt version of sending transactions and execution scripts don't have
// import resolver built in, so we need to provide addressMap to it
const FlowManager = await getManagerAddress()
const addressMap = {FlowManager}

const [oldOffset] = await getTimestampOffset({addressMap})

const [oldOffset] = await getTimestampOffset()
expect(oldOffset).toBe("0.00000000")

const offset = 42
await shallPass(setTimestampOffset(offset))

const [newOffset] = await getTimestampOffset()
expect(newOffset).toBe(offset.toFixed(8))
})

const [txResult] = await setTimestampOffset(offset)
expect(txResult.errorMessage).toBe("")
it("should update offset in contract", async () => {
await shallPass(
deployContract({
code: `
pub contract TimestampTest {
pub fun currentTime(): UFix64 {
return getCurrentBlock().timestamp
}
init() {}
}
`,
})
)

const [newOffset] = await getTimestampOffset({addressMap})
const offset = 42
await shallPass(manager.setTimestampOffset(offset))

const realTimestamp = await query({
cadence: `
pub fun main(): UFix64 {
return getCurrentBlock().timestamp
}
`,
})

const [currentTimestamp] = await shallResolve(
executeScript({
code: `
import TimestampTest from 0x01
pub fun main(): UFix64 {
return TimestampTest.currentTime()
}
`,
})
)

expect(newOffset).toBe(offset.toFixed(8))
expect(Number(currentTimestamp)).toBe(Number(realTimestamp) + offset)
})
})

Expand Down
Loading

0 comments on commit 4ac3741

Please sign in to comment.