Skip to content

Commit

Permalink
more editing
Browse files Browse the repository at this point in the history
  • Loading branch information
ijonele committed Dec 25, 2024
1 parent 156bbc2 commit 157a857
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,53 @@ sidebar_position: 6

# Create the OrderFactory contract

The `OrderFactory` contract manages agent creation and tracking with enhanced security features.
## Overview

Create a contract `OrderFactory.sol`.
The **`OrderFactory` contract** securely manages the creation and tracking of orders.

**Key features:**

1. Factory pattern:

- Creates new order instances
- Tracks order creators
- Supports multiple order types

2. Management:

- Ownable for admin control
- Scheduler management
- Registry integration

3. Order creation:

- Basic orders supported
- Advanced orders placeholder
- Order registration in Registry

## Create the `OrderFactory` contract

Implement order creation and tracking in a file `OrderFactory.sol`:

:::note GitHub
You can find the full code on GitHub: [`/src/OrderFactory.sol`](https://github.com/warden-protocol/wardenprotocol/blob/main/solidity/orders/src/OrderFactory.sol)
:::

```solidity title="/src/OrderFactory.sol"
contract OrderFactory is Ownable {
// NEW: Enhanced tracking and security
// Track order creators
mapping(address orderAddress => address orderCreator) public orders;
Registry public immutable REGISTRY;
address public scheduler;
// NEW: Event for better tracking
// An event emitted when an order is created
event OrderCreated(
address indexed orderCreator,
OrderType indexed orderType,
address indexed orderContact
);
// Create an order
function createOrder(
Types.OrderData calldata _orderData,
CommonTypes.Coin[] calldata maxKeychainFees,
Expand All @@ -39,24 +64,6 @@ contract OrderFactory is Ownable {
}
```

**Key features:**

1. Factory pattern:

- Creates new order instances
- Tracks order creators
- Supports multiple order types

2. Management:

- Ownable for admin control
- Scheduler management
- Registry integration

3. Order creation:

- Basic orders supported
- Advanced orders placeholder
- Order registration in Registry
## Next steps

After creating the Orderfactory contract, you can [implement the deployment scripts](deploy_script).
After creating the `OrderFactory` contract, you can [implement the deployment scripts](deploy_script).
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_position: 5

## Overview

Let's create our main trading agent that implements the automated Uniswap trading logic.
Let's create our main **trading Agent** that implements the automated Uniswap trading logic.

Create the `BasicOrder` contract

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This tutorial will guide you through the process of building a **Basic Warden Ag

In the more advanced tutorial you will learn how to build a **Warden Agent** that executes orders based on AI predictions.

You can find the full sample code on [GitHub](https://github.com/warden-protocol/wardenprotocol/tree/main/solidity)
You can find the full sample code on [GitHub](https://github.com/warden-protocol/wardenprotocol/tree/main/solidity).

## Architecture

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ sidebar_position: 4

## Overview

Mock precompiles are essential for end-to-end testing of the Basic Agent.
**Mock precompiles** are essential for end-to-end testing of the Basic Agent. This article explains how to build and test two mock precompiles: Slinky and Warden.

This article explains how to build and test two mock precompiles: Slinky and Warden. Before you proceed, create a `/mock` directory for storing them.

:::note
- For the full code of mock precompiles, see GitHub: [/orders/mock](https://github.com/warden-protocol/wardenprotocol/tree/main/solidity/orders)
- You can test the precompiles in [/orders/test](https://github.com/warden-protocol/wardenprotocol/tree/main/solidity/orders/test)
:::note Structure
- Before you proceed, create a [`/mocks`](https://github.com/warden-protocol/wardenprotocol/blob/main/solidity/orders/mocks) directory for storing mock precompiles.
- You can test the precompiles in [/orders/test](https://github.com/warden-protocol/wardenprotocol/tree/main/solidity/orders/test).
:::


## 1. Create mock precompiles

### 1.1. Create a Slinky precompile
Expand Down Expand Up @@ -117,8 +114,9 @@ contract MockWardenPrecompile {
}
}
```
## 2. Test precompiles

## 2. Implement integration testing
### 2.1. Create a helper contract

Create a helper contract for testing mock precompiles:

Expand Down Expand Up @@ -155,72 +153,68 @@ contract PrecompileTestHelper {
}
```

## 3. Create test scenarios

### 3.1. Test the price feed

To create a scenario for testing the price feed, use the following code:

```solidity
contract SlinkyTest is PrecompileTestHelper {
function testPriceMovement() public {
// Set up a price scenario
setupPriceScenario("ETH", "USD", 3000e9, 3500e9);
// Test the order execution
Types.OrderData memory orderData = createTestOrder(
3200e9, // The threshold
Types.PriceCondition.GTE
);
BasicOrder order = new BasicOrder(
orderData,
new CommonTypes.Coin[](0),
address(this),
address(registry)
);
assertTrue(order.canExecute());
}
function testPriceFeedErrors() public {
vm.expectRevert("Price not set");
slinky.getPrice("UNKNOWN", "PAIR");
}
}
```

### 3.2. Test transaction signing

To create a scenario for testing transaction signing, use the following code:


```solidity
contract WardenTest is PrecompileTestHelper {
function testSigningFlow() public {
// Set up keys
warden.addKey(1, true); // A valid key
warden.addKey(2, false); // An invalid key
// Test successful signing
Types.SignRequestData memory goodRequest = createSignRequest(1);
assertTrue(executeOrder(goodRequest));
// Test failed signing
Types.SignRequestData memory badRequest = createSignRequest(2);
assertFalse(executeOrder(badRequest));
}
function testInvalidInputs() public {
vm.expectRevert("Empty approve expression");
Types.SignRequestData memory invalidRequest = createSignRequest(1);
invalidRequest.expectedApproveExpression = "";
executeOrder(invalidRequest);
}
}
```

## 4. Use precompiles in scripts
### 2.3. Create test scenarios

1. To create a scenario for testing the price feed, use code below:

```solidity
contract SlinkyTest is PrecompileTestHelper {
function testPriceMovement() public {
// Set up a price scenario
setupPriceScenario("ETH", "USD", 3000e9, 3500e9);
// Test the order execution
Types.OrderData memory orderData = createTestOrder(
3200e9, // The threshold
Types.PriceCondition.GTE
);
BasicOrder order = new BasicOrder(
orderData,
new CommonTypes.Coin[](0),
address(this),
address(registry)
);
assertTrue(order.canExecute());
}
function testPriceFeedErrors() public {
vm.expectRevert("Price not set");
slinky.getPrice("UNKNOWN", "PAIR");
}
}
```

2. To test transaction signing, use the following:


```solidity
contract WardenTest is PrecompileTestHelper {
function testSigningFlow() public {
// Set up keys
warden.addKey(1, true); // A valid key
warden.addKey(2, false); // An invalid key
// Test successful signing
Types.SignRequestData memory goodRequest = createSignRequest(1);
assertTrue(executeOrder(goodRequest));
// Test failed signing
Types.SignRequestData memory badRequest = createSignRequest(2);
assertFalse(executeOrder(badRequest));
}
function testInvalidInputs() public {
vm.expectRevert("Empty approve expression");
Types.SignRequestData memory invalidRequest = createSignRequest(1);
invalidRequest.expectedApproveExpression = "";
executeOrder(invalidRequest);
}
}
```

## 3. Use precompiles in scripts

This is how you can use precompiles in scripts:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ sidebar_position: 3

## Overview

The main part of the Basic Warden Agent is the trading Agent executing orders created by users.
The main part of the Basic Warden Agent is the **trading Agent** that executes orders. This article will guide you through building a foundation for the trading Agent: you'll implement the core data structures and interfaces.

This article will guide you through building a foundation for the trading Agent: you'll implement the core data structures and interfaces in the `/src` directory.
:::note Directory
Store the trading Agent structure in the [`/src`](https://github.com/warden-protocol/wardenprotocol/blob/main/solidity/orders/src) directory.
:::

## 1. Define trading types

Expand Down Expand Up @@ -88,7 +90,7 @@ You can find the full code on GitHub: [`/src/Registry.sol`](https://github.com/w

```solidity title="/src/Registry.sol"
contract Registry is ReentrancyGuard {
// Track creators of the order
// Track order creators
mapping(address executionAddress => address orderCreator) public executions;
// Store the transaction data
Expand Down

0 comments on commit 157a857

Please sign in to comment.