Skip to content

Commit

Permalink
Express Relay: directly submit Bids (#1747)
Browse files Browse the repository at this point in the history
* SDKs now directly submitting to bid endpoint

* bump versions

* address comments

* remove development configs

* fix lint

* Add mode + better error handling

* Check chain ids before running simple searcher

* Update constants and adjust a bit for mode

---------

Co-authored-by: Amin Moghaddam <amin@pyth.network>
  • Loading branch information
anihamde and m30m authored Jul 4, 2024
1 parent 07a7767 commit 7ef9aa2
Show file tree
Hide file tree
Showing 11 changed files with 463 additions and 266 deletions.
2 changes: 1 addition & 1 deletion express_relay/sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/express-relay-evm-js",
"version": "0.7.1",
"version": "0.8.0",
"description": "Utilities for interacting with the express relay protocol",
"homepage": "https://github.com/pyth-network/pyth-crosschain/tree/main/express_relay/sdk/js",
"author": "Douro Labs",
Expand Down
103 changes: 103 additions & 0 deletions express_relay/sdk/js/src/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
export const executeOpportunityAbi = {
type: "function",
name: "executeOpportunity",
inputs: [
{
name: "params",
type: "tuple",
internalType: "struct ExecutionParams",
components: [
{
name: "permit",
type: "tuple",
internalType: "struct ISignatureTransfer.PermitBatchTransferFrom",
components: [
{
name: "permitted",
type: "tuple[]",
internalType: "struct ISignatureTransfer.TokenPermissions[]",
components: [
{
name: "token",
type: "address",
internalType: "address",
},
{
name: "amount",
type: "uint256",
internalType: "uint256",
},
],
},
{
name: "nonce",
type: "uint256",
internalType: "uint256",
},
{
name: "deadline",
type: "uint256",
internalType: "uint256",
},
],
},
{
name: "witness",
type: "tuple",
internalType: "struct ExecutionWitness",
components: [
{
name: "buyTokens",
type: "tuple[]",
internalType: "struct TokenAmount[]",
components: [
{
name: "token",
type: "address",
internalType: "address",
},
{
name: "amount",
type: "uint256",
internalType: "uint256",
},
],
},
{
name: "executor",
type: "address",
internalType: "address",
},
{
name: "targetContract",
type: "address",
internalType: "address",
},
{
name: "targetCalldata",
type: "bytes",
internalType: "bytes",
},
{
name: "targetCallValue",
type: "uint256",
internalType: "uint256",
},
{
name: "bidAmount",
type: "uint256",
internalType: "uint256",
},
],
},
],
},
{
name: "signature",
type: "bytes",
internalType: "bytes",
},
],
outputs: [],
stateMutability: "payable",
};
23 changes: 23 additions & 0 deletions express_relay/sdk/js/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OpportunityAdapterConfig } from "./types";

export const OPPORTUNITY_ADAPTER_CONFIGS: Record<
string,
OpportunityAdapterConfig
> = {
op_sepolia: {
chain_id: 11155420,
opportunity_adapter_factory: "0xfA119693864b2F185742A409c66f04865c787754",
opportunity_adapter_init_bytecode_hash:
"0x3d71516d94b96a8fdca4e3a5825a6b41c9268a8e94610367e69a8462cc543533",
permit2: "0x000000000022D473030F116dDEE9F6B43aC78BA3",
weth: "0x74A4A85C611679B73F402B36c0F84A7D2CcdFDa3",
},
mode: {
chain_id: 34443,
opportunity_adapter_factory: "0x59F78DE21a0b05d96Ae00c547BA951a3B905602f",
opportunity_adapter_init_bytecode_hash:
"0xd53b8e32ab2ecba07c3e3a17c3c5e492c62e2f7051b89e5154f52e6bfeb0e38f",
permit2: "0x000000000022D473030F116dDEE9F6B43aC78BA3",
weth: "0x4200000000000000000000000000000000000006",
},
};
18 changes: 12 additions & 6 deletions express_relay/sdk/js/src/examples/simpleSearcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { checkHex, Client } from "../index";
import { privateKeyToAccount } from "viem/accounts";
import { isHex } from "viem";
import { BidStatusUpdate, Opportunity } from "../types";
import { OPPORTUNITY_ADAPTER_CONFIGS } from "../const";

const DAY_IN_SECONDS = 60 * 60 * 24;

Expand Down Expand Up @@ -47,25 +48,25 @@ class SimpleSearcher {
}

async opportunityHandler(opportunity: Opportunity) {
const bid = BigInt(argv.bid);
const bidAmount = BigInt(argv.bid);
// Bid info should be generated by evaluating the opportunity
// here for simplicity we are using a constant bid and 24 hours of validity
// TODO: generate nonce more intelligently, to reduce gas costs
const nonce = BigInt(Math.floor(Math.random() * 2 ** 50));
const bidParams = {
amount: bid,
amount: bidAmount,
nonce: nonce,
deadline: BigInt(Math.round(Date.now() / 1000 + DAY_IN_SECONDS)),
};
const opportunityBid = await this.client.signOpportunityBid(
const bid = await this.client.signBid(
opportunity,
bidParams,
checkHex(argv.privateKey)
);
try {
const bidId = await this.client.submitOpportunityBid(opportunityBid);
const bidId = await this.client.submitBid(bid);
console.log(
`Successful bid. Opportunity id ${opportunityBid.opportunityId} Bid id ${bidId}`
`Successful bid. Opportunity id ${opportunity.opportunityId} Bid id ${bidId}`
);
} catch (error) {
console.error(
Expand Down Expand Up @@ -102,7 +103,7 @@ const argv = yargs(hideBin(process.argv))
.option("bid", {
description: "Bid amount in wei",
type: "string",
default: "20000000000000000",
default: "10000000000000000",
})
.option("private-key", {
description:
Expand Down Expand Up @@ -132,6 +133,11 @@ async function run() {
argv.privateKey,
argv.apiKey
);
if (OPPORTUNITY_ADAPTER_CONFIGS[argv.chainId] === undefined) {
throw new Error(
`Opportunity adapter config not found for chain ${argv.chainId}`
);
}
await searcher.start();
}

Expand Down
Loading

0 comments on commit 7ef9aa2

Please sign in to comment.