forked from 0xsequence/sequence.js
-
Notifications
You must be signed in to change notification settings - Fork 2
0x v2 #739
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
0x v2 #739
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e0dc186
provider: eip-5792 support
attente e408c8c
provider: use eip-5792
attente 1b898de
eip-7795
attente cb8c280
stack@eip-7795 bindings
attente f0fd1f4
fixup! eip-7795
attente f6d7a3d
Merge remote-tracking branch 'origin/eip-7795' into 0x-v2
googleworkspace-bot 77e2f46
Update packages/relayer/src/precondition.ts
Dargon789 441bd4a
Merge branch 'master' into 0x-v2
googleworkspace-bot 63f8699
Add SequenceBatchService for transaction batching
Dargon789 8be805e
Add TypeScript interfaces for batch calls and ERC20
Dargon789 3d2ea66
Implement SequenceBatchBuilder for batch transactions
Dargon789 8e5b0e5
Add example for SequenceBatchBuilder usage
Dargon789 17496f8
Implement BatchWalletUI for multi-action transactions
Dargon789 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,272 @@ | ||
| import { ethers } from 'ethers' | ||
|
|
||
| import { proto } from './rpc-relayer' | ||
|
|
||
| export type Precondition = | ||
| | NativeBalancePrecondition | ||
| | Erc20BalancePrecondition | ||
| | Erc20ApprovalPrecondition | ||
| | Erc721OwnershipPrecondition | ||
| | Erc721ApprovalPrecondition | ||
| | Erc1155BalancePrecondition | ||
| | Erc1155ApprovalPrecondition | ||
|
|
||
| export function isPrecondition(precondition: any): precondition is Precondition { | ||
| return [ | ||
| isNativeBalancePrecondition, | ||
| isErc20BalancePrecondition, | ||
| isErc20ApprovalPrecondition, | ||
| isErc721OwnershipPrecondition, | ||
| isErc721ApprovalPrecondition, | ||
| isErc1155BalancePrecondition, | ||
| isErc1155ApprovalPrecondition | ||
| ].some(predicate => predicate(precondition)) | ||
| } | ||
|
|
||
| export function encodePrecondition(precondition: Precondition): proto.Precondition { | ||
| if (isNativeBalancePrecondition(precondition)) { | ||
| return encodeNativeBalancePrecondition(precondition) | ||
| } else if (isErc20BalancePrecondition(precondition)) { | ||
| return encodeErc20BalancePrecondition(precondition) | ||
| } else if (isErc20ApprovalPrecondition(precondition)) { | ||
| return encodeErc20ApprovalPrecondition(precondition) | ||
| } else if (isErc721OwnershipPrecondition(precondition)) { | ||
| return encodeErc721OwnershipPrecondition(precondition) | ||
| } else if (isErc721ApprovalPrecondition(precondition)) { | ||
| return encodeErc721ApprovalPrecondition(precondition) | ||
| } else if (isErc1155BalancePrecondition(precondition)) { | ||
| return encodeErc1155BalancePrecondition(precondition) | ||
| } else if (isErc1155ApprovalPrecondition(precondition)) { | ||
| return encodeErc1155ApprovalPrecondition(precondition) | ||
| } else { | ||
| throw new Error('unreachable') | ||
| } | ||
| } | ||
|
|
||
| type NativeBalancePrecondition = { | ||
| type: 'native-balance' | ||
| address: `0x${string}` | ||
| min?: ethers.BigNumberish | ||
| max?: ethers.BigNumberish | ||
| } | ||
|
|
||
| function isNativeBalancePrecondition(precondition: any): precondition is NativeBalancePrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition && | ||
| precondition.type === 'native-balance' && | ||
| ethers.isAddress(precondition.address) && | ||
| (precondition.min === undefined || isBigNumberish(precondition.min)) && | ||
| (precondition.max === undefined || isBigNumberish(precondition.max)) | ||
| ) | ||
| } | ||
|
|
||
| function encodeNativeBalancePrecondition(precondition: NativeBalancePrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { | ||
| ...precondition, | ||
| type: undefined, | ||
| min: encodeBigNumberish(precondition.min), | ||
| max: encodeBigNumberish(precondition.max) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type Erc20BalancePrecondition = { | ||
| type: 'erc20-balance' | ||
| address: `0x${string}` | ||
| token: `0x${string}` | ||
| min?: ethers.BigNumberish | ||
| max?: ethers.BigNumberish | ||
| } | ||
|
|
||
| function isErc20BalancePrecondition(precondition: any): precondition is Erc20BalancePrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition && | ||
| precondition.type === 'erc20-balance' && | ||
| ethers.isAddress(precondition.address) && | ||
| ethers.isAddress(precondition.token) && | ||
| (precondition.min === undefined || isBigNumberish(precondition.min)) && | ||
| (precondition.max === undefined || isBigNumberish(precondition.max)) | ||
| ) | ||
| } | ||
|
|
||
| function encodeErc20BalancePrecondition(precondition: Erc20BalancePrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { | ||
| ...precondition, | ||
| type: undefined, | ||
| min: encodeBigNumberish(precondition.min), | ||
| max: encodeBigNumberish(precondition.max) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type Erc20ApprovalPrecondition = { | ||
| type: 'erc20-approval' | ||
| address: `0x${string}` | ||
| token: `0x${string}` | ||
| operator: `0x${string}` | ||
| min: ethers.BigNumberish | ||
| } | ||
|
|
||
| function isErc20ApprovalPrecondition(precondition: any): precondition is Erc20ApprovalPrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition && | ||
| precondition.type === 'erc20-approval' && | ||
| ethers.isAddress(precondition.address) && | ||
| ethers.isAddress(precondition.token) && | ||
| ethers.isAddress(precondition.operator) && | ||
| isBigNumberish(precondition.min) | ||
| ) | ||
| } | ||
|
|
||
| function encodeErc20ApprovalPrecondition(precondition: Erc20ApprovalPrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { ...precondition, type: undefined, min: encodeBigNumberish(precondition.min) } | ||
| } | ||
| } | ||
|
|
||
| type Erc721OwnershipPrecondition = { | ||
| type: 'erc721-ownership' | ||
| address: `0x${string}` | ||
| token: `0x${string}` | ||
| tokenId: ethers.BigNumberish | ||
| owned?: boolean | ||
| } | ||
|
|
||
| function isErc721OwnershipPrecondition(precondition: any): precondition is Erc721OwnershipPrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition.type === 'erc721-ownership' && | ||
| ethers.isAddress(precondition.address) && | ||
| ethers.isAddress(precondition.token) && | ||
| isBigNumberish(precondition.tokenId) && | ||
| (precondition.owned === undefined || typeof precondition.owned === 'boolean') | ||
|
Dargon789 marked this conversation as resolved.
|
||
| ) | ||
| } | ||
|
|
||
| function encodeErc721OwnershipPrecondition(precondition: Erc721OwnershipPrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { | ||
| ...precondition, | ||
| type: undefined, | ||
| tokenId: encodeBigNumberish(precondition.tokenId), | ||
| owned: precondition.owned !== false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type Erc721ApprovalPrecondition = { | ||
| type: 'erc721-approval' | ||
| address: `0x${string}` | ||
| token: `0x${string}` | ||
| tokenId: ethers.BigNumberish | ||
| operator: `0x${string}` | ||
| } | ||
|
|
||
| function isErc721ApprovalPrecondition(precondition: any): precondition is Erc721ApprovalPrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition.type === 'erc721-approval' && | ||
| ethers.isAddress(precondition.address) && | ||
| ethers.isAddress(precondition.token) && | ||
| isBigNumberish(precondition.tokenId) && | ||
| ethers.isAddress(precondition.operator) | ||
| ) | ||
| } | ||
|
|
||
| function encodeErc721ApprovalPrecondition(precondition: Erc721ApprovalPrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { ...precondition, type: undefined, tokenId: encodeBigNumberish(precondition.tokenId) } | ||
| } | ||
| } | ||
|
|
||
| type Erc1155BalancePrecondition = { | ||
| type: 'erc1155-balance' | ||
| address: `0x${string}` | ||
| token: `0x${string}` | ||
| tokenId: ethers.BigNumberish | ||
| min?: ethers.BigNumberish | ||
| max?: ethers.BigNumberish | ||
| } | ||
|
|
||
| function isErc1155BalancePrecondition(precondition: any): precondition is Erc1155BalancePrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition && | ||
| precondition.type === 'erc1155-balance' && | ||
| ethers.isAddress(precondition.address) && | ||
| ethers.isAddress(precondition.token) && | ||
| isBigNumberish(precondition.tokenId) && | ||
| (precondition.min === undefined || isBigNumberish(precondition.min)) && | ||
| (precondition.max === undefined || isBigNumberish(precondition.max)) | ||
| ) | ||
| } | ||
|
|
||
| function encodeErc1155BalancePrecondition(precondition: Erc1155BalancePrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { | ||
| ...precondition, | ||
| type: undefined, | ||
| tokenId: encodeBigNumberish(precondition.tokenId), | ||
| min: encodeBigNumberish(precondition.min), | ||
| max: encodeBigNumberish(precondition.max) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type Erc1155ApprovalPrecondition = { | ||
| type: 'erc1155-approval' | ||
| address: `0x${string}` | ||
| token: `0x${string}` | ||
| tokenId: ethers.BigNumberish | ||
| operator: `0x${string}` | ||
| min: ethers.BigNumberish | ||
| } | ||
|
|
||
| function isErc1155ApprovalPrecondition(precondition: any): precondition is Erc1155ApprovalPrecondition { | ||
| return ( | ||
| typeof precondition === 'object' && | ||
| precondition && | ||
| precondition.type === 'erc1155-approval' && | ||
| ethers.isAddress(precondition.address) && | ||
| ethers.isAddress(precondition.token) && | ||
| isBigNumberish(precondition.tokenId) && | ||
| ethers.isAddress(precondition.operator) && | ||
| isBigNumberish(precondition.min) | ||
| ) | ||
| } | ||
|
|
||
| function encodeErc1155ApprovalPrecondition(precondition: Erc1155ApprovalPrecondition): proto.Precondition { | ||
| return { | ||
| type: precondition.type, | ||
| precondition: { | ||
| ...precondition, | ||
| type: undefined, | ||
| tokenId: encodeBigNumberish(precondition.tokenId), | ||
| min: encodeBigNumberish(precondition.min) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function isBigNumberish(value: any): value is ethers.BigNumberish { | ||
| try { | ||
| ethers.toBigInt(value) | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function encodeBigNumberish(value: ethers.BigNumberish | undefined): string | undefined { | ||
| return value !== undefined ? ethers.toBigInt(value).toString() : undefined | ||
| } | ||
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,43 @@ | ||
| import { sequence } from '@0xsequence/provider' | ||
| import { ethers } from 'ethers' | ||
| import { SequenceBatchBuilder } from './services/sequence/SequenceBatchBuilder' | ||
|
|
||
| async function runExample() { | ||
| const wallet = await sequence.initWallet('mainnet') | ||
| const signer = wallet.getSigner(56) // BSC Chain ID: 56 | ||
|
|
||
| const busdAddress = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56' | ||
| const dexRouter = '0x1111111254fb6c44bac0bed2854e76f90643097d' | ||
| const recipient = '0x9a72807e1BC8A5e1E178f51E26239d58F511EB3D' | ||
|
|
||
| // Initialize Helper | ||
| const batchBuilder = new SequenceBatchBuilder(signer) | ||
|
|
||
| // batchBuilder (Chainable) | ||
| batchBuilder | ||
| .addERC20Approve({ | ||
| tokenAddress: busdAddress, | ||
| spender: dexRouter, | ||
| amount: ethers.utils.parseUnits('100.0', 18) | ||
| }) | ||
| .addERC20Transfer({ | ||
| tokenAddress: busdAddress, | ||
| recipient: recipient, | ||
| amount: ethers.utils.parseUnits('50.0', 18) | ||
| }) | ||
|
|
||
| // (Optional) pull Payload out Simulate on Tenderly | ||
| const payloadForSimulation = batchBuilder.getBatchQueue() | ||
| console.log('Payload for Simulation:', JSON.stringify(payloadForSimulation, null, 2)) | ||
|
|
||
| // 3. sent Execute | ||
| try { | ||
| const tx = await batchBuilder.execute() | ||
| console.log('Batch Transaction Submitted! Tx Hash:', tx.hash) | ||
|
|
||
| const receipt = await tx.wait() | ||
| console.log('✅ Success in Block:', receipt.blockNumber) | ||
| } catch (error) { | ||
| console.error('❌ Batch Execution Failed:', error) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.