ethabi_next/
constructor.rs

1// Copyright 2015-2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Contract constructor call builder.
10use crate::{encode, Bytes, Error, Param, ParamType, Result, Token};
11use serde::Deserialize;
12
13/// Contract constructor specification.
14#[derive(Debug, Clone, PartialEq, Deserialize)]
15pub struct Constructor {
16	/// Constructor input.
17	pub inputs: Vec<Param>,
18}
19
20impl Constructor {
21	/// Returns all input params of given constructor.
22	fn param_types(&self) -> Vec<ParamType> {
23		self.inputs.iter().map(|p| p.kind.clone()).collect()
24	}
25
26	/// Prepares ABI constructor call with given input params.
27	pub fn encode_input(&self, code: Bytes, tokens: &[Token]) -> Result<Bytes> {
28		let params = self.param_types();
29
30		if Token::types_check(tokens, &params) {
31			Ok(code.into_iter().chain(encode(tokens)).collect())
32		} else {
33			Err(Error::InvalidData)
34		}
35	}
36}