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

Subscription SC #2

Merged
merged 38 commits into from
Sep 15, 2023
Merged

Subscription SC #2

merged 38 commits into from
Sep 15, 2023

Conversation

dorin-iancu
Copy link
Contributor

@dorin-iancu dorin-iancu commented Sep 1, 2023

The current PR contains 3 contracts:

  • one main "fees" contract, in which users deposit fees
  • one subscriber contract, which contains a common interface that has to be implemented by all contracts of its type. Each type of contract may have more than one action available. Daily, each contract will be called by an external service and it will perform the operation implemented, depending on the periodicity the user selected.
  • an example subscriber for metabonding

subscription-fee/src/lib.rs Outdated Show resolved Hide resolved
subscription-fee/src/low_level_actions.rs Outdated Show resolved Hide resolved
subscription-fee/src/low_level_actions.rs Outdated Show resolved Hide resolved
subscription-fee/src/pair_actions.rs Show resolved Hide resolved
subscription-fee/src/service.rs Outdated Show resolved Hide resolved
subscription-fee/src/daily_operations.rs Outdated Show resolved Hide resolved
subscription-fee/src/service.rs Outdated Show resolved Hide resolved
subscription-fee/src/service.rs Outdated Show resolved Hide resolved
subscription-fee/src/daily_operations.rs Outdated Show resolved Hide resolved
subscription-fee/src/daily_operations.rs Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Sep 11, 2023

Contract comparison - from fcaab6f to 3ec44bf

Path                                                                                             size                  has-allocator                     has-format
mx-subscription-fee-rs
- subscription-fee/subscription-fee.wasm 232 ➡️ 18460 🔴 No No
- metabonding-subscriber/metabonding-subscriber.wasm N/A ➡️ 9998 ⚠️ N/A ➡️ No ⚠️ N/A ➡️ No ⚠️
- subscriber/subscriber.wasm N/A ➡️ 1959 ⚠️ N/A ➡️ No ⚠️ N/A ➡️ No ⚠️

subscriber/src/service.rs Outdated Show resolved Hide resolved
subscription-fee/src/fees.rs Outdated Show resolved Hide resolved
tokens_to_withdraw.to_vec()
};

let user_fees_mapper = self.user_deposited_fees(caller_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

mate 2 endpoints, withdrawFund and withdrawFees

fees_contract_address.clone(),
);
if subtract_result.is_err() {
return CONTINUE_OP;
Copy link
Contributor

Choose a reason for hiding this comment

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

if this is an error - the whole execution will fail anyway. No need for this check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, this is a custom error. If I made it that way, the contract would get stuck if a user didn't deposit tokens for fees.

Copy link
Contributor

@sasurobert sasurobert Sep 13, 2023

Choose a reason for hiding this comment

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

but if the ExecuteOnDestContext fails - the whole contract will fail anyway. So it does not matter if it is a custom error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, it's just an error, but not a VM panic. It simply says "hey, this didn't work".

&service_info,
user_data.borrow(),
);
if action_results.is_err() {
Copy link
Contributor

Choose a reason for hiding this comment

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

if this is an error - the whole execution will fail anyway. No need for this check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above.

Copy link
Contributor

Choose a reason for hiding this comment

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

but if the ExecuteOnDestContext fails - the whole contract will fail anyway. So it does not matter if it is a custom error.

subscriber/src/daily_operations.rs Outdated Show resolved Hide resolved
subscriber/src/user_tokens.rs Outdated Show resolved Hide resolved
subscriber/src/user_tokens.rs Outdated Show resolved Hide resolved
subscriber/src/user_tokens.rs Outdated Show resolved Hide resolved
subscriber/src/daily_operations.rs Show resolved Hide resolved
subscription-fee/src/subtract_payments.rs Outdated Show resolved Hide resolved
MyVeryOwnScResult::Err(_) => unreachable_unchecked(),
}
}
}

Choose a reason for hiding this comment

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

