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

Relayer fees #51

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 .gitmodules

This file was deleted.

8 changes: 1 addition & 7 deletions shielder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,13 @@ check-drink-tests: ## Run cargo checks on drink tests.
.PHONY: check
check: check-contract check-mocked-zk check-drink-tests ## Run cargo checks

.PHONY: build-psp22
build-psp22: ## Builds psp22 contracts.
@echo "Building psp22 contract" ; \
git submodule update --init ; \
cargo contract build --quiet --manifest-path PSP22/Cargo.toml --features "contract" --release ; \

.PHONY: build-shielder
build-shielder: ## Builds shielder contracts.
@echo "Building shielder contract" ; \
cargo contract build --quiet --manifest-path contract/Cargo.toml --release ; \

.PHONY: setup-tests
setup-tests: build-psp22 build-shielder ## Builds contracts and generates wrappers.
setup-tests: build-shielder ## Builds contracts and generates wrappers.
Comment on lines -41 to +35
Copy link
Member

Choose a reason for hiding this comment

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

nit: things like removing psp submodule can be easily done in separate mini PRs


.PHONY: shielder-unit-tests
shielder-unit-tests: ## Runs unit tests for contract.
Expand Down
1 change: 0 additions & 1 deletion shielder/PSP22
Submodule PSP22 deleted from 94a8f7
80 changes: 80 additions & 0 deletions shielder/contract/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod types;

/// Contract module
#[ink::contract]
#[allow(clippy::large_enum_variant)]
pub mod contract {

use crate::{errors::ShielderError, merkle::MerkleTree, traits::psp22::PSP22Error, types::Set};
Expand Down Expand Up @@ -111,6 +112,85 @@ pub mod contract {
)
.returns::<Result<(), PSP22Error>>()
.invoke()?,
OpPub::DepositRelayer {
amount,
token,
user,
fee,
fee_token,
relayer,
} => {
build_call::<DefaultEnvironment>()
.call(AccountId::from(token.bytes))
.call_v1()
.gas_limit(0)
.transferred_value(0)
.exec_input(
ExecutionInput::new(Selector::new(ink::selector_bytes!(
"PSP22::transfer_from"
)))
.push_arg(AccountId::from(user.bytes))
.push_arg(self.env().account_id())
.push_arg(amount)
.push_arg([].to_vec() as ink::prelude::vec::Vec<u8>),
)
.returns::<Result<(), PSP22Error>>()
.invoke()?;
Comment on lines +123 to +138
Copy link
Member

Choose a reason for hiding this comment

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

please reduce repetitions across this module - you can crate functions dedicated for transfer and transfer_from that would hide the whole boilerplate

build_call::<DefaultEnvironment>()
.call(AccountId::from(fee_token.bytes))
.call_v1()
.gas_limit(0)
.transferred_value(0)
.exec_input(
ExecutionInput::new(Selector::new(ink::selector_bytes!(
"PSP22::transfer"
)))
.push_arg(AccountId::from(relayer.bytes))
.push_arg(fee)
.push_arg([].to_vec() as ink::prelude::vec::Vec<u8>),
)
.returns::<Result<(), PSP22Error>>()
.invoke()?;
}
OpPub::WithdrawRelayer {
amount,
token,
user,
fee,
fee_token,
relayer,
} => {
build_call::<DefaultEnvironment>()
.call(AccountId::from(token.bytes))
.call_v1()
.gas_limit(0)
.transferred_value(0)
.exec_input(
ExecutionInput::new(Selector::new(ink::selector_bytes!(
"PSP22::transfer"
)))
.push_arg(AccountId::from(user.bytes))
.push_arg(amount)
.push_arg([].to_vec() as ink::prelude::vec::Vec<u8>),
)
.returns::<Result<(), PSP22Error>>()
.invoke()?;
build_call::<DefaultEnvironment>()
.call(AccountId::from(fee_token.bytes))
.call_v1()
.gas_limit(0)
.transferred_value(0)
.exec_input(
ExecutionInput::new(Selector::new(ink::selector_bytes!(
"PSP22::transfer"
)))
.push_arg(AccountId::from(relayer.bytes))
.push_arg(fee)
.push_arg([].to_vec() as ink::prelude::vec::Vec<u8>),
)
.returns::<Result<(), PSP22Error>>()
.invoke()?;
}
};
Ok(())
}
Expand Down
9 changes: 9 additions & 0 deletions shielder/drink_tests/Cargo.lock

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

