-
Notifications
You must be signed in to change notification settings - Fork 173
feat(zetaclient): PTB building for Sui withdrawAndCall transaction #3793
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
59 commits
Select commit
Hold shift + click to select a range
dc76d03
add example contract
lumtis 5075844
add example
lumtis f3d230c
add ptb building for withdraw and call
lumtis df06a9b
lint
lumtis ff0e859
changelogs
lumtis a6732de
add todos
lumtis c6412cd
Merge branch 'develop' of https://github.com/zeta-chain/node into sui…
ws4charlie c20b6e3
make e2e sui setup cleaner; deploy example package
ws4charlie b956ae3
Merge branch 'develop' of https://github.com/zeta-chain/node into sui…
ws4charlie c63161f
add sui Example package information to E2E runner
ws4charlie eeee8cf
sui withdraw and call e2e using example contract; refactor sui signer…
ws4charlie 10827dc
use example package id as receiver address in e2e; fix missing logger…
ws4charlie b0f2762
uncomment sui e2e tests; remove redundant debug log print
ws4charlie d964e45
remove redundant log print; use hardcoded version to replicate Invali…
ws4charlie 0e376ef
fix PTB withdraw and call InvalidBCSBytes and other execution errors
ws4charlie 80aa52e
make Sui withdraw and call E2E test working
ws4charlie 1d8d7ab
Merge branch 'develop' into sui-ptb
ws4charlie e3e995e
Merge branch 'develop' into sui-ptb
ws4charlie dfb2c38
improve sui withdraw and call e2e test
ws4charlie 351f607
simplify json integer extraction; fix code generate; improve comment
ws4charlie 3b165a8
improve readability of function getWithdrawAndCallObjectRefs
ws4charlie 8c58f15
move PTB argument related function to pkg contracts
ws4charlie 6189872
add pkg contracts for sui withdraw and call
ws4charlie 57ff9ae
fix gosec
ws4charlie a4123de
simplify the code for sui contract fields assignment
ws4charlie 385b6ab
use actual gas budget in the CCTX for PTB transaction
ws4charlie ad9e246
Merge branch 'develop' of https://github.com/zeta-chain/node into sui…
ws4charlie 1cfda65
get latest version of TSS SUI coin object for gas payment
ws4charlie ee2268c
improve naming and add additional sui contract pkg unit test
ws4charlie b047c7a
add unit test to ptb tx builder; split ptb tx builder into small func…
ws4charlie 7007c0a
Merge branch 'develop' of https://github.com/zeta-chain/node into sui…
ws4charlie 60642a2
fix unit test compile
ws4charlie c032afa
add withdraw and call signer test
ws4charlie 2cc71fa
Merge branch 'develop' into sui-ptb
ws4charlie b77955d
use a struct to hold the config of Sui example package; a name revert
ws4charlie 06e75d6
use the r.chain.method pattern for SUI contract deployment related fu…
ws4charlie 65086b3
use hardcoded address in the E2E test to receive withdawAndCall funds
ws4charlie 0adb35a
add unit test for gateway pure method extractInteger
ws4charlie edc687f
return error if SUI coin object version is invalid
ws4charlie c61476e
use error wrapping; remove TODO
ws4charlie eb06d0a
removed TODO in sui e2e withdraw and call test
ws4charlie 003d155
clean redundant comment; use name suiptb for pattonkan sui go library…
ws4charlie 5516185
unexport typeSeparator; switch return type from pointer to value; rem…
ws4charlie 8a9ce13
create a MoveCall struct to hold parsed packageID, module and functio…
ws4charlie 31a92f2
use ptb as prefix for tx building functions; pass gasBudget param as …
ws4charlie dc0951b
move withdrawAndCallPTB as Signer method; group withdrawAndCallPTB ar…
ws4charlie 63e4bd6
move getWithdrawAndCallObjectRefs to a Signer method
ws4charlie fcb377f
add Makefile to rebuild example package and update compiled mv files
ws4charlie 05986c4
ensure the on_call method gets called in withdraw and call E2E test
ws4charlie 2376ca9
cancel withdrawAndCall if on_call object is not shared or immutable
ws4charlie bd2f5da
add sui withdrawAndCall revert E2E test
ws4charlie b96c4d4
add readme file to briefly describe how sui withdrawAndCall works usi…
ws4charlie 1d6a126
Merge branch 'develop' into sui-ptb
ws4charlie e2a9995
fix CI unit test failure
ws4charlie 356092d
Merge branch 'develop' into sui-ptb
swift1337 177b255
name and log improvement
ws4charlie 08cf305
Merge branch 'develop' into sui-ptb
ws4charlie d557692
move GetSuiCoinObjectRef RPC call into getWithdrawAndCallObjectRefs; …
ws4charlie 85449be
fix CI unit test
ws4charlie 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
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
Binary file not shown.
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 @@ | ||
| build/* |
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,22 @@ | ||
| .PHONY: clean build all | ||
|
|
||
| # Default target | ||
| all: clean build | ||
|
|
||
| # Clean build directory | ||
| clean: | ||
| rm -rf build/ | ||
|
|
||
| # Build the package and generate bytecode | ||
| build: | ||
| sui move build | ||
| cp build/example/bytecode_modules/token.mv . | ||
| cp build/example/bytecode_modules/connected.mv . | ||
|
|
||
| # Help target | ||
| help: | ||
| @echo "Available targets:" | ||
| @echo " all - Clean and build everything (default)" | ||
| @echo " clean - Remove build directory" | ||
| @echo " build - Build the package and generate bytecode" | ||
| @echo " help - Show this help message" |
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,58 @@ | ||
| # @generated by Move, please check-in and do not edit manually. | ||
|
|
||
| [move] | ||
| version = 3 | ||
| manifest_digest = "795BC4DE1AC42C1015B85547951A89D7F9AD63EA50982960E095E815C560BFCC" | ||
| deps_digest = "397E6A9F7A624706DBDFEE056CE88391A15876868FD18A88504DA74EB458D697" | ||
| dependencies = [ | ||
| { id = "Bridge", name = "Bridge" }, | ||
| { id = "DeepBook", name = "DeepBook" }, | ||
| { id = "MoveStdlib", name = "MoveStdlib" }, | ||
| { id = "Sui", name = "Sui" }, | ||
| { id = "SuiSystem", name = "SuiSystem" }, | ||
| ] | ||
|
|
||
| [[move.package]] | ||
| id = "Bridge" | ||
| source = { git = "https://github.com/MystenLabs/sui.git", rev = "9c04e1840eb5", subdir = "crates/sui-framework/packages/bridge" } | ||
|
|
||
| dependencies = [ | ||
| { id = "MoveStdlib", name = "MoveStdlib" }, | ||
| { id = "Sui", name = "Sui" }, | ||
| { id = "SuiSystem", name = "SuiSystem" }, | ||
| ] | ||
|
|
||
| [[move.package]] | ||
| id = "DeepBook" | ||
| source = { git = "https://github.com/MystenLabs/sui.git", rev = "9c04e1840eb5", subdir = "crates/sui-framework/packages/deepbook" } | ||
|
|
||
| dependencies = [ | ||
| { id = "MoveStdlib", name = "MoveStdlib" }, | ||
| { id = "Sui", name = "Sui" }, | ||
| ] | ||
|
|
||
| [[move.package]] | ||
| id = "MoveStdlib" | ||
| source = { git = "https://github.com/MystenLabs/sui.git", rev = "9c04e1840eb5", subdir = "crates/sui-framework/packages/move-stdlib" } | ||
|
|
||
| [[move.package]] | ||
| id = "Sui" | ||
| source = { git = "https://github.com/MystenLabs/sui.git", rev = "9c04e1840eb5", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
|
||
| dependencies = [ | ||
| { id = "MoveStdlib", name = "MoveStdlib" }, | ||
| ] | ||
|
|
||
| [[move.package]] | ||
| id = "SuiSystem" | ||
| source = { git = "https://github.com/MystenLabs/sui.git", rev = "9c04e1840eb5", subdir = "crates/sui-framework/packages/sui-system" } | ||
|
|
||
| dependencies = [ | ||
| { id = "MoveStdlib", name = "MoveStdlib" }, | ||
| { id = "Sui", name = "Sui" }, | ||
| ] | ||
|
|
||
| [move.toolchain-version] | ||
| compiler-version = "1.45.0" | ||
| edition = "2024.beta" | ||
| flavor = "sui" |
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,37 @@ | ||
| [package] | ||
| name = "example" | ||
| edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move | ||
| # license = "" # e.g., "MIT", "GPL", "Apache 2.0" | ||
| # authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"] | ||
|
|
||
| [dependencies] | ||
| #Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.41.1" } | ||
|
|
||
| # For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`. | ||
| # Revision can be a branch, a tag, and a commit hash. | ||
| # MyRemotePackage = { git = "https://some.xwremote/host.git", subdir = "remote/path", rev = "main" } | ||
|
|
||
| # For local dependencies use `local = path`. Path is relative to the package root | ||
| # Local = { local = "../path/to" } | ||
|
|
||
| # To resolve a version conflict and force a specific version for dependency | ||
| # override use `override = true` | ||
| # Override = { local = "../conflicting/version", override = true } | ||
|
|
||
| [addresses] | ||
| example = "0x0" | ||
|
|
||
| # Named addresses will be accessible in Move as `@name`. They're also exported: | ||
| # for example, `std = "0x1"` is exported by the Standard Library. | ||
| # alice = "0xA11CE" | ||
|
|
||
| [dev-dependencies] | ||
| # The dev-dependencies section allows overriding dependencies for `--test` and | ||
| # `--dev` modes. You can introduce test-only dependencies here. | ||
| # Local = { local = "../path/to/dev-build" } | ||
|
|
||
| [dev-addresses] | ||
| # The dev-addresses section allows overwriting named addresses for the `--test` | ||
| # and `--dev` modes. | ||
| # alice = "0xB0B" | ||
|
|
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,69 @@ | ||
| # SUI WithdrawAndCall with PTB Transactions | ||
|
|
||
| This document explains how the SUI `withdrawAndCall` functionality works using Programmable Transaction Blocks (PTB) in the ZetaChain protocol. | ||
|
|
||
| ## Overview | ||
|
|
||
| The `withdrawAndCall` operation in ZetaChain allows users to withdraw tokens from ZEVM to the Sui blockchain and simultaneously calls a `on_call` function in the `connected` module on the Sui side. | ||
|
|
||
| This is implemented as a single atomic transaction using Sui's Programmable Transaction Blocks (PTB). | ||
|
|
||
| ## Transaction Flow | ||
|
|
||
| 1. **User Initiates Withdrawal**: A user initiates a withdrawal from ZEVM to Sui with a `on_call` payload. | ||
|
|
||
| 2. **ZEVM Processing**: The ZEVM gateway processes the withdrawal request and prepares the transaction. | ||
|
|
||
| 3. **PTB Construction**: A Programmable Transaction Block is constructed with the following steps: | ||
| - **Withdraw**: The first command in the PTB is the `withdraw_impl` function call, which: | ||
| - Verifies the withdrawal parameters | ||
| - Withdraw and returns two coin objects: the main withdrawn coins and the gas budget coins | ||
| - **Gas Budget Transfer**: The second command transfers the gas budget coins to the TSS address to cover transaction fees. | ||
ws4charlie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - The gas budget is the SUI coin withdrawn from sui vault, together with withdrawn CCTX's coinType. | ||
| - The gas budget needs to be forwarded to TSS address to cover the transaction fee. | ||
| - **Connected Module Call**: The third command calls the `on_call` function in the connected module, passing: | ||
| - The withdrawn coins | ||
| - The call payload from the user | ||
| - Any additional parameters required by the connected module | ||
|
|
||
| 4. **Transaction Execution**: The entire PTB is executed atomically on the Sui blockchain. | ||
|
|
||
| ## PTB Structure | ||
|
|
||
| The PTB for a `withdrawAndCall` transaction consists of three main commands: | ||
|
|
||
| ``` | ||
| PTB { | ||
| // Command 0: Withdraw Implementation | ||
| MoveCall { | ||
| package: gateway_package_id, | ||
| module: gateway_module, | ||
| function: withdraw_impl, | ||
| arguments: [ | ||
| gateway_object_ref, | ||
| withdraw_cap_object_ref, | ||
| coin_type, | ||
| amount, | ||
| nonce, | ||
| gas_budget | ||
| ] | ||
| } | ||
|
|
||
| // Command 1: Gas Budget Transfer | ||
| TransferObjects { | ||
| from: withdraw_impl_result[1], // Gas budget coins | ||
| to: tss_address | ||
| } | ||
|
|
||
| // Command 2: Connected Module Call | ||
| MoveCall { | ||
| package: target_package_id, | ||
| module: connected_module, | ||
| function: on_call, | ||
| arguments: [ | ||
| withdraw_impl_result[0], // Main withdrawn coins | ||
| on_call_payload | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
Binary file not shown.
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,66 @@ | ||
| module example::connected; | ||
|
|
||
| use sui::address::from_bytes; | ||
| use sui::coin::Coin; | ||
|
|
||
| // stub for shared objects | ||
| public struct GlobalConfig has key { | ||
ws4charlie marked this conversation as resolved.
Show resolved
Hide resolved
ws4charlie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| id: UID, | ||
| called_count: u64, | ||
| } | ||
|
|
||
| public struct Partner has key { | ||
| id: UID, | ||
| } | ||
|
|
||
| public struct Clock has key { | ||
| id: UID, | ||
| } | ||
|
|
||
| public struct Pool<phantom CoinA, phantom CoinB> has key { | ||
| id: UID, | ||
| } | ||
|
|
||
| // share objects | ||
| fun init(ctx: &mut TxContext) { | ||
| let global_config = GlobalConfig { | ||
| id: object::new(ctx), | ||
| called_count: 0, | ||
| }; | ||
| let pool = Pool<sui::sui::SUI, example::token::TOKEN> { | ||
| id: object::new(ctx), | ||
| }; | ||
| let partner = Partner { | ||
| id: object::new(ctx), | ||
| }; | ||
| let clock = Clock { | ||
| id: object::new(ctx), | ||
| }; | ||
|
|
||
| transfer::share_object(global_config); | ||
| transfer::share_object(pool); | ||
| transfer::share_object(partner); | ||
| transfer::share_object(clock); | ||
| } | ||
|
|
||
| public entry fun on_call<SOURCE_COIN, TARGET_COIN>( | ||
| in_coins: Coin<SOURCE_COIN>, | ||
| cetus_config: &mut GlobalConfig, | ||
| _pool: &mut Pool<SOURCE_COIN, TARGET_COIN>, | ||
| _cetus_partner: &mut Partner, | ||
| _clock: &Clock, | ||
| data: vector<u8>, | ||
| _ctx: &mut TxContext, | ||
| ) { | ||
| let receiver = decode_receiver(data); | ||
|
|
||
ws4charlie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // transfer the coins to the provided address | ||
| transfer::public_transfer(in_coins, receiver); | ||
|
|
||
| // increment the called count | ||
| cetus_config.called_count = cetus_config.called_count + 1; | ||
| } | ||
|
|
||
| fun decode_receiver(data: vector<u8>): address { | ||
| from_bytes(data) | ||
| } | ||
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,29 @@ | ||
| module example::token; | ||
|
|
||
| use sui::coin::{Self, TreasuryCap}; | ||
|
|
||
| public struct TOKEN has drop {} | ||
|
|
||
| fun init(witness: TOKEN, ctx: &mut TxContext) { | ||
| let (treasury, metadata) = coin::create_currency( | ||
| witness, | ||
| 6, | ||
| b"TOKEN", | ||
| b"", | ||
| b"", | ||
| option::none(), | ||
| ctx, | ||
| ); | ||
| transfer::public_freeze_object(metadata); | ||
| transfer::public_transfer(treasury, ctx.sender()) | ||
| } | ||
ws4charlie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public entry fun mint( | ||
| treasury_cap: &mut TreasuryCap<TOKEN>, | ||
| amount: u64, | ||
| recipient: address, | ||
| ctx: &mut TxContext, | ||
| ) { | ||
| let coin = coin::mint(treasury_cap, amount, ctx); | ||
| transfer::public_transfer(coin, recipient) | ||
| } | ||
Binary file not shown.
Binary file not shown.
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.