Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
resolver = "2"
resolver = "3"

members = [
"data/codec",
Expand Down
5 changes: 0 additions & 5 deletions contracts/examples/crowdfunding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ path = "../../../framework/base"
version = "0.64.1"
features = ["wasmer-experimental"]
path = "../../../framework/scenario"

[dev-dependencies]
num-bigint = "0.4"
num-traits = "0.2"
hex = "0.4"
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"from": "address:my_address",
"contractCode": "mxsc:../output/crowdfunding.mxsc.json",
"arguments": [
"str:CROWD-123456",
"500,000,000,000",
"123,000",
"str:CROWD-123456"
"123,000"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"from": "address:my_address",
"contractCode": "mxsc:../output/crowdfunding.mxsc.json",
"arguments": [
"str:EGLD-000000",
"500,000,000,000",
"123,000",
"str:EGLD-000000"
"123,000"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
Expand Down
42 changes: 21 additions & 21 deletions contracts/examples/crowdfunding/src/crowdfunding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

use multiversx_sc::{chain_core::types::TimestampMillis, derive_imports::*, imports::*};
use multiversx_sc::{derive_imports::*, imports::*};
pub mod crowdfunding_proxy;

#[type_abi]
Expand All @@ -14,18 +14,18 @@ pub enum Status {
#[multiversx_sc::contract]
pub trait Crowdfunding {
#[init]
fn init(&self, target: BigUint, deadline: TimestampMillis, token_identifier: TokenId) {
fn init(&self, token_identifier: TokenId, target: BigUint, deadline: TimestampMillis) {
require!(token_identifier.is_valid(), "Invalid token provided");
self.cf_token_id().set(token_identifier);

require!(target > 0, "Target must be more than 0");
self.target().set(target);

require!(
deadline > self.get_current_time_ms(),
deadline > self.get_current_time_millis(),
"Deadline can't be in the past"
);
self.deadline().set(deadline);

require!(token_identifier.is_valid(), "Invalid token provided");
self.cf_token_identifier().set(token_identifier);
}

#[endpoint]
Expand All @@ -34,12 +34,13 @@ pub trait Crowdfunding {
let payment = self.call_value().single();

require!(
self.status() == Status::FundingPeriod,
"cannot fund after deadline"
payment.token_identifier == self.cf_token_id().get(),
"wrong token"
);
require!(payment.is_fungible(), "only fungible tokens accepted");
require!(
payment.token_identifier == self.cf_token_identifier().get(),
"wrong token"
self.status() == Status::FundingPeriod,
"cannot fund after deadline"
);
Comment on lines 36 to 44
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation order is suboptimal. The self.status() check (line 42) calls get_current_time_millis() and get_current_funds(), which involve blockchain queries and storage reads. The payment.is_fungible() check (line 40) is a simple local check. Moving the fungibility check before the status check would avoid unnecessary expensive operations when the payment is not fungible (e.g., NFTs).

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter, failure consumes all the gas either way.


let caller = self.blockchain().get_caller();
Expand All @@ -49,7 +50,7 @@ pub trait Crowdfunding {

#[view]
fn status(&self) -> Status {
if self.get_current_time_ms() < self.deadline().get() {
if self.get_current_time_millis() < self.deadline().get() {
Status::FundingPeriod
} else if self.get_current_funds() >= self.target().get() {
Status::Successful
Expand All @@ -61,9 +62,8 @@ pub trait Crowdfunding {
#[view(getCurrentFunds)]
#[title("currentFunds")]
fn get_current_funds(&self) -> BigUint {
let token = self.cf_token_identifier().get();

self.blockchain().get_sc_balance(&token, 0)
let token_id = self.cf_token_id().get();
self.blockchain().get_sc_balance(&token_id, 0)
}

#[endpoint]
Expand All @@ -77,13 +77,13 @@ pub trait Crowdfunding {
"only owner can claim successful funding"
);

let token_identifier = self.cf_token_identifier().get();
let token_id = self.cf_token_id().get();
let sc_balance = self.get_current_funds();

if let Some(sc_balance_non_zero) = sc_balance.into_non_zero() {
self.tx()
.to(&caller)
.payment(Payment::new(token_identifier, 0, sc_balance_non_zero))
.payment(Payment::new(token_id, 0, sc_balance_non_zero))
.transfer();
}
}
Expand All @@ -92,14 +92,14 @@ pub trait Crowdfunding {
let deposit = self.deposit(&caller).get();

if deposit > 0u32 {
let token_identifier = self.cf_token_identifier().get();
let token_id = self.cf_token_id().get();

self.deposit(&caller).clear();

if let Some(deposit_non_zero) = deposit.into_non_zero() {
self.tx()
.to(&caller)
.payment(Payment::new(token_identifier, 0, deposit_non_zero))
.payment(Payment::new(token_id, 0, deposit_non_zero))
.transfer();
}
}
Expand All @@ -109,7 +109,7 @@ pub trait Crowdfunding {

// private

fn get_current_time_ms(&self) -> TimestampMillis {
fn get_current_time_millis(&self) -> TimestampMillis {
self.blockchain().get_block_timestamp_millis()
}

Expand All @@ -130,8 +130,8 @@ pub trait Crowdfunding {
#[storage_mapper("deposit")]
fn deposit(&self, donor: &ManagedAddress) -> SingleValueMapper<BigUint>;

#[view(getCrowdfundingTokenIdentifier)]
#[view(getCrowdfundingTokenId)]
#[title("tokenIdentifier")]
#[storage_mapper("tokenIdentifier")]
fn cf_token_identifier(&self) -> SingleValueMapper<TokenId>;
fn cf_token_id(&self) -> SingleValueMapper<TokenId>;
}
18 changes: 9 additions & 9 deletions contracts/examples/crowdfunding/src/crowdfunding_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ where
Gas: TxGas<Env>,
{
pub fn init<
Arg0: ProxyArg<BigUint<Env::Api>>,
Arg1: ProxyArg<TimestampMillis>,
Arg2: ProxyArg<TokenId<Env::Api>>,
Arg0: ProxyArg<TokenId<Env::Api>>,
Arg1: ProxyArg<BigUint<Env::Api>>,
Arg2: ProxyArg<TimestampMillis>,
>(
self,
target: Arg0,
deadline: Arg1,
token_identifier: Arg2,
token_identifier: Arg0,
target: Arg1,
deadline: Arg2,
) -> TxTypedDeploy<Env, From, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_deploy()
.argument(&token_identifier)
.argument(&target)
.argument(&deadline)
.argument(&token_identifier)
.original_result()
}
}
Expand Down Expand Up @@ -138,12 +138,12 @@ where
.original_result()
}

pub fn cf_token_identifier(
pub fn cf_token_id(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, TokenId<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getCrowdfundingTokenIdentifier")
.raw_call("getCrowdfundingTokenId")
.original_result()
}
}
Expand Down
Loading