openethereum / pwasm-tutorial

A step-by-step tutorial on how to write contracts in Wasm for Kovan
GNU General Public License v3.0
229 stars 34 forks source link

Unable to compile due to "out of scope error" #38

Closed 0zAND1z closed 4 years ago

0zAND1z commented 6 years ago

Here's the code:

#![no_std]
#![allow(non_snake_case)]
#![feature(alloc)]
#![feature(use_extern_macros)]
#![feature(proc_macro_gen)]

extern crate pwasm_std;
extern crate pwasm_ethereum;
extern crate pwasm_abi;
extern crate pwasm_abi_derive;
/// Bigint used for 256-bit arithmetic

pub mod hello {
    // add code here
    use pwasm_ethereum;
    use pwasm_abi::types::*;

    // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html
    use pwasm_abi_derive::eth_abi;

    #[eth_abi(HelloEndpoint)]
    pub trait HelloInterface {
        /// The constructor
        fn constructor(&mut self);
        /// Total amount of tokens
        fn sayHello(&mut self) -> String;
    }

    pub struct HelloContract;

    impl HelloInterface for HelloContract {
        fn constructor (&mut self) {

        }

        fn sayHello(&mut self) -> String {
            println("Hallo!");
        }
    }
}

// Declares the dispatch and dispatch_ctor methods
use pwasm_abi::eth::EndpointInterface;

#[no_mangle]
pub fn call() {
    let mut endpoint = hello::HelloEndpoint::new(hello::HelloContract{});

    // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details
    pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input()));
}

#[no_mangle]
pub fn deploy() {
    let mut endpoint = hello::HelloEndpoint::new(hello::HelloContract{});
    //
    endpoint.dispatch_ctor(&pwasm_ethereum::input());
}

The log while running ./build.sh:

error[E0433]: failed to resolve. Could not find `HelloEndpoint` in `hello`
  --> src/lib.rs:92:31
   |
92 |     let mut endpoint = hello::HelloEndpoint::new(hello::HelloContract{});
   |                               ^^^^^^^^^^^^^ Could not find `HelloEndpoint` in `hello`

error[E0433]: failed to resolve. Could not find `HelloEndpoint` in `hello`
   --> src/lib.rs:100:31
    |
100 |     let mut endpoint = hello::HelloEndpoint::new(hello::HelloContract{});
    |                               ^^^^^^^^^^^^^ Could not find `HelloEndpoint` in `hello`

error[E0412]: cannot find type `String` in this scope
  --> src/lib.rs:21:5
   |
21 |     #[eth_abi(TokenEndpoint)]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
   |
15 |     use pwasm_std::String;
   |

error[E0412]: cannot find type `String` in this scope
  --> src/lib.rs:36:35
   |
36 |         fn sayHello(&mut self) -> String {
   |                                   ^^^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
   |
15 |     use pwasm_std::String;
   |

error[E0425]: cannot find function `println` in this scope
  --> src/lib.rs:37:13
   |
37 |             println("Hallo!");
   |             ^^^^^^^ not found in this scope

warning: unused import: `pwasm_ethereum`
  --> src/lib.rs:15:9
   |
15 |     use pwasm_ethereum;
   |         ^^^^^^^^^^^^^^
   |
   = note: #[warn(unused_imports)] on by default

warning: unused import: `pwasm_abi::types::*`
  --> src/lib.rs:16:9
   |
16 |     use pwasm_abi::types::*;
   |         ^^^^^^^^^^^^^^^^^^^

warning: unused import: `pwasm_abi::eth::EndpointInterface`
  --> src/lib.rs:73:5
   |
73 | use pwasm_abi::eth::EndpointInterface;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

Some errors occurred: E0412, E0425, E0433.
For more information about an error, try `rustc --explain E0412`.
error: Could not compile `pwasm-tutorial-contract`.

To learn more, run the command again with --verbose.
AchalaSB commented 5 years ago

@fckt I'm facing same issue. How to solve this problem?

lexfrl commented 5 years ago

The problem is that the String type is not supported by the pwasm-abi. Try to use &[u8]

AchalaSB commented 5 years ago

Thanks for reverting. As per my knowledge the type u8 and its byte slice &[u8] will convert from String to integer type. (Sorry if I'm wrong) How Can I use &[u8] without using String type but String is not supported by pwasm-abi?

lexfrl commented 5 years ago

have you tried to convert String to &[u8]?

AchalaSB commented 5 years ago

I tried by changing String type to &[u8] as mentioned above. But getting an error

error: custom attribute panicked
  --> src/lib.rs:21:5
   |
21 |     #[eth_abi(HelloEndpoint)]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: message: Unable to handle param of type Rptr(None, MutTy { ty: Slice(Path(None, Path { global: false, segments: [PathSegment { ident: Ident("u8"), parameters: AngleBracketed(AngleBracketedParameterData { lifetimes: [], types: [], bindings: [] }) }] })), mutability: Immutable }): not supported by abi

error: aborting due to previous error

error: Could not compile `pwasm-tutorial-contract`.

To learn more, run the command again with --verbose.
lexfrl commented 5 years ago

so, haven't you tried Vec<u8>?

AchalaSB commented 5 years ago

@fckt Writing a #![no_std] crate disables the standard library, including vec! and Vec But pwasm-std itself compiled with no_std. Removing #![no_std] gives following error

error: duplicate lang item in crate `pwasm_std`: `panic_impl`.
  |
  = note: first defined in crate `std`.

error: duplicate lang item in crate `pwasm_std`: `oom`.
  |
  = note: first defined in crate `std`.

error: aborting due to 2 previous errors

how can use both pwasm-std and Vec

lexfrl commented 5 years ago

use pwasm_abi::types::*; imports Vec as well. Take a look here for more examples

AchalaSB commented 5 years ago

I have used use pwasm_abi ::types :: *; already.

AchalaSB commented 5 years ago

@fckt Is Vec<u8> is compatible with web3provider? Im getting an error ‘Transaction Error. Exception thrown in contract code.’ while testing

If I use Vec<u8> to convert my string is it gives me array of characters ? or How it look like? Here's my code

fn sayHello(&mut self) -> Vec<u8> {
            use pwasm_std :: String;
            let s = String::from("this is a string");
            let v = Vec::from(s);
            v
        }
0zAND1z commented 4 years ago

Stale thread.