{"id":15448647,"url":"https://github.com/openethereum/pwasm-test","last_synced_at":"2025-10-11T12:31:36.694Z","repository":{"id":113253781,"uuid":"105679870","full_name":"openethereum/pwasm-test","owner":"openethereum","description":"pwasm-test is a set of tools to make it easy to test internal logic of contracts written using pwasm-std","archived":true,"fork":false,"pushed_at":"2018-11-13T12:59:26.000Z","size":1465,"stargazers_count":6,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-07T09:01:48.037Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openethereum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-03T17:13:58.000Z","updated_at":"2023-01-28T10:35:12.000Z","dependencies_parsed_at":"2023-05-04T16:35:32.314Z","dependency_job_id":null,"html_url":"https://github.com/openethereum/pwasm-test","commit_stats":null,"previous_names":["paritytech/pwasm-test"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/openethereum/pwasm-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openethereum%2Fpwasm-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openethereum%2Fpwasm-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openethereum%2Fpwasm-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openethereum%2Fpwasm-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openethereum","download_url":"https://codeload.github.com/openethereum/pwasm-test/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openethereum%2Fpwasm-test/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279007167,"owners_count":26084247,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T20:29:12.699Z","updated_at":"2025-10-11T12:31:36.690Z","avatar_url":"https://github.com/openethereum.png","language":"Rust","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/paritytech/pwasm-test.svg?branch=master)](https://travis-ci.org/paritytech/pwasm-test)\n\n`pwasm-test` is a set of tools to make it easy to test internal logic of contracts written using [pwasm-ethereum](https://github.com/paritytech/pwasm-ethereum).\n\n## Usage\nLet's assume we have a simple TokenContract. Let's see how we use `pwasm_test` to mock `pwasm_ethereum::sender()` call:\n\n```rust\nextern crate parity_hash;\nextern crate pwasm_ethereum;\nextern crate uint;\n\nuse parity_hash::{H256, Address};\nuse pwasm_ethereum;\nuse uint::U256;\nuse pwasm_abi_derive::eth_abi;\n\nstatic TOTAL_SUPPLY_KEY: H256 = H256([2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);\nstatic OWNER_KEY: H256 = H256([3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);\n\npub trait TokenContract {\n\tfn constructor(\u0026mut self, _total_supply: U256);\n\t#[constant]\n\tfn totalSupply(\u0026mut self) -\u003e U256;\n\t#[constant]\n\tfn balanceOf(\u0026mut self, _owner: Address) -\u003e U256;\n\tfn transfer(\u0026mut self, _to: Address, _amount: U256) -\u003e bool;\n\t/// Event declaration\n\t#[event]\n\tfn Transfer(\u0026mut self, indexed_from: Address, indexed_to: Address, _value: U256);\n}\n\npub struct TokenContractInstance;\n\n// Reads balance by address\nfn read_balance_of(owner: \u0026Address) -\u003e U256 {\n\tpwasm_ethereum::read(\u0026balance_key(owner)).into()\n}\n\n// Generates a balance key for some address.\n// Used to map balances with their owners.\nfn balance_key(address: \u0026Address) -\u003e H256 {\n\tlet mut key = H256::from(address);\n\tkey[0] = 1; // just a naiive \"namespace\";\n\tkey\n}\n\nimpl TokenContract for TokenContractInstance {\n\tfn constructor(\u0026mut self, total_supply: U256) {\n\t\tlet sender = pwasm_ethereum::sender();\n\t\t// Set up the total supply for the token\n\t\tpwasm_ethereum::write(\u0026TOTAL_SUPPLY_KEY, \u0026total_supply.into());\n\t\t// Give all tokens to the contract owner\n\t\tpwasm_ethereum::write(\u0026balance_key(\u0026sender), \u0026total_supply.into());\n\t\t// Set the contract owner\n\t\tpwasm_ethereum::write(\u0026OWNER_KEY, \u0026H256::from(sender).into());\n\t}\n\n\tfn totalSupply(\u0026mut self) -\u003e U256 {\n\t\tpwasm_ethereum::read(\u0026TOTAL_SUPPLY_KEY).into()\n\t}\n\n\tfn balanceOf(\u0026mut self, owner: Address) -\u003e U256 {\n\t\tread_balance_of(\u0026owner)\n\t}\n\n\tfn transfer(\u0026mut self, to: Address, amount: U256) -\u003e bool {\n\t\tlet sender = pwasm_ethereum::sender();\n\t\tlet senderBalance = read_balance_of(\u0026sender);\n\t\tlet recipientBalance = read_balance_of(\u0026to);\n\t\tif amount == 0.into() || senderBalance \u003c amount {\n\t\t\tfalse\n\t\t} else {\n\t\t\tlet new_sender_balance = senderBalance - amount;\n\t\t\tlet new_recipient_balance = recipientBalance + amount;\n\t\t\tpwasm_ethereum::write(\u0026balance_key(\u0026sender), \u0026new_sender_balance.into());\n\t\t\tpwasm_ethereum::write(\u0026balance_key(\u0026to), \u0026new_recipient_balance.into());\n\t\t\tself.Transfer(sender, to, amount);\n\t\t\ttrue\n\t\t}\n\t}\n}\n\n#[cfg(test)]\n#[macro_use]\nextern crate pwasm_test;\n\n#[cfg(test)]\n#[allow(non_snake_case)]\nmod tests {\n    extern crate std;\n    use super::*;\n    use pwasm_test::{ext_reset, ext_get};\n    use parity_hash::Address;\n\n    #[test]\n    fn should_succeed_transfering_1000_from_owner_to_another_address() {\n        let mut contract = TokenContractInstance{};\n        let owner_address = Address::from(\"0xea674fdde714fd979de3edf0f56aa9716b898ec8\");\n        let sam_address = Address::from(\"0xdb6fd484cfa46eeeb73c71edee823e4812f9e2e1\");\n        // Here we're creating an External context using ExternalBuilder and set the `sender` to the `owner_address`\n        // so `pwasm_ethereum::sender()` in TokenContract::constructor() will return that `owner_address`\n        ext_reset(|e| e.sender(owner_address.clone()));\n        let total_supply = 10000.into();\n        contract.constructor(total_supply);\n        assert_eq!(contract.balanceOf(owner_address), total_supply);\n        assert_eq!(contract.transfer(sam_address, 1000.into()), true);\n        assert_eq!(contract.balanceOf(owner_address), 9000.into());\n        assert_eq!(contract.balanceOf(sam_address), 1000.into());\n\t\t// 1 log entry should be created\n        assert_eq!(ext_get().logs().len(), 1);\n    }\n}\n```\nFor more usage examples take a look:\n* https://github.com/paritytech/pwasm-token-example/blob/master/contract/src/lib.rs\n* https://github.com/paritytech/pwasm-repo-contract/blob/master/contract/src/lib.rs\n\n## Run tests\n\n`cargo test --all`\n\n[Parity Wasm Tutorial](https://github.com/paritytech/pwasm-tutorial) - a full fledged tutorial on how to write contracts in Webassembly for Kovan and other Wasm-enabled networks.\n\n# License\n\n`parity-test` is primarily distributed under the terms of both the MIT\nlicense and the Apache License (Version 2.0), at your choice.\n\nSee LICENSE-APACHE, and LICENSE-MIT for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenethereum%2Fpwasm-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenethereum%2Fpwasm-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenethereum%2Fpwasm-test/lists"}