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 12 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
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
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
15 changes: 15 additions & 0 deletions contracts/on-chain-claim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[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.45.1"
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies.multiversx-sc-scenario]
version = "0.45.1"
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.
28 changes: 28 additions & 0 deletions contracts/on-chain-claim/interaction/devnet.snippets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
WALLET="${PWD}/wallet.pem"
PROJECT="${PWD}"
PROXY=https://devnet-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-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}"
}
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 = [ "you",]

[dev-dependencies]

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

[dependencies.multiversx-sc-meta]
version = "0.45.1"
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",
]
24 changes: 24 additions & 0 deletions contracts/on-chain-claim/src/address_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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,
}

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

use crate::address_info::*;

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

#[multiversx_sc::module]
pub trait ConfigModule {
#[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();

address_info.last_epoch_claimed + 2 == self.blockchain().get_block_epoch()
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved
}

#[view(getAddressInfo)]
#[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>;
}
113 changes: 113 additions & 0 deletions contracts/on-chain-claim/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#![no_std]
#![allow(unused_attributes)]

pub use address_info::AddressInfo;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

pub mod address_info;
pub mod config;

use crate::config::{SFT_AMOUNT, MAX_REPAIR_GAP};

/// An empty contract. To be used as a template when starting a new contract from scratch.
#[multiversx_sc::contract]
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved
pub trait OnChainClaimContract:
config::ConfigModule {
#[init]
fn init(
&self,
repair_streak_token_id: TokenIdentifier,
) {
self.repair_streak_token_identifier()
.set(repair_streak_token_id);
}

#[endpoint(claim)]
fn claim(&self) {
let caller = self.blockchain().get_caller();
require!(
!self.blockchain().is_smart_contract(&caller),
"Only user accounts can perform claim"
);

let current_epoch = self.blockchain().get_block_epoch();

let address_info_mapper = self.address_info(&caller);
if address_info_mapper.is_empty() {
let address_info = AddressInfo::new(1, current_epoch, 1);
self.address_info(&caller).set(address_info);
return;
}

address_info_mapper.update(|address_info| {
require!(
address_info.last_epoch_claimed < current_epoch,
"epoch already claimed"
);

if address_info.last_epoch_claimed + 1 == current_epoch {
address_info.current_streak += 1;
} else {
address_info.current_streak = 1;
}

address_info.total_epochs_claimed += 1;
address_info.last_epoch_claimed = current_epoch;
});
}
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved

#[payable("*")]
#[endpoint(claimAndRepair)]
fn claim_and_repair(&self) {
let caller = self.blockchain().get_caller();
require!(
!self.blockchain().is_smart_contract(&caller),
"Only user accounts can perform claim and repair"
);
let payment = self.call_value().single_esdt();
let repair_streak_token_identifier = self.repair_streak_token_identifier().get();
require!(
payment.token_identifier == repair_streak_token_identifier,
"Bad payment token"
);
require!(payment.amount == SFT_AMOUNT, "Bad payment amount");
vladbucur1 marked this conversation as resolved.
Show resolved Hide resolved

let current_epoch = self.blockchain().get_block_epoch();

let address_info_mapper = self.address_info(&caller);

require!(!address_info_mapper.is_empty(), "can't repair streak for address");

address_info_mapper.update(|address_info| {
require!(
address_info.last_epoch_claimed + MAX_REPAIR_GAP == current_epoch,
"can't repair streak for current epoch"
);

address_info.current_streak += MAX_REPAIR_GAP;
address_info.total_epochs_claimed += MAX_REPAIR_GAP;
address_info.last_epoch_claimed = current_epoch;
});

self.send().esdt_local_burn(
&payment.token_identifier,
payment.token_nonce,
&payment.amount,
);
}

#[only_owner]
#[endpoint(updateState)]
fn update_state(
&self,
address: &ManagedAddress,
current_streak: u64,
last_epoch_claimed: u64,
total_epochs_claimed: u64,
) {
let address_info = AddressInfo::new(current_streak, last_epoch_claimed, total_epochs_claimed);
self.address_info(address).set(address_info);
}
}
Loading