Skip to content

Commit 24311b8

Browse files
authored
Merge pull request #62 from lfglabs-dev/fix/rpc_version
fix: use v0_9 rpc version
2 parents 159f91f + f5c9a85 commit 24311b8

9 files changed

Lines changed: 104 additions & 137 deletions

File tree

bot/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ serde = { version = "1.0.152", features = ["derive"] }
1414
toml = "0.5.10"
1515
futures = "0.3"
1616
url = "2.3.1"
17-
bigdecimal = "0.3.0"
1817
anyhow = "1.0"
1918
serenity = "0.11.5"
2019
serde_json = "1.0.96"
2120
lazy_static = "1.4.0"
2221
reqwest = "0.11.18"
2322
num-integer = "0.1.45"
24-
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "c974e5cb42e8d8344cee910b76005ec46b4dd3ed" }
25-
starknet-id = { git = "https://github.com/starknet-id/starknetid.rs", rev = "2b30c2453b96789a628c86d2edebb1023fa2e77d" }
23+
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "85906137d634c86b07de20fa33071e5cf186ce21" }
24+
starknet-id = { git = "https://github.com/starknet-id/starknetid.rs", rev = "04958762b3085311d4f0d865941295a6411a694b" }
2625
serde_derive = "1.0.183"
2726
env_logger = "0.10.0"
2827
tonic = "0.10.0"
2928
prost = "0.12.1"
29+
bigdecimal = { version = "0.4", features = ["serde"] }
3030

3131
[build-dependencies]
3232
tonic-build = "0.10.0"

bot/src/bot.rs

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ use chrono::Utc;
1212
use futures::stream::{self, StreamExt};
1313
use mongodb::options::FindOneOptions;
1414
use starknet::accounts::ConnectedAccount;
15+
use starknet::core::types::Call;
1516
use starknet::{
16-
accounts::{Account, Call, SingleOwnerAccount},
17-
core::types::FieldElement,
17+
accounts::{Account, SingleOwnerAccount},
18+
core::types::Felt,
1819
macros::selector,
1920
providers::{jsonrpc::HttpTransport, JsonRpcClient},
2021
signers::LocalWallet,
@@ -34,7 +35,7 @@ use crate::utils::{from_uint256, hex_to_bigdecimal, to_uint256};
3435
use crate::{config::Config, models::AppState};
3536

3637
lazy_static::lazy_static! {
37-
static ref RENEW_TIME: FieldElement = FieldElement::from_dec_str("365").unwrap();
38+
static ref RENEW_TIME: Felt = Felt::from_dec_str("365").unwrap();
3839
}
3940

4041
/// Filters out entries that were renewed less than 364 days ago
@@ -52,15 +53,15 @@ pub async fn get_domains_ready_for_renewal(
5253
config: &Config,
5354
state: &Arc<AppState>,
5455
logger: &Logger,
55-
) -> Result<HashMap<FieldElement, AggregateResults>> {
56+
) -> Result<HashMap<Felt, AggregateResults>> {
5657
let mut results = get_auto_renewal_data(config, state).await?;
5758
let mut results_altcoins = get_auto_renewal_altcoins_data(config, state).await?;
5859

5960
// Filter out entries that were renewed less than 364 days ago
6061
filter_recent_renewals(&mut results);
6162
filter_recent_renewals(&mut results_altcoins);
6263

63-
let mut grouped_results: HashMap<FieldElement, AggregateResults> = HashMap::new();
64+
let mut grouped_results: HashMap<Felt, AggregateResults> = HashMap::new();
6465

6566
if results.is_empty() && results_altcoins.is_empty() {
6667
logger.warning("No domains to renew".to_string());
@@ -81,14 +82,12 @@ pub async fn get_domains_ready_for_renewal(
8182
// merge all results together
8283
results.extend(results_altcoins.iter().cloned());
8384

85+
println!("renewers_mapping: {:?}", config.renewers_mapping);
86+
8487
// Fetch balances for all renewers
8588
let renewer_and_erc20: Vec<(String, String)> = results
8689
.iter()
8790
.map(|result| {
88-
let test = config
89-
.renewers_mapping
90-
.get(&result.auto_renew_contract)
91-
.unwrap();
9291
(
9392
result.renewer_address.clone(),
9493
// get the erc20 address for the given auto_renew_contract
@@ -138,25 +137,24 @@ pub async fn get_domains_ready_for_renewal(
138137
.cloned()
139138
.expect("Balance not found for this erc20");
140139
let renewal_price_eth = get_renewal_price_eth(result.domain.clone());
141-
let renewal_price =
142-
if FieldElement::from_hex_be(erc20).unwrap() == config.contract.erc20 {
143-
renewal_price_eth
144-
} else {
145-
match get_altcoin_quote(config, erc20.to_string()).await {
146-
Ok(quote) => {
147-
(quote * renewal_price_eth)
148-
/ BigInt::from_str("1000000000000000000").unwrap()
149-
}
150-
Err(e) => {
151-
// in case get_altcoin_quote endpoint returns an error we panic with the error
152-
logger.severe(format!(
153-
"Error while fetching quote on starknetid server: {:?}",
154-
e
155-
));
156-
panic!("Error while fetching quote on starknetid server: {:?}", e)
157-
}
140+
let renewal_price = if Felt::from_hex(erc20).unwrap() == config.contract.erc20 {
141+
renewal_price_eth
142+
} else {
143+
match get_altcoin_quote(config, erc20.to_string()).await {
144+
Ok(quote) => {
145+
(quote * renewal_price_eth)
146+
/ BigInt::from_str("1000000000000000000").unwrap()
158147
}
159-
};
148+
Err(e) => {
149+
// in case get_altcoin_quote endpoint returns an error we panic with the error
150+
logger.severe(format!(
151+
"Error while fetching quote on starknetid server: {:?}",
152+
e
153+
));
154+
panic!("Error while fetching quote on starknetid server: {:?}", e)
155+
}
156+
}
157+
};
160158
let output = process_aggregate_result(
161159
state,
162160
result.clone(),
@@ -227,7 +225,7 @@ async fn process_aggregate_result(
227225
return None;
228226
}
229227

230-
let renewer_addr = FieldElement::from_hex_be(&result.renewer_address).unwrap();
228+
let renewer_addr = Felt::from_hex(&result.renewer_address).unwrap();
231229
// map the vec of approval_values to get the approval_value for the erc20_addr selected
232230
let erc20_allowance = if let Some(approval_value) = result
233231
.approval_values
@@ -243,9 +241,9 @@ async fn process_aggregate_result(
243241

244242
// Check user meta hash
245243
let mut tax_price = BigDecimal::from(0);
246-
let mut meta_hash = FieldElement::ZERO;
244+
let mut meta_hash = Felt::ZERO;
247245
if let Some(hash) = result.meta_hash {
248-
meta_hash = FieldElement::from_hex_be(&hash).unwrap();
246+
meta_hash = Felt::from_hex(&hash).unwrap();
249247
if hash != "0" {
250248
let decimal_meta_hash =
251249
BigInt::parse_bytes(hash.trim_start_matches("0x").as_bytes(), 16).unwrap();
@@ -322,7 +320,7 @@ pub async fn renew_domains(
322320
config: &Config,
323321
account: &SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet>,
324322
mut aggregate_results: AggregateResults,
325-
auto_renew_contract: &FieldElement,
323+
auto_renew_contract: &Felt,
326324
logger: &Logger,
327325
) -> Result<()> {
328326
logger.info(format!(
@@ -341,13 +339,12 @@ pub async fn renew_domains(
341339
&& !aggregate_results.meta_hashes.is_empty()
342340
{
343341
let size = aggregate_results.domains.len().min(75);
344-
let domains_to_renew: Vec<FieldElement> =
345-
aggregate_results.domains.drain(0..size).collect();
346-
let renewers: Vec<FieldElement> = aggregate_results.renewers.drain(0..size).collect();
342+
let domains_to_renew: Vec<Felt> = aggregate_results.domains.drain(0..size).collect();
343+
let renewers: Vec<Felt> = aggregate_results.renewers.drain(0..size).collect();
347344
let domain_prices: Vec<BigDecimal> =
348345
aggregate_results.domain_prices.drain(0..size).collect();
349346
let tax_prices: Vec<BigDecimal> = aggregate_results.tax_prices.drain(0..size).collect();
350-
let meta_hashes: Vec<FieldElement> = aggregate_results.meta_hashes.drain(0..size).collect();
347+
let meta_hashes: Vec<Felt> = aggregate_results.meta_hashes.drain(0..size).collect();
351348

352349
match send_transaction(
353350
account,
@@ -379,7 +376,7 @@ pub async fn renew_domains(
379376
});
380377

381378
// We only inscrease nonce if no error occurred in the previous transaction
382-
nonce += FieldElement::ONE;
379+
nonce += Felt::ONE;
383380
}
384381
Err(e) => {
385382
if e.to_string().contains("Error while estimating fee") {
@@ -436,20 +433,16 @@ pub async fn renew_domains(
436433

437434
pub async fn send_transaction(
438435
account: &SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet>,
439-
auto_renew_contract: FieldElement,
436+
auto_renew_contract: Felt,
440437
aggregate_results: AggregateResults,
441-
nonce: FieldElement,
442-
) -> Result<FieldElement> {
443-
let mut calldata: Vec<FieldElement> = Vec::new();
444-
calldata
445-
.push(FieldElement::from_dec_str(&aggregate_results.domains.len().to_string()).unwrap());
438+
nonce: Felt,
439+
) -> Result<Felt> {
440+
let mut calldata: Vec<Felt> = Vec::new();
441+
calldata.push(Felt::from_dec_str(&aggregate_results.domains.len().to_string()).unwrap());
446442
calldata.extend_from_slice(&aggregate_results.domains);
447-
calldata
448-
.push(FieldElement::from_dec_str(&aggregate_results.renewers.len().to_string()).unwrap());
443+
calldata.push(Felt::from_dec_str(&aggregate_results.renewers.len().to_string()).unwrap());
449444
calldata.extend_from_slice(&aggregate_results.renewers);
450-
calldata.push(
451-
FieldElement::from_dec_str(&aggregate_results.domain_prices.len().to_string()).unwrap(),
452-
);
445+
calldata.push(Felt::from_dec_str(&aggregate_results.domain_prices.len().to_string()).unwrap());
453446

454447
println!("domains:");
455448
for x in &aggregate_results.domains {
@@ -482,31 +475,29 @@ pub async fn send_transaction(
482475
calldata.push(high);
483476
}
484477

485-
calldata
486-
.push(FieldElement::from_dec_str(&aggregate_results.tax_prices.len().to_string()).unwrap());
478+
calldata.push(Felt::from_dec_str(&aggregate_results.tax_prices.len().to_string()).unwrap());
487479
for tax_price in &aggregate_results.tax_prices {
488480
let (low, high) = to_uint256(tax_price.to_bigint().unwrap());
489481
calldata.push(low);
490482
calldata.push(high);
491483
}
492-
calldata.push(
493-
FieldElement::from_dec_str(&aggregate_results.meta_hashes.len().to_string()).unwrap(),
494-
);
484+
calldata.push(Felt::from_dec_str(&aggregate_results.meta_hashes.len().to_string()).unwrap());
495485
calldata.extend_from_slice(&aggregate_results.meta_hashes);
496486

497487
let execution = account
498-
.execute(vec![Call {
488+
.execute_v3(vec![Call {
499489
to: auto_renew_contract,
500490
selector: selector!("batch_renew"),
501491
calldata: calldata.clone(),
502492
}])
503-
.fee_estimate_multiplier(5.0f64);
493+
.gas_estimate_multiplier(5.0f64);
494+
// .fee_estimate_multiplier(5.0f64);
504495

505496
match execution.estimate_fee().await {
506497
Ok(_) => match execution
507498
.nonce(nonce)
508499
// harcode max fee to 10$ = 0.0028 ETH
509-
.max_fee(FieldElement::from(2800000000000000_u64))
500+
// .max_fee(Felt::from(2800000000000000_u64))
510501
.send()
511502
.await
512503
{

bot/src/config.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::de::MapAccess;
22
use serde::de::Visitor;
33
use serde::Deserialize;
44
use serde::Deserializer;
5-
use starknet::core::types::FieldElement;
5+
use starknet::core::types::Felt;
66
use std::collections::HashMap;
77
use std::env;
88
use std::fmt;
@@ -18,12 +18,12 @@ macro_rules! pub_struct {
1818
}
1919

2020
pub_struct!(Clone, Deserialize; Contract {
21-
starknetid: FieldElement,
22-
naming: FieldElement,
23-
renewal: FieldElement,
24-
erc20: FieldElement,
25-
pricing: FieldElement,
26-
multicall: FieldElement,
21+
starknetid: Felt,
22+
naming: Felt,
23+
renewal: Felt,
24+
erc20: Felt,
25+
pricing: Felt,
26+
multicall: Felt,
2727
});
2828

2929
pub_struct!(Clone, Deserialize; Database {
@@ -34,8 +34,8 @@ pub_struct!(Clone, Deserialize; Database {
3434
});
3535

3636
pub_struct!(Clone, Deserialize; MyAccount {
37-
private_key: FieldElement,
38-
address: FieldElement,
37+
private_key: Felt,
38+
address: Felt,
3939
});
4040

4141
pub_struct!(Clone, Deserialize; Renewals {
@@ -71,8 +71,8 @@ pub_struct!(Clone, Deserialize; Server {
7171
});
7272

7373
pub_struct!(Clone, Deserialize; Renewer {
74-
address: FieldElement,
75-
renewal_contract: FieldElement,
74+
address: Felt,
75+
renewal_contract: Felt,
7676
});
7777

7878
pub_struct!(Clone; Config {
@@ -84,7 +84,7 @@ pub_struct!(Clone; Config {
8484
rpc: Rpc,
8585
watchtower: Watchtower,
8686
server: Server,
87-
renewers_mapping: HashMap<FieldElement, FieldElement>,
87+
renewers_mapping: HashMap<Felt, Felt>,
8888
});
8989

9090
impl<'de> Deserialize<'de> for Config {

bot/src/models.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bigdecimal::BigDecimal;
44
use bson::DateTime;
55
use mongodb::Database;
66
use serde::{Deserialize, Serialize};
7-
use starknet::core::types::FieldElement;
7+
use starknet::core::types::Felt;
88

99
pub struct AppState {
1010
pub db: Database,
@@ -57,27 +57,27 @@ pub struct DomainAggregateResult {
5757
pub last_renewal: Option<i64>,
5858
pub meta_hash: Option<String>,
5959
pub _cursor: Cursor,
60-
pub auto_renew_contract: FieldElement,
60+
pub auto_renew_contract: Felt,
6161
pub approval_values: Vec<AutoRenewAllowance>,
6262
}
6363

6464
pub struct AggregateResult {
65-
pub domain: FieldElement,
66-
pub renewer_addr: FieldElement,
65+
pub domain: Felt,
66+
pub renewer_addr: Felt,
6767
pub domain_price: BigDecimal,
6868
pub tax_price: BigDecimal,
69-
pub meta_hash: FieldElement,
70-
pub auto_renew_contract: FieldElement,
69+
pub meta_hash: Felt,
70+
pub auto_renew_contract: Felt,
7171
}
7272

7373
#[derive(Clone, Debug, Deserialize, Serialize)]
7474
pub struct AggregateResults {
75-
pub domains: Vec<FieldElement>,
76-
pub renewers: Vec<FieldElement>,
75+
pub domains: Vec<Felt>,
76+
pub renewers: Vec<Felt>,
7777
pub domain_prices: Vec<BigDecimal>,
7878
pub tax_prices: Vec<BigDecimal>,
79-
pub meta_hashes: Vec<FieldElement>,
80-
pub auto_renew_contracts: Vec<FieldElement>,
79+
pub meta_hashes: Vec<Felt>,
80+
pub auto_renew_contracts: Vec<Felt>,
8181
}
8282

8383
#[derive(Serialize, Deserialize, Debug)]
@@ -102,7 +102,7 @@ pub struct States {
102102

103103
#[derive(Debug, Clone)]
104104
pub struct TxResult {
105-
pub tx_hash: FieldElement,
105+
pub tx_hash: Felt,
106106
pub reverted: Option<bool>,
107107
pub revert_reason: Option<String>,
108108
pub domains_renewed: usize,

bot/src/pipelines.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use anyhow::Result;
22
use bson::{doc, Bson};
33
use chrono::{Duration, Utc};
44
use futures::TryStreamExt;
5-
use starknet::core::types::FieldElement;
65
use std::sync::Arc;
76

87
use crate::{
@@ -18,7 +17,7 @@ pub async fn get_auto_renewal_data(
1817
let auto_renews_collection = state.db.collection::<Domain>("auto_renew_flows");
1918
let min_expiry_date = Utc::now() + Duration::days(config.renewals.expiry_days);
2019
let erc20_addr = to_hex(config.contract.erc20);
21-
let auto_renew_contract = FieldElement::to_string(&config.contract.renewal);
20+
let auto_renew_contract = to_hex(config.contract.renewal);
2221
println!("timestamp: {}", min_expiry_date.timestamp());
2322
// Define aggregate pipeline
2423
let pipeline = vec![

0 commit comments

Comments
 (0)