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

feat(express-relay): Add permit2 #1693

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion express_relay/sdk/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.6.0",
"version": "0.7.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
1 change: 1 addition & 0 deletions express_relay/sdk/js/src/examples/simpleSearcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SimpleSearcher {
// here for simplicity we are using a constant bid and 24 hours of validity
const bidParams = {
amount: bid,
nonce: BigInt(Math.floor(Math.random() * 2 ** 50)),
validUntil: BigInt(Math.round(Date.now() / 1000 + DAY_IN_SECONDS)),
};
const opportunityBid = await this.client.signOpportunityBid(
Expand Down
93 changes: 78 additions & 15 deletions express_relay/sdk/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ export function checkTokenQty(token: {
};
}

class ChainConfig {
wethAddress: Address;
opportunityAdapterAddress: Address;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the chainId not also fit in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we are not using it in the sdk.

constructor(wethAddress: Address, opportunityAdapterAddress: Address) {
this.wethAddress = wethAddress;
this.opportunityAdapterAddress = opportunityAdapterAddress;
}
}

const CHAIN_CONFIG: Record<string, ChainConfig> = {
op_sepolia: new ChainConfig("0x0", "0x0"),
}; // TODO: Fill in the correct addresses

export class Client {
public clientOptions: ClientOptions;
public wsOptions: WsOptions;
Expand Down Expand Up @@ -149,10 +162,12 @@ export class Client {
eip712Domain: components["schemas"]["EIP712Domain"]
): EIP712Domain {
return {
name: eip712Domain.name,
version: eip712Domain.version,
name: eip712Domain.name ?? undefined,
version: eip712Domain.version ?? undefined,
verifyingContract: checkAddress(eip712Domain.verifying_contract),
chainId: BigInt(eip712Domain.chain_id),
chainId: eip712Domain.chain_id
? BigInt(eip712Domain.chain_id)
: undefined,
};
}

Expand Down Expand Up @@ -303,6 +318,35 @@ export class Client {
}
}

getPermittedTokens(
sellTokens: TokenAmount[],
bidAmount: bigint,
value: bigint,
wethAddress: Address
) {
const permittedTokens = sellTokens.map((token) => ({
...token,
}));
const extraWethAmount = bidAmount + value;
if (extraWethAmount <= 0) {
return permittedTokens;
}

const wethIndex = permittedTokens.findIndex(
(token) => token.token === wethAddress
);
if (wethIndex >= 0) {
permittedTokens[wethIndex].amount =
permittedTokens[wethIndex].amount + extraWethAmount;
} else {
permittedTokens.push({
token: wethAddress,
amount: extraWethAmount,
});
}
return permittedTokens;
}

/**
* Creates a signed bid for an opportunity
* @param opportunity Opportunity to bid on
Expand All @@ -315,20 +359,29 @@ export class Client {
privateKey: Hex
): Promise<OpportunityBid> {
const types = {
ExecutionParams: [
{ name: "sellTokens", type: "TokenAmount[]" },
PermitBatchWitnessTransferFrom: [
{ name: "permitted", type: "TokenPermissions[]" },
{ name: "spender", type: "address" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
{ name: "witness", type: "OpportunityWitness" },
],
OpportunityWitness: [
{ name: "buyTokens", type: "TokenAmount[]" },
{ name: "executor", type: "address" },
{ name: "targetContract", type: "address" },
{ name: "targetCalldata", type: "bytes" },
{ name: "targetCallValue", type: "uint256" },
{ name: "validUntil", type: "uint256" },
{ name: "bidAmount", type: "uint256" },
],
TokenAmount: [
{ name: "token", type: "address" },
{ name: "amount", type: "uint256" },
],
TokenPermissions: [
{ name: "token", type: "address" },
{ name: "amount", type: "uint256" },
],
};

const account = privateKeyToAccount(privateKey);
Expand All @@ -339,16 +392,25 @@ export class Client {
chainId: Number(opportunity.eip712Domain.chainId),
},
types,
primaryType: "ExecutionParams",
primaryType: "PermitBatchWitnessTransferFrom",
message: {
sellTokens: opportunity.sellTokens,
buyTokens: opportunity.buyTokens,
executor: account.address,
targetContract: opportunity.targetContract,
targetCalldata: opportunity.targetCalldata,
targetCallValue: opportunity.targetCallValue,
validUntil: bidParams.validUntil,
bidAmount: bidParams.amount,
permitted: this.getPermittedTokens(
opportunity.sellTokens,
bidParams.amount,
opportunity.targetCallValue,
CHAIN_CONFIG[opportunity.chainId].wethAddress
),
spender: CHAIN_CONFIG[opportunity.chainId].opportunityAdapterAddress,
nonce: bidParams.nonce,
deadline: bidParams.validUntil,
witness: {
buyTokens: opportunity.buyTokens,
executor: account.address,
targetContract: opportunity.targetContract,
targetCalldata: opportunity.targetCalldata,
targetCallValue: opportunity.targetCallValue,
bidAmount: bidParams.amount,
},
},
});

Expand All @@ -366,6 +428,7 @@ export class Client {
): components["schemas"]["OpportunityBid"] {
return {
amount: bid.bid.amount.toString(),
nonce: bid.bid.nonce.toString(),
executor: bid.executor,
permission_key: bid.permissionKey,
signature: bid.signature,
Expand Down
11 changes: 8 additions & 3 deletions express_relay/sdk/js/src/serverTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ export interface components {
* @description The network chain id parameter for EIP712 domain.
* @example 31337
*/
chain_id: string;
chain_id?: string | null;
/**
* @description The name parameter for the EIP712 domain.
* @example OpportunityAdapter
*/
name: string;
name?: string | null;
/**
* @description The verifying contract address parameter for the EIP712 domain.
* @example 0xcA11bde05977b3631167028862bE2a173976CA11
Expand All @@ -179,7 +179,7 @@ export interface components {
* @description The version parameter for the EIP712 domain.
* @example 1
*/
version: string;
version?: string | null;
};
ErrorBodyResponse: {
error: string;
Expand All @@ -195,6 +195,11 @@ export interface components {
* @example 0x5FbDB2315678afecb367f032d93F642f64180aa2
*/
executor: string;
/**
* @description The nonce of the bid permit signature
* @example 123
*/
nonce: string;
/**
* @description The opportunity permission key
* @example 0xdeadbeefcafe
Expand Down
10 changes: 7 additions & 3 deletions express_relay/sdk/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export type BidParams = {
* Bid amount in wei
*/
amount: bigint;
/**
* The nonce used to sign the bid
*/
nonce: bigint;
/**
* Unix timestamp for when the bid is no longer valid in seconds
*/
Expand All @@ -30,19 +34,19 @@ export type EIP712Domain = {
/**
* The network chain id for the EIP712 domain.
*/
chainId: bigint;
chainId?: bigint;
/**
* The verifying contract address for the EIP712 domain.
*/
verifyingContract: Address;
/**
* The name parameter for the EIP712 domain.
*/
name: string;
name?: string;
/**
* The version parameter for the EIP712 domain.
*/
version: string;
version?: string;
};
/**
* Represents a valid opportunity ready to be executed
Expand Down
Loading
Loading