Skip to content

Commit

Permalink
Merge pull request #1034 from rainlanguage/2024-11-30-qoute-gas-option
Browse files Browse the repository at this point in the history
add gas option for quote ops
  • Loading branch information
rouzwelt authored Dec 1, 2024
2 parents 8ecda48 + 6bb1f32 commit 7d0c170
Show file tree
Hide file tree
Showing 18 changed files with 252 additions and 49 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/manual-package-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ jobs:
# set npm version to rust crate version
- name: Set Version
run: echo "NEW_VERSION=$(node ./scripts/version.js ${{ inputs.version-level }})" >> $GITHUB_ENV
working-directory: packages/orderbook

# Commit changes and tag
- name: Commit And Tag
run: |
git add "package.json"
git add "packages/orderbook/package.json"
git add "package-lock.json"
git commit -m "Release ${{ env.NEW_VERSION }}"
git tag ${{ env.NEW_VERSION }}
Expand All @@ -95,17 +96,18 @@ jobs:

# Create npm package tarball to put in release files
- name: Create NPM Package Tarball
run: echo "NPM_PACKAGE=$(npm pack --silent)" >> $GITHUB_ENV
run: echo "NPM_PACKAGE=$(npm pack --silent -w @rainlanguage/orderbook)" >> $GITHUB_ENV

- name: Rename NPM Package Tarball
run: mv ${{ env.NPM_PACKAGE }} orderbook_npm_package_${{ env.NEW_VERSION }}.tgz

# publish to npm
- name: Publish To NPM
uses: JS-DevTools/npm-publish@v1
uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.NPM_TOKEN }}
access: public
package: orderbook_npm_package_${{ env.NEW_VERSION }}.tgz

# Create gitHub release with npm tarball
- name: Create GitHub Release
Expand Down
36 changes: 28 additions & 8 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://github.com/rainprotocol/rain.orderbook"
[workspace.dependencies]
foundry-block-explorers = "0.2.6"
alloy = { version = "0.1.4" }
alloy-ethers-typecast = { git = "https://github.com/rainlanguage/alloy-ethers-typecast", rev = "0881930a22e84db49ba955c5b88e790e1266ac66" }
alloy-ethers-typecast = { git = "https://github.com/rainlanguage/alloy-ethers-typecast", rev = "0e636d2bc865a49c6eaea95ab139465708a65698" }
anyhow = "1.0.70"
async-trait = "0.1.77"
clap = { version = "4.2.5", features = ["cargo", "derive"] }
Expand Down Expand Up @@ -52,7 +52,7 @@ derive_builder = "0.20.0"
thirtyfour = "0.31.0"
test-context = "0.3.0"
portpicker = "0.1.1"
rain-erc = { git = "https://github.com/rainlanguage/rain.erc", rev = "80bb411b161c1ee992a7974d325a783214046d5b" }
rain-erc = { git = "https://github.com/rainlanguage/rain.erc", rev = "e86b46bfce535f4c562b93a212171fbf19953a8f" }
rain-error-decoding = { git = "https://github.com/rainlanguage/rain.error", rev = "72d9577fdaf7135113847027ba951f9a43b41827" }

[workspace.dependencies.rain_orderbook_bindings]
Expand Down
14 changes: 14 additions & 0 deletions crates/bindings/src/wasm_traits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

pub mod prelude {
pub use serde_wasm_bindgen::{from_value, to_value};
pub use tsify::Tsify;
Expand All @@ -9,6 +11,18 @@ pub mod prelude {
};
}

// A trait for converting types to U256
pub trait TryIntoU256 {
type Error;
fn try_into_u256(&self) -> Result<alloy::primitives::U256, Self::Error>;
}
impl TryIntoU256 for js_sys::BigInt {
type Error = alloy::primitives::ruint::ParseError;
fn try_into_u256(&self) -> Result<alloy::primitives::U256, Self::Error> {
alloy::primitives::U256::from_str(&format!("{}", &self))
}
}

