The SimpleGateway system is a modular token exchange platform that allows users to create orders for token exchanges at specified rates. This guide covers the key components, functionality, and how to interact with the system.
The main contract that users interact with to:
- Create exchange orders
- Check order status
- Process or cancel orders
The "brain" of the system that:
- Calculates exchange rates
- Determines if orders should be fulfilled
- Executes token transfers
A test ERC20 token for development purposes.
- User creates an order with source token, target token, amount and expected rate
- Gateway holds the funds and assigns a unique order ID
- Processor calls the gateway to process the order
- Aggregator determines current rate and decides whether to fulfill
- Order is either fulfilled (tokens exchanged) or refunded (original tokens returned)
Users deposit tokens and specify their desired exchange parameters:
gateway.createOrder(
sourceTokenAddress, // Token you're sending
targetTokenAddress, // Token you want to receive
amount, // Amount of source tokens
expectedRate // Minimum exchange rate you'll accept
)Example:
// Creating an order to exchange 100 TokenA for TokenB at a rate of at least 1:0.5
const tx = await gateway.createOrder(
tokenA.address,
tokenB.address,
ethers.utils.parseEther("100"),
ethers.utils.parseEther("0.5")
);Users can check the status of their orders:
gateway.getOrder(orderId)Order Status:
0= Pending (awaiting processing)1= Fulfilled (exchange completed)2= Refunded (original tokens returned)
Orders can be cancelled if they're still pending:
gateway.cancelOrder(orderId)Authorized processors can initiate order processing:
gateway.processOrder(orderId)Admin can update which aggregator is used:
gateway.setAggregator(aggregatorAddress)The SimpleAggregator determines the exchange rate between tokens. For simplicity in this implementation:
- Rate checks compare the expected rate against the current rate
- Orders are fulfilled if the current rate is equal to or better than the expected rate
- Otherwise, orders are refunded
- User creates order with 100 TokenA for TokenB at 1:0.5 rate
- Current rate is 1:0.6 (better than requested)
- Order is fulfilled, user receives 60 TokenB
- User creates order with 100 TokenA for TokenB at 1:0.5 rate
- Current rate is 1:0.4 (worse than requested)
- Order is refunded, user receives their 100 TokenA back
For proper system setup, deploy in this order:
- Deploy MockToken (for testing)
- Deploy SimpleGateway
- Deploy SimpleAggregator with gateway address
- Set the aggregator in the gateway
// 1. Deploy contracts
const MockToken = await ethers.getContractFactory("MockToken");
const mockToken = await MockToken.deploy("TestToken", "TTK");
const SimpleGateway = await ethers.getContractFactory("SimpleGateway");
const gateway = await SimpleGateway.deploy();
const SimpleAggregator = await ethers.getContractFactory("SimpleAggregator");
const aggregator = await SimpleAggregator.deploy(gateway.address);
// 2. Set aggregator in gateway
await gateway.setAggregator(aggregator.address);
// 3. Mint tokens for testing
await mockToken.mint(userAddress, ethers.utils.parseEther("1000"));
// 4. Approve tokens for gateway
await mockToken.approve(gateway.address, ethers.utils.parseEther("100"));
// 5. Create an order
const tx = await gateway.createOrder(
mockToken.address,
mockToken.address,
ethers.utils.parseEther("100"),
ethers.utils.parseEther("1")
);
const receipt = await tx.wait();
// 6. Get order ID from event
const event = receipt.events.find(e => e.event === 'OrderCreated');
const orderId = event.args.orderId;
// 7. Process the order
await gateway.processOrder(orderId);
// 8. Check order status
const order = await gateway.getOrder(orderId);
console.log("Order status:", order.status);- Token Approval: Always approve only the exact amount needed
- Rate Checking: Verify expected rates carefully before creating orders
- Status Verification: Always check order status after processing
Issue: Transaction reverts when creating order Solution: Ensure you've approved enough tokens for the gateway
Issue: Order processing fails Solution: Check that the aggregator is properly set in the gateway
Issue: Unexpected exchange rate Solution: Verify the rate calculation in the aggregator contract
The SimpleGateway system provides a flexible way to create token exchange orders with rate protection. By separating order management from rate determination, it maintains a clean architecture that can be extended for more complex exchange scenarios.
For additional support or custom implementations, refer to the contract documentation or contact the development team. npm run deploy
yarn deploy
> [!IMPORTANT]
> This requires a secret key to make it work. Get your secret key [here](https://thirdweb.com/dashboard/settings/api-keys).
> Pass your secret key as a value after `-k` flag.
> ```bash
> npm run deploy -- -k <your-secret-key>
> # or
> yarn deploy -k <your-secret-key>
## Releasing Contracts
If you want to release a version of your contracts publicly, you can use one of the followings command:
```bash
npm run release
# or
yarn release