Skip to content

Add crosschain asset 'STAR' supporting for aptos #23

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Move.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "Poly-STC-Bridge"
version = "1.0.12"
version = "1.0.13"

[addresses]
StarcoinFramework = "0x1"
Bridge = "0xe52552637c5897a2d499fbf08216f73e"
SwapAdmin = "0x8c109349c6bd91411d6bc962e080c4a3"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swap项目依赖Bridge,如果Bridge又依赖Swap,就成双向依赖,貌似Move里双向依赖会有问题?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只是简单的把地址放进来了,没有实际依赖,因为需要依赖asset


[dependencies]
StarcoinFramework = { git = "https://github.com/starcoinorg/starcoin-framework.git", rev = "01c84198819310620f2417413c3c800df8292ae5" }
2 changes: 1 addition & 1 deletion integration-tests/cross-chain/cross_chain_script.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 6 tasks

task 3 'run'. lines 7-21:
{
"gas_used": 25738111,
"gas_used": 26969809,
"status": "Executed"
}

Expand Down
5 changes: 5 additions & 0 deletions sources/asset/erc20/STAR.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Mock STAR from starswap
/// Origin decleration in https://github.com/Elements-Studio/starswap-core/blob/master/sources/gov/STAR.move
module SwapAdmin::STAR {
struct STAR has copy, drop, store {}
}
12 changes: 12 additions & 0 deletions sources/cross-chain/CrossChainConstant.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module Bridge::CrossChainConstant {

const DEFAULT_CHAINID_STARCOIN: u64 = 31;
const DEFAULT_CHAINID_ETHEREUM: u64 = 2;
const DEFAULT_CHAINID_APTOS: u64 = 41;

const PROXY_HASH_STARCOIN: vector<u8> = b"::CrossChainScript";

const ASSET_HASH_STC: vector<u8> = b"0x00000000000000000000000000000001::STC::STC";
const ASSET_HASH_XETH: vector<u8> = b"::XETH::XETH";
const ASSET_HASH_XUSDT: vector<u8> = b"::XUSDT::XUSDT";
const ASSET_HASH_STAR: vector<u8> = b"::STAR::STAR";

public fun get_proxy_hash_starcoin(): vector<u8> {
let ret = Vector::empty<u8>();
Expand All @@ -37,6 +39,13 @@ module Bridge::CrossChainConstant {
ret
}

public fun get_asset_hash_star(): vector<u8> {
let ret = Vector::empty<u8>();
Vector::append(&mut ret, CrossChainLibrary::address_to_hex_string(@SwapAdmin));
Vector::append(&mut ret, ASSET_HASH_STAR);
ret
}

public fun get_default_chain_id_starcoin(): u64 {
DEFAULT_CHAINID_STARCOIN
}
Expand All @@ -45,5 +54,8 @@ module Bridge::CrossChainConstant {
DEFAULT_CHAINID_ETHEREUM
}

public fun get_default_chain_id_aptos(): u64 {
DEFAULT_CHAINID_APTOS
}
}

2 changes: 2 additions & 0 deletions sources/cross-chain/CrossChainGlobal.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module Bridge::CrossChainGlobal {

struct ETHEREUM_CHAIN has key, store {}

struct APTOS_CHAIN has key, store {}

struct ExecutionCapability {
tx_data: vector<u8>,
proof_tx_non_exists: bool,
Expand Down
174 changes: 117 additions & 57 deletions sources/cross-chain/CrossChainRouter.move
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Bridge::CrossChainRouter {

use StarcoinFramework::STC;
use StarcoinFramework::Errors;

use Bridge::XUSDT;
use Bridge::XETH;
use Bridge::SMTProofUtils;
use Bridge::CrossChainManager;
use Bridge::CrossChainGlobal;
use Bridge::LockProxy;
use Bridge::CrossChainManager;
use Bridge::CrossChainProcessCombinator;
use Bridge::LockProxy;
use Bridge::SMTProofUtils;
use Bridge::XETH;
use Bridge::XUSDT;

use StarcoinFramework::Errors;
use StarcoinFramework::STC;
use SwapAdmin::STAR;

const ERROR_DECREPTED: u64 = 1;

Expand All @@ -25,29 +26,35 @@ module Bridge::CrossChainRouter {
// This function is meant to be invoked by the user,
// a certin amount teokens will be locked in the proxy contract the invoker/msg.sender immediately.
// Then the same amount of tokens will be unloked from target chain proxy contract at the target chain with chainId later.
public fun lock(signer: &signer,
from_asset_hash: &vector<u8>,
to_chain_id: u64,
to_address: &vector<u8>,
amount: u128) {
public fun lock(
signer: &signer,
from_asset_hash: &vector<u8>,
to_chain_id: u64,
to_address: &vector<u8>,
amount: u128
) {
if (CrossChainGlobal::asset_hash_match<STC::STC>(from_asset_hash)) {
inner_do_lock<STC::STC>(signer, to_chain_id, to_address, amount);
} else if (CrossChainGlobal::asset_hash_match<XUSDT::XUSDT>(from_asset_hash)) {
inner_do_lock<XUSDT::XUSDT>(signer, to_chain_id, to_address, amount);
} else if (CrossChainGlobal::asset_hash_match<XETH::XETH>(from_asset_hash)) {
inner_do_lock<XETH::XETH>(signer, to_chain_id, to_address, amount);
} else if (CrossChainGlobal::asset_hash_match<STAR::STAR>(from_asset_hash)) {
inner_do_lock<STAR::STAR>(signer, to_chain_id, to_address, amount);
} else {
assert!(false, Errors::invalid_state(ERROR_NO_SUPPORT_BIND_ASSET_TYPE));
}
}

public fun lock_with_stc_fee(signer: &signer,
from_asset_hash: &vector<u8>,
to_chain_id: u64,
to_address: &vector<u8>,
amount: u128,
fee: u128,
id: u128) {
public fun lock_with_stc_fee(
signer: &signer,
from_asset_hash: &vector<u8>,
to_chain_id: u64,
to_address: &vector<u8>,
amount: u128,
fee: u128,
id: u128
) {
if (CrossChainGlobal::asset_hash_match<STC::STC>(from_asset_hash)) {
inner_do_lock<STC::STC>(signer, to_chain_id, to_address, amount);
LockProxy::lock_stc_fee<STC::STC>(signer, to_chain_id, to_address, amount, fee, id);
Expand All @@ -57,45 +64,75 @@ module Bridge::CrossChainRouter {
} else if (CrossChainGlobal::asset_hash_match<XETH::XETH>(from_asset_hash)) {
inner_do_lock<XETH::XETH>(signer, to_chain_id, to_address, amount);
LockProxy::lock_stc_fee<XETH::XETH>(signer, to_chain_id, to_address, amount, fee, id);
} else if (CrossChainGlobal::asset_hash_match<STAR::STAR>(from_asset_hash)) {
inner_do_lock<STAR::STAR>(signer, to_chain_id, to_address, amount);
LockProxy::lock_stc_fee<STAR::STAR>(signer, to_chain_id, to_address, amount, fee, id);
} else {
assert!(false, Errors::invalid_state(ERROR_NO_SUPPORT_BIND_ASSET_TYPE));
}
}

// Do lock operation on inner calling
fun inner_do_lock<TokenT: store>(signer: &signer,
to_chain_id: u64,
to_address: &vector<u8>,
amount: u128) {
fun inner_do_lock<TokenT: store>(
signer: &signer,
to_chain_id: u64,
to_address: &vector<u8>,
amount: u128
) {
if (CrossChainGlobal::chain_id_match<CrossChainGlobal::STARCOIN_CHAIN>(to_chain_id)) {
let (lock_parameters, event) =
LockProxy::lock_with_param_pack<TokenT, CrossChainGlobal::STARCOIN_CHAIN>(signer, to_chain_id, to_address, amount);
LockProxy::lock_with_param_pack<TokenT, CrossChainGlobal::STARCOIN_CHAIN>(
signer,
to_chain_id,
to_address,
amount
);
// Do crosschain option from cross chain manager
CrossChainManager::cross_chain_with_param_pack(signer, lock_parameters);
// Publish lock event
LockProxy::emit_lock_event(event);
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::ETHEREUM_CHAIN>(to_chain_id)) {
let (lock_parameters, event) =
LockProxy::lock_with_param_pack<TokenT, CrossChainGlobal::ETHEREUM_CHAIN>(signer, to_chain_id, to_address, amount);
LockProxy::lock_with_param_pack<TokenT, CrossChainGlobal::ETHEREUM_CHAIN>(
signer,
to_chain_id,
to_address,
amount
);
// Do crosschain option from cross chain manager
CrossChainManager::cross_chain_with_param_pack(signer, lock_parameters);
// Publish lock event
LockProxy::emit_lock_event(event);
} else {
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::APTOS_CHAIN>(to_chain_id)) {
let (lock_parameters, event) =
LockProxy::lock_with_param_pack<TokenT, CrossChainGlobal::APTOS_CHAIN>(
signer,
to_chain_id,
to_address,
amount
);
// Do crosschain option from cross chain manager
CrossChainManager::cross_chain_with_param_pack(signer, lock_parameters);
// Publish lock event
LockProxy::emit_lock_event(event);
}
else {
assert!(false, Errors::invalid_state(ERROR_NO_SUPPORT_LOCK_CHAIN_TYPE));
};
}


// Verify header and execute transaction
public fun verify_header_and_execute_tx(proof: &vector<u8>,
raw_header: &vector<u8>,
header_proof: &vector<u8>,
cur_raw_header: &vector<u8>,
header_sig: &vector<u8>,
merkle_proof_root: &vector<u8>,
merkle_proof_leaf: &vector<u8>,
input_merkle_proof_siblings: &vector<u8>) {
public fun verify_header_and_execute_tx(
proof: &vector<u8>,
raw_header: &vector<u8>,
header_proof: &vector<u8>,
cur_raw_header: &vector<u8>,
header_sig: &vector<u8>,
merkle_proof_root: &vector<u8>,
merkle_proof_leaf: &vector<u8>,
input_merkle_proof_siblings: &vector<u8>
) {
CrossChainGlobal::require_not_freezing();

// Verify header and parse method and args from proof vector
Expand Down Expand Up @@ -125,6 +162,8 @@ module Bridge::CrossChainRouter {
inner_do_unlock_with_param_pack<XUSDT::XUSDT>(header_verified_params, certificate)
} else if (CrossChainGlobal::asset_hash_match<XETH::XETH>(&to_asset_hash)) {
inner_do_unlock_with_param_pack<XETH::XETH>(header_verified_params, certificate)
} else if (CrossChainGlobal::asset_hash_match<STAR::STAR>(&to_asset_hash)) {
inner_do_unlock_with_param_pack<STAR::STAR>(header_verified_params, certificate)
} else {
false
};
Expand All @@ -134,67 +173,88 @@ module Bridge::CrossChainRouter {
};
}

public fun inner_do_unlock<TokenT: store>(_from_chain_id: u64,
_from_contract: &vector<u8>,
_to_asset_hash: &vector<u8>,
_to_address: &vector<u8>,
_amount: u128,
_tx_hash: &vector<u8>,
_cap: &CrossChainGlobal::ExecutionCapability): bool {
public fun inner_do_unlock<TokenT: store>(
_from_chain_id: u64,
_from_contract: &vector<u8>,
_to_asset_hash: &vector<u8>,
_to_address: &vector<u8>,
_amount: u128,
_tx_hash: &vector<u8>,
_cap: &CrossChainGlobal::ExecutionCapability
): bool {
abort Errors::invalid_state(ERROR_DECREPTED)
}

// Do unlock operation on inner calling
fun inner_do_unlock_with_param_pack<TokenT: store>(header_verified_params: CrossChainProcessCombinator::HeaderVerifyedParamPack,
certificate: CrossChainProcessCombinator::MerkleProofCertificate): bool {
fun inner_do_unlock_with_param_pack<TokenT: store>(
header_verified_params: CrossChainProcessCombinator::HeaderVerifyedParamPack,
certificate: CrossChainProcessCombinator::MerkleProofCertificate
): bool {
let from_chain_id = CrossChainProcessCombinator::lookup_from_chain_id(&header_verified_params);
let ret = if (CrossChainGlobal::chain_id_match<CrossChainGlobal::STARCOIN_CHAIN>(from_chain_id)) {
let unlock_event = LockProxy::unlock_with_pack<TokenT, CrossChainGlobal::STARCOIN_CHAIN>(header_verified_params, certificate);
let unlock_event = LockProxy::unlock_with_pack<TokenT, CrossChainGlobal::STARCOIN_CHAIN>(
header_verified_params,
certificate
);
LockProxy::emit_unlock_event<TokenT, CrossChainGlobal::STARCOIN_CHAIN>(unlock_event)
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::ETHEREUM_CHAIN>(from_chain_id)) {
let unlock_event = LockProxy::unlock_with_pack<TokenT, CrossChainGlobal::ETHEREUM_CHAIN>(header_verified_params, certificate);
let unlock_event = LockProxy::unlock_with_pack<TokenT, CrossChainGlobal::ETHEREUM_CHAIN>(
header_verified_params,
certificate
);
LockProxy::emit_unlock_event<TokenT, CrossChainGlobal::ETHEREUM_CHAIN>(unlock_event)
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::APTOS_CHAIN>(from_chain_id)) {
let unlock_event = LockProxy::unlock_with_pack<TokenT, CrossChainGlobal::APTOS_CHAIN>(
header_verified_params,
certificate
);
LockProxy::emit_unlock_event<TokenT, CrossChainGlobal::APTOS_CHAIN>(unlock_event)
} else {
abort Errors::invalid_state(ERROR_NO_SUPPORT_UNLOCK_CHAIN_TYPE)
};
ret
}


public fun bind_proxy_hash(signer: &signer,
to_chain_id: u64,
target_proxy_hash: &vector<u8>) {
public fun bind_proxy_hash(signer: &signer, to_chain_id: u64, target_proxy_hash: &vector<u8>) {
if (CrossChainGlobal::chain_id_match<CrossChainGlobal::STARCOIN_CHAIN>(to_chain_id)) {
LockProxy::bind_proxy_hash<CrossChainGlobal::STARCOIN_CHAIN>(signer, to_chain_id, target_proxy_hash);
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::ETHEREUM_CHAIN>(to_chain_id)) {
LockProxy::bind_proxy_hash<CrossChainGlobal::ETHEREUM_CHAIN>(signer, to_chain_id, target_proxy_hash);
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::APTOS_CHAIN>(to_chain_id)) {
LockProxy::bind_proxy_hash<CrossChainGlobal::APTOS_CHAIN>(signer, to_chain_id, target_proxy_hash);
} else {
abort Errors::invalid_state(ERROR_NO_SUPPORT_BIND_CHAIN_TYPE)
};
}

public fun bind_asset_hash(signer: &signer,
from_asset_hash: &vector<u8>,
to_chain_id: u64,
to_asset_hash: &vector<u8>) {
public fun bind_asset_hash(
signer: &signer,
from_asset_hash: &vector<u8>,
to_chain_id: u64,
to_asset_hash: &vector<u8>
) {
if (CrossChainGlobal::asset_hash_match<STC::STC>(from_asset_hash)) {
inner_do_bind_asset_hash<STC::STC>(signer, to_chain_id, to_asset_hash);
} else if (CrossChainGlobal::asset_hash_match<XUSDT::XUSDT>(from_asset_hash)) {
inner_do_bind_asset_hash<XUSDT::XUSDT>(signer, to_chain_id, to_asset_hash);
} else if (CrossChainGlobal::asset_hash_match<XETH::XETH>(from_asset_hash)) {
inner_do_bind_asset_hash<XETH::XETH>(signer, to_chain_id, to_asset_hash);
} else {
} else if (CrossChainGlobal::asset_hash_match<STAR::STAR>(from_asset_hash)) {
inner_do_bind_asset_hash<STAR::STAR>(signer, to_chain_id, to_asset_hash);
}
else {
assert!(false, Errors::invalid_state(ERROR_NO_SUPPORT_BIND_ASSET_TYPE));
};
}

fun inner_do_bind_asset_hash<TokenT: store>(signer: &signer,
to_chain_id: u64,
to_asset_hash: &vector<u8>) {
fun inner_do_bind_asset_hash<TokenT: store>(signer: &signer, to_chain_id: u64, to_asset_hash: &vector<u8>) {
if (CrossChainGlobal::chain_id_match<CrossChainGlobal::STARCOIN_CHAIN>(to_chain_id)) {
LockProxy::bind_asset_hash<TokenT, CrossChainGlobal::STARCOIN_CHAIN>(signer, to_chain_id, to_asset_hash);
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::ETHEREUM_CHAIN>(to_chain_id)) {
LockProxy::bind_asset_hash<TokenT, CrossChainGlobal::ETHEREUM_CHAIN>(signer, to_chain_id, to_asset_hash);
} else if (CrossChainGlobal::chain_id_match<CrossChainGlobal::APTOS_CHAIN>(to_chain_id)) {
LockProxy::bind_asset_hash<TokenT, CrossChainGlobal::APTOS_CHAIN>(signer, to_chain_id, to_asset_hash);
} else {
assert!(false, Errors::invalid_state(ERROR_NO_SUPPORT_BIND_CHAIN_TYPE));
};
Expand Down
Loading