Skip to content

Commit

Permalink
Merge branch 'main' into more-multisig-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu authored Feb 7, 2024
2 parents 2ec6997 + 23b524f commit 0b936d6
Show file tree
Hide file tree
Showing 20 changed files with 1,490 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ members = [
"contracts/nft-minter/meta",
"contracts/nft-storage-prepay",
"contracts/nft-storage-prepay/meta",
"contracts/on-chain-claim",
"contracts/on-chain-claim/meta",
"contracts/order-book/factory",
"contracts/order-book/factory/meta",
"contracts/order-book/pair",
Expand Down
4 changes: 3 additions & 1 deletion contracts/check-pause/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
// Total number of exported functions: 6

#![no_std]
#![allow(internal_features)]

// Configuration that works with rustc < 1.73.0.
// TODO: Recommended rustc version: 1.73.0 or newer.
#![feature(lang_items)]

multiversx_sc_wasm_adapter::allocator!();
Expand Down
10 changes: 10 additions & 0 deletions contracts/on-chain-claim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The mxpy output
/output*/

# Mandos test trace
trace*.scen.json
18 changes: 18 additions & 0 deletions contracts/on-chain-claim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "on-chain-claim"
version = "0.0.0"
authors = [ "Vlad Bucur <[email protected]>"]
edition = "2021"
publish = false

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

[dependencies.multiversx-sc]
version = "0.46.0"

[dependencies.multiversx-sc-modules]
version = "0.46.0"

[dev-dependencies.multiversx-sc-scenario]
version = "0.46.0"
3 changes: 3 additions & 0 deletions contracts/on-chain-claim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# On Chain Claim

`On Chain Claim` is a simple Smart Contract.
52 changes: 52 additions & 0 deletions contracts/on-chain-claim/interaction/devnet.snippets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
WALLET="${PWD}/wallet.pem"
PROJECT="${PWD}"
PROXY=https://devnet-gateway.multiversx.com
CHAINID=D

DEPLOY_GAS="25000000"
SFT_IDENTIFIER=0x585354525245504149522d653162363733 #XSTRREPAIR-e1b673

CONTRACT_ADDRESS="erd1qqqqqqqqqqqqqpgqkm3wla3wk0yqk7lk725wee8yh0e2zeru76ls3gr0nj"

deploy() {
mxpy --verbose contract deploy \
--bytecode="output/on-chain-claim.wasm" \
--arguments ${SFT_IDENTIFIER} \
--pem=${WALLET} \
--gas-limit=${DEPLOY_GAS} \
--proxy=${PROXY} \
--chain=${CHAINID} \
--recall-nonce \
--send \
--outfile="deploy-devnet.interaction.json" || return

TRANSACTION=$(mxpy data parse --file="deploy-devnet.interaction.json" --expression="data['emittedTransactionHash']")
ADDRESS=$(mxpy data parse --file="deploy-devnet.interaction.json" --expression="data['contractAddress']")

mxpy data store --key=address-devnet --value=${ADDRESS}
mxpy data store --key=deployTransaction-devnet --value=${TRANSACTION}

echo ""
echo "Smart contract address: ${ADDRESS}"
}

upgrade() {
mxpy --verbose contract upgrade ${CONTRACT_ADDRESS} \
--bytecode="output/on-chain-claim.wasm" \
--pem=${WALLET} \
--gas-limit=${DEPLOY_GAS} \
--proxy=${PROXY} \
--chain=${CHAINID} \
--recall-nonce \
--send \
--outfile="upgrade-devnet.interaction.json" || return

TRANSACTION=$(mxpy data parse --file="upgrade-devnet.interaction.json" --expression="data['emittedTransactionHash']")
ADDRESS=$(mxpy data parse --file="upgrade-devnet.interaction.json" --expression="data['contractAddress']")

mxpy data store --key=address-devnet --value=${ADDRESS}
mxpy data store --key=upgradeTransaction-devnet --value=${TRANSACTION}

echo ""
echo "Smart contract address: ${ADDRESS}"
}
52 changes: 52 additions & 0 deletions contracts/on-chain-claim/interaction/mainnet.snippets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
WALLET="${PWD}/wallet.pem"
PROJECT="${PWD}"
PROXY=https://gateway.multiversx.com
CHAINID=D

DEPLOY_GAS="30000000"
SFT_IDENTIFIER=0x585354525245504149522d653162363733 #XSTRREPAIR-e1b673

CONTRACT_ADDRESS="erd1qqqqqqqqqqqqqpgqkm3wla3wk0yqk7lk725wee8yh0e2zeru76ls3gr0nj"

deploy() {
mxpy --verbose contract deploy \
--bytecode="output/on-chain-claim.wasm" \
--arguments ${SFT_IDENTIFIER} \
--pem=${WALLET} \
--gas-limit=${DEPLOY_GAS} \
--proxy=${PROXY} \
--chain=${CHAINID} \
--recall-nonce \
--send \
--outfile="deploy-mainnet.interaction.json" || return

TRANSACTION=$(mxpy data parse --file="deploy-mainnet.interaction.json" --expression="data['emittedTransactionHash']")
ADDRESS=$(mxpy data parse --file="deploy-mainnet.interaction.json" --expression="data['contractAddress']")

mxpy data store --key=address-mainnet --value=${ADDRESS}
mxpy data store --key=deployTransaction-mainnet --value=${TRANSACTION}

echo ""
echo "Smart contract address: ${ADDRESS}"
}

upgrade() {
mxpy --verbose contract upgrade ${CONTRACT_ADDRESS} \
--bytecode="output/on-chain-claim.wasm" \
--pem=${WALLET} \
--gas-limit=${DEPLOY_GAS} \
--proxy=${PROXY} \
--chain=${CHAINID} \
--recall-nonce \
--send \
--outfile="upgrade-mainnet.interaction.json" || return

TRANSACTION=$(mxpy data parse --file="upgrade-mainnet.interaction.json" --expression="data['emittedTransactionHash']")
ADDRESS=$(mxpy data parse --file="upgrade-mainnet.interaction.json" --expression="data['contractAddress']")

mxpy data store --key=address-mainnet --value=${ADDRESS}
mxpy data store --key=upgradeTransaction-mainnet --value=${TRANSACTION}

echo ""
echo "Smart contract address: ${ADDRESS}"
}
28 changes: 28 additions & 0 deletions contracts/on-chain-claim/interaction/testnet.snippets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
WALLET="${PWD}/wallet.pem"
PROJECT="${PWD}"
PROXY=https://testnet-gateway.multiversx.com
CHAINID=D

DEPLOY_GAS="30000000"
SFT_IDENTIFIER=0x54525245504149522d626435323730 #XSTRREPAIR-e1b673
deploy() {
mxpy --verbose contract deploy \
--bytecode="output/on-chain-claim.wasm" \
--arguments ${SFT_IDENTIFIER} \
--pem=${WALLET} \
--gas-limit=${DEPLOY_GAS} \
--proxy=${PROXY} \
--chain=${CHAINID} \
--recall-nonce \
--send \
--outfile="deploy-testnet.interaction.json" || return

TRANSACTION=$(mxpy data parse --file="deploy-testnet.interaction.json" --expression="data['emittedTransactionHash']")
ADDRESS=$(mxpy data parse --file="deploy-testnet.interaction.json" --expression="data['contractAddress']")

mxpy data store --key=address-testnet --value=${ADDRESS}
mxpy data store --key=deployTransaction-testnet --value=${TRANSACTION}

echo ""
echo "Smart contract address: ${ADDRESS}"
}
15 changes: 15 additions & 0 deletions contracts/on-chain-claim/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "on-chain-claim-meta"
version = "0.0.0"
edition = "2018"
publish = false
authors = [ "Vlad Bucur <[email protected]>"]

[dev-dependencies]

[dependencies.on-chain-claim]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.46.0"
default-features = false
3 changes: 3 additions & 0 deletions contracts/on-chain-claim/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta::cli_main::<on_chain_claim::AbiProvider>();
}
3 changes: 3 additions & 0 deletions contracts/on-chain-claim/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
19 changes: 19 additions & 0 deletions contracts/on-chain-claim/mxsc-template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "on-chain-claim"
contract_trait = "OnChainClaim"
src_file = "contract.rs"
rename_pairs = [
[
"blockchain.set_current_dir_from_workspace(\"contracts/examples/on-chain-claim\");",
"// blockchain.set_current_dir_from_workspace(\"relative path to your workspace, if applicable\");",
],
]
files_include = [
"meta",
"scenarios",
"src",
"tests",
"wasm/Cargo.toml",
"Cargo.toml",
"README.md",
"multiversx.json",
]
35 changes: 35 additions & 0 deletions contracts/on-chain-claim/src/address_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(Default, NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
pub struct AddressInfo {
pub current_streak: u64,
pub last_epoch_claimed: u64,
pub total_epochs_claimed: u64,
pub best_streak: u64,
}

impl AddressInfo {
pub fn new(
current_streak: u64,
last_epoch_claimed: u64,
total_epochs_claimed: u64,
best_streak: u64,
) -> Self {
AddressInfo {
current_streak,
last_epoch_claimed,
total_epochs_claimed,
best_streak,
}
}

pub fn new_with_epoch(current_epoch: u64) -> Self {
AddressInfo {
current_streak: 1,
last_epoch_claimed: current_epoch,
total_epochs_claimed: 1,
best_streak: 1,
}
}
}
58 changes: 58 additions & 0 deletions contracts/on-chain-claim/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::address_info::*;

pub const SFT_AMOUNT: u64 = 1;
pub const MAX_REPAIR_GAP: u64 = 5;

#[multiversx_sc::module]
pub trait ConfigModule {
fn require_same_shard(&self, address: &ManagedAddress) {
let address_shard = self.blockchain().get_shard_of_address(address);
let sc_address = self.blockchain().get_sc_address();
let sc_shard = self.blockchain().get_shard_of_address(&sc_address);

require!(address_shard == sc_shard, "wrong shard");
}

fn get_missed_epochs(&self, current_epoch: u64, last_epoch_claimed: u64) -> u64 {
if current_epoch <= last_epoch_claimed {
return 0;
}

current_epoch - last_epoch_claimed - 1
}

#[view(getAddressInfo)]
fn get_address_info(&self, address: &ManagedAddress) -> AddressInfo {
let address_info_mapper = self.address_info(address);

if address_info_mapper.is_empty() {
return AddressInfo::default();
}

address_info_mapper.get()
}

#[view(canBeRepaired)]
fn can_be_repaired(&self, address: &ManagedAddress) -> bool {
let address_info_mapper = self.address_info(address);
if address_info_mapper.is_empty() {
return false;
}

let address_info = address_info_mapper.get();
let current_epoch = self.blockchain().get_block_epoch();
let missed_epochs = self.get_missed_epochs(current_epoch, address_info.last_epoch_claimed);

missed_epochs > 0 && missed_epochs <= MAX_REPAIR_GAP
}

#[storage_mapper("address_info")]
fn address_info(&self, address: &ManagedAddress) -> SingleValueMapper<AddressInfo>;

#[view(getRepairStreakTokenIdentifier)]
#[storage_mapper("repair_streak_token_identifier")]
fn repair_streak_token_identifier(&self) -> SingleValueMapper<TokenIdentifier>;
}
Loading

0 comments on commit 0b936d6

Please sign in to comment.