Skip to content

Commit

Permalink
finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Aug 3, 2023
1 parent b1ba1e1 commit 5bf0eb3
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 232 deletions.
1 change: 1 addition & 0 deletions code/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions code/xcvm/cosmwasm/contracts/gateway/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ fn transfer_from_user(
return Err(ContractError::InsufficientFunds)?
}
},
msg::AssetReference::Virtual { cw20_address } =>
transfers.push(Cw20Contract(cw20_address).call(Cw20ExecuteMsg::TransferFrom {
msg::AssetReference::Cw20 { contract } =>
transfers.push(Cw20Contract(contract).call(Cw20ExecuteMsg::TransferFrom {
owner: user.to_string(),
recipient: self_address.to_string(),
amount: (*amount).into(),
Expand Down Expand Up @@ -232,8 +232,8 @@ fn send_funds_to_interpreter(
amount: vec![Coin::new(amount, denom)],
}
.into(),
msg::AssetReference::Virtual { cw20_address } => {
let contract = Cw20Contract(cw20_address);
msg::AssetReference::Cw20 { contract } => {
let contract = Cw20Contract(contract);
contract.call(Cw20ExecuteMsg::Transfer {
recipient: interpreter_address.clone(),
amount: amount.into(),
Expand Down
30 changes: 15 additions & 15 deletions code/xcvm/cosmwasm/contracts/interpreter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ pub fn interpret_call(
asset_id,
)?;
match reference.local {
AssetReference::Virtual { cw20_address } =>
Cow::Owned(cw20_address.into_string().into()),
AssetReference::Cw20 { contract } =>
Cow::Owned(contract.into_string().into()),
AssetReference::Native { denom } => Cow::Owned(denom.into()),
}
},
Expand All @@ -242,10 +242,10 @@ pub fn interpret_call(
asset_id,
)?;
let amount = match reference.local {
AssetReference::Virtual { cw20_address } => apply_amount_to_cw20_balance(
AssetReference::Cw20 { contract } => apply_amount_to_cw20_balance(
deps,
&balance,
&cw20_address,
&contract,
&env.contract.address,
),
AssetReference::Native { denom } =>
Expand Down Expand Up @@ -308,10 +308,10 @@ pub fn interpret_spawn(
.apply(coin.amount.into())
.map_err(|_| ContractError::ArithmeticError)
},
AssetReference::Virtual { cw20_address } => apply_amount_to_cw20_balance(
AssetReference::Cw20 { contract } => apply_amount_to_cw20_balance(
deps.as_ref(),
&balance,
cw20_address,
contract,
&env.contract.address,
),
}?;
Expand All @@ -324,8 +324,8 @@ pub fn interpret_spawn(
to_address: gateway_address.clone().into(),
amount: vec![Coin { denom, amount: transfer_amount.into() }],
}),
AssetReference::Virtual { cw20_address } => response.add_message(
Cw20Contract(cw20_address).call(Cw20ExecuteMsg::Transfer {
AssetReference::Cw20 { contract } => response.add_message(
Cw20Contract(contract).call(Cw20ExecuteMsg::Transfer {
recipient: gateway_address.clone().into(),
amount: transfer_amount.into(),
})?,
Expand Down Expand Up @@ -396,12 +396,12 @@ pub fn interpret_transfer(
amount: vec![coin],
})
},
AssetReference::Virtual { cw20_address } => {
let contract = Cw20Contract(cw20_address.clone());
AssetReference::Cw20 { contract } => {
let contract = Cw20Contract(contract.clone());
let transfer_amount = apply_amount_to_cw20_balance(
deps.as_ref(),
&balance,
&cw20_address,
&contract.0,
&env.contract.address,
)?;
response.add_message(contract.call(Cw20ExecuteMsg::Transfer {
Expand Down Expand Up @@ -463,25 +463,25 @@ fn handle_call_result(deps: DepsMut, msg: Reply) -> StdResult<Response> {
/// Calculates and returns the actual balance to process
///
/// * `balance`: Balance to be transformed into the actual balance
/// * `cw20_address`: Address of the corresponding cw20 contract
/// * `contract`: Address of the corresponding cw20 contract
/// * `self_address`: This interpreter's address
fn apply_amount_to_cw20_balance<A: Into<String> + Clone>(
deps: Deps,
balance: &Balance,
cw20_address: A,
contract: A,
self_address: A,
) -> Result<u128> {
let balance_response =
deps.querier.query::<BalanceResponse>(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: cw20_address.clone().into(),
contract_addr: contract.clone().into(),
msg: to_binary(&Cw20QueryMsg::Balance { address: self_address.into() })?,
}))?;

if balance.is_unit {
// If the balance is unit, we need to take `decimals` into account.
let token_info =
deps.querier.query::<TokenInfoResponse>(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: cw20_address.into(),
contract_addr: contract.into(),
msg: to_binary(&Cw20QueryMsg::TokenInfo {})?,
}))?;
balance
Expand Down
3 changes: 3 additions & 0 deletions code/xcvm/lib/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ strum.workspace = true
thiserror = { workspace = true }
ibc-proto = { workspace = true, default-features = false, features = ["serde", "parity-scale-codec"]}
serde-cw-value = { workspace = true, default-features = false}
xcm = { workspace = true, default-features = false, optional = true}

[build-dependencies]
prost-build = { workspace = true }

[features]
default = ["std"]
cosmwasm = ["cw-storage-plus", "cw20"]
substrate = ["xcm"]
std = [
"cosmwasm-std/std",
"xcm/std",
"cw-storage-plus/std",
"dep:cosmwasm-schema",
"dep:schemars",
Expand Down
Loading

0 comments on commit 5bf0eb3

Please sign in to comment.