Unverified Commit 9eb92645 authored by Andrew Jones's avatar Andrew Jones Committed by GitHub

Extern function to generate metadata (#460)

* Hack to see if #[start] works

* Hack to see if #[no_mangle] works

* Remove incrementer bin target

* Use Rust extern api and pass back InkProject struct directly

* Uglify name

* Update remaining examples to use generate metadata function

* Do not generate for ink-as-dependency to make delegator work

* Remove explicit version from abi dependency

* Remove duplication

* Remove GenerateAbi trait
parent d4cbfcea
Pipeline #97874 failed with stages
in 11 minutes and 17 seconds
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "delegator" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::Delegator as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "dns" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::DomainNameService as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "erc20" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::Erc20 as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "erc721" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::Erc721 as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "flipper" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::Flipper as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "incrementer" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::Incrementer as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "multisig_plain" }
ink_lang = { path = "../../../../lang", package = "ink_lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::MultisigPlain as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -10,6 +10,7 @@ name = "abi-gen"
path = "main.rs"
[dependencies]
ink_abi = { path = "../../../../abi" }
contract = { path = "../..", package = "runtime_storage" }
ink_lang = { path = "../../../../lang" }
serde = "1.0"
......
extern crate contract;
extern "Rust" {
fn __ink_generate_metadata() -> ink_abi::InkProject;
}
fn main() -> Result<(), std::io::Error> {
let abi = <::contract::RuntimeStorage as ::ink_lang::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
let ink_project = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&ink_project)?;
std::fs::create_dir("target").ok();
std::fs::write("target/metadata.json", contents)?;
Ok(())
......
......@@ -34,24 +34,22 @@ pub struct GenerateAbi<'a> {
impl GenerateCode for GenerateAbi<'_> {
fn generate_code(&self) -> TokenStream2 {
let storage_ident = &self.contract.storage.ident;
let contract = self.generate_contract();
let layout = self.generate_layout();
quote! {
#[cfg(feature = "std")]
#[cfg(not(feature = "ink-as-dependency"))]
const _: () = {
impl ::ink_lang::GenerateAbi for #storage_ident {
fn generate_abi() -> ::ink_abi::InkProject {
let contract: ::ink_abi::ContractSpec = {
#contract
};
let layout: ::ink_abi::layout2::Layout = {
#layout
};
::ink_abi::InkProject::new(layout, contract)
}
#[no_mangle]
pub fn __ink_generate_metadata() -> ::ink_abi::InkProject {
let contract: ::ink_abi::ContractSpec = {
#contract
};
let layout: ::ink_abi::layout2::Layout = {
#layout
};
::ink_abi::InkProject::new(layout, contract)
}
};
}
......
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Types implementing this trait can generate their metadata.
pub trait GenerateAbi {
/// Generates the virtual ABI (metadata).
fn generate_abi() -> ink_abi::InkProject;
}
......@@ -14,9 +14,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
mod abi;
mod contract;
mod cross_calling;
mod dispatcher;
......@@ -27,9 +24,6 @@ mod traits;
pub use ink_lang_macro::contract;
#[cfg(feature = "std")]
pub use self::abi::GenerateAbi;
pub use self::{
contract::{
DispatchMode,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment