-
Notifications
You must be signed in to change notification settings - Fork 192
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
Permit2 sdk #1724
Merged
Merged
Permit2 sdk #1724
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9c73691
python sdk
anihamde 1ea65b9
add todo note
anihamde f64a534
up the version python
anihamde cf2b9ac
js sdk
anihamde 87090c8
bump version number js
anihamde b209835
get rid of dist files from git
anihamde a382445
add nonce TODO to js
anihamde 048d056
revert sdks to static OAConfigs
anihamde 05d4301
add lock file
anihamde 7f6c8f4
Merge branch 'main' into permit2_sdk
anihamde 25f18fd
try fix
anihamde e201d2c
fixes
anihamde 91ba624
use viem getContractAddress
anihamde 79a9927
Update addresses and address comments
m30m a6d2393
Fix lint issues
m30m e15d34e
Update addresses
m30m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,14 @@ import type { components, paths } from "./serverTypes"; | |
import createClient, { | ||
ClientOptions as FetchClientOptions, | ||
} from "openapi-fetch"; | ||
import { Address, Hex, isAddress, isHex } from "viem"; | ||
import { | ||
Address, | ||
Hex, | ||
isAddress, | ||
isHex, | ||
keccak256, | ||
getContractAddress, | ||
} from "viem"; | ||
import { privateKeyToAccount, signTypedData } from "viem/accounts"; | ||
import WebSocket from "isomorphic-ws"; | ||
import { | ||
|
@@ -11,11 +18,12 @@ import { | |
BidParams, | ||
BidStatusUpdate, | ||
Opportunity, | ||
EIP712Domain, | ||
OpportunityAdapterConfig, | ||
OpportunityBid, | ||
OpportunityParams, | ||
TokenAmount, | ||
BidsResponse, | ||
TokenPermissions, | ||
} from "./types"; | ||
|
||
export * from "./types"; | ||
|
@@ -59,6 +67,55 @@ export function checkTokenQty(token: { | |
}; | ||
} | ||
|
||
// TODO: update, remove "development" and replace with real chains | ||
export const OPPORTUNITY_ADAPTER_CONFIGS: Record< | ||
string, | ||
OpportunityAdapterConfig | ||
> = { | ||
development: { | ||
chain_id: 31337, | ||
opportunity_adapter_factory: "0x610178da211fef7d417bc0e6fed39f05609ad788", | ||
opportunity_adapter_init_bytecode_hash: | ||
"0xfd1080f6c2d71672806f31108cb2f7d7709878e613b8d6bf028482184dcd70a4", | ||
permit2: "0x8a791620dd6260079bf849dc5567adc3f2fdc318", | ||
weth: "0x5fc8d32690cc91d4c39d9d3abcbd16989f875707", | ||
}, | ||
}; | ||
|
||
/** | ||
* Converts sellTokens, bidAmount, and callValue to permitted tokens | ||
* @param tokens List of sellTokens | ||
* @param bidAmount | ||
* @param callValue | ||
* @param weth | ||
* @returns List of permitted tokens | ||
*/ | ||
function getPermittedTokens( | ||
tokens: TokenAmount[], | ||
bidAmount: bigint, | ||
callValue: bigint, | ||
weth: Address | ||
): TokenPermissions[] { | ||
const permitted: TokenPermissions[] = []; | ||
|
||
for (let i = 0; i < tokens.length; i++) { | ||
permitted.push({ token: tokens[i].token, amount: tokens[i].amount }); | ||
} | ||
|
||
for (let i = 0; i < permitted.length; i++) { | ||
if (permitted[i].token === weth) { | ||
permitted[i].amount = permitted[i].amount + bidAmount + callValue; | ||
return permitted; | ||
} | ||
} | ||
|
||
if (bidAmount + callValue > 0) { | ||
permitted.push({ token: weth, amount: bidAmount + callValue }); | ||
} | ||
|
||
return permitted; | ||
} | ||
|
||
export class Client { | ||
public clientOptions: ClientOptions; | ||
public wsOptions: WsOptions; | ||
|
@@ -145,21 +202,11 @@ export class Client { | |
}); | ||
} | ||
|
||
private convertEIP712Domain( | ||
eip712Domain: components["schemas"]["EIP712Domain"] | ||
): EIP712Domain { | ||
return { | ||
name: eip712Domain.name, | ||
version: eip712Domain.version, | ||
verifyingContract: checkAddress(eip712Domain.verifying_contract), | ||
chainId: BigInt(eip712Domain.chain_id), | ||
}; | ||
} | ||
|
||
/** | ||
* Converts an opportunity from the server to the client format | ||
* Returns undefined if the opportunity version is not supported | ||
* @param opportunity | ||
* @returns Opportunity in the converted client format | ||
*/ | ||
private convertOpportunity( | ||
opportunity: components["schemas"]["OpportunityParamsWithMetadata"] | ||
|
@@ -179,7 +226,6 @@ export class Client { | |
targetCallValue: BigInt(opportunity.target_call_value), | ||
sellTokens: opportunity.sell_tokens.map(checkTokenQty), | ||
buyTokens: opportunity.buy_tokens.map(checkTokenQty), | ||
eip712Domain: this.convertEIP712Domain(opportunity.eip_712_domain), | ||
}; | ||
} | ||
|
||
|
@@ -256,6 +302,7 @@ export class Client { | |
/** | ||
* Fetches opportunities | ||
* @param chainId Chain id to fetch opportunities for. e.g: sepolia | ||
* @returns List of opportunities | ||
*/ | ||
async getOpportunities(chainId?: string): Promise<Opportunity[]> { | ||
const client = createClient<paths>(this.clientOptions); | ||
|
@@ -307,48 +354,78 @@ export class Client { | |
* Creates a signed bid for an opportunity | ||
* @param opportunity Opportunity to bid on | ||
* @param bidParams Bid amount and valid until timestamp | ||
* @param opportunityAdapterConfig Opportunity adapter config | ||
* @param privateKey Private key to sign the bid with | ||
* @returns Signed opportunity bid | ||
*/ | ||
async signOpportunityBid( | ||
opportunity: Opportunity, | ||
bidParams: BidParams, | ||
opportunityAdapterConfig: OpportunityAdapterConfig, | ||
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); | ||
const create2Address = getContractAddress({ | ||
bytecodeHash: | ||
opportunityAdapterConfig.opportunity_adapter_init_bytecode_hash, | ||
from: opportunityAdapterConfig.opportunity_adapter_factory, | ||
opcode: "CREATE2", | ||
salt: `0x${account.address.replace("0x", "").padStart(64, "0")}`, | ||
}); | ||
|
||
const signature = await signTypedData({ | ||
privateKey, | ||
domain: { | ||
...opportunity.eip712Domain, | ||
chainId: Number(opportunity.eip712Domain.chainId), | ||
name: "Permit2", | ||
verifyingContract: checkAddress(opportunityAdapterConfig.permit2), | ||
chainId: opportunityAdapterConfig.chain_id, | ||
}, | ||
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: getPermittedTokens( | ||
opportunity.sellTokens, | ||
bidParams.amount, | ||
opportunity.targetCallValue, | ||
checkAddress(opportunityAdapterConfig.weth) | ||
), | ||
spender: create2Address, | ||
nonce: bidParams.nonce, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nonce should have a default random value. Otherwise, we should document somewhere on what this value should be |
||
deadline: bidParams.deadline, | ||
witness: { | ||
buyTokens: opportunity.buyTokens, | ||
executor: account.address, | ||
targetContract: opportunity.targetContract, | ||
targetCalldata: opportunity.targetCalldata, | ||
targetCallValue: opportunity.targetCallValue, | ||
bidAmount: bidParams.amount, | ||
}, | ||
}, | ||
}); | ||
|
||
|
@@ -369,7 +446,8 @@ export class Client { | |
executor: bid.executor, | ||
permission_key: bid.permissionKey, | ||
signature: bid.signature, | ||
valid_until: bid.bid.validUntil.toString(), | ||
deadline: bid.bid.deadline.toString(), | ||
nonce: bid.bid.nonce.toString(), | ||
}; | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use map