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

Commit 7b7e747

Browse files
committed
Make block/timestamp offset utilities work in contracts & deprecate builtInMethods transformer
1 parent 47cba8f commit 7b7e747

File tree

13 files changed

+310
-186
lines changed

13 files changed

+310
-186
lines changed

.changeset/red-vans-explode.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@onflow/flow-js-testing": patch
3+
---
4+
5+
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.
6+
7+
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.
8+
9+
[See more here](/TRANSITIONS.md#0002-depreaction-of-builtinmethods-code-transformer)

TRANSITIONS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Transitions
22

3+
## 0002 Depreaction of `builtInMethods` code transformer
4+
5+
- **Date:** Aug 8 2022
6+
- **Type:** Depreaction of `builtInMethods` code transformer
7+
8+
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.
9+
10+
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.
11+
12+
Please remove this transformer from all your existing `sendTransaction` & `executeScript` calls if you were using any block or timestamp offset utilities.
13+
314
## 0001 Deprecate `emulator.start()` port argument
415

516
- **Date:** Jun 28 2022

dev-test/utilities.test.js

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
setBlockOffset,
1313
getTimestampOffset,
1414
setTimestampOffset,
15+
deployContract,
1516
} from "../src"
1617
import {extractParameters} from "../src/interaction"
1718
import {
@@ -21,6 +22,7 @@ import {
2122
} from "../src/transformers"
2223
import {getManagerAddress} from "../src/manager"
2324
import * as manager from "../src/manager"
25+
import {query} from "@onflow/fcl"
2426

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

5153
const offset = 42
5254
await shallPass(sendTransaction("set-block-offset", [manager], [offset]))
55+
5356
const [newOffset] = await executeScript("get-block-offset")
5457
expect(newOffset).toBe(String(offset))
5558
})
5659

5760
it("should read offset with utility method", async () => {
58-
// CadUt version of sending transactions and execution scripts don't have
59-
// import resolver built in, so we need to provide addressMap to it
60-
const FlowManager = await getManagerAddress()
61-
const addressMap = {FlowManager}
62-
63-
const [offSet] = await getBlockOffset({addressMap})
64-
65-
expect(offSet).toBe("0")
61+
const [offset] = await getBlockOffset()
62+
expect(offset).toBe("0")
6663
})
6764

6865
it("should update offset with utility method", async () => {
69-
// CadUt version of sending transactions and execution scripts don't have
70-
// import resolver built in, so we need to provide addressMap to it
71-
const FlowManager = await getManagerAddress()
72-
const addressMap = {FlowManager}
73-
74-
const [oldOffset] = await getBlockOffset({addressMap})
75-
66+
const [oldOffset] = await getBlockOffset()
7667
expect(oldOffset).toBe("0")
7768

7869
const offset = 42
70+
await shallPass(setBlockOffset(offset))
7971

80-
const [txResult] = await setBlockOffset(offset)
81-
expect(txResult.errorMessage).toBe("")
72+
const [newOffset] = await getBlockOffset()
73+
expect(newOffset).toBe(String(offset))
74+
})
8275

83-
const [newOffset] = await getBlockOffset({addressMap})
76+
it("should update offset in contract", async () => {
77+
await shallPass(
78+
deployContract({
79+
code: `
80+
pub contract BlockTest {
81+
pub fun currentHeight(): UInt64 {
82+
return getCurrentBlock().height
83+
}
84+
85+
init() {}
86+
}
87+
`,
88+
})
89+
)
8490

85-
expect(newOffset).toBe(String(offset))
91+
const offset = 42
92+
await shallPass(manager.setBlockOffset(offset))
93+
94+
const realBlock = await query({
95+
cadence: `
96+
pub fun main(): UInt64 {
97+
return getCurrentBlock().height
98+
}
99+
`,
100+
})
101+
102+
const [currentBlock] = await shallResolve(
103+
executeScript({
104+
code: `
105+
import BlockTest from 0x01
106+
pub fun main(): UInt64 {
107+
return BlockTest.currentHeight()
108+
}
109+
`,
110+
})
111+
)
112+
113+
// Expect 1 higher than initial block height + offset due to sealed TX @ manager.setBlockOffset
114+
expect(Number(currentBlock)).toBe(Number(realBlock) + offset)
86115
})
87116
})
88117

@@ -148,34 +177,59 @@ describe("timestamp offset", () => {
148177
})
149178

150179
it("should read offset with utility method", async () => {
151-
// CadUt version of sending transactions and execution scripts don't have
152-
// import resolver built in, so we need to provide addressMap to it
153-
const FlowManager = await getManagerAddress()
154-
const addressMap = {FlowManager}
155-
156-
const [offSet] = await getTimestampOffset({addressMap})
157-
180+
const [offSet] = await getTimestampOffset()
158181
expect(offSet).toBe("0.00000000")
159182
})
160183

161184
it("should update offset with utility method", async () => {
162-
// CadUt version of sending transactions and execution scripts don't have
163-
// import resolver built in, so we need to provide addressMap to it
164-
const FlowManager = await getManagerAddress()
165-
const addressMap = {FlowManager}
166-
167-
const [oldOffset] = await getTimestampOffset({addressMap})
168-
185+
const [oldOffset] = await getTimestampOffset()
169186
expect(oldOffset).toBe("0.00000000")
170187

171188
const offset = 42
189+
await shallPass(setTimestampOffset(offset))
190+
191+
const [newOffset] = await getTimestampOffset()
192+
expect(newOffset).toBe(offset.toFixed(8))
193+
})
172194

173-
const [txResult] = await setTimestampOffset(offset)
174-
expect(txResult.errorMessage).toBe("")
195+
it("should update offset in contract", async () => {
196+
await shallPass(
197+
deployContract({
198+
code: `
199+
pub contract TimestampTest {
200+
pub fun currentTime(): UFix64 {
201+
return getCurrentBlock().timestamp
202+
}
203+
204+
init() {}
205+
}
206+
`,
207+
})
208+
)
175209

176-
const [newOffset] = await getTimestampOffset({addressMap})
210+
const offset = 42
211+
await shallPass(manager.setTimestampOffset(offset))
212+
213+
const realTimestamp = await query({
214+
cadence: `
215+
pub fun main(): UFix64 {
216+
return getCurrentBlock().timestamp
217+
}
218+
`,
219+
})
220+
221+
const [currentTimestamp] = await shallResolve(
222+
executeScript({
223+
code: `
224+
import TimestampTest from 0x01
225+
pub fun main(): UFix64 {
226+
return TimestampTest.currentTime()
227+
}
228+
`,
229+
})
230+
)
177231

178-
expect(newOffset).toBe(offset.toFixed(8))
232+
expect(Number(currentTimestamp)).toBe(Number(realTimestamp) + offset)
179233
})
180234
})
181235

0 commit comments

Comments
 (0)