Would be better to use addresses instead of IDs for arguments on endpoints. For external integrations is easier to work directly with address instead of ID

Copy link
Contributor

Choose a reason for hiding this comment

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

it is cheaper for the bot. to have IDs.

subscription-fee/src/fees.rs Show resolved Hide resolved
subscription-fee/src/fees.rs Outdated Show resolved Hide resolved
subscription-fee/src/fees.rs Outdated Show resolved Hide resolved
subscription-fee/src/fees.rs Outdated Show resolved Hide resolved
}

let mut opt_found_token_index = None;
for (index, user_token) in all_user_tokens.iter().enumerate() {

Choose a reason for hiding this comment

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

Can we remove this for by using a storage?

subscription-fee/src/fees.rs Show resolved Hide resolved
subscription-fee/src/subtract_payments.rs Show resolved Hide resolved
subscription-fee/src/subtract_payments.rs Outdated Show resolved Hide resolved
subscription-fee/src/subtract_payments.rs Outdated Show resolved Hide resolved
@dorin-iancu dorin-iancu marked this pull request as ready for review September 14, 2023 11:16
MyVeryOwnScResult::Err(_) => unreachable_unchecked(),
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

it is cheaper for the bot. to have IDs.

service_info: &ServiceInfo<<Self::SubSc as ContractBase>::Api>,
additional_data: &<Self as SubscriberContract>::AdditionalDataType,
) -> Result<InterpretedResult<<Self::SubSc as ContractBase>::Api>, ()> {
let rewards_vec = sc.claim_metaboding_rewards(
Copy link
Contributor

Choose a reason for hiding this comment

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

we actually want boosted-rewards-subscriber - not metabonding. Where we claim boosted rewards from a set of farm/metastaking and feescollector addresses for that user.

Metabonding can stay, but we need a new contract where perform action will call claimBoostedRewards

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is only an example, it's a lot easier to integrate something like metabonding for testing purposes.


let mut services = ManagedVec::<Self::Api, _>::new();
for arg in args {
let (sc_address, energy_threshold, payment_type) = arg.into_tuple();
Copy link
Contributor

Choose a reason for hiding this comment

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

No energy threshold in this contract. This contract does not know about energy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still missing feature of registering multiple services from the same address.

Copy link
Contributor

Choose a reason for hiding this comment

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

Periodicity has to be saved here, and only allow to take_payment once per periodicity.

#[endpoint(registerService)]
fn register_service(
&self,
args: MultiValueEncoded<MultiValue3<ManagedAddress, BigUint, PaymentType<Self::Api>>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

arguments are not energy_threshold. But payment and periodicity.

let opt_user_address = self.user_id().get_address(user_id);
let user_address = opt_user_address?;
let user_energy = self.get_energy_amount(&user_address);
let cost = if user_energy >= service_info.energy_threshold {
Copy link
Contributor

Choose a reason for hiding this comment

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

no energy threshold here.

#[endpoint(subtractPayment)]
fn subtract_payment(
&self,
service_index: usize,
Copy link
Contributor

Choose a reason for hiding this comment

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

so the caller can have multiple serviceIDs. Check if user is subscriber for that service ID and check the periodicity. Like when was the last time payment was subsctracted.

subscriber/src/daily_operations.rs Show resolved Hide resolved
output_esdt.push(payment);
}

let action_results = SC::perform_action(
Copy link
Contributor

Choose a reason for hiding this comment

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

actually we do several actions per one user per one payment. Like we claim boosted rewards for all his farms and fees collector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each subscriber contract can do whatever they want in their perform_action implementation. If they want to perform several actions for one payment, that's fine.

Copy link
Contributor

@sasurobert sasurobert left a comment

Choose a reason for hiding this comment

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

Comments will be resolved in next PR.

@dorin-iancu dorin-iancu merged commit 1d7f7c0 into main Sep 15, 2023
4 checks passed
@dorin-iancu dorin-iancu deleted the subscription-sc branch September 15, 2023 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants