refactor(curve): stateless contracts as state attribute instead of static.

This commit is contained in:
Florian Pellissier
2024-08-15 10:42:06 +02:00
parent 420cf13466
commit c218252548
7 changed files with 775 additions and 679 deletions

View File

@@ -12,6 +12,8 @@ struct PoolQueryParams {
contracts: Option<Vec<String>>,
tx_hash: String,
tokens: Vec<String>,
static_attribute_keys: Option<Vec<String>>,
static_attribute_vals: Option<Vec<String>>,
attribute_keys: Option<Vec<String>>,
attribute_vals: Option<Vec<String>>,
}
@@ -32,7 +34,7 @@ struct PoolQueryParams {
pub fn emit_specific_pools(
params: &str,
tx: &TransactionTrace,
) -> Result<Option<ProtocolComponent>> {
) -> Result<Option<(ProtocolComponent, Vec<EntityChanges>)>> {
let pools = parse_params(params)?;
create_component(tx, pools)
}
@@ -40,61 +42,81 @@ pub fn emit_specific_pools(
fn create_component(
tx: &TransactionTrace,
pools: HashMap<String, PoolQueryParams>,
) -> Result<Option<ProtocolComponent>> {
) -> Result<Option<(ProtocolComponent, Vec<EntityChanges>)>> {
let encoded_hash = hex::encode(tx.hash.clone());
if let Some(pool) = pools.get(&encoded_hash) {
Ok(Some(ProtocolComponent {
id: pool.address.clone(),
tx: Some(Transaction {
to: tx.to.clone(),
from: tx.from.clone(),
hash: tx.hash.clone(),
index: tx.index.into(),
}),
tokens: pool
.tokens
.clone()
.into_iter()
.map(|token| Result::Ok(hex::decode(token)?))
.collect::<Result<Vec<_>>>()
.with_context(|| "Token addresses were not formatted properly")?,
static_att: zip(
pool.attribute_keys
Ok(Some((
ProtocolComponent {
id: pool.address.clone(),
tx: Some(Transaction {
to: tx.to.clone(),
from: tx.from.clone(),
hash: tx.hash.clone(),
index: tx.index.into(),
}),
tokens: pool
.tokens
.clone()
.unwrap_or(vec![]),
pool.attribute_vals
.clone()
.unwrap_or(vec![]),
)
.clone()
.map(|(key, value)| Attribute {
name: key,
value: value.into(),
change: ChangeType::Creation.into(),
})
.collect::<Vec<_>>(),
contracts: pool
.contracts
.into_iter()
.map(|token| Result::Ok(hex::decode(token)?))
.collect::<Result<Vec<_>>>()
.with_context(|| "Token addresses were not formatted properly")?,
static_att: zip(
pool.static_attribute_keys
.clone()
.unwrap_or(vec![]),
pool.static_attribute_vals
.clone()
.unwrap_or(vec![]),
)
.clone()
.unwrap_or_default()
.into_iter()
.map(|contract| {
hex::decode(contract)
.with_context(|| "Pool contracts was not formatted properly")
.map(|(key, value)| Attribute {
name: key,
value: value.into(),
change: ChangeType::Creation.into(),
})
.chain(std::iter::once(
hex::decode(&pool.address)
.with_context(|| "Pool address was not formatted properly"),
))
.collect::<Result<Vec<Vec<u8>>>>()?,
change: ChangeType::Creation.into(),
protocol_type: Some(ProtocolType {
name: "curve_pool".into(),
financial_type: FinancialType::Swap.into(),
attribute_schema: Vec::new(),
implementation_type: ImplementationType::Vm.into(),
}),
}))
.collect::<Vec<_>>(),
contracts: pool
.contracts
.clone()
.unwrap_or_default()
.into_iter()
.map(|contract| {
hex::decode(contract)
.with_context(|| "Pool contracts was not formatted properly")
})
.chain(std::iter::once(
hex::decode(&pool.address)
.with_context(|| "Pool address was not formatted properly"),
))
.collect::<Result<Vec<Vec<u8>>>>()?,
change: ChangeType::Creation.into(),
protocol_type: Some(ProtocolType {
name: "curve_pool".into(),
financial_type: FinancialType::Swap.into(),
attribute_schema: Vec::new(),
implementation_type: ImplementationType::Vm.into(),
}),
},
vec![EntityChanges {
component_id: format!("0x{}", pool.address.clone()),
attributes: zip(
pool.attribute_keys
.clone()
.unwrap_or(vec![]),
pool.attribute_vals
.clone()
.unwrap_or(vec![]),
)
.clone()
.map(|(key, value)| Attribute {
name: key,
value: value.into(),
change: ChangeType::Creation.into(),
})
.collect::<Vec<_>>(),
}],
)))
} else {
Ok(None)
}
@@ -134,6 +156,8 @@ mod tests {
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string(),
"0xdac17f958d2ee523a2206206994597c13d831ec7".to_string(),
],
static_attribute_keys: None,
static_attribute_vals: None,
attribute_keys: Some(vec!["key1".to_string()]),
attribute_vals: Some(vec!["val1".to_string()]),
},