Skip to content
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

Refactor rosetta-client interface #207

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
355 changes: 354 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions chains/astar/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ subxt = { workspace = true, features = ["substrate-compat"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

[dev-dependencies]
alloy-primitives = { version = "0.5" }
alloy-sol-types = { version = "0.5" }
ethers-solc = "2.0"
rosetta-client.workspace = true
rosetta-docker = { workspace = true, features = ["tests"] }
Expand Down
51 changes: 32 additions & 19 deletions chains/astar/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use rosetta_config_astar::metadata::{
dev as astar_metadata,
dev::runtime_types::{frame_system::AccountInfo, pallet_balances::types::AccountData},
};
use rosetta_config_ethereum::{EthereumMetadata, EthereumMetadataParams};
use rosetta_config_ethereum::{
EthereumMetadata, EthereumMetadataParams, Query as EthQuery, QueryResult as EthQueryResult,
};
use rosetta_core::{
crypto::{
address::{Address, AddressFormat},
PublicKey,
},
types::{
Block, BlockIdentifier, CallRequest, Coin, PartialBlockIdentifier, Transaction,
TransactionIdentifier,
Block, BlockIdentifier, Coin, PartialBlockIdentifier, Transaction, TransactionIdentifier,
},
BlockchainClient, BlockchainConfig,
};
use rosetta_server::ws::default_client;
use rosetta_server_ethereum::MaybeWsEthereumClient;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sp_core::crypto::Ss58AddressFormat;
use std::sync::Arc;
use subxt::{
Expand Down Expand Up @@ -124,6 +124,8 @@ impl BlockchainClient for AstarClient {
type MetadataParams = AstarMetadataParams;
type Metadata = AstarMetadata;
type EventStream<'a> = <MaybeWsEthereumClient as BlockchainClient>::EventStream<'a>;
type Call = EthQuery;
type CallResult = EthQueryResult;

fn config(&self) -> &BlockchainConfig {
self.client.config()
Expand Down Expand Up @@ -221,7 +223,7 @@ impl BlockchainClient for AstarClient {
self.client.block_transaction(block_identifier, tx).await
}

async fn call(&self, req: &CallRequest) -> Result<Value> {
async fn call(&self, req: &EthQuery) -> Result<EthQueryResult> {
self.client.call(req).await
}

Expand All @@ -233,11 +235,21 @@ impl BlockchainClient for AstarClient {
#[cfg(test)]
mod tests {
use super::*;
use alloy_sol_types::{sol, SolCall};
use ethers_solc::{artifacts::Source, CompilerInput, EvmVersion, Solc};
use rosetta_docker::Env;
use sha3::Digest;
use std::{collections::BTreeMap, path::Path};

sol! {
interface TestContract {
event AnEvent();
function emitEvent() public {
emit AnEvent();
}
}
}

pub async fn client_from_config(config: BlockchainConfig) -> Result<AstarClient> {
let url = config.node_uri.to_string();
AstarClient::from_config(config, url.as_str()).await
Expand Down Expand Up @@ -297,24 +309,25 @@ mod tests {
wallet.faucet(faucet).await?;

let bytes = compile_snippet(
r#"
r"
event AnEvent();
function emitEvent() public {
emit AnEvent();
}
"#,
",
)?;
let tx_hash = wallet.eth_deploy_contract(bytes).await?;

let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let contract_address = receipt.get("contractAddress").and_then(Value::as_str).unwrap();
let tx_hash =
wallet.eth_send_call(contract_address, "function emitEvent()", &[], 0).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let logs = receipt.get("logs").and_then(Value::as_array).unwrap();
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
let contract_address = receipt.contract_address.unwrap();
let tx_hash = {
let data = TestContract::emitEventCall::SELECTOR.to_vec();
wallet.eth_send_call(contract_address.0, data, 0).await?
};
let receipt = wallet.eth_transaction_receipt(tx_hash).await?;
let logs = receipt.logs;
assert_eq!(logs.len(), 1);
let topic = logs[0]["topics"][0].as_str().unwrap();
let expected = format!("0x{}", hex::encode(sha3::Keccak256::digest("AnEvent()")));
let topic = logs[0].topics[0];
let expected = H256::from_slice(sha3::Keccak256::digest("AnEvent()").as_ref());
assert_eq!(topic, expected);
env.shutdown().await?;
Ok(())
Expand All @@ -331,14 +344,14 @@ mod tests {
wallet.faucet(faucet).await?;

let bytes = compile_snippet(
r#"
r"
function identity(bool a) public view returns (bool) {
return a;
}
"#,
",
)?;
let tx_hash = wallet.eth_deploy_contract(bytes).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let receipt = wallet.eth_transaction_receipt(tx_hash).await?;
let contract_address = receipt["contractAddress"].as_str().unwrap();

let response = wallet
Expand Down
1 change: 1 addition & 0 deletions chains/bitcoin/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rosetta-config-bitcoin.workspace = true
rosetta-core.workspace = true
serde_json = "1.0"
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
void = "1.0"

[dev-dependencies]
rosetta-docker = { workspace = true, features = ["tests"] }
11 changes: 6 additions & 5 deletions chains/bitcoin/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use bitcoincore_rpc_async::{bitcoin::BlockHash, Auth, Client, RpcApi};
use rosetta_core::{
crypto::{address::Address, PublicKey},
types::{
Block, BlockIdentifier, CallRequest, Coin, PartialBlockIdentifier, Transaction,
TransactionIdentifier,
Block, BlockIdentifier, Coin, PartialBlockIdentifier, Transaction, TransactionIdentifier,
},
BlockchainClient, BlockchainConfig,
};
use serde_json::Value;
use std::str::FromStr;
use void::{unreachable, Void};

pub type BitcoinMetadataParams = ();
pub type BitcoinMetadata = ();
Expand Down Expand Up @@ -57,6 +56,8 @@ impl BlockchainClient for BitcoinClient {
type MetadataParams = BitcoinMetadataParams;
type Metadata = BitcoinMetadata;
type EventStream<'a> = rosetta_core::EmptyEventStream;
type Call = Void;
type CallResult = ();

fn config(&self) -> &BlockchainConfig {
&self.config
Expand Down Expand Up @@ -170,8 +171,8 @@ impl BlockchainClient for BitcoinClient {
anyhow::bail!("not implemented")
}

async fn call(&self, _req: &CallRequest) -> Result<Value> {
anyhow::bail!("not implemented")
async fn call(&self, req: &Void) -> Result<()> {
unreachable(*req)
}
}

Expand Down
5 changes: 5 additions & 0 deletions chains/ethereum/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ description = "Ethereum configuration."

[dependencies]
anyhow = "1.0"
ethereum-types = { version = "0.14", default-features = false, features = ["rlp", "ethbloom", "codec", "num-traits"] }
rosetta-config-astar = { workspace = true }
rosetta-core.workspace = true
serde.workspace = true

[features]
default = ["std"]
std = ["ethereum-types/std", "ethereum-types/serialize"]
22 changes: 5 additions & 17 deletions chains/ethereum/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
mod types;

use anyhow::Result;
pub use ethereum_types;

use rosetta_config_astar::config as astar_config;
use rosetta_core::{
crypto::{address::AddressFormat, Algorithm},
BlockchainConfig, NodeUri,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
pub use types::*;

/// Retrieve the [`BlockchainConfig`] from the provided polygon `network`
///
Expand Down Expand Up @@ -116,19 +120,3 @@ fn evm_config(
testnet: is_dev,
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EthereumMetadataParams {
pub destination: Vec<u8>,
pub amount: [u64; 4],
pub data: Vec<u8>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct EthereumMetadata {
pub chain_id: u64,
pub nonce: u64,
pub max_priority_fee_per_gas: [u64; 4],
pub max_fee_per_gas: [u64; 4],
pub gas_limit: [u64; 4],
}
Loading
Loading