-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add bridge withdraw command
- Loading branch information
Showing
9 changed files
with
226 additions
and
30 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{ | ||
bridge::convert_tez_to_mutez, | ||
config::{Config, NetworkName}, | ||
error::{bail_user_error, Result}, | ||
run, | ||
term::styles, | ||
utils::AddressOrAlias, | ||
}; | ||
use log::debug; | ||
|
||
pub async fn exec( | ||
to: AddressOrAlias, | ||
amount: f64, | ||
network: Option<NetworkName>, | ||
) -> Result<()> { | ||
let cfg = Config::load()?; | ||
|
||
// Check network | ||
if cfg.network_name(&network)? == NetworkName::Dev && cfg.sandbox.is_none() { | ||
bail_user_error!( | ||
"No sandbox is currently running. Please run {}.", | ||
styles::command("jstz sandbox start") | ||
); | ||
} | ||
|
||
let to_pkh = to.resolve_l1(&cfg, &network)?; | ||
debug!("resolved `to` -> {}", &to_pkh.to_base58()); | ||
|
||
let amount = convert_tez_to_mutez(amount)?; | ||
let url = "tezos://jstz/withdraw".to_string(); | ||
let http_method = "POST".to_string(); | ||
let gas_limit = 10; // TODO: set proper gas limit | ||
let withdraw = jstz_proto::executor::withdraw::Withdrawal { | ||
amount, | ||
receiver: to_pkh, | ||
}; | ||
let json_data = serde_json::to_string(&withdraw)?; | ||
run::exec(url, http_method, gas_limit, Some(json_data), network, false).await | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
run() { | ||
# Get the current level of the chain. | ||
level="$(curl -s "http://127.0.0.1:18730/chains/main/blocks/head/metadata" | jq .level_info.level)" | ||
if [ -z "$level" ]; then | ||
echo 'Error: No "level_info" found from octez-client rpc get "/chains/main/blocks/head/metadata".\n' | ||
return 1 # Failure | ||
fi | ||
octez_client="$1" | ||
found=false | ||
counter=0 | ||
max="200" | ||
# Search back a $max number of levels for a non-empty rollup outbox message. | ||
while [ "$found" = "false" ] && [ "$counter" -lt "$max" ]; do | ||
msg=$(curl -s "http://127.0.0.1:8932/global/block/head/outbox/${level}/messages") | ||
case "$msg" in | ||
*'[]'*) | ||
level=$((level - 1)) | ||
;; | ||
*) | ||
found=true | ||
;; | ||
esac | ||
counter=$((counter + 1)) | ||
done | ||
if [ "$found" = "true" ]; then | ||
echo "Found outbox message at $level" | ||
while true; do | ||
payload=$(curl -s "http://127.0.0.1:8932/global/block/head/helpers/proofs/outbox/${level}/messages?index=0") | ||
proof=$(echo $payload | jq -r ".proof" 2>/dev/null) | ||
if [ "$?" -ne 0 ]; then | ||
sleep 1 | ||
else | ||
break | ||
fi | ||
done | ||
proof=$(echo $payload | jq -r ".proof") | ||
commitment_hash=$(echo $payload | jq -r ".commitment") | ||
$octez_client -E http://127.0.0.1:18730 -d "$(cat ~/.jstz/config.json | jq -r ".sandbox.octez_client_dir")" execute outbox message of smart rollup jstz_rollup from bootstrap1 for commitment hash $commitment_hash and output proof "0x$proof" --burn-cap 999 | ||
else | ||
echo "No messages in the last $max levels.\n" | ||
return 1 | ||
fi | ||
} | ||
|
||
run $1 |