Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On chain claim #44

Merged
merged 28 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
795cc73
On Chain Claim contract
vladbucur1 Dec 14, 2023
fc9867f
code review fixes
vladbucur1 Dec 14, 2023
4209ae8
code review fixes
vladbucur1 Dec 14, 2023
436768e
Added claimAndrepair endpoint and updateState
vladbucur1 Dec 15, 2023
e17dc61
removed ds_store file
vladbucur1 Dec 15, 2023
9455995
code review fixes
vladbucur1 Dec 15, 2023
8fae39f
Code review fixes
vladbucur1 Dec 15, 2023
4bddeac
Added burn sft for claimAndRepair endpoint
vladbucur1 Dec 18, 2023
42cfed3
Added tests
vladbucur1 Dec 18, 2023
424d21c
fixes
vladbucur1 Dec 18, 2023
45fe5d6
fixes
vladbucur1 Dec 18, 2023
638e4c9
Merge branch 'main' into on-chain-claim
psorinionut Dec 21, 2023
eeeec30
Code review fixes
vladbucur1 Dec 22, 2023
bff6b6a
Upgraded packages + new best streak field + increase max gap to 5
vladbucur1 Jan 8, 2024
73fc249
Added only admin module
vladbucur1 Jan 10, 2024
6140012
Fixes
vladbucur1 Jan 10, 2024
b5dfbba
added upgrade fn
vladbucur1 Jan 10, 2024
ea624b4
Merge branch 'main' into on-chain-claim
vladbucur1 Jan 10, 2024
3befdd3
Fixes
vladbucur1 Jan 10, 2024
1122cae
Fixes
vladbucur1 Jan 10, 2024
05c5ad5
Fixed get address info view
vladbucur1 Jan 10, 2024
670a902
Fixes
vladbucur1 Jan 11, 2024
6cd6133
Fixes
vladbucur1 Jan 11, 2024
9313c2d
Merge branch 'main' into on-chain-claim
sasurobert Jan 22, 2024
d728050
Code review fixes
vladbucur1 Jan 22, 2024
aaf6360
code review fixes
vladbucur1 Jan 22, 2024
242dd90
Merge branch 'main' into on-chain-claim
vladbucur1 Feb 7, 2024
e2f3a46
Added set repair streak token identifier endpoint
vladbucur1 Feb 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -51,6 +51,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}"
}
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="25000000"
SFT_IDENTIFIER=0x585354525245504149522d653162363733 #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",
]
27 changes: 27 additions & 0 deletions contracts/on-chain-claim/src/address_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
multiversx_sc::imports!();
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved
multiversx_sc::derive_imports!();

#[derive(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 {
#[inline]
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,
}
}
}
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 <= 1 {
return 0;
}
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved

current_epoch - last_epoch_claimed - 1
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved
}

#[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::new(0, 0, 0, 0);
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved
}

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