ethers_abi/
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.
10
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14#[cfg(not(feature = "std"))]
15use crate::no_std_prelude::*;
16use crate::{encode, Bytes, Error, Param, ParamType, Result, Token};
17
18/// Contract constructor specification.
19#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20#[derive(Debug, Clone, PartialEq)]
21pub struct Constructor {
22	/// Constructor input.
23	pub inputs: Vec<Param>,
24}
25
26impl Constructor {
27	/// Returns all input params of given constructor.
28	fn param_types(&self) -> Vec<ParamType> {
29		self.inputs.iter().map(|p| p.kind.clone()).collect()
30	}
31
32	/// Prepares ABI constructor call with given input params.
33	pub fn encode_input(&self, code: Bytes, tokens: &[Token]) -> Result<Bytes> {
34		let params = self.param_types();
35
36		if Token::types_check(tokens, &params) {
37			Ok(code.into_iter().chain(encode(tokens)).collect())
38		} else {
39			Err(Error::InvalidData)
40		}
41	}
42}