Skip to content

Commit

Permalink
solana: auction participant example processes (#87)
Browse files Browse the repository at this point in the history
Co-authored-by: gator-boi <[email protected]>
Co-authored-by: A5 Pickle <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 05e36a8 commit ed261b1
Show file tree
Hide file tree
Showing 38 changed files with 2,622 additions and 287 deletions.
12 changes: 6 additions & 6 deletions evm/env/testnet/arbitrum_sepolia.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export RELEASE_OWNER_ASSISTANT_ADDRESS=0x


### Token Router Proxy (evm address)
export RELEASE_TOKEN_ROUTER_ADDRESS=0x
export RELEASE_TOKEN_ROUTER_ADDRESS=0xc1Cf3501ef0b26c8A47759F738832563C7cB014A


############################### Matching Engine ###############################


### Matching Engine Proxy (universal evm address)
export RELEASE_MATCHING_ENGINE_CHAIN=6
export RELEASE_MATCHING_ENGINE_ADDRESS=0x
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x
export RELEASE_MATCHING_ENGINE_DOMAIN=1
export RELEASE_MATCHING_ENGINE_CHAIN=1
export RELEASE_MATCHING_ENGINE_ADDRESS=0x3e374fcd3aaf2ed067f3c93d21416855ec7916cfd2c2127bcbc68b3b1fb73077
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x58b82fca98f022ca29cfe822d1a3f92930020970910f0a99a0d9b995b21f99d5
export RELEASE_MATCHING_ENGINE_DOMAIN=5


############################## Test Params ###############################
Expand All @@ -57,6 +57,6 @@ export TEST_AMOUNT_IN=500000000
export TEST_TARGET_CHAIN=
export TEST_REDEEMER=
export TEST_IS_FAST=true
export TEST_BASE_AUCTION_PRICE=
export TEST_FEE=
export TEST_DEADLINE=0

17 changes: 8 additions & 9 deletions evm/env/testnet/avalanche.env
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ export RELEASE_FEE_RECIPIENT_ADDRESS=0x
############################### Token Router #################################

### Token Router Proxy (evm address)
export RELEASE_TOKEN_ROUTER_ADDRESS=0x
export RELEASE_TOKEN_ROUTER_ADDRESS=0x7353B29FDc79435dcC7ECc9Ac9F9b61d83B4E0F4


############################### Matching Engine ###############################

### Matching Engine Proxy (universal evm address)
export RELEASE_MATCHING_ENGINE_CHAIN=6
export RELEASE_MATCHING_ENGINE_ADDRESS=0x
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x
export RELEASE_MATCHING_ENGINE_DOMAIN=1
export RELEASE_MATCHING_ENGINE_CHAIN=1
export RELEASE_MATCHING_ENGINE_ADDRESS=0x3e374fcd3aaf2ed067f3c93d21416855ec7916cfd2c2127bcbc68b3b1fb73077
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x58b82fca98f022ca29cfe822d1a3f92930020970910f0a99a0d9b995b21f99d5
export RELEASE_MATCHING_ENGINE_DOMAIN=5

### Auction Parameters
export RELEASE_USER_REWARD_BPS=250000
Expand All @@ -62,11 +62,10 @@ export RELEASE_PENALTY_BLOCKS=20

############################## Test Params ###############################


export TEST_AMOUNT_IN=500000000
export TEST_TARGET_CHAIN=
export TEST_AMOUNT_IN=10000000
export TEST_TARGET_CHAIN=2
export TEST_REDEEMER=
export TEST_IS_FAST=true
export TEST_BASE_AUCTION_PRICE=
export TEST_FEE=2000000
export TEST_DEADLINE=0

12 changes: 6 additions & 6 deletions evm/env/testnet/base_sepolia.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export RELEASE_OWNER_ASSISTANT_ADDRESS=0x


### Token Router Proxy (evm address)
export RELEASE_TOKEN_ROUTER_ADDRESS=0x
export RELEASE_TOKEN_ROUTER_ADDRESS=0x4452B708C01d6aD7058a7541A3A82f0aD0A1abB1


############################### Matching Engine ###############################


### Matching Engine Proxy (universal evm address)
export RELEASE_MATCHING_ENGINE_CHAIN=6
export RELEASE_MATCHING_ENGINE_ADDRESS=0x
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x
export RELEASE_MATCHING_ENGINE_DOMAIN=1
export RELEASE_MATCHING_ENGINE_CHAIN=1
export RELEASE_MATCHING_ENGINE_ADDRESS=0x3e374fcd3aaf2ed067f3c93d21416855ec7916cfd2c2127bcbc68b3b1fb73077
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x58b82fca98f022ca29cfe822d1a3f92930020970910f0a99a0d9b995b21f99d5
export RELEASE_MATCHING_ENGINE_DOMAIN=5


############################## Test Params ###############################
Expand All @@ -57,5 +57,5 @@ export TEST_AMOUNT_IN=500000000
export TEST_TARGET_CHAIN=
export TEST_REDEEMER=
export TEST_IS_FAST=true
export TEST_BASE_AUCTION_PRICE=
export TEST_FEE=
export TEST_DEADLINE=0
12 changes: 6 additions & 6 deletions evm/env/testnet/optimism_sepolia.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export RELEASE_OWNER_ASSISTANT_ADDRESS=0x


### Token Router Proxy (evm address)
export RELEASE_TOKEN_ROUTER_ADDRESS=0x
export RELEASE_TOKEN_ROUTER_ADDRESS=0xc1Cf3501ef0b26c8A47759F738832563C7cB014A


############################### Matching Engine ###############################


### Matching Engine Proxy (universal evm address)
export RELEASE_MATCHING_ENGINE_CHAIN=6
export RELEASE_MATCHING_ENGINE_ADDRESS=0x
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x
export RELEASE_MATCHING_ENGINE_DOMAIN=1
export RELEASE_MATCHING_ENGINE_CHAIN=1
export RELEASE_MATCHING_ENGINE_ADDRESS=0x3e374fcd3aaf2ed067f3c93d21416855ec7916cfd2c2127bcbc68b3b1fb73077
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x58b82fca98f022ca29cfe822d1a3f92930020970910f0a99a0d9b995b21f99d5
export RELEASE_MATCHING_ENGINE_DOMAIN=5


############################## Test Params ###############################
Expand All @@ -57,5 +57,5 @@ export TEST_AMOUNT_IN=500000000
export TEST_TARGET_CHAIN=
export TEST_REDEEMER=
export TEST_IS_FAST=true
export TEST_BASE_AUCTION_PRICE=
export TEST_FEE=
export TEST_DEADLINE=0
12 changes: 6 additions & 6 deletions evm/env/testnet/polygon.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export RELEASE_OWNER_ASSISTANT_ADDRESS=0x


### Token Router Proxy (evm address)
export RELEASE_TOKEN_ROUTER_ADDRESS=0x
export RELEASE_TOKEN_ROUTER_ADDRESS=0x3Ce8a3aC230Eb4bCE3688f2A1ab21d986a0A0B06


############################### Matching Engine ###############################


### Matching Engine Proxy (universal evm address)
export RELEASE_MATCHING_ENGINE_CHAIN=6
export RELEASE_MATCHING_ENGINE_ADDRESS=0x
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x
export RELEASE_MATCHING_ENGINE_DOMAIN=1
export RELEASE_MATCHING_ENGINE_CHAIN=1
export RELEASE_MATCHING_ENGINE_ADDRESS=0x3e374fcd3aaf2ed067f3c93d21416855ec7916cfd2c2127bcbc68b3b1fb73077
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x58b82fca98f022ca29cfe822d1a3f92930020970910f0a99a0d9b995b21f99d5
export RELEASE_MATCHING_ENGINE_DOMAIN=5


############################## Test Params ###############################
Expand All @@ -57,5 +57,5 @@ export TEST_AMOUNT_IN=500000000
export TEST_TARGET_CHAIN=
export TEST_REDEEMER=
export TEST_IS_FAST=true
export TEST_BASE_AUCTION_PRICE=
export TEST_FEE=
export TEST_DEADLINE=0
12 changes: 6 additions & 6 deletions evm/env/testnet/sepolia.env
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export RELEASE_OWNER_ASSISTANT_ADDRESS=0x


### Token Router Proxy (evm address)
export RELEASE_TOKEN_ROUTER_ADDRESS=0x
export RELEASE_TOKEN_ROUTER_ADDRESS=0x603541d1Cf7178C407aA7369b67CB7e0274952e2


############################### Matching Engine ###############################


### Matching Engine Proxy (universal evm address)
export RELEASE_MATCHING_ENGINE_CHAIN=6
export RELEASE_MATCHING_ENGINE_ADDRESS=0x
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x
export RELEASE_MATCHING_ENGINE_DOMAIN=1
export RELEASE_MATCHING_ENGINE_CHAIN=1
export RELEASE_MATCHING_ENGINE_ADDRESS=0x3e374fcd3aaf2ed067f3c93d21416855ec7916cfd2c2127bcbc68b3b1fb73077
export RELEASE_MATCHING_ENGINE_MINT_RECIPIENT=0x58b82fca98f022ca29cfe822d1a3f92930020970910f0a99a0d9b995b21f99d5
export RELEASE_MATCHING_ENGINE_DOMAIN=5


############################## Test Params ###############################
Expand All @@ -58,5 +58,5 @@ export TEST_AMOUNT_IN=500000000
export TEST_TARGET_CHAIN=
export TEST_REDEEMER=
export TEST_IS_FAST=true
export TEST_BASE_AUCTION_PRICE=
export TEST_FEE=
export TEST_DEADLINE=0
6 changes: 3 additions & 3 deletions evm/forge/scripts/TestTransfer.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ contract TestTransfer is Script {
address immutable _router = vm.envAddress("RELEASE_TOKEN_ROUTER_ADDRESS");

// Transfer params.
uint256 _amountIn = vm.envUint("TEST_AMOUNT_IN");
uint64 _amountIn = uint64(vm.envUint("TEST_AMOUNT_IN"));
uint16 _targetChain = uint16(vm.envUint("TEST_TARGET_CHAIN"));
bytes32 _redeemer = vm.envBytes32("TEST_REDEEMER");
bool isFast = vm.envBool("TEST_IS_FAST");
bytes _redeemerMessage = hex"deadbeef";
uint128 _baseAuctionPrice = uint128(vm.envUint("TEST_BASE_AUCTION_PRICE"));
uint64 _maxFee = uint64(vm.envUint("TEST_FEE"));
uint32 _deadline = uint32(vm.envUint("TEST_DEADLINE"));

function transfer() public {
SafeERC20.safeIncreaseAllowance(IERC20(_token), _router, _amountIn);
if (isFast) {
ITokenRouter(_router).placeFastMarketOrder(
_amountIn, _targetChain, _redeemer, _redeemerMessage, _baseAuctionPrice, _deadline
_amountIn, _targetChain, _redeemer, _redeemerMessage, _maxFee, _deadline
);
} else {
ITokenRouter(_router).placeMarketOrder(
Expand Down
1 change: 0 additions & 1 deletion evm/forge/scripts/UpgradeTokenRouter.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ contract UpgradeTokenRouter is CheckWormholeContracts, Script {
address immutable _token = vm.envAddress("RELEASE_TOKEN_ADDRESS");
address immutable _wormhole = vm.envAddress("RELEASE_WORMHOLE_ADDRESS");
address immutable _cctpTokenMessenger = vm.envAddress("RELEASE_TOKEN_MESSENGER_ADDRESS");
address immutable _ownerAssistantAddress = vm.envAddress("RELEASE_OWNER_ASSISTANT_ADDRESS");
bytes32 immutable _matchingEngineAddress = vm.envBytes32("RELEASE_MATCHING_ENGINE_ADDRESS");
bytes32 immutable _matchingEngineMintRecipient =
vm.envBytes32("RELEASE_MATCHING_ENGINE_MINT_RECIPIENT");
Expand Down
2 changes: 2 additions & 0 deletions solana/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.vscode
**/*.rs.bk
/artifacts-*
/cfg/**/*.json
!/cfg/**/sample.*.json
/node_modules
/target/*
!/target/idl
Expand Down
129 changes: 76 additions & 53 deletions solana/README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,114 @@
# Example Token Router on Solana
# Example Liquidity Layer on Solana

## Dependencies
## Assets

- Matching Engine
- Token Router
- Upgrade Manager

> **Warning**
> Only Solana versions >= 1.14.14 and < 1.15 are supported.
## Dependencies

First, you will need `cargo` and `anchor` CLI tools. If you need these tools,
please visit the [Anchor book] for more details.
- cargo -- Get started using [rustup](https://rustup.rs/)
- npm -- Get started using [nvm](https://github.com/nvm-sh/nvm?tab=readme-ov-file#install--update-script)
- solana -- Install the latest version [here](https://docs.solanalabs.com/cli/install#use-solanas-install-tool)
- anchor -- Install [avm](https://book.anchor-lang.com/getting_started/installation.html#anchor)

## Build

Once you have the above CLI tools, you can build the programs by simply running:
Currently there are two features that represent target Solana
networks:

- `NETWORK=testnet make`
- localnet (Solana Test Validator)
- testnet (Solana devnet)

This will also install this subdirectory's dependencies, such as
`node_modules` and the Wormhole programs from the `solana` directory of the
[Wormhole repo]. This will also create a keypair for the program in the
`target/deploy/`. This keypair can be used for devnet, testnet and mainnet. Do not delete this
key after deploying to the network of your choice.
Make sure the program pubkeys for whichever network you plan on deploying for is set as the correct
const values found in [this lib.rs file](modules/common/src/lib.rs).

## Tests
So for testnet, you would specify the NETWORK env and execute `make build`. For example:

To run both unit and integration tests, run `make test`. If you want to isolate
your testing, use either of these commands:
```sh
NETWORK=testnet make build
```

- `make unit-test` - Runs `cargo clippy` and `cargo test`
- `make integration-test` - Spawns a solana local validator and uses `ts-mocha`
with `@solana/web3.js` to interact with the example programs.
This will create an artifacts directory (in the example above, _artifacts_testnet_).

## Deployment
## Tests

First, generate a program public key by running the following command:
To run both unit and integration tests, run `make test`.

- `solana-keygen pubkey target/deploy/token_bridge_relayer-keypair.json`
## Deployment

Add your program's public key to the following file:
First [build](#build) for a specific network.

- `programs/src/lib.rs`
Following the same example in the build section, assuming your keypair reflects the Upgrade
Manager's pubkey `ucdP9ktgrXgEUnn6roqD2SfdGMR2JSiWHUKv23oXwxt`, you would deploy a new program by
running the following command using the Solana CLI:

Then, build based on the target network. The deployment options are `devnet`, `testnet` and `mainnet`. We will use `testnet` as an example for this README.
```sh
solana program deploy -u d --program-id ucdP9ktgrXgEUnn6roqD2SfdGMR2JSiWHUKv23oXwxt artifacts-testnet/upgrade_manager.so
```

- `NETWORK=testnet make build`
## Managing Upgrades

Next, we will need to create some keypairs for the deployment. The keypair that is used to deploy the program will become the `owner` of the program. Optionally, you can create a new keypair for the `assistant` and the `fee_recipient`, however, the same keypair can be used for all three. Create the keypair(s) in a location of your choice by running:
TODO

- `solana-keygen new -o path/to/keypair.json`
## Testnet Example Solver

Then set the `FEE_RECIPIENT`, `ASSISTANT` and `TOKEN_ROUTER_PID` in the `env/tesnet.env` file. This env file will be used for your deployment, as well as setting up the program.
The example solver is split up into three processes: `vaaAuctionRelayer`, `improveOffer` and
`executeOrder`. All three rely on the same configuration file, which can created by copying the
sample file `cfg/testnet/sample.config.json`.

Finally, deploy the program (from the `solana`) directory with the following command:
To get started, create a durable nonce account (see these
[instructions](https://solana.com/developers/guides/advanced/introduction-to-durable-nonces)) with
your Solana keypair. Copy the public key and move it to the `nonceAccount` field in your config.

```
solana program deploy target/deploy/token_bridge_relayer.so \
--program-id target/deploy/token_bridge_relayer-keypair.json \
--commitment confirmed \
-u your_testnet_rpc \
-k your_deployment_keypair.json`
```
**NOTE: We encourage using a durable nonce to avoid an expired blockhash error in case there is
network congestion. We demonstrate how to use this nonce account in the `vaaAuctionRelayer`
process.**

## Program Setup
Next, you'll need an RPC for each network that you wish to relay `FastMarketOrders` from. Add each
RPC to the config for the corresponding chain name.

### Step 1: Env File
Finally, you will need a funded USDC Associated Token Account (ATA), whose owner is your keypair.

You should still have your environment file from the [deployment](#deployment) section of this README. However (if you deleted it) create a new one and set the `FEE_RECIPIENT`, `ASSISTANT` and `TOKEN_ROUTER_PID` environment variables.
### Vaa Auction Relayer

### Step 2: Setup Configuration File
The `vaaAuctionRelayer` listens for `FastMarketOrder` VAAs emitted by the Liquidity Layer's network
of contracts. It determines if the `maxFee` encoded in the `FastMarketOrder` VAA is high enough to
participate in an auction, if it is, it executes a `place_initial_offer` instruction on the Solana
`MatchingEngine`. If any known token accounts are the highest bidder at the end of an auction, this
process will settle the auction by executing the `settle_auction_complete` instruction and posting
the finalized VAA associated with the auction's `FastMarketOrder` VAA.

Depending on your target network, there should be an example config file in the `cfg` directory. Open your file of choice and configure it to your liking. DO NOT change the name of this file.
To run the `vaaAuctionRelayer` execute the following command:

### Step 3: Initialize the program
```sh
npx ts-node ts/auction-participant/vaaAuctionRelayer/app.ts path/to/config/your.config.json
```

Run the following command to initialize the program. Make sure to supply the keypair that was used to deploy the program:
### Improve Offers

- `source env/testnet.env && yarn initialize -k your_deployment_keypair.json`
The `improveOffer` process listens for `AuctionUpdated` events on the `MatchingEngine` via
websocket. Once an auction has been initiated, this process will determine if it is willing to
improve the offer based on the `pricing` parameters in your config.

### Step 4: Register Foreign Contracts
To run the `improveOffer` script, execute the following command:

- `source env/testnet.env && yarn register-contracts -k your_deployment_keypair.json -n testnet`
```sh
npx ts-node ts/auction-participant/improveOffer/app.ts path/to/config/your.config.json
```

### Step 5: Register Tokens (Sets Swap Rate and Max Swap Amount)
### Execute Fast Orders

- `source env/testnet.env && yarn register-tokens -k your_deployment_keypair.json -n testnet`
The `executeOrder` process listens for `AuctionUpdated` events on the `MatchingEngine` via
websocket. At the end of an auction's duration (see `endSlot` of the `AuctionUpdated` event), this
process will execute the order reflecting this auction within the auction's grace period.

### Step 6: Set Relayer Fees
**NOTE: You will need an address lookup table for the execute order instructions because these
instructions require so many accounts. This LUT address can be added to your config.**

- `source env/testnet.env && yarn set-relayer-fees -k your_deployment_keypair.json -n testnet`
To run the `executeOrder` script, execute the following command:

[anchor book]: https://book.anchor-lang.com/getting_started/installation.html
[wormhole repo]: https://github.com/wormhole-foundation/wormhole/tree/dev.v2/solana
```sh
npx ts-node ts/auction-participant/executeOrder/app.ts path/to/config/your.config.json
```
Loading

0 comments on commit ed261b1

Please sign in to comment.