├── .gitignore ├── res └── dummy.wasm ├── .rustfmt.toml ├── deploy.sh ├── setup.sh ├── Cargo.toml ├── src ├── utils.rs └── lib.rs ├── README.md └── tests └── general.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | instructions.md 4 | -------------------------------------------------------------------------------- /res/dummy.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thor314/near-template/HEAD/res/dummy.wasm -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | tab_spaces = 2 2 | hard_tabs = false # default, but explicit spaces not tabs 3 | merge_imports = true 4 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | near deploy --wasmFile res/dummy.wasm --initFunction "new" --initArgs '' --accountId $1.testnet 4 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # replace all instances of the word dummy 4 | sed -i "s/dummy/$1/g" Cargo.toml 5 | sed -i "s/dummy/$1/g" build.sh 6 | sed -i "s/dummy/$1/g" deploy.sh 7 | sed -i "s/dummy/$1/g" tests/general.rs 8 | 9 | target=`echo $1 | tr [A-Z] [a-z] | sed -e 's/^./\U&/g; s/ ./\U&/g'` 10 | sed -i "s/Dummy/$target/g" src/lib.rs 11 | sed -i "s/Dummy/$target/g" tests/general.rs 12 | 13 | mv README.md instructions.md 14 | echo "# $1" >> README.md 15 | git remote remove origin 16 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "dummy" 3 | version = "0.1.0" 4 | authors = ["Thor Kamphefner ({ 14 | let log_message = format!($($arg)*); 15 | let log = log_message.as_bytes(); 16 | env::log(&log) 17 | }) 18 | } 19 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_imports)] 2 | #![allow(dead_code)] 3 | use near_sdk::{ 4 | borsh::{self, BorshDeserialize, BorshSerialize}, 5 | collections::*, 6 | *, 7 | }; 8 | 9 | mod utils; 10 | 11 | #[cfg(target = "wasm32")] 12 | #[global_allocator] 13 | static ALLOC: near_sdk::wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 14 | 15 | #[near_bindgen] 16 | #[derive(BorshDeserialize, BorshSerialize)] 17 | pub struct Dummy {} 18 | 19 | /// Default must be implemented for wasm compilation. 20 | impl Default for Dummy { 21 | fn default() -> Self { 22 | panic!("Dummy") 23 | } 24 | } 25 | #[near_bindgen] 26 | impl Dummy { 27 | #[init] 28 | pub fn new() -> Self { 29 | assert!(!env::state_exists(), "Already, initialized"); 30 | Self {} 31 | } 32 | } 33 | 34 | #[cfg(all(test, not(target_arch = "wasm32")))] 35 | mod tests { 36 | fn test() { 37 | assert!(true); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Template for a NEAR project 2 | 3 | If you're looking for a `no-std` version of this template, go [here](https://github.com/thor314/near-no-std-template). 4 | 5 | Contains: 6 | - a setup script 7 | - Cargo.toml setup with simulation testing and NEAR sdk crates 8 | - build and deploy scripts 9 | - a src directory containing lib.rs, with common imports and ignore attributes 10 | - a test directory containing a simulation test setup 11 | - my preferred .gitignore and .rustfmt.toml setup 12 | 13 | ## Setup script usage: 14 | `./setup.sh my_project_name` 15 | will change all instances of "dummy" to whatever you'd like to call this project, and rename this 16 | README to `instructions.md`. 17 | 18 | ## Build script usage: 19 | `./build.sh` to build the wasm blob. The wasm blob will be copied into `res/`. 20 | 21 | ## Deploy script usage: 22 | `./deploy.sh MYADDRESS` to deploy your wasm blob to the NEAR testnet. 23 | 24 | Note that if you change the constructor arguments, You will have to modify the arguments int the `deploy.sh` 25 | script. If my constructor is `new(name: String, number: u32) -> Self`, and I want to initialize with ("Todd", 1), I 26 | will change the script to: 27 | 28 | `near deploy --wasmFile res/dummy.wasm --initFunction "new" --initArgs '{"name": "Todd", "number": 1}' --accountId $1.testnet` 29 | 30 | and call `./deploy.sh MYADDRESS`, or to provide arguments, change `deploy.sh` to: 31 | 32 | `near deploy --wasmFile res/dummy.wasm --initFunction "new" --initArgs "{\"name\": $1, \"number\": $2}" --accountId $1.testnet` 33 | 34 | and call `./deploy.sh MYADDRESS MYNAME MYNUM`. 35 | -------------------------------------------------------------------------------- /tests/general.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_imports)] 2 | #![allow(unused_variables)] 3 | #![allow(dead_code)] 4 | use near_sdk::{ 5 | borsh::{self, BorshDeserialize, BorshSerialize}, 6 | json_types::U128, 7 | serde::{Deserialize, Serialize}, 8 | serde_json::json, 9 | *, 10 | }; 11 | use near_sdk_sim::{ 12 | account::AccessKey, call, deploy, init_simulator, near_crypto::Signer, to_yocto, view, 13 | ContractAccount, ExecutionResult, UserAccount, DEFAULT_GAS, STORAGE_AMOUNT, 14 | }; 15 | 16 | // Bring contract crate into namespace 17 | extern crate dummy; 18 | use dummy::Dummy; 19 | 20 | // Load contracts' bytes. 21 | near_sdk_sim::lazy_static! { 22 | static ref WASM_BYTES: &'static [u8] = include_bytes!("../res/dummy.wasm").as_ref(); 23 | } 24 | 25 | /// Deploy the contract(s) and create some dummy accounts. Returns: 26 | /// - The Dummy Contract 27 | /// - Root Account 28 | /// - Testnet Account (utility suffix for building other addresses) 29 | /// - A deployer account address 30 | fn init( 31 | initial_balance: u128, 32 | deploy_to: &str, 33 | ) -> ( 34 | ContractAccount, 35 | UserAccount, // root 36 | UserAccount, // testnet suffix 37 | UserAccount, // deployer account 38 | ) { 39 | // Root account has address: "root" 40 | let root_account = init_simulator(None); 41 | 42 | // Other accounts may be created from the root account 43 | // Note: address naming is fully expressive: we may create any suffix we desire, ie testnet, near, etc. 44 | // but only those two (.testnet, .near) will be used in practice. 45 | let testnet = root_account.create_user("testnet".to_string(), to_yocto("1000000")); 46 | 47 | // We need an account to deploy the contracts from. We may create subaccounts of "testnet" as follows: 48 | let deployer = testnet.create_user(deploy_to.to_string(), to_yocto("1000000")); 49 | 50 | // uses default values for deposit and gas 51 | let dummy_contract = deploy!( 52 | contract: DummyContract, 53 | contract_id: "dummy", 54 | bytes: &WASM_BYTES, 55 | // User deploying the contract 56 | signer_account: deployer, 57 | // init method 58 | init_method: new() 59 | ); 60 | (dummy_contract, root_account, testnet, deployer) 61 | } 62 | 63 | /// Helper to log ExecutionResult outcome of a call/view 64 | fn print_helper(res: ExecutionResult) { 65 | println!("Promise results: {:#?}", res.promise_results()); 66 | //println!("Receipt results: {:#?}", res.get_receipt_results()); 67 | println!("Profiling: {:#?}", res.profile_data()); 68 | //println!("Result: {:#?}", res); 69 | assert!(res.is_ok()); 70 | } 71 | 72 | #[test] 73 | fn simtest() { 74 | let (dummy_contract, root_account, testnet, deployer) = init(to_yocto("1000"), "me"); 75 | 76 | // let view = view!(dummy_contract.MYMETHOD()); 77 | // print_helper(res); 78 | 79 | /* 80 | let res = call!( 81 | deployer, 82 | dummy_contract.MYMETHOD(), 83 | deposit = STORAGE_AMOUNT // send this amount to a payable function, or exclude this line if send 0 84 | ); 85 | print_helper(res); 86 | */ 87 | } 88 | --------------------------------------------------------------------------------