Skip to content

Commit bfb9c1d

Browse files
Update to Latest Emulator & Test Framework Changes (#2188)
* Update to Cadence v1.8.3 * Update to latest Fork Testing Changes (#2171) * Update to latest test framework * Update test framework * Update dependencies * Update to latest dependencies & new pragma API * Add metrics for fork testing (#2186) * upgrade emulator (#2184) Co-authored-by: Jordan Ribbink <[email protected]> * Fix CI Test Failure * Update docs * Update dependency addresses * Cleanup TX for simplicity * Fix dependencies --------- Co-authored-by: Janez Podhostnik <[email protected]>
1 parent 436ccd1 commit bfb9c1d

File tree

9 files changed

+418
-172
lines changed

9 files changed

+418
-172
lines changed

go.mod

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ require (
1313
github.com/gosuri/uilive v0.0.4
1414
github.com/logrusorgru/aurora/v4 v4.0.0
1515
github.com/onflow/cadence v1.8.3
16-
github.com/onflow/cadence-tools/languageserver v1.7.2
17-
github.com/onflow/cadence-tools/lint v1.6.1
18-
github.com/onflow/cadence-tools/test v1.8.1
16+
github.com/onflow/cadence-tools/languageserver v1.7.3
17+
github.com/onflow/cadence-tools/lint v1.6.2
18+
github.com/onflow/cadence-tools/test v1.9.1
1919
github.com/onflow/fcl-dev-wallet v0.8.0
20-
github.com/onflow/flixkit-go/v2 v2.6.1
20+
github.com/onflow/flixkit-go/v2 v2.6.2
2121
github.com/onflow/flow-core-contracts/lib/go/templates v1.9.2
22-
github.com/onflow/flow-emulator v1.12.0
23-
github.com/onflow/flow-evm-gateway v1.3.6
24-
github.com/onflow/flow-go v0.44.0-experimental-cadence-v1.8.3.0.20251107135716-b259c922f498
25-
github.com/onflow/flow-go-sdk v1.9.2
22+
github.com/onflow/flow-emulator v1.13.0
23+
github.com/onflow/flow-evm-gateway v1.4.0
24+
github.com/onflow/flow-go v0.44.0-experimental-cadence-v1.8.3.0.20251117184523-ab0655178589
25+
github.com/onflow/flow-go-sdk v1.9.3
2626
github.com/onflow/flow/protobuf/go/flow v0.4.18
27-
github.com/onflow/flowkit/v2 v2.7.2
27+
github.com/onflow/flowkit/v2 v2.8.0
2828
github.com/onflowser/flowser/v3 v3.2.1-0.20240131200229-7d4d22715f48
2929
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
3030
github.com/pkg/errors v0.9.1
@@ -42,13 +42,13 @@ require (
4242
)
4343

4444
require (
45-
cloud.google.com/go v0.120.0 // indirect
45+
cloud.google.com/go v0.121.6 // indirect
4646
cloud.google.com/go/auth v0.16.4 // indirect
4747
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
4848
cloud.google.com/go/compute/metadata v0.8.0 // indirect
49-
cloud.google.com/go/iam v1.5.2 // indirect
50-
cloud.google.com/go/kms v1.23.1 // indirect
51-
cloud.google.com/go/longrunning v0.6.7 // indirect
49+
cloud.google.com/go/iam v1.5.3 // indirect
50+
cloud.google.com/go/kms v1.23.2 // indirect
51+
cloud.google.com/go/longrunning v0.7.0 // indirect
5252
github.com/DataDog/zstd v1.5.6-0.20230824185856-869dae002e5e // indirect
5353
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
5454
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect
@@ -280,21 +280,21 @@ require (
280280
go.uber.org/zap v1.27.0 // indirect
281281
go.yaml.in/yaml/v2 v2.4.2 // indirect
282282
go.yaml.in/yaml/v3 v3.0.4 // indirect
283-
golang.org/x/crypto v0.41.0 // indirect
284-
golang.org/x/net v0.43.0 // indirect
283+
golang.org/x/crypto v0.42.0 // indirect
284+
golang.org/x/net v0.44.0 // indirect
285285
golang.org/x/oauth2 v0.30.0 // indirect
286-
golang.org/x/sync v0.16.0 // indirect
286+
golang.org/x/sync v0.17.0 // indirect
287287
golang.org/x/sys v0.37.0 // indirect
288-
golang.org/x/text v0.28.0 // indirect
288+
golang.org/x/text v0.29.0 // indirect
289289
golang.org/x/time v0.12.0 // indirect
290290
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
291291
gonum.org/v1/gonum v0.16.0 // indirect
292292
google.golang.org/api v0.247.0 // indirect
293293
google.golang.org/appengine v1.6.8 // indirect
294-
google.golang.org/genproto v0.0.0-20250603155806-513f23925822 // indirect
295-
google.golang.org/genproto/googleapis/api v0.0.0-20250818200422-3122310a409c // indirect
296-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250811230008-5f3141c8851a // indirect
297-
google.golang.org/protobuf v1.36.9 // indirect
294+
google.golang.org/genproto v0.0.0-20251111163417-95abcf5c77ba // indirect
295+
google.golang.org/genproto/googleapis/api v0.0.0-20251103181224-f26f9409b101 // indirect
296+
google.golang.org/genproto/googleapis/rpc v0.0.0-20251103181224-f26f9409b101 // indirect
297+
google.golang.org/protobuf v1.36.10 // indirect
298298
gopkg.in/yaml.v2 v2.4.0 // indirect
299299
gopkg.in/yaml.v3 v3.0.1 // indirect
300300
lukechampine.com/blake3 v1.4.1 // indirect

go.sum

Lines changed: 58 additions & 58 deletions
Large diffs are not rendered by default.

internal/super/generator/templates/README_defi_actions.md.tmpl

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ flow project deploy --network emulator
5555
```
5656

5757
This deploys all required contracts to the emulator:
58-
- `DeFiActionsMathUtils` - Math utilities for DeFi Actions
5958
- `DeFiActionsUtils` - Helper utilities for DeFi Actions
6059
- `DeFiActions` - Core DeFi Actions framework
6160
- `ExampleConnectors` - Your TokenSink connector
@@ -75,28 +74,53 @@ This sends `1.0` FLOW from the emulator account to itself (`0xf8d6e0586b0a20c7`)
7574

7675
## Testing
7776

78-
Run the test suite to verify the connector works correctly:
77+
This project includes two types of tests to demonstrate different testing approaches:
78+
79+
### Unit Tests
80+
81+
Run the test suite:
7982

8083
```bash
81-
flow test
84+
flow test cadence/tests/ExampleConnectors_test.cdc
8285
```
8386

84-
The tests run in an isolated environment and automatically:
85-
1. Deploy all DeFi Actions dependencies (`DeFiActionsMathUtils`, `DeFiActionsUtils`, `DeFiActions`)
86-
2. Deploy the `ExampleConnectors` contract
87-
3. Execute the `DepositViaSink` transaction
88-
4. Verify tokens are deposited successfully
87+
This test runs in the Cadence Testing Framework and manually:
88+
1. Deploys all DeFi Actions dependencies (`DeFiActionsUtils`, `DeFiActions`)
89+
2. Deploys the `ExampleConnectors` contract
90+
3. Executes the `DepositViaSink` transaction
91+
4. Verifies tokens are deposited successfully
92+
93+
**Note**: The testing framework manages its own test environment - no emulator needs to be running.
94+
95+
### Fork Testing
96+
97+
This project includes a fork test (`test_incrementfi_swap_on_fork.cdc`) that demonstrates testing your contracts against real mainnet state. Fork testing allows you to:
98+
99+
- Test interactions with production DeFi protocols (like IncrementFi)
100+
- Validate your connectors against real deployed contracts
101+
- Use actual mainnet account data without deploying anything
102+
- Debug issues with historical blockchain state
89103

90-
**Note**: Tests don't require the emulator to be running - they use their own test environment.
104+
Run the fork test against mainnet:
105+
106+
```bash
107+
flow test cadence/tests/test_incrementfi_swap_on_fork.cdc
108+
```
109+
110+
The fork test executes a real swap from FLOW → stFlow using IncrementFi's deployed contracts on mainnet. It uses account impersonation to test transactions as any mainnet account, with all changes happening locally in your test environment.
111+
112+
**Learn more**: See the [Fork Testing Tutorial](https://developers.flow.com/blockchain-development-tutorials/cadence/fork-testing) and [Testing Strategy Guide](https://developers.flow.com/build/cadence/smart-contracts/testing-strategy) for detailed information on when and how to use fork testing.
91113

92114
## Project Structure
93115

94116
- `cadence/contracts/` - Smart contracts
95117
- `ExampleConnectors.cdc` - TokenSink connector implementation
96118
- `cadence/transactions/` - Transaction files
97119
- `DepositViaSink.cdc` - Example transaction using TokenSink
120+
- `incrementfi_swap.cdc` - IncrementFi swap transaction
98121
- `cadence/tests/` - Test files
99122
- `ExampleConnectors_test.cdc` - Integration test for TokenSink
123+
- `test_incrementfi_swap_on_fork.cdc` - Fork test against mainnet IncrementFi
100124
- `flow.json` - Flow project configuration with DeFiActions dependencies
101125

102126
## Dependencies
@@ -110,11 +134,15 @@ This project includes the following dependencies (already installed):
110134
**DeFi Actions Framework:**
111135
- `DeFiActions` - Core framework for composable DeFi connectors
112136
- `DeFiActionsUtils` - Helper utilities
113-
- `DeFiActionsMathUtils` - Math utilities for DeFi operations
137+
138+
**DeFi Protocol Dependencies:**
139+
- `stFlowToken` - Liquid staking token (used in fork test example)
140+
- `SwapConfig` - IncrementFi swap configuration utilities
141+
- `IncrementFiSwapConnectors` - IncrementFi swap connectors (used in fork test example)
114142

115143
**Network Configuration:**
116-
- **Testnet**: All DeFi Actions contracts available at `0x4c2ff9dd03ab442f`
117-
- **Mainnet**: All DeFi Actions contracts available at `0x92195d814edf9cb0`
144+
- **Testnet**: DeFi Actions contracts at `0x0b11b1848a8aa2c0`, IncrementFi at `0x494536c102537e1e`
145+
- **Mainnet**: DeFi Actions contracts at `0x6d888f175c158410`, IncrementFi at `0xe844c7cf7430a77c`
118146
- **Emulator**: Contracts are deployed from source to your emulator account
119147

120148
## Understanding the TokenSink Connector

internal/super/generator/templates/contract_example_connectors_test.cdc.tmpl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@ access(all) let recipient = Test.createAccount()
99

1010
access(all) fun setup() {
1111
// Deploy DeFi Actions dependencies first
12+
// Note: The Cadence Test Framework requires manual deployment of dependencies
13+
// unless used in Fork Testing mode.
1214
var err = Test.deployContract(
13-
name: "DeFiActionsMathUtils",
14-
path: "../../imports/92195d814edf9cb0/DeFiActionsMathUtils.cdc",
15-
arguments: []
16-
)
17-
Test.expect(err, Test.beNil())
18-
19-
err = Test.deployContract(
2015
name: "DeFiActionsUtils",
21-
path: "../../imports/92195d814edf9cb0/DeFiActionsUtils.cdc",
16+
path: "../../imports/6d888f175c158410/DeFiActionsUtils.cdc",
2217
arguments: []
2318
)
2419
Test.expect(err, Test.beNil())
2520

2621
err = Test.deployContract(
2722
name: "DeFiActions",
28-
path: "../../imports/92195d814edf9cb0/DeFiActions.cdc",
23+
path: "../../imports/6d888f175c158410/DeFiActions.cdc",
2924
arguments: []
3025
)
3126
Test.expect(err, Test.beNil())
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Fork Testing Example
2+
//
3+
// The #test_fork pragma configures this test to run against a snapshot of mainnet state.
4+
// This allows testing against real deployed contracts and production data without deploying
5+
// anything to a live network. All mutations happen locally in your test environment.
6+
//
7+
// To run this test: flow test cadence/tests/test_incrementfi_swap_on_fork.cdc
8+
//
9+
// Learn more about fork testing in the README.md file or at:
10+
// https://developers.flow.com/blockchain-development-tutorials/cadence/fork-testing
11+
#test_fork(network: "mainnet", height: nil)
12+
13+
import Test
14+
import "DeFiActions"
15+
import "FlowToken"
16+
import "stFlowToken"
17+
18+
// Executes a minimal swap from FLOW -> stFlow using IncrementFi on a forked mainnet.
19+
// Withdraws a tiny amount from a known FLOW holder and swaps via IncrementFi router.
20+
access(all) fun testIncrementFi_SwapOnFork() {
21+
// Arbitrary mainnet account that has FLOW balance and vaults already setup
22+
// Fork testing allows impersonating any mainnet account for testing
23+
let HOLDER = Test.getAccount(0x42a06f24a1049154)
24+
let AMOUNT_IN: UFix64 = 0.001
25+
26+
let txCode = Test.readFile("../transactions/incrementfi_swap.cdc")
27+
28+
// Define vault types for FLOW -> stFlow swap
29+
let flowVaultType = Type<@FlowToken.Vault>()
30+
let stFlowVaultType = Type<@stFlowToken.Vault>()
31+
32+
let res = Test.executeTransaction(
33+
Test.Transaction(
34+
code: txCode,
35+
authorizers: [HOLDER.address],
36+
signers: [HOLDER],
37+
arguments: [AMOUNT_IN, flowVaultType, stFlowVaultType]
38+
)
39+
)
40+
41+
Test.expect(res, Test.beSucceeded())
42+
43+
// Log all swap events emitted during the transaction
44+
let swapEvents = Test.eventsOfType(Type<DeFiActions.Swapped>())
45+
log("Swap events:")
46+
for event in swapEvents {
47+
log(event)
48+
}
49+
}
50+
51+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Transaction to swap tokens using IncrementFi's DeFi Actions connector.
2+
// This demonstrates using IncrementFi's Swapper connector to execute a token swap.
3+
//
4+
// Arguments:
5+
// - amountIn: The amount of input tokens to swap
6+
// - inVaultType: The vault type for the input token (e.g., Type<@FlowToken.Vault>())
7+
// - outVaultType: The vault type for the output token (e.g., Type<@stFlowToken.Vault>())
8+
//
9+
// This transaction is used by the fork test to demonstrate real DeFi protocol integration.
10+
11+
import "DeFiActions"
12+
import "FungibleToken"
13+
import "FlowToken"
14+
import "IncrementFiSwapConnectors"
15+
import "SwapConfig"
16+
17+
transaction(amountIn: UFix64, inVaultType: Type, outVaultType: Type) {
18+
prepare(acct: auth(BorrowValue) &Account) {
19+
let opID = DeFiActions.createUniqueIdentifier()
20+
21+
// Construct swap path from vault types
22+
let swapPath = [
23+
SwapConfig.SliceTokenTypeIdentifierFromVaultType(vaultTypeIdentifier: inVaultType.identifier),
24+
SwapConfig.SliceTokenTypeIdentifierFromVaultType(vaultTypeIdentifier: outVaultType.identifier)
25+
]
26+
27+
let swapper = IncrementFiSwapConnectors.Swapper(
28+
path: swapPath,
29+
inVault: inVaultType,
30+
outVault: outVaultType,
31+
uniqueID: opID
32+
)
33+
let flowVaultRef = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
34+
?? panic("Holder missing FlowToken vault")
35+
let payment <- flowVaultRef.withdraw(amount: amountIn)
36+
let out <- swapper.swap(quote: nil, inVault: <-payment)
37+
assert(out.balance > 0.0, message: "Expected positive output")
38+
destroy out
39+
}
40+
}
41+
42+

0 commit comments

Comments
 (0)