Skip to content

Commit

Permalink
chore: update custom strategy guide
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlak committed Dec 14, 2023
1 parent 00fc099 commit 17da0fb
Showing 1 changed file with 72 additions and 29 deletions.
101 changes: 72 additions & 29 deletions contracts/strategies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,91 @@ Portfolio will rely on specific functions that the custom strategy contracts mus

## Strategy functions

### Swap Lifecycle
Here is a summary of the workflow between a user, the `Portfolio` contract and a custom strategy contract:

```
┌────┐┌─────────┐ ┌────────┐
│User││Portfolio│ │Strategy│
└─┬──┘└────┬────┘ └───┬────┘
│ │ │
│ swap() │ │
│───────>│ │
│ │ │
│ │validatePool()│
│ │─────────────>│
│ │ │
│ │ beforeSwap() │
│ │─────────────>│
│ │ │
│ │validateSwap()│
│ │─────────────>│
┌─┴──┐┌────┴────┐ ┌───┴────┐
│User││Portfolio│ │Strategy│
└────┘└─────────┘ └────────┘
┌────┐ ┌────────┐ ┌─────────┐
│User│ │Strategy│ │Portfolio│
└─┬──┘ └───┬────┘ └────┬────┘
│ │ │
│getStrategyData()│ │
│────────────────>│ │
│ │ │
│ createPool() │
│───────────────────────────────>│
│ │ │
│ │validatePool()│
│ │<─────────────│
│ │ │
│ updatePool() │
│───────────────────────────────>│
│ │ │
│ swap() │
│───────────────────────────────>│
│ │ │
│ │ beforeSwap() │
│ │<─────────────│
│ │ │
│ │validateSwap()│
│ │<─────────────│
┌─┴──┐ ┌───┴────┐ ┌────┴────┐
│User│ │Strategy│ │Portfolio│
└────┘ └────────┘ └─────────┘
```

### `getStrategyData()`

```solidity
function getStrategyData(
bytes memory data
) external pure returns (
bytes memory strategyData,
uint256 initialX,
uint256 initialY
);
```

This function is meant to be called by users willing to create a new pool using a custom strategy. This is the only function called directly by the users (instead of `Portfolio` contract itself). It returns the data that should be passed to the `createPool` function of the Portfolio contract.

### `afterCreate()`

```solidity
function afterCreate(
uint64 poolId,
bytes calldata strategyArgs
) external returns (bool success);
function afterCreate(
uint64 poolId,
bytes calldata strategyArgs
) external returns (bool success);
```

This function is called after the creation of a pool and allows the strategy to perform any initialization logic. The `strategyArgs` parameter is the same as the one passed to the `createPool` function of the Portfolio contract.
This function is called by `Portfolio` after the creation of a pool in the `Portfolio` contract and allows the strategy to perform any initialization logic. The `strategyArgs` parameter is the same as the one passed to the `createPool` function of the Portfolio contract.

### `beforeSwap()`

```solidity
function beforeSwap(
uint64 poolId,
bool sellAsset,
address swapper
) external returns (bool success, int256 invariant);
function beforeSwap(
uint64 poolId,
bool sellAsset,
address swapper
) external returns (bool success, int256 invariant);
```

This function is called before a swap is executed and must return a boolean indicating whether the swap should be allowed or not, along with the current invariant.

### `validateSwap()`

```solidity
```

This function is called after a swap is executed and allows the strategy to perform any validation logic.

### `updatePool()`

```solidity
function updatePool(
uint64 poolId,
address caller,
bytes memory data
) external;
```

The `updatePool` function provides an flexible way to update pool parameters. It is meant to be called via the `Portfolio` contract and requires a `poolId` along with some data encoded as `bytes`. Keep in mind that the `Portfolio` contract does not perform any validation on the data but simply pass them along.

0 comments on commit 17da0fb

Please sign in to comment.