Skip to content

Commit

Permalink
this is how it should be
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Nov 25, 2023
1 parent 86dff9d commit ad805f8
Show file tree
Hide file tree
Showing 127 changed files with 22,550 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,5 @@ cython_debug/
# Built Visual Studio Code Extensions
*.vsix
.vscode/settings.json

**/schema/**
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ strum = "^0.25"
strum_macros = "^0.25"
tuples = { version = "^1.14.0" }

no-panic = "^0.1"

bip32 = { version = "^0.5.1", default-features = false, features = ["alloc", "secp256k1", "mnemonic", "bip39"] }
prost-types = { version = "^0.12.3", default-features = false }
Expand Down
35 changes: 35 additions & 0 deletions contracts/cosmwasm/executor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
authors = ["Composable Developers"]
description = "CVM Executor contract"
edition = "2021"
name = "cw-xc-executor"
repository = "https://github.com/ComposableFi/composable"
version = "0.2.0"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
library = []
std = ["xc-core/std", "dep:cosmwasm-schema", "dep:serde_json"]
default = ["std"]

[dependencies]
serde_json = { workspace = true, optional = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cosmwasm-schema = { workspace = true, optional = true }
cw-utils = { workspace = true }
cw2 = { workspace = true }
cw20 = { workspace = true }
num = { workspace = true }
hex = { workspace = true, default-features = false, features = ["alloc"] }
schemars = { workspace = true }
serde = { workspace = true }
serde-json-wasm = { workspace = true }
serde-cw-value = { workspace = true }
thiserror = { workspace = true }
prost = { workspace = true, features = ["prost-derive"] }
xc-core = { path = "../../../lib/core", features = [
"cosmwasm",
], default-features = false }
96 changes: 96 additions & 0 deletions contracts/cosmwasm/executor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# CVM Executor

Receives and stores user funds.
Fully owned by user.
Delegates cross chain execution to gateway.

Instantiated as many instances of the CVM interpreter contract. On some chains, we can use probabilistically generated sub_accounts, but for most, we instantiate a contract instance.

## Events

Note that these events will be yield from the router in production.

### Instantiate contract
```json
{
"type": "wasm-xcvm.executor.instantiated",
"attributes": [
{
"key": "data",
"value": "{BASE64_ENCODED_DATA}"
}
]
}
```

- **BASE64_ENCODED_DATA**: base64 encoded `(network_id, user_id)` pair.

### Execute contract
```json
{
"type": "wasm-xcvm.executor.executed",
"attributes": [
{
"key": "program",
"value": "{XCVM_PROGRAM_TAG}"
}
]
}
```

- **XCVM_PROGRAM_TAG**: Tag of the executed XCVM program

### Execute spawn instruction

```json
{
"type": "wasm-xcvm.executor.spawn",
"attributes": [
{
"key": "origin_network_id",
"value": "{ORIGIN_NETWORK_ID}"
},
{
"key": "origin_user_id",
"value": "{ORIGIN_USER_ID}"
},
{
"key": "program",
"value": "{XCVM_PROGRAM}"
}
]
}
```

- **ORIGIN_NETWORK_ID**: Network id of the origin. Eg. Picasso, Ethereum
- **ORIGIN_USER_ID**: Chain agnostic user identifier of the origin. Eg. contract_address in Juno
- **XCVM_PROGRAM**: Json-encoded xcvm program. Note that although it is json-encoded, it is put as a string because of the restrictions of cosmwasm.

## Usage

The XCVM interpreter contract interprets the XCVM programs. Available instructions are:


### Call
Which is used to call a contract. See that the encoded payload must be in a format:
```
{
"address": "contract-addr",
"payload": "json-encoded ExecuteMsg struct"
}
```

### Transfer
Queries `gateway`, gets the contract address and then executes that contract to do the transfer.

### Spawn
Emits `spawn` event with the given parameters.

## Compile

```sh
RUSTFLAGS='-C link-arg=-s' cargo b --package=xcvm-interpreter --target=wasm32-unknown-unknown --profile="cosmwasm-contracts"
```

* `-C link-arg=-s` is used for stripping the binary which reduces the binary size drastically.
* `--profile="cosmwasm-contracts"` must be used for cosmwasm contracts.
9 changes: 9 additions & 0 deletions contracts/cosmwasm/executor/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
disallowed-types = [
# { path = "usize", reason = "variable size" }, # cannot on now, because serde, even if there is no usize in type
{ path = "f64", resdason = "harware dependant" },
{ path = "f32", reason = "harware dependant" },
{ path = "num_traits::float::*", reason = "harware dependant" },
{ path = "serde_json::to_string", reason = "use serde_json_wasm::* and serde_cw_value::*" },
]

disallowed-methods = ["std::time::Duration::as_secs_f64"]
20 changes: 20 additions & 0 deletions contracts/cosmwasm/executor/src/authenticate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{
error::{ContractError, Result},
state::OWNERS,
};
use cosmwasm_std::{Addr, Deps};

/// Authenticated token, MUST be private and kept in this module.
/// MUST ONLY be instantiated by [`ensure_owner`].
pub struct Authenticated(());

/// Ensure that the caller is either the current interpreter or listed in the owners of the
/// interpreter.
/// Any operation executing against the interpreter must pass this check.
pub fn ensure_owner(deps: Deps, self_addr: &Addr, sender: Addr) -> Result<Authenticated> {
if sender == self_addr || OWNERS.has(deps.storage, sender) {
Ok(Authenticated(()))
} else {
Err(ContractError::NotAuthorized)
}
}
27 changes: 27 additions & 0 deletions contracts/cosmwasm/executor/src/bin/interpreter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[cfg(feature = "std")]
#[allow(clippy::disallowed_methods)]
fn main() {
use cosmwasm_schema::write_api;
use cw_xc_executor::msg::*;

write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
}
let events = schemars::gen::SchemaGenerator::default()
.into_root_schema_for::<cw_xc_executor::events::CvmInterpreter>();

// same as in above macro
let mut out_dir = std::env::current_dir().unwrap();
out_dir.push("schema");

use ::std::fs::write;

let path = out_dir.join(concat!("events", ".json"));

write(&path, serde_json::to_string_pretty(&events).unwrap()).unwrap();
}

#[cfg(not(feature = "std"))]
fn main() {}
Loading

0 comments on commit ad805f8

Please sign in to comment.