Skip to content

Commit

Permalink
add tests and snippets, update sc framework
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Jan 6, 2023
1 parent 4588bdd commit e323e8b
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 868 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

# The erdpy output
# output

# Output interactions data
*.interaction.json
86 changes: 31 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[package]
name = "piggybank"
version = "0.3.0"
version = "0.4.0"
authors = ["Julian Ćwirko <[email protected]>"]
edition = "2018"
edition = "2021"
publish = false

[lib]
path = "src/lib.rs"

[dependencies.elrond-wasm]
version = "0.34.1"
version = "0.38.0"

[dev-dependencies.elrond-wasm-debug]
version = "0.34.1"
version = "0.38.0"

[dev-dependencies]
num-bigint = "0.4.2"
Expand Down
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
### PiggyBank - Elrond Smart Contract for learning purposes
### PiggyBank - MultiversX Smart Contract for learning purposes

The logic is quite simple:
1. You create a Piggy Bank where you define the lock time using Epoch Timestamp as the argument
2. You can send an amount of EGLD to the smart contract, and it will be locked till the configured date
3. You can then payout the amount, but only after the configured lock date

> **⚠ Proper docs, updates, tests, and dapp are on the way! Stay tuned.**
Expand All @@ -11,7 +16,8 @@ To interact with the PiggyBank Smart Contract, you would need to have:

