generated from multiversx/mx-template-sc
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Paymaster SC #24
Merged
Merged
Paymaster SC #24
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e30711
Paymaster SC
CostinCarabas 71b89a7
Remove Pair proxy code
CostinCarabas 6e7ccab
Fixes after review
CostinCarabas b345a36
Refactor forward_call
CostinCarabas 79e9192
Add blackbox test setup
CostinCarabas ef0289e
Fix compilation
CostinCarabas 7dd9a99
Paymaster tests
CostinCarabas 41570b9
Paymaster: Add adder dependecies
CostinCarabas f52d822
Paymaster: Add README + 1 test
CostinCarabas a3100e4
Fixes after review
CostinCarabas 56675a9
Improve README
CostinCarabas 0c3d5a8
Fixes after review
CostinCarabas 64c591f
Merge remote-tracking branch 'origin/main' into paymaster
CostinCarabas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -7,4 +7,4 @@ publish = false | |
path = ".." | ||
|
||
[dependencies.multiversx-sc-meta] | ||
version = "0.41.3" | ||
version = "0.43.4" |
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
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,7 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
*/target/ | ||
|
||
# The mxpy output | ||
/output*/ |
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,23 @@ | ||
[package] | ||
name = "paymaster" | ||
version = "0.0.0" | ||
authors = [ "MultiversX <[email protected]>" ] | ||
edition = "2018" | ||
publish = false | ||
readme = "README.md" | ||
|
||
|
||
[lib] | ||
path = "src/paymaster.rs" | ||
|
||
[dev-dependencies] | ||
num-bigint = "0.4.2" | ||
|
||
[dependencies.multiversx-sc] | ||
version = "0.43.4" | ||
|
||
[dev-dependencies.multiversx-sc-scenario] | ||
version = "0.43.4" | ||
|
||
[dev-dependencies.adder] | ||
path = "../adder" |
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 @@ | ||
# Paymaster SC | ||
|
||
## Overview | ||
|
||
Paymaster is a SC that makes relayed transactions sustainable. | ||
This means that a user who doesn't own EGLD (native token) can make transactions by paying a fee in another token. | ||
|
||
The Paymaster's objective is twofold: | ||
- take a fee and send it to the Relayers; | ||
- execute what the user wants to be executed. | ||
|
||
## Implementation | ||
|
||
The contract has only one endpoint: `forward_execution` and can be called by anyone. | ||
The user will use `MultiESDTNFTTransfer` support to send multiple payments: | ||
- first payment is always the fee that will be sent to the Relayer; | ||
- rest of the payments will be what users want to send. | ||
|
||
One example of userTX is: | ||
``` | ||
MultiESDTNFTTransfer@paymasterSCAddr@feeTokenID@nonce@value@listofOther(tokenID,nonce,value)@forwardExecution@relayerAddr@destination@endpoint@extraArguments | ||
|
||
``` | ||
|
||
After sending the Relayer the fee, `forward_execution` endpoint will make an *asynchronous call* to the destination. | ||
The destionation can be a user or a smart contract. | ||
|
||
We register a callback to the *asynchronous call*. In case of failure the paymaster SC sends the tokens back to the user. |
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,14 @@ | ||
[package] | ||
name = "paymaster-meta" | ||
version = "0.0.0" | ||
edition = "2018" | ||
publish = false | ||
authors = [ "you",] | ||
|
||
[dev-dependencies] | ||
|
||
[dependencies.paymaster] | ||
path = ".." | ||
|
||
[dependencies.multiversx-sc-meta] | ||
version = "0.43.4" |
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::<paymaster::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,18 @@ | ||
name = "empty" | ||
contract_trait = "EmptyContract" | ||
src_file = "empty.rs" | ||
rename_pairs = [ | ||
[ | ||
"blockchain.set_current_dir_from_workspace(\"contracts/examples/empty\");", | ||
"// blockchain.set_current_dir_from_workspace(\"relative path to your workspace, if applicable\");", | ||
], | ||
] | ||
files_include = [ | ||
"meta", | ||
"scenarios", | ||
"src", | ||
"tests", | ||
"wasm/Cargo.toml", | ||
"Cargo.toml", | ||
"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,39 @@ | ||
{ | ||
"name": "paymaster", | ||
"steps": [ | ||
{ | ||
"step": "setState", | ||
"accounts": { | ||
"address:owner": { | ||
"nonce": "1", | ||
"balance": "0" | ||
} | ||
}, | ||
"newAddresses": [ | ||
{ | ||
"creatorAddress": "address:owner", | ||
"creatorNonce": "1", | ||
"newAddress": "sc:empty" | ||
} | ||
] | ||
}, | ||
{ | ||
"step": "scDeploy", | ||
"id": "deploy", | ||
"tx": { | ||
"from": "address:owner", | ||
"contractCode": "file:../output/paymaster.wasm", | ||
"arguments": [], | ||
"gasLimit": "5,000,000", | ||
"gasPrice": "0" | ||
}, | ||
"expect": { | ||
"out": [], | ||
"status": "", | ||
"logs": [], | ||
"gas": "*", | ||
"refund": "*" | ||
} | ||
} | ||
] | ||
} |
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,53 @@ | ||
multiversx_sc::imports!(); | ||
|
||
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>; | ||
|
||
static ERR_CALLBACK_MSG: &[u8] = b"Error received in callback:"; | ||
pub const ESDT_TRANSFER_FUNC_NAME: &str = "ESDTTransfer"; | ||
#[multiversx_sc::module] | ||
pub trait ForwardCall { | ||
fn forward_call( | ||
&self, | ||
dest: ManagedAddress, | ||
endpoint_name: ManagedBuffer, | ||
endpoint_args: MultiValueEncoded<ManagedBuffer>, | ||
payments: PaymentsVec<Self::Api>, | ||
) { | ||
let original_caller = self.blockchain().get_caller(); | ||
|
||
self.send() | ||
.contract_call::<()>(dest, endpoint_name) | ||
.with_raw_arguments(endpoint_args.to_arg_buffer()) | ||
.with_multi_token_transfer(payments) | ||
.async_call() | ||
.with_callback(self.callbacks().transfer_callback(original_caller)) | ||
.call_and_exit(); | ||
} | ||
|
||
#[callback] | ||
fn transfer_callback( | ||
&self, | ||
original_caller: ManagedAddress, | ||
#[call_result] result: ManagedAsyncCallResult<MultiValueEncoded<ManagedBuffer>>, | ||
) -> MultiValueEncoded<ManagedBuffer> { | ||
let initial_payments = self.call_value().all_esdt_transfers(); | ||
|
||
match result { | ||
ManagedAsyncCallResult::Ok(return_values) => return_values, | ||
ManagedAsyncCallResult::Err(err) => { | ||
if !initial_payments.is_empty() { | ||
self.send() | ||
.direct_multi(&original_caller, &initial_payments); | ||
} | ||
|
||
let mut err_result = MultiValueEncoded::new(); | ||
err_result.push(ManagedBuffer::new_from_bytes(ERR_CALLBACK_MSG)); | ||
err_result.push(err.err_msg.clone()); | ||
|
||
sc_print!("{}", err.err_msg); | ||
|
||
err_result | ||
} | ||
} | ||
} | ||
} |
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,39 @@ | ||
#![no_std] | ||
|
||
multiversx_sc::imports!(); | ||
|
||
pub mod forward_call; | ||
const FEE_PAYMENT: usize = 0; | ||
|
||
/// An empty contract. To be used as a template when starting a new contract from scratch. | ||
#[multiversx_sc::contract] | ||
pub trait PaymasterContract: forward_call::ForwardCall { | ||
#[init] | ||
fn init(&self) {} | ||
|
||
#[endpoint(forwardExecution)] | ||
#[payable("*")] | ||
fn forward_execution( | ||
&self, | ||
relayer_addr: ManagedAddress, | ||
dest: ManagedAddress, | ||
endpoint_name: ManagedBuffer, | ||
endpoint_args: MultiValueEncoded<ManagedBuffer>, | ||
) { | ||
let payments = self.call_value().all_esdt_transfers(); | ||
require!(!payments.is_empty(), "There is no fee for payment!"); | ||
|
||
let fee_payment = payments.get(FEE_PAYMENT); | ||
self.send().direct_esdt( | ||
&relayer_addr, | ||
&fee_payment.token_identifier, | ||
0, | ||
&fee_payment.amount, | ||
); | ||
|
||
let mut payments_without_fee = payments.clone_value(); | ||
payments_without_fee.remove(FEE_PAYMENT); | ||
|
||
self.forward_call(dest, endpoint_name, endpoint_args, payments_without_fee); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you check that the fee is equal to some amount?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is checked at the relayer level.