#[macro_export]
macro_rules! impl_main_wasm_traits {
($type_name:path) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/common/src/add_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl AddOrderArgs {
address: input.token.address,
call: decimalsCall {},
block_number: None,
gas: None,
};
let decimals = client.read(parameters).await?._0;
inputs.push(IO {
Expand All @@ -118,6 +119,7 @@ impl AddOrderArgs {
address: output.token.address,
call: decimalsCall {},
block_number: None,
gas: None,
};
let decimals = client.read(parameters).await?._0;
outputs.push(IO {
Expand Down
4 changes: 4 additions & 0 deletions crates/common/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl ERC20 {
address: self.address,
call: decimalsCall {},
block_number: None,
gas: None,
};
Ok(client.read(parameters).await?._0)
}
Expand All @@ -60,6 +61,7 @@ impl ERC20 {
address: self.address,
call: nameCall {},
block_number: None,
gas: None,
};
Ok(client.read(parameters).await?._0)
}
Expand All @@ -70,6 +72,7 @@ impl ERC20 {
address: self.address,
call: symbolCall {},
block_number: None,
gas: None,
};
Ok(client.read(parameters).await?._0)
}
Expand All @@ -79,6 +82,7 @@ impl ERC20 {

let results = client
.read(ReadContractParameters {
gas: None,
address: multicall_address
.map_or(Address::from_hex(MULTICALL3_ADDRESS).unwrap(), |s| {
Address::from_str(&s).unwrap_or(Address::default())
Expand Down
8 changes: 7 additions & 1 deletion crates/quote/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ impl Quoter {
pub async fn run(&self) -> anyhow::Result<QuoterResult> {
let result: QuoterResult = match self.input.read_content()? {
InputContentType::Target(v) => v
.do_quote(self.rpc.as_str(), self.block_number, self.multicall_address)
.do_quote(
self.rpc.as_str(),
self.block_number,
None,
self.multicall_address,
)
.await?
.into(),
InputContentType::Spec(v) => {
Expand All @@ -99,6 +104,7 @@ impl Quoter {
sg.as_str(),
self.rpc.as_str(),
self.block_number,
None,
self.multicall_address,
)
.await?
Expand Down
38 changes: 34 additions & 4 deletions crates/quote/src/js_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use alloy::primitives::{
Address, U256,
};
use rain_orderbook_bindings::js_api::{Quote, SignedContextV1};
use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*};
use rain_orderbook_bindings::{
impl_all_wasm_traits,
wasm_traits::{prelude::*, TryIntoU256},
};
use rain_orderbook_subgraph_client::{types::common::Order, utils::make_order_id};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
Expand Down Expand Up @@ -82,6 +85,7 @@ pub async fn do_quote_targets(
quote_targets: &BatchQuoteTarget,
rpc_url: &str,
block_number: Option<u64>,
gas: Option<js_sys::BigInt>,
multicall_address: Option<String>,
) -> Result<JsValue, Error> {
let mut multicall_address_error = "multicall address, ".to_string();
Expand All @@ -90,14 +94,20 @@ pub async fn do_quote_targets(
.inspect_err(|e| multicall_address_error.push_str(&e.to_string()))
.expect_throw(&multicall_address_error)
});
let mut gas_error = "gas, ".to_string();
let gas_value = gas.map(|v| {
v.try_into_u256()
.inspect_err(|e| gas_error.push_str(&e.to_string()))
.expect_throw(&gas_error)
});
let quote_targets: Vec<MainQuoteTarget> = quote_targets
.0
.iter()
.map(|v| MainQuoteTarget::from(v.clone()))
.collect();
let batch_quote_target = MainBatchQuoteTarget(quote_targets);
match batch_quote_target
.do_quote(rpc_url, block_number, multicall_address)
.do_quote(rpc_url, block_number, gas_value, multicall_address)
.await
{
Err(e) => Err(e),
Expand All @@ -116,6 +126,7 @@ pub async fn do_quote_specs(
subgraph_url: &str,
rpc_url: &str,
block_number: Option<u64>,
gas: Option<js_sys::BigInt>,
multicall_address: Option<String>,
) -> Result<JsValue, Error> {
let mut multicall_address_error = "multicall address, ".to_string();
Expand All @@ -124,14 +135,26 @@ pub async fn do_quote_specs(
.inspect_err(|e| multicall_address_error.push_str(&e.to_string()))
.expect_throw(&multicall_address_error)
});
let mut gas_error = "gas, ".to_string();
let gas_value = gas.map(|v| {
v.try_into_u256()
.inspect_err(|e| gas_error.push_str(&e.to_string()))
.expect_throw(&gas_error)
});
let quote_specs: Vec<MainQuoteSpec> = quote_specs
.0
.iter()
.map(|v| MainQuoteSpec::from(v.clone()))
.collect();
let batch_quote_spec = MainBatchQuoteSpec(quote_specs);
match batch_quote_spec
.do_quote(subgraph_url, rpc_url, block_number, multicall_address)
.do_quote(
subgraph_url,
rpc_url,
block_number,
gas_value,
multicall_address,
)
.await
{
Err(e) => Err(e),
Expand Down Expand Up @@ -194,9 +217,16 @@ pub async fn get_order_quote(
order: Vec<Order>,
rpc_url: &str,
block_number: Option<u64>,
gas: Option<js_sys::BigInt>,
) -> Result<JsValue, Error> {
let mut gas_error = "gas, ".to_string();
let gas_value = gas.map(|v| {
v.try_into_u256()
.inspect_err(|e| gas_error.push_str(&e.to_string()))
.expect_throw(&gas_error)
});
Ok(to_value(
&get_order_quotes(order, block_number, rpc_url.to_string())
&get_order_quotes(order, block_number, rpc_url.to_string(), gas_value)
.await
.map(|v| {
v.into_iter()
Expand Down
5 changes: 3 additions & 2 deletions crates/quote/src/order_quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub async fn get_order_quotes(
orders: Vec<Order>,
block_number: Option<u64>,
rpc_url: String,
gas: Option<U256>,
) -> Result<Vec<BatchOrderQuotesResponse>, Error> {
let mut results: Vec<BatchOrderQuotesResponse> = Vec::new();

Expand Down Expand Up @@ -99,7 +100,7 @@ pub async fn get_order_quotes(
);

let quote_values = BatchQuoteTarget(quote_targets)
.do_quote(&rpc_url, Some(req_block_number), None)
.do_quote(&rpc_url, Some(req_block_number), gas, None)
.await;

if let Ok(quote_values) = quote_values {
Expand Down Expand Up @@ -314,7 +315,7 @@ amount price: context<3 0>() context<4 0>();
trades: vec![],
};

let result = get_order_quotes(vec![order], None, local_evm.url())
let result = get_order_quotes(vec![order], None, local_evm.url(), None)
.await
.unwrap();

Expand Down
Loading

0 comments on commit 7d0c170

Please sign in to comment.