To derive the wallet pem file, check the docs [Deriving the Wallet PEM file](https://docs.elrond.com/sdk-and-tools/erdpy/deriving-the-wallet-pem-file/)

### The article on how to prepare dev environment:
### The articles on how to prepare dev environment:
- [MultiversX docs tutorial](https://docs.elrond.com/developers/tutorials/staking-contract/#prerequisites)
- [Linux environment for development](https://elrond-dev-guild.gitbook.io/scrolls/readme/linux-environment-for-development)

### Old dapp (it will be rewritten soon)
Expand All @@ -28,7 +34,7 @@ erdpy contract build
To use testnet switch to `--chain="T"` and --proxy="https://testnet-gateway.elrond.com".

### Example erdpy interaction commands
##### These commands should be run one folder up from the cloned 'elrond-simple-sc' folder
##### These commands should be run one folder up from the cloned 'multiversx-simple-sc' folder or you would need to adjust your --project path

**Deploy the contract:**

Expand All @@ -39,38 +45,56 @@ erdpy --verbose contract deploy --chain="D" --project=elrond-simple-sc --pem="el
Smart Contract deployment. You will need to do this once.
In the example, the project name is `elrond-simple-sc`, and the pem file is located in `wallets/test.pem`.

**Upgrade the contract:**
(here, with the working SC address example, change it, if you deployed yours, you should have one)

```
erdpy --verbose contract upgrade erd1qqqqqqqqqqqqqpgq9xrwp7qnfamenqsxpwsf84x9e9sx8vxhgtksusvhgs --chain="D" --project=elrond-simple-sc --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=80000000 --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
```
You can also use [VSCode MultiversX SDK](https://marketplace.visualstudio.com/items?itemName=Elrond.vscode-elrond-ide) and run a snippet called `deploy`.

**Create the Piggy:**
(here, with the working SC address example, change it, if you deployed yours, you should have one)

```
erdpy --verbose contract call erd1qqqqqqqqqqqqqpgq9xrwp7qnfamenqsxpwsf84x9e9sx8vxhgtksusvhgs --chain="D" --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=5000000 --function="createPiggy" --arguments 1655316103 --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
erdpy --verbose contract call erd1qqqqqqqqqqqqqpgq59rkyerlfv70635d5ym7s8tmx37e6q5avafsxcqpta --chain="D" --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=5000000 --function="createPiggy" --arguments 1655316103 --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
```

As an argument for the `createPiggy` function, we will pass the timestamp for the lock time (it should be in the future, of course, use https://www.epochconverter.com/).

You can also use [VSCode MultiversX SDK](https://marketplace.visualstudio.com/items?itemName=Elrond.vscode-elrond-ide) and run a snippet called `createPiggy`.

**Add amount:**
(here, with the working SC address example, change it, if you deployed yours, you should have one)

```
erdpy --verbose contract call erd1qqqqqqqqqqqqqpgq9xrwp7qnfamenqsxpwsf84x9e9sx8vxhgtksusvhgs --chain="D" --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=5000000 --function="addAmount" --value=1000000000000000000 --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
erdpy --verbose contract call erd1qqqqqqqqqqqqqpgq59rkyerlfv70635d5ym7s8tmx37e6q5avafsxcqpta --chain="D" --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=5000000 --function="addAmount" --value=1000000000000000000 --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
```

We are adding one xEGLD (denomination 18, this is why it is, in fact, 1000000000000000000).

You can also use [VSCode MultiversX SDK](https://marketplace.visualstudio.com/items?itemName=Elrond.vscode-elrond-ide) and run a snippet called `addAmount`.

**Payout:**
(here, with the working SC address example, change it, if you deployed yours, you should have one)

```
erdpy --verbose contract call erd1qqqqqqqqqqqqqpgq9xrwp7qnfamenqsxpwsf84x9e9sx8vxhgtksusvhgs --chain="D" --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=5000000 --function="payOut" --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
erdpy --verbose contract call erd1qqqqqqqqqqqqqpgq59rkyerlfv70635d5ym7s8tmx37e6q5avafsxcqpta --chain="D" --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=5000000 --function="payOut" --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
```

It will check if you can withdraw. It will compare lock time and the current block timestamp.

Proper docs for the beginners soon!
You can also use [VSCode MultiversX SDK](https://marketplace.visualstudio.com/items?itemName=Elrond.vscode-elrond-ide) and run a snippet called `payOut`.

**Upgrade the contract:**
(here, with the working SC address example, change it, if you deployed yours, you should have one)

```
erdpy --verbose contract upgrade erd1qqqqqqqqqqqqqpgq59rkyerlfv70635d5ym7s8tmx37e6q5avafsxcqpta --chain="D" --project=elrond-simple-sc --pem="elrond-simple-sc/wallets/test.pem" --gas-limit=20000000 --proxy="https://devnet-gateway.elrond.com" --recall-nonce --send
```

You can also use [VSCode MultiversX SDK](https://marketplace.visualstudio.com/items?itemName=Elrond.vscode-elrond-ide) and run a snippet called `upgrade`.

### Snippets

You will find interaction snippets in `interactions/snippets.sh`. It is convenient to use when working with VSCode and MultiversX SDK plugin.

After deploying the smart contract, you need to edit the `SC_ADDRESS`. Also, make sure that the relative path to your wallet PEM file is correct. You will need to set it in `USER_PEM`.

### Testing

You will find tests in `tests/piggybank_rust_test.rs`. To run a test, you can use click on the `Run Test` button from under the test name in VS Code or you can run it with `cargo test --test piggybank_rust_test`.
50 changes: 50 additions & 0 deletions interactions/snippets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
USER_PEM="../walletKey.pem"
PROXY="https://devnet-gateway.elrond.com"
CHAIN_ID="D"

SC_ADDRESS=erd1qqqqqqqqqqqqqpgqz22zn8njz6649aazgqwnspefekcd3pkrvafs4x732r
LOCK_EPOCH_TIMESTAMP=1675690008
AMOUNT_TO_LOCK=1000000000000000000

deploy() {
erdpy --verbose contract deploy --project=${PROJECT} \
--recall-nonce --pem=${USER_PEM} \
--gas-limit=10000000 \
--send --outfile="deploy-devnet.interaction.json" \
--proxy=${PROXY} --chain=${CHAIN_ID} || return
}

createPiggy() {
erdpy --verbose contract call ${SC_ADDRESS} \
--proxy=${PROXY} --chain=${CHAIN_ID} \
--send --recall-nonce --pem=${USER_PEM} \
--gas-limit=5000000 \
--arguments ${LOCK_EPOCH_TIMESTAMP} \
--function="createPiggy"
}

addAmount() {
erdpy --verbose contract call ${SC_ADDRESS} \
--proxy=${PROXY} --chain=${CHAIN_ID} \
--send --recall-nonce --pem=${USER_PEM} \
--gas-limit=5000000 \
--value=${AMOUNT_TO_LOCK} \
--function="addAmount"
}

payOut() {
erdpy --verbose contract call ${SC_ADDRESS} \
--proxy=${PROXY} --chain=${CHAIN_ID} \
--send --recall-nonce --pem=${USER_PEM} \
--gas-limit=5000000 \
--function="payOut"
}

upgrade() {
erdpy --verbose contract upgrade ${SC_ADDRESS} \
--project=${PROJECT} \
--recall-nonce --pem=${USER_PEM} \
--gas-limit=20000000 \
--send --outfile="upgrade-devnet.interaction.json" \
--proxy=${PROXY} --chain=${CHAIN_ID} || return
}
Loading

0 comments on commit e323e8b

Please sign in to comment.