1 change: 1 addition & 0 deletions shielder/drink_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rand = { version = "0.8.5", default-features = false }
anyhow = { version = "1.0.79", default-features = false }
mocked_zk = { path = "../mocked_zk", default-features = false }
shielder-contract = { path = "../contract", default-features = false, features = ["ink-as-dependency"] }
psp22 = { git = "https://github.com/Cardinal-Cryptography/PSP22.git", rev = "676768b", default-features = false, features = ["ink-as-dependency", "contract"] }
93 changes: 87 additions & 6 deletions shielder/drink_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ mod tests {
session = session.with_actor(alice.clone());

let psp22_address = deploy_test_token(&mut session, 100)?;
let shielder_address = deploy_shielder(&mut session, &psp22_address)?;
let shielder_address = deploy_shielder(&mut session, vec![psp22_address.clone()])?;

// CREATE ACCOUNT
let user_shielded_data = create_shielder_account(
&mut session,
&shielder_address,
&psp22_address,
vec![psp22_address.clone()],
rng.gen::<u128>().into(),
)?;

Expand Down Expand Up @@ -88,13 +88,13 @@ mod tests {
}

let psp22_address = deploy_test_token(&mut session, 100)?;
let shielder_address = deploy_shielder(&mut session, &psp22_address)?;
let shielder_address = deploy_shielder(&mut session, vec![psp22_address.clone()])?;

// CREATE ACCOUNT
let mut user_shielded_data = create_shielder_account(
&mut session,
&shielder_address,
&psp22_address,
vec![psp22_address.clone()],
rng.gen::<u128>().into(),
)?;

Expand Down Expand Up @@ -158,7 +158,7 @@ mod tests {
}

let psp22_address = deploy_test_token(&mut session, 800)?;
let shielder_address = deploy_shielder(&mut session, &psp22_address)?;
let shielder_address = deploy_shielder(&mut session, vec![psp22_address.clone()])?;

for depositor_addr in &depositors {
psp22_transfer(&mut session, &psp22_address, depositor_addr, 100)?;
Expand All @@ -173,7 +173,7 @@ mod tests {
user_shielded_data.push(create_shielder_account(
&mut session,
&shielder_address,
&psp22_address,
vec![psp22_address.clone()],
rng.gen::<u128>().into(),
)?);

Expand Down Expand Up @@ -211,4 +211,85 @@ mod tests {

Ok(())
}

#[drink::test]
fn relayer_single_deposit_single_withdraw(mut session: Session) -> Result<()> {
let mut rng = StdRng::seed_from_u64(1);

let alice = init_alice(&mut session)?;
let bob = init_bob(&mut session)?;
let relayer = init_relayer(&mut session)?;

session = session.with_actor(alice.clone());

let psp22_address = deploy_test_token(&mut session, 100)?;
let azero_address = deploy_azero_test_token(&mut session, 100)?;
let shielder_address = deploy_shielder(
&mut session,
vec![psp22_address.clone(), azero_address.clone()],
)?;

// CREATE ACCOUNT
let user_shielded_data = create_shielder_account(
&mut session,
&shielder_address,
vec![psp22_address.clone(), azero_address.clone()],
rng.gen::<u128>().into(),
)?;

// APPROVE TRANSFER
psp22_approve(&mut session, &azero_address, &shielder_address, 10)?;

// DEPOSIT
let user_shielded_data = shielder_update(
&mut session,
&shielder_address,
deposit_op(&azero_address, &alice, 10),
user_shielded_data,
rng.gen::<u128>().into(),
)?;

// APPROVE TRANSFER
psp22_approve(&mut session, &psp22_address, &shielder_address, 10)?;

session = session.with_actor(relayer.clone());
// DEPOSIT THROUGH RELAYER
let user_shielded_data = shielder_update(
&mut session,
&shielder_address,
deposit_op_relayer(&psp22_address, &alice, 10, &azero_address, &relayer, 1),
user_shielded_data,
rng.gen::<u128>().into(),
)?;

let alice_psp22_balance = get_psp22_balance(&mut session, &psp22_address, &alice)?;
assert_eq!(alice_psp22_balance, 90);
let shielder_psp22_balance =
get_psp22_balance(&mut session, &psp22_address, &shielder_address)?;
assert_eq!(shielder_psp22_balance, 10);
let relayer_psp22_balance = get_psp22_balance(&mut session, &azero_address, &relayer)?;
assert_eq!(relayer_psp22_balance, 1);

// SWITCH TO bob
session = session.with_actor(bob.clone());

// WITHDRAW THROUGH RELAYER
let _ = shielder_update(
&mut session,
&shielder_address,
withdraw_op_relayer(&psp22_address, &bob, 1, &azero_address, &relayer, 1),
user_shielded_data,
rng.gen::<u128>().into(),
)?;

let bob_psp22_balance = get_psp22_balance(&mut session, &psp22_address, &bob)?;
assert_eq!(bob_psp22_balance, 1);
let shielder_psp22_balance =
get_psp22_balance(&mut session, &psp22_address, &shielder_address)?;
assert_eq!(shielder_psp22_balance, 9);
let relayer_psp22_balance = get_psp22_balance(&mut session, &azero_address, &relayer)?;
assert_eq!(relayer_psp22_balance, 2);

Ok(())
}
}
6 changes: 6 additions & 0 deletions shielder/drink_tests/src/utils/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ pub fn init_bob(session: &mut Session<MinimalSandbox>) -> Result<AccountId32> {
init_acc_with_balance(session, &res)?;
Ok(res)
}

pub fn init_relayer(session: &mut Session<MinimalSandbox>) -> Result<AccountId32> {
let res = AccountId32::new([4u8; 32]);
Copy link
Member

Choose a reason for hiding this comment

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

please use constants for all actors in this module

init_acc_with_balance(session, &res)?;
Ok(res)
}
46 changes: 46 additions & 0 deletions shielder/drink_tests/src/utils/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,49 @@ pub fn withdraw_op(
},
}
}

