Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port web demo from suave-viem examples #34

Merged
merged 10 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
[submodule "lib/suave-std"]
path = lib/suave-std
url = https://github.com/flashbots/suave-std
[submodule "examples/web-demo/contracts/lib/suave-std"]
zeroXbrock marked this conversation as resolved.
Show resolved Hide resolved
path = examples/web-demo/contracts/lib/suave-std
url = https://github.com/flashbots/suave-std
27 changes: 27 additions & 0 deletions examples/web-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

# build artifact
deployedAddress.json
6 changes: 6 additions & 0 deletions examples/web-demo/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "contracts/lib/suave-std"]
path = contracts/lib/suave-std
url = https://github.com/flashbots/suave-std
18 changes: 18 additions & 0 deletions examples/web-demo/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "off"
},
"style": {
"noNonNullAssertion": "off"
}
}
}
}
Binary file added examples/web-demo/bun.lockb
Binary file not shown.
34 changes: 34 additions & 0 deletions examples/web-demo/contracts/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: workflow_dispatch

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions examples/web-demo/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
66 changes: 66 additions & 0 deletions examples/web-demo/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
6 changes: 6 additions & 0 deletions examples/web-demo/contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions examples/web-demo/contracts/lib/suave-std
Submodule suave-std added at e3ae5c
117 changes: 117 additions & 0 deletions examples/web-demo/contracts/src/OFA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.8;

import "suave-std/suavelib/Suave.sol";
import {Bundle} from "suave-std/protocols/Bundle.sol";
import "solady/src/utils/LibString.sol";

contract OFAPrivate {
// Struct to hold hint-related information for an order.
struct HintOrder {
Suave.DataId id;
bytes hint;
}

event HintEvent(Suave.DataId id, bytes hint);

event BundleEmitted(string bundleRawResponse);

event Debug(string message, bytes data);

// Internal function to save order details and generate a hint.
function saveOrder(uint64 decryptionCondition) internal returns (HintOrder memory) {
// Retrieve the bundle data from the confidential inputs
bytes memory bundleData = Suave.confidentialInputs();
Bundle.BundleObj memory bundle = abi.decode(bundleData, (Bundle.BundleObj));
bundleData = Bundle.encodeBundle(bundle).body;

// Simulate the bundle and extract its score.
uint64 egp = Suave.simulateBundle(bundleData);

// Extract a hint about this bundle that is going to be leaked
// to external applications.
bytes memory extractHintPayload = abi.encodePacked('{"revertingHashes": [], "txs": [');
for (uint256 i = 0; i < bundle.txns.length; i++) {
extractHintPayload = abi.encodePacked(extractHintPayload, '"', LibString.toHexString(bundle.txns[i]), '"');
if (i < bundle.txns.length - 1) {
extractHintPayload = abi.encodePacked(extractHintPayload, ",");
}
}
extractHintPayload = abi.encodePacked(extractHintPayload, "]}");
bytes memory hint = Suave.extractHint(extractHintPayload);

address[] memory allowedList = new address[](2);
allowedList[0] = address(this);
allowedList[1] = 0x0000000000000000000000000000000043200001;

// Store the bundle and the simulation results in the confidential datastore.
Suave.DataRecord memory dataRecord = Suave.newDataRecord(decryptionCondition, allowedList, allowedList, "");
Suave.confidentialStore(dataRecord.id, "mevshare:v0:ethBundles", bundleData);
Suave.confidentialStore(dataRecord.id, "mevshare:v0:ethBundleSimResults", abi.encode(egp));

HintOrder memory hintOrder;
hintOrder.id = dataRecord.id;
hintOrder.hint = hint;

return hintOrder;
}

function emitHint(HintOrder memory order) public payable {
emit HintEvent(order.id, order.hint);
}

// Function to create a new user order
function newOrder(uint64 decryptionCondition) external payable returns (bytes memory) {
HintOrder memory hintOrder = saveOrder(decryptionCondition);
return abi.encodeWithSelector(this.emitHint.selector, hintOrder);
}

// Function to match and backrun another dataRecord.
function newMatch(Suave.DataId shareDataRecordId, uint64 decryptionCondition)
external
payable
returns (bytes memory)
{
HintOrder memory hintOrder = saveOrder(decryptionCondition);

// Merge the dataRecords and store them in the confidential datastore.
// The 'fillMevShareBundle' precompile will use this information to send the bundles.
Suave.DataId[] memory dataRecords = new Suave.DataId[](2);
dataRecords[0] = shareDataRecordId;
dataRecords[1] = hintOrder.id;
Suave.confidentialStore(hintOrder.id, "mevshare:v0:mergedDataRecords", abi.encode(dataRecords));

return abi.encodeWithSelector(this.emitHint.selector, hintOrder);
}

function emitMatchDataRecordAndHintCallback(string memory bundleRawResponse) external payable {
emit BundleEmitted(bundleRawResponse);
}

function emitMatchDataRecordAndHint(string memory builderUrl, Suave.DataId dataRecordId)
external
payable
returns (bytes memory)
{
bytes memory bundleData = Suave.fillMevShareBundle(dataRecordId);
bytes memory response = submitBundle(builderUrl, bundleData);

return abi.encodeWithSelector(this.emitMatchDataRecordAndHintCallback.selector, response);
}

function submitBundle(string memory builderUrl, bytes memory bundleData) internal returns (bytes memory) {
// encode the jsonrpc request in JSON format.
bytes memory body =
abi.encodePacked('{"jsonrpc":"2.0","method":"mev_sendBundle","params":[', bundleData, '],"id":1}');

Suave.HttpRequest memory request;
request.url = builderUrl;
request.method = "POST";
request.body = body;
request.headers = new string[](1);
request.headers[0] = "Content-Type: application/json";
request.withFlashbotsSignature = true;

return Suave.doHTTPRequest(request);
}
}
10 changes: 10 additions & 0 deletions examples/web-demo/deployContracts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

FUNDED_PRV_KEY=0x91ab9a7e53c220e6210460b65a7a3bb2ca181412a8a7b43ff336b3df1737ce12
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd $SCRIPT_DIR/contracts
forge build
createOutput=$(forge create --private-key $FUNDED_PRV_KEY --chain-id 16813125 -r http://localhost:8545 src/OFA.sol:OFAPrivate)
deployedAddress=$(echo "$createOutput" | grep 'Deployed to:' | awk '{print $3}')
echo "Deployed to: $deployedAddress"
echo '{"address": ''"'$deployedAddress'"}' > $SCRIPT_DIR/deployedAddress.json
14 changes: 14 additions & 0 deletions examples/web-demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/flashbots_icon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SUAVE Web Demo</title>
</head>
<body>
<div id="app"></div>
<div id="footer"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions examples/web-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "web-demo",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"@biomejs/biome": "1.5.3",
"typescript": "^5.2.2",
"vite": "^5.1.0"
},
"dependencies": {
"@flashbots/suave-viem": "main"
}
}
1 change: 1 addition & 0 deletions examples/web-demo/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading