diff --git a/Cargo.lock b/Cargo.lock index f9cd0aa..a2b7d18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -319,7 +319,7 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "core-mx-life-bonding-sc" -version = "0.0.0" +version = "1.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-scenario", @@ -327,7 +327,7 @@ dependencies = [ [[package]] name = "core-mx-life-bonding-sc-meta" -version = "0.0.0" +version = "1.0.0" dependencies = [ "core-mx-life-bonding-sc", "multiversx-sc-meta", @@ -938,9 +938,9 @@ checksum = "b59072fa0624b55ae5ae3fa6bfa91515bbeb4ac440214bc4a509e2c8806d6e9f" [[package]] name = "multiversx-sc" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40b69a83a9423be4b543edee969205aa43ef4397c48b965a65bad857e022db5c" +checksum = "34215a81b2c37619a234db4d6facbef5853e4022df3494746b29ada5ff35859f" dependencies = [ "bitflags 2.4.2", "hex-literal", @@ -974,9 +974,9 @@ dependencies = [ [[package]] name = "multiversx-sc-derive" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5fe6400e2f4e57f7c9c00ad76108794aa962657cf62b4d4f49b99f02d9a6ba5" +checksum = "c41ce6becb22f45383e9ff60c88fa1f7946d92915c8409e9fbe3c3719325146f" dependencies = [ "hex", "proc-macro2", @@ -987,9 +987,9 @@ dependencies = [ [[package]] name = "multiversx-sc-meta" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12dec6cc0735acb48fce290b367c9d22684103055f00a7dec057840fc134973d" +checksum = "6297142f63e2b6e822d9780510f2ae0a3a19d12efdc928c1c4c90b085ff7fe61" dependencies = [ "clap", "colored", @@ -1014,9 +1014,9 @@ dependencies = [ [[package]] name = "multiversx-sc-scenario" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b71bc382a5ba02deb9452ec29453806c176200ee5d0e5c7c948f52dc84181382" +checksum = "342844787f7d617c50e69ec4b1d79e6a4934bb2f0f860e7894e392e2c5fa3ba1" dependencies = [ "base64", "bech32", diff --git a/Cargo.toml b/Cargo.toml index 14115d4..98f48c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "core-mx-life-bonding-sc" -version = "0.0.0" +version = "1.0.0" authors = ["Bucur David - Itheum"] edition = "2021" publish = false @@ -9,10 +9,10 @@ publish = false path = "src/lib.rs" [dependencies.multiversx-sc] -version = "0.47.4" +version = "0.47.8" [dev-dependencies.multiversx-sc-scenario] -version = "0.47.4" +version = "0.47.8" [workspace] members = [ diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e0d684 --- /dev/null +++ b/README.md @@ -0,0 +1,125 @@ +# Itheum Core MultiversX - Itheum Life Bonding Contract + +## Abstract + +The Itheum Life bonding contract coordinates data creator $ITHEUM token bonding actions and "Liveliness" reputation scores for data creators. + +## Prerequisites + +This documentation assumes the user has previous programming experience. Moreover, the user should have a basic understanding of the MultiversX blockchain. If you are new to the blockchain, please refer to the [MultiversX documentation](https://docs.multiversx.com/). In order to develop MultiversX smart contract related solutions, one needs to have installed [mxpy](https://docs.multiversx.com/sdk-and-tools/sdk-py/installing-mxpy). + +Understanding this document is also easier if one knows how [ESDT token transactions](https://docs.multiversx.com/developers/esdt-tokens/#transfers-to-a-smart-contract) are structured on the MultiversX blockchain and how [NFT tokens](https://docs.multiversx.com/tokens/nft-tokens/) work on the MultiversX Blockchain. + +## Itheum deployed contract addresses + +| Devnet | Mainnet | +| -------------------------------------------------------------- | ------- | +| erd1qqqqqqqqqqqqqpgq4xqxlq8p8zenrq4f0htgcwjzdlwmrhwdfsxsmavcuq | | + +## Endpoints + +See `devnet.snippets.sh` for list of available endpoints for user testing. + +## Development + +### Setting up dev environment (project development bootstrap) + how to build (and upgrade) + +- Uses `multiversx-sc-* 0.47.5` (In v1.0.0, we used 0.47.5) SDK libs (see Cargo.toml) +- Building requires minimum **mxpy 9.5.1** (In v1.0.0, we used mxpy 9.5.1). Check version using `mxpy --version` +- To build the project, requires minimum Rust version `1.78.0-nightly` (In v1.0.0, we used 1.78.0-nightly). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default nightly`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default nightly` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`. +- After you make sure you have the minimum Rust version you can then begin development. After you clone repo and before you run build, deploy or run the tests - follow these steps (most likely only needed the 1st time) +- [Upgrades] Note that when we upgrade smart contract, we should again follow the steps below too as lib version may have changed (but for upgrade I skipped the rustup default nightly cmd and did the others) + +``` +rustup default nightly +mxpy deps install rust --overwrite +cargo clean +cargo build +``` + +- The above should all work without any errors, next you can successfully run the following command to build via mxpy: `mxpy contract build` +- mxpy may ask you to install `nodejs` and `wasm-opt` to optimize the build, if so then follow instructions given by mxpy and do this +- You can now run the tests. See "How to test" section below +- You can now update code as needed + +### Architecture + +Coming Soon... + +### How to test + +The tests are located in the tests folder, in the rust_tests file. In order to run the tests one can use the command: + +```shell + cargo test +``` + +Another way of running the tests is by using the rust-analyzer extension in Visual Studio Code, which is also very helpful for MultiversX Smart Contract development. If one has the extension installed, they can go open and go to the top of the rust_tests file and click the Run Tests button. + +Note: In order to run the tests, one has to use the rust nightly version. One can switch to the nightly version by using: + +```shell + rustup default nightly +``` + +### How to deploy + +In order to deploy the smart contract on devnet one can use the interaction snippets present in the devnet. snippets file (which is located in the interactions folder). Before using the snippets, make sure to add your pem file in the root of the project under the name "wallet.pem" (or change the name to whichever one you wish to use in the interaction snippets). If you need info about how to derive a pem file you can find them [here](https://docs.multiversx.com/sdk-and-tools/sdk-py/deriving-the-wallet-pem-file/). To run the functions from the interaction file, one can use: + +```shell + source interaction/devnet.snippets.sh +``` + +After using that, to deploy one can simply use: + +```shell + deploy +``` + +### How to interact + +After deployment, one can interact with the smart contract and test its functionality. To do so, one can use the interaction snippets already presented above. More explanations can be found about the snippets inside the devnet.snippets file. + +### Mainnet Deployment (via Reproducible Builds) + +- After the security audit has passed the Mainnet deployment need to be verified to match the version that was audited. This guarantee is given via [Reproducible Builds](https://docs.multiversx.com/developers/reproducible-contract-builds/#how-to-run-a-reproducible-build-using-mxpy) + +**Step 1 (Final build + Code Hash):** + +- Be in the latest `main` branch. On the commit that was audited. Update the cargo.toml files with the correct version. This should match the version we use in our requirements files (i.e Notion). e.g. 1.0.0. you need to update the `cargo.toml` files in the root folder, wasm folder and meta folder. + +- In the `cargo.toml` files make sure you set the correct `edition`. i.e. edition = "2021" + +- As the `cargo.toml` files has been updated. Build locally as normal. i.e. see "how to build" above and also run tests as per "how to test". This will reflect the `cargo.toml` update in the linked cargo.lock files and produces the final local meta build files to keep the final github check-in and version tagging perfect. + +**Step 2 (Final build + Code Hash):** +Once the main commit is locked in, we can then produce the code hash and build to deploy to devnet 1st (for final testing) and then to mainnet (after sending the code hash to the auditor) + +1. Make sure your mxpy version is >= 6 (In v1.0.0, we used mxpy 9.5.1). +2. If Cargo.lock is in gitignore, remove it, build the contract and make a new commit. Otherwise this step can be skipped. (see Step 1 and repeat if needed) +3. Run the following in the root of the repository (run the latest Docker client in your computer. Used `Docker Desktop 4.18.0 (104112) on MacOS`): + +`mxpy contract reproducible-build --docker-image="multiversx/sdk-rust-contract-builder:v6.1.1"` + +Note that if you already have a output-docker from a previous build and deploy then delete this folder. + +Also note that if you are upgrading you may need to use a newer docker `sdk-rust-contract-builder` version. You can see the tags here https://hub.docker.com/r/multiversx/sdk-rust-contract-builder/tags. In v1.0.0, we used v6.1.1 for the build to upgrade to. We tested this on devnet before doing it on mainnet. + +This process may take some time. After it's done you should see "Docker build ran successfully!". An output-docker folder will be created containing the WASM files built in a reproducible way and artifacts.json containing the code hash of the WASM files. + +You can then share the auditor the code hash. The auditor will follow the same steps and compare the code hash with yours. If they match, we will be good to go! + +Note that "output-docker" folder should not be check-into GIT. + +**Step 4 (Send Code Hash to auditor to verify against devnet and give us all final clear):** +We should have got this final clear in Step 2, but we still do a final check here. + +**Step 5 (Deploy to Devnet as final build for testing + Move ABI to all apps that need it):** + +**Step 6 (Tag the commit in the main branch of Github with the version that was deployed. e.g. 1.0.0):** + +**Step 7 (Deploy SC to Mainnet):** + +## Contributing + +Feel free the contact the development team if you wish to contribute or if you have any questions. If you find any issues, please report them in the Issues sections of the repository. You can also create your own pull requests which will be analyzed by the team. diff --git a/interaction/devnet.snippets.sh b/interaction/devnet.snippets.sh index 868f2f2..cf311af 100644 --- a/interaction/devnet.snippets.sh +++ b/interaction/devnet.snippets.sh @@ -1,8 +1,8 @@ PROXY=https://devnet-gateway.multiversx.com CHAIN_ID="D" -WALLET="../wallet2.pem" -USER="../wallet2.pem" +WALLET="./wallet.pem" +USER="./wallet2.pem" ADDRESS=$(mxpy data load --key=address-devnet) DEPLOY_TRANSACTION=$(mxpy data load --key=deployTransaction-devnet) @@ -11,12 +11,12 @@ TOKEN="ITHEUM-fce905" TOKEN_HEX="0x$(echo -n ${TOKEN} | xxd -p -u | tr -d '\n')" # to deploy from last reprodubible build, we need to change or vice versa -# --bytecode output/datanftmint.wasm \ +# --bytecode output/core-mx-life-bonding-sc.wasm \ # to -# --bytecode output-docker/datanftmint/datanftmint.wasm \ +# --bytecode output-docker/core-mx-life-bonding-sc/core-mx-life-bonding-sc.wasm \ deploy(){ mxpy --verbose contract deploy \ - --bytecode output/core-mx-life-bonding-sc.wasm \ + --bytecode output-docker/core-mx-life-bonding-sc/core-mx-life-bonding-sc.wasm \ --outfile deployOutput \ --metadata-not-readable \ --metadata-payable-by-sc \ @@ -53,6 +53,15 @@ upgrade(){ --send || return } +# if you interact without calling deploy(), then you need to 1st run this to restore the vars from data +restoreDeployData() { + TRANSACTION=$(mxpy data parse --file="./interaction/deploy-devnet.interaction.json" --expression="data['emittedTransactionHash']") + ADDRESS=$(mxpy data parse --file="./interaction/deploy-devnet.interaction.json" --expression="data['contractAddress']") + + # after we upgraded to mxpy 8.1.2, mxpy data parse seems to load the ADDRESS correctly but it breaks when used below with a weird "Bad address" error + # so, we just hardcode the ADDRESS here. Just make sure you use the "data['contractAddress'] from the latest deploy-devnet.interaction.json file + ADDRESS="erd1qqqqqqqqqqqqqpgq4xqxlq8p8zenrq4f0htgcwjzdlwmrhwdfsxsmavcuq" +} setAdministrator(){ # $1 = address @@ -70,7 +79,6 @@ setAdministrator(){ --send || return } - setContractStateActive(){ mxpy --verbose contract call ${ADDRESS} \ --recall-nonce \ @@ -82,7 +90,6 @@ setContractStateActive(){ --send || return } - setContractStateInactive(){ mxpy --verbose contract call ${ADDRESS} \ --recall-nonce \ @@ -94,10 +101,7 @@ setContractStateInactive(){ --send || return } - - setAcceptedCallers(){ - # $1 = address address="0x$(mxpy wallet bech32 --decode ${1})" @@ -113,9 +117,7 @@ setAcceptedCallers(){ --send || return } - setBondToken(){ - mxpy --verbose contract call ${ADDRESS} \ --recall-nonce \ --pem=${WALLET} \ @@ -127,11 +129,9 @@ setBondToken(){ --send || return } - -setPeriodsBonds(){ - +setPeriodsBonds(){ # $1 = lockPeriod - # $2 = bonds + # $2 = bond mxpy --verbose contract call ${ADDRESS} \ --recall-nonce \ @@ -144,9 +144,7 @@ setPeriodsBonds(){ --send || return } - setMinimumPenalty(){ - # $1 = minimumPenalty mxpy --verbose contract call ${ADDRESS} \ @@ -158,11 +156,9 @@ setMinimumPenalty(){ --proxy ${PROXY} \ --chain ${CHAIN_ID} \ --send || return - } -setMaximumPenalty(){ - +setMaximumPenalty(){ # $1 = maximumPenalty mxpy --verbose contract call ${ADDRESS} \ @@ -177,7 +173,6 @@ setMaximumPenalty(){ } setWithdrawPenalty(){ - # $1 = withdrawPenalty mxpy --verbose contract call ${ADDRESS} \ @@ -191,8 +186,6 @@ setWithdrawPenalty(){ --send || return } - - sanction(){ # $1 = token identifier # $2 = nonce @@ -212,10 +205,8 @@ sanction(){ --proxy ${PROXY} \ --chain ${CHAIN_ID} \ --send || return - } - modifyBond(){ # $1 = token identifier # $2 = nonce @@ -231,13 +222,9 @@ modifyBond(){ --proxy ${PROXY} \ --chain ${CHAIN_ID} \ --send || return - } - - withdraw(){ - # $1 = token identifier # $2 = nonce @@ -252,12 +239,9 @@ withdraw(){ --proxy ${PROXY} \ --chain ${CHAIN_ID} \ --send || return - } - -renew(){ - +renew(){ # $1 = token identifier # $2 = nonce @@ -274,10 +258,7 @@ renew(){ --send || return } - - -renewWithNewLockPeriod(){ - +renewWithNewLockPeriod(){ # $1 = token identifier # $2 = nonce # $3 = lockPeriod @@ -293,4 +274,4 @@ renewWithNewLockPeriod(){ --proxy ${PROXY} \ --chain ${CHAIN_ID} \ --send || return -} \ No newline at end of file +} diff --git a/meta/Cargo.toml b/meta/Cargo.toml index c13b4e6..51dfaaf 100644 --- a/meta/Cargo.toml +++ b/meta/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "core-mx-life-bonding-sc-meta" -version = "0.0.0" +version = "1.0.0" edition = "2021" publish = false @@ -8,5 +8,5 @@ publish = false path = ".." [dependencies.multiversx-sc-meta] -version = "0.47.4" +version = "0.47.8" default-features = false diff --git a/src/admin.rs b/src/admin.rs index 05db018..68ec4cf 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -52,12 +52,7 @@ pub trait AdminModule: self.add_to_blacklist_event(&compensation_id, &addresses); for address in addresses.into_iter() { - if self - .compensation_blacklist(compensation_id) - .contains(&address) - { - sc_panic!(ERR_ALREADY_IN_STORAGE); - } + require!(!self.compensation_blacklist(compensation_id).contains(&address), ERR_ALREADY_IN_STORAGE); self.compensation_blacklist(compensation_id).insert(address); } } @@ -78,12 +73,7 @@ pub trait AdminModule: self.remove_from_blacklist_event(&compensation_id, &addresses); for address in addresses.into_iter() { - if !self - .compensation_blacklist(compensation_id) - .contains(&address) - { - sc_panic!(ERR_NOT_IN_STORAGE); - } + require!(self.compensation_blacklist(compensation_id).contains(&address), ERR_NOT_IN_STORAGE); self.compensation_blacklist(compensation_id) .swap_remove(&address); } @@ -211,9 +201,7 @@ pub trait AdminModule: only_privileged!(self, ERR_NOT_PRIVILEGED); self.set_accepted_callers_event(&callers); for caller in callers.into_iter() { - if self.accepted_callers().contains(&caller) { - sc_panic!(ERR_ALREADY_IN_STORAGE) - } + require!(!self.accepted_callers().contains(&caller), ERR_ALREADY_IN_STORAGE); self.accepted_callers().insert(caller); } } @@ -223,9 +211,7 @@ pub trait AdminModule: only_privileged!(self, ERR_NOT_PRIVILEGED); self.remove_accepted_callers_event(&callers); for caller in callers.into_iter() { - if !self.accepted_callers().contains(&caller) { - sc_panic!(ERR_NOT_IN_STORAGE) - } + require!(self.accepted_callers().contains(&caller), ERR_NOT_IN_STORAGE); self.accepted_callers().swap_remove(&caller); } } @@ -247,9 +233,7 @@ pub trait AdminModule: for input in args.into_iter() { let (lock_period, bond) = input.into_tuple(); - if self.lock_periods().contains(&lock_period) { - sc_panic!(ERR_ALREADY_IN_STORAGE); - } + require!(!self.lock_periods().contains(&lock_period), ERR_ALREADY_IN_STORAGE); self.set_period_and_bond_event(&lock_period, &bond); self.lock_periods().insert(lock_period); @@ -261,9 +245,7 @@ pub trait AdminModule: fn remove_lock_periods_with_bonds(&self, lock_periods: MultiValueEncoded) { only_privileged!(self, ERR_NOT_PRIVILEGED); for period in lock_periods.into_iter() { - if !self.lock_periods().contains(&period) { - sc_panic!(ERR_NOT_IN_STORAGE); - } + require!(self.lock_periods().contains(&period), ERR_NOT_IN_STORAGE); self.remove_period_and_bond_event(&period, &self.lock_period_bond_amount(period).get()); self.lock_periods().remove(&period); self.lock_period_bond_amount(period).clear(); diff --git a/src/lib.rs b/src/lib.rs index 7cd05c9..4ea79a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,15 +157,14 @@ pub trait LifeBondingContract: * &BigUint::from(self.withdraw_penalty().get()) / &BigUint::from(10_000u64); - if &bond_cache.bond_amount - &penalty_amount < compensation_cache.accumulated_amount { - sc_panic!(ERR_PENALTIES_EXCEED_WITHDRAWAL_AMOUNT); - } + require!(&bond_cache.remaining_amount > &penalty_amount, ERR_PENALTIES_EXCEED_WITHDRAWAL_AMOUNT); + require!(&bond_cache.remaining_amount - &penalty_amount >= compensation_cache.accumulated_amount,ERR_PENALTIES_EXCEED_WITHDRAWAL_AMOUNT); self.send().direct_esdt( &caller, &self.bond_payment_token().get(), 0u64, - &(&bond_cache.bond_amount - &penalty_amount), + &(&bond_cache.remaining_amount - &penalty_amount), ); compensation_cache.accumulated_amount += &penalty_amount; diff --git a/src/storage.rs b/src/storage.rs index 69bc805..eee2ed5 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -92,7 +92,7 @@ pub struct Refund { #[multiversx_sc::module] pub trait StorageModule { // Compensation storage - #[storage_mapper("compensations_ids")] + #[storage_mapper("compensations_tokens_nonces")] fn compensations_ids(&self) -> ObjectToIdMapper; #[storage_mapper("compensations_ids")] diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index d7f383b..e7d2212 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -22,7 +22,7 @@ checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "core-mx-life-bonding-sc" -version = "0.0.0" +version = "1.0.0" dependencies = [ "multiversx-sc", ] @@ -55,9 +55,9 @@ checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" [[package]] name = "multiversx-sc" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40b69a83a9423be4b543edee969205aa43ef4397c48b965a65bad857e022db5c" +checksum = "34215a81b2c37619a234db4d6facbef5853e4022df3494746b29ada5ff35859f" dependencies = [ "bitflags", "hex-literal", @@ -90,9 +90,9 @@ dependencies = [ [[package]] name = "multiversx-sc-derive" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5fe6400e2f4e57f7c9c00ad76108794aa962657cf62b4d4f49b99f02d9a6ba5" +checksum = "c41ce6becb22f45383e9ff60c88fa1f7946d92915c8409e9fbe3c3719325146f" dependencies = [ "hex", "proc-macro2", @@ -103,9 +103,9 @@ dependencies = [ [[package]] name = "multiversx-sc-wasm-adapter" -version = "0.47.4" +version = "0.47.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "304900b34dfae6fd5baddd4c98345f1b7fa7e839257dac961023733be40dd81e" +checksum = "73d1089c0fac8d27347f3f7d61285d304342cb7b57ac1cf1989c7efce6edc17a" dependencies = [ "multiversx-sc", ] diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 03a4e0a..6a480ff 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -25,7 +25,7 @@ overflow-checks = false path = ".." [dependencies.multiversx-sc-wasm-adapter] -version = "0.47.4" +version = "0.47.8" [workspace] members = ["."]