Integration with zgp whitelist contract (#4215)
* zgp-transactions checker * polishing * rename + refactor * refuse-service-transactions cl option * fixed tests compilation
This commit is contained in:
committed by
Gav Wood
parent
220084d77d
commit
092e24b9f2
@@ -17,6 +17,8 @@
|
||||
// Rust/Parity ABI struct autogenerator.
|
||||
// By Gav Wood, 2016.
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
String.prototype.replaceAll = function(f, t) { return this.split(f).join(t); }
|
||||
String.prototype.toSnake = function(){
|
||||
return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
|
||||
@@ -24,6 +26,7 @@ String.prototype.toSnake = function(){
|
||||
|
||||
function makeContractFile(name, json, prefs) {
|
||||
return `// Autogenerated from JSON contract definition using Rust contract convertor.
|
||||
// Command line: ${process.argv.slice(2).join(' ')}
|
||||
#![allow(unused_imports)]
|
||||
use std::string::String;
|
||||
use std::result::Result;
|
||||
@@ -39,14 +42,15 @@ function convertContract(name, json, prefs) {
|
||||
return `${prefs._pub ? "pub " : ""}struct ${name} {
|
||||
contract: ethabi::Contract,
|
||||
address: util::Address,
|
||||
do_call: Box<Fn(util::Address, Vec<u8>) -> Result<Vec<u8>, String> + Send ${prefs._sync ? "+ Sync " : ""}+ 'static>,
|
||||
${prefs._explicit_do_call ? "" : `do_call: Box<Fn(util::Address, Vec<u8>) -> Result<Vec<u8>, String> + Send${prefs._sync ? " + Sync " : ""}+ 'static>,`}
|
||||
}
|
||||
impl ${name} {
|
||||
pub fn new<F>(address: util::Address, do_call: F) -> Self where F: Fn(util::Address, Vec<u8>) -> Result<Vec<u8>, String> + Send ${prefs._sync ? "+ Sync " : ""}+ 'static {
|
||||
pub fn new${prefs._explicit_do_call ? "" : "<F>"}(address: util::Address${prefs._explicit_do_call ? "" : `", do_call: F"`}) -> Self
|
||||
${prefs._explicit_do_call ? "" : `where F: Fn(util::Address, Vec<u8>) -> Result<Vec<u8>, String> + Send ${prefs._sync ? "+ Sync " : ""}+ 'static`} {
|
||||
${name} {
|
||||
contract: ethabi::Contract::new(ethabi::Interface::load(b"${JSON.stringify(json.filter(a => a.type == 'function')).replaceAll('"', '\\"')}").expect("JSON is autogenerated; qed")),
|
||||
address: address,
|
||||
do_call: Box::new(do_call),
|
||||
${prefs._explicit_do_call ? "" : `do_call: Box::new(do_call),`}
|
||||
}
|
||||
}
|
||||
fn as_string<T: fmt::Debug>(e: T) -> String { format!("{:?}", e) }
|
||||
@@ -205,6 +209,7 @@ function tokenExtract(expr, type, _prefs) {
|
||||
}
|
||||
|
||||
function convertFunction(json, _prefs) {
|
||||
let cprefs = _prefs || {};
|
||||
let prefs = (_prefs || {})[json.name] || (_prefs || {})['_'] || {};
|
||||
let snakeName = json.name.toSnake();
|
||||
let params = json.inputs.map((x, i) => (x.name ? x.name.toSnake() : ("_" + (i + 1))) + ": " + mapType(x.name, x.type, prefs[x.name]));
|
||||
@@ -212,18 +217,35 @@ function convertFunction(json, _prefs) {
|
||||
return `
|
||||
/// Auto-generated from: \`${JSON.stringify(json)}\`
|
||||
#[allow(dead_code)]
|
||||
pub fn ${snakeName}(&self${params.length > 0 ? ', ' + params.join(", ") : ''}) -> Result<${returns}, String> {
|
||||
pub fn ${snakeName}${cprefs._explicit_do_call ? "<F>" : ""}(&self${cprefs._explicit_do_call ? `, do_call: &F` : ""}${params.length > 0 ? ', ' + params.join(", ") : ''}) -> Result<${returns}, String>
|
||||
${cprefs._explicit_do_call ? `where F: Fn(util::Address, Vec<u8>) -> Result<Vec<u8>, String> + Send ${prefs._sync ? "+ Sync " : ""}` : ""} {
|
||||
let call = self.contract.function("${json.name}".into()).map_err(Self::as_string)?;
|
||||
let data = call.encode_call(
|
||||
vec![${json.inputs.map((x, i) => convertToken(x.name ? x.name.toSnake() : ("_" + (i + 1)), x.type, prefs[x.name])).join(', ')}]
|
||||
).map_err(Self::as_string)?;
|
||||
${json.outputs.length > 0 ? 'let output = ' : ''}call.decode_output((self.do_call)(self.address.clone(), data)?).map_err(Self::as_string)?;
|
||||
${json.outputs.length > 0 ? 'let output = ' : ''}call.decode_output((${cprefs._explicit_do_call ? "" : "self."}do_call)(self.address.clone(), data)?).map_err(Self::as_string)?;
|
||||
${json.outputs.length > 0 ? 'let mut result = output.into_iter().rev().collect::<Vec<_>>();' : ''}
|
||||
Ok((${json.outputs.map((o, i) => tokenExtract('result.pop().ok_or("Invalid return arity")?', o.type, prefs[o.name])).join(', ')}))
|
||||
}`;
|
||||
}
|
||||
|
||||
// default preferences:
|
||||
let prefs = {"_pub": true, "_": {"_client": {"string": true}, "_platform": {"string": true}}, "_sync": true};
|
||||
// default contract json ABI
|
||||
let jsonabi = [{"constant":true,"inputs":[],"name":"getValidators","outputs":[{"name":"","type":"address[]"}],"payable":false,"type":"function"}];
|
||||
|
||||
let out = makeContractFile("Contract", jsonabi, {"_pub": true, "_": {"_client": {"string": true}, "_platform": {"string": true}}, "_sync": true});
|
||||
// parse command line options
|
||||
for (let i = 1; i < process.argv.length; ++i) {
|
||||
let arg = process.argv[i];
|
||||
if (arg.indexOf("--jsonabi") == 0) {
|
||||
jsonabi = arg.slice(10);
|
||||
if (fs.existsSync(jsonabi)) {
|
||||
jsonabi = JSON.parse(fs.readFileSync(jsonabi).toString());
|
||||
}
|
||||
} else if (arg.indexOf("--explicit-do-call") == 0) {
|
||||
prefs._explicit_do_call = true;
|
||||
}
|
||||
}
|
||||
|
||||
let out = makeContractFile("Contract", jsonabi, prefs);
|
||||
console.log(`${out}`);
|
||||
|
||||
Reference in New Issue
Block a user