generated from multiversx/mx-template-sc
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from multiversx/on-chain-claim
On chain claim
- Loading branch information
Showing
20 changed files
with
1,490 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"language": "rust" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
Oops, something went wrong.