Skip to content
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
42 changes: 21 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ require (
github.com/gosuri/uilive v0.0.4
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/onflow/cadence v1.8.3
github.com/onflow/cadence-tools/languageserver v1.7.2
github.com/onflow/cadence-tools/lint v1.6.1
github.com/onflow/cadence-tools/test v1.8.1
github.com/onflow/cadence-tools/languageserver v1.7.3
github.com/onflow/cadence-tools/lint v1.6.2
github.com/onflow/cadence-tools/test v1.9.1
github.com/onflow/fcl-dev-wallet v0.8.0
github.com/onflow/flixkit-go/v2 v2.6.1
github.com/onflow/flixkit-go/v2 v2.6.2
github.com/onflow/flow-core-contracts/lib/go/templates v1.9.2
github.com/onflow/flow-emulator v1.12.0
github.com/onflow/flow-evm-gateway v1.3.6
github.com/onflow/flow-go v0.44.0-experimental-cadence-v1.8.3.0.20251107135716-b259c922f498
github.com/onflow/flow-go-sdk v1.9.2
github.com/onflow/flow-emulator v1.13.0
github.com/onflow/flow-evm-gateway v1.4.0
github.com/onflow/flow-go v0.44.0-experimental-cadence-v1.8.3.0.20251117184523-ab0655178589
github.com/onflow/flow-go-sdk v1.9.3
github.com/onflow/flow/protobuf/go/flow v0.4.18
github.com/onflow/flowkit/v2 v2.7.2
github.com/onflow/flowkit/v2 v2.8.0
github.com/onflowser/flowser/v3 v3.2.1-0.20240131200229-7d4d22715f48
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pkg/errors v0.9.1
Expand All @@ -42,13 +42,13 @@ require (
)

require (
cloud.google.com/go v0.120.0 // indirect
cloud.google.com/go v0.121.6 // indirect
cloud.google.com/go/auth v0.16.4 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
cloud.google.com/go/compute/metadata v0.8.0 // indirect
cloud.google.com/go/iam v1.5.2 // indirect
cloud.google.com/go/kms v1.23.1 // indirect
cloud.google.com/go/longrunning v0.6.7 // indirect
cloud.google.com/go/iam v1.5.3 // indirect
cloud.google.com/go/kms v1.23.2 // indirect
cloud.google.com/go/longrunning v0.7.0 // indirect
github.com/DataDog/zstd v1.5.6-0.20230824185856-869dae002e5e // indirect
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect
Expand Down Expand Up @@ -280,21 +280,21 @@ require (
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/crypto v0.42.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/oauth2 v0.30.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/text v0.29.0 // indirect
golang.org/x/time v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
gonum.org/v1/gonum v0.16.0 // indirect
google.golang.org/api v0.247.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20250603155806-513f23925822 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250818200422-3122310a409c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250811230008-5f3141c8851a // indirect
google.golang.org/protobuf v1.36.9 // indirect
google.golang.org/genproto v0.0.0-20251111163417-95abcf5c77ba // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20251103181224-f26f9409b101 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251103181224-f26f9409b101 // indirect
google.golang.org/protobuf v1.36.10 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.4.1 // indirect
Expand Down
116 changes: 58 additions & 58 deletions go.sum

Large diffs are not rendered by default.

52 changes: 40 additions & 12 deletions internal/super/generator/templates/README_defi_actions.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ flow project deploy --network emulator
```

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

## Testing

Run the test suite to verify the connector works correctly:
This project includes two types of tests to demonstrate different testing approaches:

### Unit Tests

Run the test suite:

```bash
flow test
flow test cadence/tests/ExampleConnectors_test.cdc
```

The tests run in an isolated environment and automatically:
1. Deploy all DeFi Actions dependencies (`DeFiActionsMathUtils`, `DeFiActionsUtils`, `DeFiActions`)
2. Deploy the `ExampleConnectors` contract
3. Execute the `DepositViaSink` transaction
4. Verify tokens are deposited successfully
This test runs in the Cadence Testing Framework and manually:
1. Deploys all DeFi Actions dependencies (`DeFiActionsUtils`, `DeFiActions`)
2. Deploys the `ExampleConnectors` contract
3. Executes the `DepositViaSink` transaction
4. Verifies tokens are deposited successfully

**Note**: The testing framework manages its own test environment - no emulator needs to be running.

### Fork Testing

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:

- Test interactions with production DeFi protocols (like IncrementFi)
- Validate your connectors against real deployed contracts
- Use actual mainnet account data without deploying anything
- Debug issues with historical blockchain state

**Note**: Tests don't require the emulator to be running - they use their own test environment.
Run the fork test against mainnet:

```bash
flow test cadence/tests/test_incrementfi_swap_on_fork.cdc
```

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.

**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.

## Project Structure

- `cadence/contracts/` - Smart contracts
- `ExampleConnectors.cdc` - TokenSink connector implementation
- `cadence/transactions/` - Transaction files
- `DepositViaSink.cdc` - Example transaction using TokenSink
- `incrementfi_swap.cdc` - IncrementFi swap transaction
- `cadence/tests/` - Test files
- `ExampleConnectors_test.cdc` - Integration test for TokenSink
- `test_incrementfi_swap_on_fork.cdc` - Fork test against mainnet IncrementFi
- `flow.json` - Flow project configuration with DeFiActions dependencies

## Dependencies
Expand All @@ -110,11 +134,15 @@ This project includes the following dependencies (already installed):
**DeFi Actions Framework:**
- `DeFiActions` - Core framework for composable DeFi connectors
- `DeFiActionsUtils` - Helper utilities
- `DeFiActionsMathUtils` - Math utilities for DeFi operations

**DeFi Protocol Dependencies:**
- `stFlowToken` - Liquid staking token (used in fork test example)
- `SwapConfig` - IncrementFi swap configuration utilities
- `IncrementFiSwapConnectors` - IncrementFi swap connectors (used in fork test example)

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

## Understanding the TokenSink Connector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@ access(all) let recipient = Test.createAccount()

access(all) fun setup() {
// Deploy DeFi Actions dependencies first
// Note: The Cadence Test Framework requires manual deployment of dependencies
// unless used in Fork Testing mode.
var err = Test.deployContract(
name: "DeFiActionsMathUtils",
path: "../../imports/92195d814edf9cb0/DeFiActionsMathUtils.cdc",
arguments: []
)
Test.expect(err, Test.beNil())

err = Test.deployContract(
name: "DeFiActionsUtils",
path: "../../imports/92195d814edf9cb0/DeFiActionsUtils.cdc",
path: "../../imports/6d888f175c158410/DeFiActionsUtils.cdc",
arguments: []
)
Test.expect(err, Test.beNil())

err = Test.deployContract(
name: "DeFiActions",
path: "../../imports/92195d814edf9cb0/DeFiActions.cdc",
path: "../../imports/6d888f175c158410/DeFiActions.cdc",
arguments: []
)
Test.expect(err, Test.beNil())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Fork Testing Example
//
// The #test_fork pragma configures this test to run against a snapshot of mainnet state.
// This allows testing against real deployed contracts and production data without deploying
// anything to a live network. All mutations happen locally in your test environment.
//
// To run this test: flow test cadence/tests/test_incrementfi_swap_on_fork.cdc
//
// Learn more about fork testing in the README.md file or at:
// https://developers.flow.com/blockchain-development-tutorials/cadence/fork-testing
#test_fork(network: "mainnet", height: nil)

import Test
import "DeFiActions"
import "FlowToken"
import "stFlowToken"

// Executes a minimal swap from FLOW -> stFlow using IncrementFi on a forked mainnet.
// Withdraws a tiny amount from a known FLOW holder and swaps via IncrementFi router.
access(all) fun testIncrementFi_SwapOnFork() {
// Arbitrary mainnet account that has FLOW balance and vaults already setup
// Fork testing allows impersonating any mainnet account for testing
let HOLDER = Test.getAccount(0x42a06f24a1049154)
Comment thread
jribbink marked this conversation as resolved.
let AMOUNT_IN: UFix64 = 0.001

let txCode = Test.readFile("../transactions/incrementfi_swap.cdc")

// Define vault types for FLOW -> stFlow swap
let flowVaultType = Type<@FlowToken.Vault>()
let stFlowVaultType = Type<@stFlowToken.Vault>()

let res = Test.executeTransaction(
Test.Transaction(
code: txCode,
authorizers: [HOLDER.address],
signers: [HOLDER],
arguments: [AMOUNT_IN, flowVaultType, stFlowVaultType]
)
)

Test.expect(res, Test.beSucceeded())

// Log all swap events emitted during the transaction
let swapEvents = Test.eventsOfType(Type<DeFiActions.Swapped>())
log("Swap events:")
for event in swapEvents {
log(event)
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Transaction to swap tokens using IncrementFi's DeFi Actions connector.
// This demonstrates using IncrementFi's Swapper connector to execute a token swap.
//
// Arguments:
// - amountIn: The amount of input tokens to swap
// - inVaultType: The vault type for the input token (e.g., Type<@FlowToken.Vault>())
// - outVaultType: The vault type for the output token (e.g., Type<@stFlowToken.Vault>())
//
// This transaction is used by the fork test to demonstrate real DeFi protocol integration.

import "DeFiActions"
import "FungibleToken"
import "FlowToken"
import "IncrementFiSwapConnectors"
import "SwapConfig"

transaction(amountIn: UFix64, inVaultType: Type, outVaultType: Type) {
prepare(acct: auth(BorrowValue) &Account) {
let opID = DeFiActions.createUniqueIdentifier()

// Construct swap path from vault types
let swapPath = [
SwapConfig.SliceTokenTypeIdentifierFromVaultType(vaultTypeIdentifier: inVaultType.identifier),
SwapConfig.SliceTokenTypeIdentifierFromVaultType(vaultTypeIdentifier: outVaultType.identifier)
]

let swapper = IncrementFiSwapConnectors.Swapper(
path: swapPath,
inVault: inVaultType,
outVault: outVaultType,
uniqueID: opID
)
let flowVaultRef = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Holder missing FlowToken vault")
let payment <- flowVaultRef.withdraw(amount: amountIn)
let out <- swapper.swap(quote: nil, inVault: <-payment)
assert(out.balance > 0.0, message: "Expected positive output")
destroy out
}
}


Loading
Loading