pub fn deposit_op_relayer(
psp22_address: &AccountId32,
user: &AccountId32,
amount: u128,
azero_address: &AccountId32,
relayer: &AccountId32,
fee: u128,
) -> UpdateOperation {
UpdateOperation {
op_pub: OpPub::DepositRelayer {
amount,
token: Scalar::from_bytes(*((*psp22_address).as_ref())),
user: Scalar::from_bytes(*((*user).as_ref())),
fee,
fee_token: Scalar::from_bytes(*((*azero_address).as_ref())),
relayer: Scalar::from_bytes(*((*relayer).as_ref())),
},
op_priv: OpPriv {
user: Scalar::from_bytes(*((*user).as_ref())),
},
}
}

pub fn withdraw_op_relayer(
psp22_address: &AccountId32,
user: &AccountId32,
amount: u128,
azero_address: &AccountId32,
relayer: &AccountId32,
fee: u128,
) -> UpdateOperation {
UpdateOperation {
op_pub: OpPub::WithdrawRelayer {
amount,
token: Scalar::from_bytes(*((*psp22_address).as_ref())),
user: Scalar::from_bytes(*((*user).as_ref())),
fee,
fee_token: Scalar::from_bytes(*((*azero_address).as_ref())),
relayer: Scalar::from_bytes(*((*relayer).as_ref())),
},
op_priv: OpPriv {
user: Scalar::from_bytes(*((*user).as_ref())),
},
}
}
26 changes: 23 additions & 3 deletions shielder/drink_tests/src/utils/psp22.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::tests::BundleProvider;
use anyhow::Result;
use drink::{
minimal::MinimalSandbox,
session::{bundle::ContractBundle, Session, NO_ENDOWMENT, NO_SALT},
session::{Session, NO_ENDOWMENT, NO_SALT},
AccountId32,
};

pub fn deploy_test_token(
session: &mut Session<MinimalSandbox>,
supply: u128,
) -> Result<AccountId32> {
let psp22_bundle =
ContractBundle::load(std::path::Path::new("../PSP22/target/ink/psp22.contract"))?;
let psp22_bundle = BundleProvider::Psp22.bundle()?;
let res = session.deploy_bundle(
psp22_bundle,
"new",
Expand All @@ -26,6 +26,26 @@ pub fn deploy_test_token(
Ok(res)
}

pub fn deploy_azero_test_token(
session: &mut Session<MinimalSandbox>,
supply: u128,
) -> Result<AccountId32> {
let psp22_bundle = BundleProvider::Psp22.bundle()?;
let res = session.deploy_bundle(
psp22_bundle,
"new",
&[
format!("{}", supply).as_str(),
"Some(\"AZERO\")",
"Some(\"AZERO\")",
"9",
],
NO_SALT,
NO_ENDOWMENT,
)?;
Ok(res)
}

pub fn get_psp22_balance(
session: &mut Session<MinimalSandbox>,
token: &AccountId32,
Expand Down
Loading
Loading