From 94c19c29c58a1445c10e19133f1c3f79d91fd49a Mon Sep 17 00:00:00 2001 From: incrypto32 Date: Mon, 2 Dec 2024 19:59:32 +0400 Subject: [PATCH] graph: Support declared calls for subgraph datasources --- chain/ethereum/src/data_source.rs | 286 ++------- graph/src/data/store/mod.rs | 1 + graph/src/data_source/common.rs | 552 +++++++++++++++++- graph/src/data_source/subgraph.rs | 20 +- .../tests/chain/ethereum/manifest.rs | 72 +++ 5 files changed, 694 insertions(+), 237 deletions(-) diff --git a/chain/ethereum/src/data_source.rs b/chain/ethereum/src/data_source.rs index fa2cb745524..9e69bcd7ccc 100644 --- a/chain/ethereum/src/data_source.rs +++ b/chain/ethereum/src/data_source.rs @@ -1,12 +1,13 @@ use anyhow::{anyhow, Error}; use anyhow::{ensure, Context}; +use graph::blockchain::block_stream::EntityWithType; use graph::blockchain::{BlockPtr, TriggerWithHandler}; use graph::components::metrics::subgraph::SubgraphInstanceMetrics; use graph::components::store::{EthereumCallCache, StoredDynamicDataSource}; use graph::components::subgraph::{HostMetrics, InstanceDSTemplateInfo, MappingError}; use graph::components::trigger_processor::RunnableTriggers; -use graph::data::value::Word; -use graph::data_source::common::{MappingABI, UnresolvedMappingABI}; +use graph::data_source::common::{CallDecls, MappingABI, UnresolvedMappingABI}; +use graph::data_source::subgraph::{EntityHandler, Mapping as SubgraphDataSourceMapping}; use graph::data_source::CausalityRegion; use graph::env::ENV_VARS; use graph::futures03::future::try_join; @@ -14,12 +15,9 @@ use graph::futures03::stream::FuturesOrdered; use graph::futures03::TryStreamExt; use graph::prelude::ethabi::ethereum_types::H160; use graph::prelude::ethabi::{StateMutability, Token}; -use graph::prelude::lazy_static; -use graph::prelude::regex::Regex; use graph::prelude::{Link, SubgraphManifestValidationError}; use graph::slog::{debug, error, o, trace}; use itertools::Itertools; -use serde::de; use serde::de::Error as ErrorD; use serde::{Deserialize, Deserializer}; use std::collections::HashSet; @@ -31,7 +29,6 @@ use tiny_keccak::{keccak256, Keccak}; use graph::{ blockchain::{self, Blockchain}, - derive::CheapClone, prelude::{ async_trait, ethabi::{Address, Event, Function, LogParam, ParamType, RawLog}, @@ -970,8 +967,59 @@ impl DeclaredCall { })? }; - let address = decl.address(log, params)?; - let args = decl.args(log, params)?; + let address = decl.address_for_log(log, params)?; + let args = decl.args_for_log(log, params)?; + + let call = DeclaredCall { + label: decl.label.clone(), + contract_name, + address, + function: function.clone(), + args, + }; + calls.push(call); + } + + Ok(calls) + } + + fn from_entity_handler( + mapping: &SubgraphDataSourceMapping, + handler: &EntityHandler, + entity: &EntityWithType, + ) -> Result, anyhow::Error> { + let mut calls = Vec::new(); + for decl in handler.calls.decls.iter() { + let contract_name = decl.expr.abi.to_string(); + let function_name = decl.expr.func.as_str(); + // Obtain the path to the contract ABI + let abi = mapping.find_abi(&contract_name)?; + // TODO: Handle overloaded functions + let function = { + // Behavior for apiVersion < 0.0.4: look up function by name; for overloaded + // functions this always picks the same overloaded variant, which is incorrect + // and may lead to encoding/decoding errors + abi.contract.function(function_name).with_context(|| { + format!( + "Unknown function \"{}::{}\" called from WASM runtime", + contract_name, function_name + ) + })? + }; + + let param_types = function + .inputs + .iter() + .map(|param| param.kind.clone()) + .collect::>(); + + let address = decl.address_for_entity_handler(entity)?; + let args = decl + .args_for_entity_handler(entity, param_types) + .context(format!( + "Failed to parse arguments for call to function \"{}\" of contract \"{}\"", + function_name, contract_name + ))?; let call = DeclaredCall { label: decl.label.clone(), @@ -1569,225 +1617,3 @@ fn string_to_h256(s: &str) -> H256 { pub struct TemplateSource { pub abi: String, } - -/// Internal representation of declared calls. In the manifest that's -/// written as part of an event handler as -/// ```yaml -/// calls: -/// - myCall1: Contract[address].function(arg1, arg2, ...) -/// - .. -/// ``` -/// -/// The `address` and `arg` fields can be either `event.address` or -/// `event.params.`. Each entry under `calls` gets turned into a -/// `CallDcl` -#[derive(Clone, CheapClone, Debug, Default, Hash, Eq, PartialEq)] -pub struct CallDecls { - pub decls: Arc>, - readonly: (), -} - -/// A single call declaration, like `myCall1: -/// Contract[address].function(arg1, arg2, ...)` -#[derive(Clone, Debug, Hash, Eq, PartialEq)] -pub struct CallDecl { - /// A user-defined label - pub label: String, - /// The call expression - pub expr: CallExpr, - readonly: (), -} -impl CallDecl { - fn address(&self, log: &Log, params: &[LogParam]) -> Result { - let address = match &self.expr.address { - CallArg::Address => log.address, - CallArg::HexAddress(address) => *address, - CallArg::Param(name) => { - let value = params - .iter() - .find(|param| ¶m.name == name.as_str()) - .ok_or_else(|| anyhow!("unknown param {name}"))? - .value - .clone(); - value - .into_address() - .ok_or_else(|| anyhow!("param {name} is not an address"))? - } - }; - Ok(address) - } - - fn args(&self, log: &Log, params: &[LogParam]) -> Result, Error> { - self.expr - .args - .iter() - .map(|arg| match arg { - CallArg::Address => Ok(Token::Address(log.address)), - CallArg::HexAddress(address) => Ok(Token::Address(*address)), - CallArg::Param(name) => { - let value = params - .iter() - .find(|param| ¶m.name == name.as_str()) - .ok_or_else(|| anyhow!("unknown param {name}"))? - .value - .clone(); - Ok(value) - } - }) - .collect() - } -} - -impl<'de> de::Deserialize<'de> for CallDecls { - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - let decls: std::collections::HashMap = - de::Deserialize::deserialize(deserializer)?; - let decls = decls - .into_iter() - .map(|(name, expr)| { - expr.parse::().map(|expr| CallDecl { - label: name, - expr, - readonly: (), - }) - }) - .collect::>() - .map(|decls| Arc::new(decls)) - .map_err(de::Error::custom)?; - Ok(CallDecls { - decls, - readonly: (), - }) - } -} - -#[derive(Clone, Debug, Hash, Eq, PartialEq)] -pub struct CallExpr { - pub abi: Word, - pub address: CallArg, - pub func: Word, - pub args: Vec, - readonly: (), -} - -/// Parse expressions of the form `Contract[address].function(arg1, arg2, -/// ...)` where the `address` and the args are either `event.address` or -/// `event.params.`. -/// -/// The parser is pretty awful as it generates error messages that aren't -/// very helpful. We should replace all this with a real parser, most likely -/// `combine` which is what `graphql_parser` uses -impl FromStr for CallExpr { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - lazy_static! { - static ref RE: Regex = Regex::new( - r"(?x) - (?P[a-zA-Z0-9_]+)\[ - (?P
[^]]+)\] - \. - (?P[a-zA-Z0-9_]+)\( - (?P[^)]*) - \)" - ) - .unwrap(); - } - let x = RE - .captures(s) - .ok_or_else(|| anyhow!("invalid call expression `{s}`"))?; - let abi = Word::from(x.name("abi").unwrap().as_str()); - let address = x.name("address").unwrap().as_str().parse()?; - let func = Word::from(x.name("func").unwrap().as_str()); - let args: Vec = x - .name("args") - .unwrap() - .as_str() - .split(',') - .filter(|s| !s.is_empty()) - .map(|s| s.trim().parse::()) - .collect::>()?; - Ok(CallExpr { - abi, - address, - func, - args, - readonly: (), - }) - } -} - -#[derive(Clone, Debug, Hash, Eq, PartialEq)] -pub enum CallArg { - HexAddress(Address), - Address, - Param(Word), -} - -lazy_static! { - // Matches a 40-character hexadecimal string prefixed with '0x', typical for Ethereum addresses - static ref ADDR_RE: Regex = Regex::new(r"^0x[0-9a-fA-F]{40}$").unwrap(); -} - -impl FromStr for CallArg { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - if ADDR_RE.is_match(s) { - if let Ok(parsed_address) = Address::from_str(s) { - return Ok(CallArg::HexAddress(parsed_address)); - } - } - - let mut parts = s.split('.'); - match (parts.next(), parts.next(), parts.next()) { - (Some("event"), Some("address"), None) => Ok(CallArg::Address), - (Some("event"), Some("params"), Some(param)) => Ok(CallArg::Param(Word::from(param))), - _ => Err(anyhow!("invalid call argument `{}`", s)), - } - } -} - -#[test] -fn test_call_expr() { - let expr: CallExpr = "ERC20[event.address].balanceOf(event.params.token)" - .parse() - .unwrap(); - assert_eq!(expr.abi, "ERC20"); - assert_eq!(expr.address, CallArg::Address); - assert_eq!(expr.func, "balanceOf"); - assert_eq!(expr.args, vec![CallArg::Param("token".into())]); - - let expr: CallExpr = "Pool[event.params.pool].fees(event.params.token0, event.params.token1)" - .parse() - .unwrap(); - assert_eq!(expr.abi, "Pool"); - assert_eq!(expr.address, CallArg::Param("pool".into())); - assert_eq!(expr.func, "fees"); - assert_eq!( - expr.args, - vec![ - CallArg::Param("token0".into()), - CallArg::Param("token1".into()) - ] - ); - - let expr: CallExpr = "Pool[event.address].growth()".parse().unwrap(); - assert_eq!(expr.abi, "Pool"); - assert_eq!(expr.address, CallArg::Address); - assert_eq!(expr.func, "growth"); - assert_eq!(expr.args, vec![]); - - let expr: CallExpr = "Pool[0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF].growth(0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF)" - .parse() - .unwrap(); - let call_arg = - CallArg::HexAddress(H160::from_str("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF").unwrap()); - assert_eq!(expr.abi, "Pool"); - assert_eq!(expr.address, call_arg); - assert_eq!(expr.func, "growth"); - assert_eq!(expr.args, vec![call_arg]); -} diff --git a/graph/src/data/store/mod.rs b/graph/src/data/store/mod.rs index 33d9286ceec..8bd29b602ea 100644 --- a/graph/src/data/store/mod.rs +++ b/graph/src/data/store/mod.rs @@ -732,6 +732,7 @@ where } } + lazy_static! { /// The name of the id attribute, `"id"` pub static ref ID: Word = Word::from("id"); diff --git a/graph/src/data_source/common.rs b/graph/src/data_source/common.rs index 789f04bb09c..de32366ea6b 100644 --- a/graph/src/data_source/common.rs +++ b/graph/src/data_source/common.rs @@ -1,9 +1,17 @@ -use crate::{components::link_resolver::LinkResolver, prelude::Link}; -use anyhow::{Context, Error}; -use ethabi::{Contract, Function}; +use crate::blockchain::block_stream::EntityWithType; +use crate::prelude::Value; +use crate::{components::link_resolver::LinkResolver, data::value::Word, prelude::Link}; +use anyhow::{anyhow, Context, Error}; +use ethabi::{Address, Contract, Function, LogParam, ParamType, Token}; +use graph_derive::CheapClone; +use lazy_static::lazy_static; +use num_bigint::Sign; +use regex::Regex; +use serde::de; use serde::Deserialize; use slog::Logger; -use std::sync::Arc; +use std::{str::FromStr, sync::Arc}; +use web3::types::{Log, H160}; #[derive(Clone, Debug, PartialEq)] pub struct MappingABI { @@ -80,3 +88,539 @@ impl UnresolvedMappingABI { }) } } + +/// Internal representation of declared calls. In the manifest that's +/// written as part of an event handler as +/// ```yaml +/// calls: +/// - myCall1: Contract[address].function(arg1, arg2, ...) +/// - .. +/// ``` +/// +/// The `address` and `arg` fields can be either `event.address` or +/// `event.params.`. Each entry under `calls` gets turned into a +/// `CallDcl` +#[derive(Clone, CheapClone, Debug, Default, Hash, Eq, PartialEq)] +pub struct CallDecls { + pub decls: Arc>, + readonly: (), +} + +/// A single call declaration, like `myCall1: +/// Contract[address].function(arg1, arg2, ...)` +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +pub struct CallDecl { + /// A user-defined label + pub label: String, + /// The call expression + pub expr: CallExpr, + readonly: (), +} + +impl CallDecl { + pub fn validate_args(&self) -> Result<(), Error> { + self.expr.validate_args() + } + + pub fn address_for_log(&self, log: &Log, params: &[LogParam]) -> Result { + let address = match &self.expr.address { + CallArg::HexAddress(address) => *address, + CallArg::Ethereum(arg) => match arg { + EthereumArg::Address => log.address, + EthereumArg::Param(name) => { + let value = params + .iter() + .find(|param| ¶m.name == name.as_str()) + .ok_or_else(|| anyhow!("unknown param {name}"))? + .value + .clone(); + value + .into_address() + .ok_or_else(|| anyhow!("param {name} is not an address"))? + } + }, + CallArg::Subgraph(_) => { + return Err(anyhow!( + "Subgraph params are not supported for when declaring calls for event handlers" + )) + } + }; + Ok(address) + } + + pub fn args_for_log(&self, log: &Log, params: &[LogParam]) -> Result, Error> { + self.expr + .args + .iter() + .map(|arg| match arg { + CallArg::HexAddress(address) => Ok(Token::Address(*address)), + CallArg::Ethereum(arg) => match arg { + EthereumArg::Address => Ok(Token::Address(log.address)), + EthereumArg::Param(name) => { + let value = params + .iter() + .find(|param| ¶m.name == name.as_str()) + .ok_or_else(|| anyhow!("unknown param {name}"))? + .value + .clone(); + Ok(value) + } + }, + CallArg::Subgraph(_) => Err(anyhow!( + "Subgraph params are not supported for when declaring calls for event handlers" + )), + }) + .collect() + } + + pub fn address_for_entity_handler(&self, entity: &EntityWithType) -> Result { + match &self.expr.address { + // Static hex address - just return it directly + CallArg::HexAddress(address) => Ok(*address), + + // Ethereum params not allowed here + CallArg::Ethereum(_) => Err(anyhow!( + "Ethereum params are not supported for entity handler calls" + )), + + // Look up address from entity parameter + CallArg::Subgraph(SubgraphArg::EntityParam(name)) => { + // Get the value for this parameter + let value = entity + .entity + .get(name.as_str()) + .ok_or_else(|| anyhow!("entity missing required param '{name}'"))?; + + // Make sure it's a bytes value and convert to address + match value { + Value::Bytes(bytes) => { + let address = H160::from_slice(bytes.as_slice()); + Ok(address) + } + _ => Err(anyhow!("param '{name}' must be an address")), + } + } + } + } + + pub fn args_for_entity_handler( + &self, + entity: &EntityWithType, + param_types: Vec, + ) -> Result, Error> { + if self.expr.args.len() != param_types.len() { + return Err(anyhow!( + "mismatched number of arguments: expected {}, got {}", + param_types.len(), + self.expr.args.len() + )); + } + + self.expr + .args + .iter() + .zip(param_types.into_iter()) + .map(|(arg, expected_type)| match arg { + CallArg::HexAddress(address) => match expected_type { + ParamType::Address => Ok(Token::Address(*address)), + _ => Err(anyhow!( + "type mismatch: hex address provided for non-address parameter" + )), + }, + CallArg::Ethereum(_) => Err(anyhow!( + "Ethereum params are not supported for entity handler calls" + )), + CallArg::Subgraph(SubgraphArg::EntityParam(name)) => { + let value = entity + .entity + .get(name.as_str()) + .ok_or_else(|| anyhow!("entity missing required param '{name}'"))?; + + match (&expected_type, value) { + (ParamType::Address, Value::Bytes(b)) => { + Ok(Token::Address(H160::from_slice(b.as_slice()))) + } + (ParamType::Bytes, Value::Bytes(b)) => { + Ok(Token::Bytes(b.as_ref().to_vec())) + } + (ParamType::FixedBytes(size), Value::Bytes(b)) if b.len() == *size => { + Ok(Token::FixedBytes(b.as_ref().to_vec())) + } + (ParamType::String, Value::String(s)) => Ok(Token::String(s.to_string())), + (ParamType::Bool, Value::Bool(b)) => Ok(Token::Bool(*b)), + (ParamType::Int(_), Value::Int(i)) => Ok(Token::Int((*i).into())), + (ParamType::Int(_), Value::Int8(i)) => Ok(Token::Int((*i).into())), + (ParamType::Int(_), Value::BigInt(i)) => Ok(Token::Int(i.to_signed_u256())), + (ParamType::Uint(_), Value::Int(i)) if *i >= 0 => { + Ok(Token::Uint((*i).into())) + } + (ParamType::Uint(_), Value::BigInt(i)) if i.sign() == Sign::Plus => { + Ok(Token::Uint(i.to_unsigned_u256())) + } + (ParamType::Array(inner_type), Value::List(values)) => { + let tokens: Result, Error> = values + .iter() + .map(|v| CallDecl::convert_value_to_token(v, inner_type.as_ref())) + .collect(); + Ok(Token::Array(tokens?)) + } + _ => Err(anyhow!( + "type mismatch for param '{name}': cannot convert {:?} to {:?}", + value, + expected_type + )), + } + } + }) + .collect() + } + + // Helper function for converting a single value + fn convert_value_to_token(value: &Value, param_type: &ParamType) -> Result { + match (param_type, value) { + (ParamType::Address, Value::Bytes(b)) => { + Ok(Token::Address(H160::from_slice(b.as_slice()))) + } + (ParamType::Bytes, Value::Bytes(b)) => Ok(Token::Bytes(b.as_ref().to_vec())), + (ParamType::FixedBytes(size), Value::Bytes(b)) if b.len() == *size => { + Ok(Token::FixedBytes(b.as_ref().to_vec())) + } + (ParamType::String, Value::String(s)) => Ok(Token::String(s.to_string())), + (ParamType::Bool, Value::Bool(b)) => Ok(Token::Bool(*b)), + (ParamType::Int(_), Value::Int(i)) => Ok(Token::Int((*i).into())), + (ParamType::Int(_), Value::Int8(i)) => Ok(Token::Int((*i).into())), + (ParamType::Int(_), Value::BigInt(i)) => Ok(Token::Int(i.to_signed_u256())), + (ParamType::Uint(_), Value::Int(i)) if *i >= 0 => Ok(Token::Uint((*i).into())), + (ParamType::Uint(_), Value::BigInt(i)) => Ok(Token::Uint(i.to_unsigned_u256())), + _ => Err(anyhow!( + "type mismatch: cannot convert {:?} to {:?}", + value, + param_type + )), + } + } +} + +impl CallDecls { + pub fn validate_args(&self) -> Result<(), Error> { + for decl in self.decls.iter() { + decl.validate_args()?; + } + Ok(()) + } +} + +impl<'de> de::Deserialize<'de> for CallDecls { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + let decls: std::collections::HashMap = + de::Deserialize::deserialize(deserializer)?; + + // First parse all expressions + let parsed_decls: Vec = decls + .into_iter() + .map(|(name, expr)| { + expr.parse::().map(|expr| CallDecl { + label: name, + expr, + readonly: (), + }) + }) + .collect::>() + .map_err(de::Error::custom)?; + + // Then validate no mixing of Ethereum and Subgraph across all expressions + let has_ethereum = parsed_decls.iter().any(|decl| { + matches!(decl.expr.address, CallArg::Ethereum(_)) + || decl + .expr + .args + .iter() + .any(|arg| matches!(arg, CallArg::Ethereum(_))) + }); + + let has_subgraph = parsed_decls.iter().any(|decl| { + matches!(decl.expr.address, CallArg::Subgraph(_)) + || decl + .expr + .args + .iter() + .any(|arg| matches!(arg, CallArg::Subgraph(_))) + }); + + if has_ethereum && has_subgraph { + return Err(de::Error::custom( + "Cannot mix Ethereum and Subgraph args across call expressions", + )); + } + + Ok(CallDecls { + decls: Arc::new(parsed_decls), + readonly: (), + }) + } +} + +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +pub struct CallExpr { + pub abi: Word, + pub address: CallArg, + pub func: Word, + pub args: Vec, + readonly: (), +} + +impl CallExpr { + fn validate_args(&self) -> Result<(), anyhow::Error> { + // Consider address along with args for checking Ethereum/Subgraph mixing + let has_ethereum = matches!(self.address, CallArg::Ethereum(_)) + || self + .args + .iter() + .any(|arg| matches!(arg, CallArg::Ethereum(_))); + + let has_subgraph = matches!(self.address, CallArg::Subgraph(_)) + || self + .args + .iter() + .any(|arg| matches!(arg, CallArg::Subgraph(_))); + + if has_ethereum && has_subgraph { + return Err(anyhow!( + "Cannot mix Ethereum and Subgraph args in the same call expression" + )); + } + + Ok(()) + } +} +/// Parse expressions of the form `Contract[address].function(arg1, arg2, +/// ...)` where the `address` and the args are either `event.address` or +/// `event.params.`. +/// +/// The parser is pretty awful as it generates error messages that aren't +/// very helpful. We should replace all this with a real parser, most likely +/// `combine` which is what `graphql_parser` uses +impl FromStr for CallExpr { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + lazy_static! { + static ref RE: Regex = Regex::new( + r"(?x) + (?P[a-zA-Z0-9_]+)\[ + (?P
[^]]+)\] + \. + (?P[a-zA-Z0-9_]+)\( + (?P[^)]*) + \)" + ) + .unwrap(); + } + let x = RE + .captures(s) + .ok_or_else(|| anyhow!("invalid call expression `{s}`"))?; + let abi = Word::from(x.name("abi").unwrap().as_str()); + let address = x.name("address").unwrap().as_str().parse()?; + let func = Word::from(x.name("func").unwrap().as_str()); + let args: Vec = x + .name("args") + .unwrap() + .as_str() + .split(',') + .filter(|s| !s.is_empty()) + .map(|s| s.trim().parse::()) + .collect::>()?; + + let call_expr = CallExpr { + abi, + address, + func, + args, + readonly: (), + }; + + // Validate the arguments after constructing the CallExpr + call_expr.validate_args()?; + + Ok(call_expr) + } +} + +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +pub enum CallArg { + // Hard-coded hex address + HexAddress(Address), + // Ethereum-specific variants + Ethereum(EthereumArg), + // Subgraph datasource specific variants + Subgraph(SubgraphArg), +} + +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +pub enum EthereumArg { + Address, + Param(Word), +} + +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +pub enum SubgraphArg { + EntityParam(Word), +} + +lazy_static! { + static ref ADDR_RE: Regex = Regex::new(r"^0x[0-9a-fA-F]{40}$").unwrap(); +} + +impl FromStr for CallArg { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + if ADDR_RE.is_match(s) { + if let Ok(parsed_address) = Address::from_str(s) { + return Ok(CallArg::HexAddress(parsed_address)); + } + } + + let mut parts = s.split('.'); + match (parts.next(), parts.next(), parts.next()) { + (Some("event"), Some("address"), None) => Ok(CallArg::Ethereum(EthereumArg::Address)), + (Some("event"), Some("params"), Some(param)) => { + Ok(CallArg::Ethereum(EthereumArg::Param(Word::from(param)))) + } + (Some("entity"), Some(param), None) => Ok(CallArg::Subgraph(SubgraphArg::EntityParam( + Word::from(param), + ))), + _ => Err(anyhow!("invalid call argument `{}`", s)), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_ethereum_call_expr() { + let expr: CallExpr = "ERC20[event.address].balanceOf(event.params.token)" + .parse() + .unwrap(); + assert_eq!(expr.abi, "ERC20"); + assert_eq!(expr.address, CallArg::Ethereum(EthereumArg::Address)); + assert_eq!(expr.func, "balanceOf"); + assert_eq!( + expr.args, + vec![CallArg::Ethereum(EthereumArg::Param("token".into()))] + ); + + let expr: CallExpr = + "Pool[event.params.pool].fees(event.params.token0, event.params.token1)" + .parse() + .unwrap(); + assert_eq!(expr.abi, "Pool"); + assert_eq!( + expr.address, + CallArg::Ethereum(EthereumArg::Param("pool".into())) + ); + assert_eq!(expr.func, "fees"); + assert_eq!( + expr.args, + vec![ + CallArg::Ethereum(EthereumArg::Param("token0".into())), + CallArg::Ethereum(EthereumArg::Param("token1".into())) + ] + ); + } + + #[test] + fn test_subgraph_call_expr() { + let expr: CallExpr = "Token[entity.id].symbol()".parse().unwrap(); + assert_eq!(expr.abi, "Token"); + assert_eq!( + expr.address, + CallArg::Subgraph(SubgraphArg::EntityParam("id".into())) + ); + assert_eq!(expr.func, "symbol"); + assert_eq!(expr.args, vec![]); + + let expr: CallExpr = "Pair[entity.pair].getReserves(entity.token0)" + .parse() + .unwrap(); + assert_eq!(expr.abi, "Pair"); + assert_eq!( + expr.address, + CallArg::Subgraph(SubgraphArg::EntityParam("pair".into())) + ); + assert_eq!(expr.func, "getReserves"); + assert_eq!( + expr.args, + vec![CallArg::Subgraph(SubgraphArg::EntityParam("token0".into()))] + ); + } + + #[test] + fn test_hex_address_call_expr() { + let addr = "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"; + let hex_address = CallArg::HexAddress(web3::types::H160::from_str(addr).unwrap()); + + // Test HexAddress in address position + let expr: CallExpr = format!("Pool[{}].growth()", addr).parse().unwrap(); + assert_eq!(expr.abi, "Pool"); + assert_eq!(expr.address, hex_address.clone()); + assert_eq!(expr.func, "growth"); + assert_eq!(expr.args, vec![]); + + // Test HexAddress in argument position + let expr: CallExpr = format!("Pool[event.address].approve({}, event.params.amount)", addr) + .parse() + .unwrap(); + assert_eq!(expr.abi, "Pool"); + assert_eq!(expr.address, CallArg::Ethereum(EthereumArg::Address)); + assert_eq!(expr.func, "approve"); + assert_eq!(expr.args.len(), 2); + assert_eq!(expr.args[0], hex_address); + } + + #[test] + fn test_invalid_call_args() { + // Invalid hex address + assert!("Pool[0xinvalid].test()".parse::().is_err()); + + // Invalid event path + assert!("Pool[event.invalid].test()".parse::().is_err()); + + // Invalid entity path + assert!("Pool[entity].test()".parse::().is_err()); + + // Empty address + assert!("Pool[].test()".parse::().is_err()); + + // Invalid parameter format + assert!("Pool[event.params].test()".parse::().is_err()); + } + + #[test] + fn test_from_str() { + // Test valid hex address + let addr = "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"; + let arg = CallArg::from_str(addr).unwrap(); + assert!(matches!(arg, CallArg::HexAddress(_))); + + // Test Ethereum Address + let arg = CallArg::from_str("event.address").unwrap(); + assert!(matches!(arg, CallArg::Ethereum(EthereumArg::Address))); + + // Test Ethereum Param + let arg = CallArg::from_str("event.params.token").unwrap(); + assert!(matches!(arg, CallArg::Ethereum(EthereumArg::Param(_)))); + + // Test Subgraph EntityParam + let arg = CallArg::from_str("entity.token").unwrap(); + assert!(matches!( + arg, + CallArg::Subgraph(SubgraphArg::EntityParam(_)) + )); + } +} diff --git a/graph/src/data_source/subgraph.rs b/graph/src/data_source/subgraph.rs index cfd17905f63..a9a28f42708 100644 --- a/graph/src/data_source/subgraph.rs +++ b/graph/src/data_source/subgraph.rs @@ -6,16 +6,16 @@ use crate::{ value::Word, }, data_source, - prelude::{DataSourceContext, DeploymentHash, Link}, + prelude::{CheapClone, DataSourceContext, DeploymentHash, Link}, }; -use anyhow::{Context, Error}; +use anyhow::{anyhow, Context, Error}; use futures03::{stream::FuturesOrdered, TryStreamExt}; use serde::Deserialize; use slog::{info, Logger}; use std::{fmt, sync::Arc}; use super::{ - common::{MappingABI, UnresolvedMappingABI}, + common::{CallDecls, MappingABI, UnresolvedMappingABI}, DataSourceTemplateInfo, TriggerWithHandler, }; @@ -84,6 +84,9 @@ impl DataSource { return None; } + // let calls = + // DeclaredCall::from_entity_handler(&self.mapping, &event_handler, &log, ¶ms)?; + Some(TriggerWithHandler::new( data_source::MappingTrigger::Subgraph(trigger.clone()), handler.handler.clone(), @@ -140,12 +143,23 @@ impl Mapping { pub fn requires_archive(&self) -> anyhow::Result { calls_host_fn(&self.runtime, "ethereum.call") } + + pub fn find_abi(&self, abi_name: &str) -> Result, Error> { + Ok(self + .abis + .iter() + .find(|abi| abi.name == abi_name) + .ok_or_else(|| anyhow!("No ABI entry with name `{}` found", abi_name))? + .cheap_clone()) + } } #[derive(Clone, Debug, Hash, Eq, PartialEq, Deserialize)] pub struct EntityHandler { pub handler: String, pub entity: String, + #[serde(default)] + pub calls: CallDecls, } #[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)] diff --git a/store/test-store/tests/chain/ethereum/manifest.rs b/store/test-store/tests/chain/ethereum/manifest.rs index 0bd682ebb20..45e15056143 100644 --- a/store/test-store/tests/chain/ethereum/manifest.rs +++ b/store/test-store/tests/chain/ethereum/manifest.rs @@ -1489,3 +1489,75 @@ dataSources: assert_eq!(4, decls.len()); }); } + + +#[test] +fn parses_eth_call_decls_for_subgraph_datasource() { + const YAML: &str = " +specVersion: 1.3.0 +schema: + file: + /: /ipfs/Qmschema +features: + - ipfsOnEthereumContracts +dataSources: + - kind: subgraph + name: Factory + entities: + - Gravatar + network: mainnet + source: + address: 'QmSWWT2yrTFDZSL8tRyoHEVrcEKAUsY2hj2TMQDfdDZU8h' + startBlock: 9562480 + mapping: + apiVersion: 0.0.6 + language: wasm/assemblyscript + entities: + - TestEntity + file: + /: /ipfs/Qmmapping + abis: + - name: Factory + file: + /: /ipfs/Qmabi + handlers: + - handler: handleEntity + entity: User + calls: + fake1: Factory[entity.address].get(entity.user) + fake3: Factory[0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF].get(entity.address) + fake4: Factory[0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF].get(0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF) +"; + + test_store::run_test_sequentially(|store| async move { + let store = store.subgraph_store(); + let unvalidated: UnvalidatedSubgraphManifest = { + let mut resolver = TextResolver::default(); + let id = DeploymentHash::new("Qmmanifest").unwrap(); + resolver.add(id.as_str(), &YAML); + resolver.add("/ipfs/Qmabi", &ABI); + resolver.add("/ipfs/Qmschema", &GQL_SCHEMA); + resolver.add("/ipfs/Qmmapping", &MAPPING_WITH_IPFS_FUNC_WASM); + + let resolver: Arc = Arc::new(resolver); + + let raw = serde_yaml::from_str(YAML).unwrap(); + UnvalidatedSubgraphManifest::resolve( + id, + raw, + &resolver, + &LOGGER, + SPEC_VERSION_1_3_0.clone(), + ) + .await + .expect("Parsing simple manifest works") + }; + + let manifest = unvalidated.validate(store.clone(), true).await.unwrap(); + let ds = &manifest.data_sources[0].as_subgraph().unwrap(); + // For more detailed tests of parsing CallDecls see the data_soure + // module in chain/ethereum + let decls = &ds.mapping.handlers[0].calls.decls; + assert_eq!(3, decls.len()); + }); +}