-
Notifications
You must be signed in to change notification settings - Fork 262
feat: Update sdks to use svm opportunities #2009
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6c9129
feat: Update sdks to use svm opportunities
m30m 51f65ef
fix
m30m 4541f94
address comments
m30m a6cac64
address comments
m30m 170d995
Put vm specific logic into separate files
m30m baf02f3
Address comments
m30m 0850c9d
fix
m30m 811dbc6
fix
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import { | ||
Bid, | ||
BidParams, | ||
OpportunityBid, | ||
OpportunityEvm, | ||
TokenAmount, | ||
TokenPermissions, | ||
} from "./types"; | ||
import { Address, encodeFunctionData, getContractAddress, Hex } from "viem"; | ||
import { privateKeyToAccount, signTypedData } from "viem/accounts"; | ||
import { checkAddress, ClientError } from "./index"; | ||
import { OPPORTUNITY_ADAPTER_CONFIGS } from "./const"; | ||
import { executeOpportunityAbi } from "./abi"; | ||
|
||
/** | ||
* 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[] = tokens.map(({ token, amount }) => ({ | ||
token, | ||
amount, | ||
})); | ||
const wethIndex = permitted.findIndex(({ token }) => token === weth); | ||
const extraWethNeeded = bidAmount + callValue; | ||
if (wethIndex !== -1) { | ||
permitted[wethIndex].amount += extraWethNeeded; | ||
return permitted; | ||
} | ||
if (extraWethNeeded > 0) { | ||
permitted.push({ token: weth, amount: extraWethNeeded }); | ||
} | ||
return permitted; | ||
} | ||
|
||
function getOpportunityConfig(chainId: string) { | ||
const opportunityAdapterConfig = OPPORTUNITY_ADAPTER_CONFIGS[chainId]; | ||
if (!opportunityAdapterConfig) { | ||
throw new ClientError( | ||
`Opportunity adapter config not found for chain id: ${chainId}` | ||
); | ||
} | ||
return opportunityAdapterConfig; | ||
} | ||
|
||
export async function signBid( | ||
opportunity: OpportunityEvm, | ||
bidParams: BidParams, | ||
privateKey: Hex | ||
): Promise<Bid> { | ||
const opportunityAdapterConfig = getOpportunityConfig(opportunity.chainId); | ||
const executor = privateKeyToAccount(privateKey).address; | ||
const permitted = getPermittedTokens( | ||
opportunity.sellTokens, | ||
bidParams.amount, | ||
opportunity.targetCallValue, | ||
checkAddress(opportunityAdapterConfig.weth) | ||
); | ||
const signature = await getSignature(opportunity, bidParams, privateKey); | ||
|
||
const calldata = makeAdapterCalldata( | ||
opportunity, | ||
permitted, | ||
executor, | ||
bidParams, | ||
signature | ||
); | ||
|
||
return { | ||
amount: bidParams.amount, | ||
targetCalldata: calldata, | ||
chainId: opportunity.chainId, | ||
targetContract: opportunityAdapterConfig.opportunity_adapter_factory, | ||
permissionKey: opportunity.permissionKey, | ||
env: "evm", | ||
}; | ||
} | ||
|
||
/** | ||
* Constructs the calldata for the opportunity adapter contract. | ||
* @param opportunity Opportunity to bid on | ||
* @param permitted Permitted tokens | ||
* @param executor Address of the searcher's wallet | ||
* @param bidParams Bid amount, nonce, and deadline timestamp | ||
* @param signature Searcher's signature for opportunity params and bidParams | ||
* @returns Calldata for the opportunity adapter contract | ||
*/ | ||
function makeAdapterCalldata( | ||
opportunity: OpportunityEvm, | ||
permitted: TokenPermissions[], | ||
executor: Address, | ||
bidParams: BidParams, | ||
signature: Hex | ||
): Hex { | ||
return encodeFunctionData({ | ||
abi: [executeOpportunityAbi], | ||
args: [ | ||
[ | ||
[permitted, bidParams.nonce, bidParams.deadline], | ||
[ | ||
opportunity.buyTokens, | ||
executor, | ||
opportunity.targetContract, | ||
opportunity.targetCalldata, | ||
opportunity.targetCallValue, | ||
bidParams.amount, | ||
], | ||
], | ||
signature, | ||
], | ||
}); | ||
} | ||
|
||
export async function getSignature( | ||
opportunity: OpportunityEvm, | ||
bidParams: BidParams, | ||
privateKey: Hex | ||
): Promise<`0x${string}`> { | ||
const types = { | ||
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: "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 executor = account.address; | ||
const opportunityAdapterConfig = getOpportunityConfig(opportunity.chainId); | ||
const permitted = getPermittedTokens( | ||
opportunity.sellTokens, | ||
bidParams.amount, | ||
opportunity.targetCallValue, | ||
checkAddress(opportunityAdapterConfig.weth) | ||
); | ||
const create2Address = getContractAddress({ | ||
bytecodeHash: | ||
opportunityAdapterConfig.opportunity_adapter_init_bytecode_hash, | ||
from: opportunityAdapterConfig.opportunity_adapter_factory, | ||
opcode: "CREATE2", | ||
salt: `0x${executor.replace("0x", "").padStart(64, "0")}`, | ||
}); | ||
|
||
return signTypedData({ | ||
privateKey, | ||
domain: { | ||
name: "Permit2", | ||
verifyingContract: checkAddress(opportunityAdapterConfig.permit2), | ||
chainId: opportunityAdapterConfig.chain_id, | ||
}, | ||
types, | ||
primaryType: "PermitBatchWitnessTransferFrom", | ||
message: { | ||
permitted, | ||
spender: create2Address, | ||
nonce: bidParams.nonce, | ||
deadline: bidParams.deadline, | ||
witness: { | ||
buyTokens: opportunity.buyTokens, | ||
executor, | ||
targetContract: opportunity.targetContract, | ||
targetCalldata: opportunity.targetCalldata, | ||
targetCallValue: opportunity.targetCallValue, | ||
bidAmount: bidParams.amount, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export async function signOpportunityBid( | ||
opportunity: OpportunityEvm, | ||
bidParams: BidParams, | ||
privateKey: Hex | ||
): Promise<OpportunityBid> { | ||
const account = privateKeyToAccount(privateKey); | ||
const signature = await getSignature(opportunity, bidParams, privateKey); | ||
|
||
return { | ||
permissionKey: opportunity.permissionKey, | ||
bid: bidParams, | ||
executor: account.address, | ||
signature, | ||
opportunityId: opportunity.opportunityId, | ||
}; | ||
} |
This file contains hidden or 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
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.
Are we trying to validate fields and type here or just checking if it's a Svm or Evm opp?
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.
just checking it's an Svm/Evm opp.