From d3bcedc98df22367b8f726e02acaef57eaa1695c Mon Sep 17 00:00:00 2001 From: Zachary Frederick Date: Fri, 5 Jan 2024 14:30:14 -0500 Subject: [PATCH] removed more code --- integration-tests/src/test/burn.test.ts | 112 --------------- pallets/creditcoin/src/benchmarking.rs | 13 -- pallets/creditcoin/src/helpers.rs | 24 ---- pallets/creditcoin/src/lib.rs | 3 - pallets/creditcoin/src/tests.rs | 182 ------------------------ pallets/creditcoin/src/weights.rs | 8 -- 6 files changed, 342 deletions(-) delete mode 100644 integration-tests/src/test/burn.test.ts diff --git a/integration-tests/src/test/burn.test.ts b/integration-tests/src/test/burn.test.ts deleted file mode 100644 index ae922fd567..0000000000 --- a/integration-tests/src/test/burn.test.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { Blockchain, KeyringPair, creditcoinApi, BN, CREDO_PER_CTC } from 'creditcoin-js'; -import { forElapsedBlocks, testData } from 'creditcoin-js/lib/testUtils'; -import { CreditcoinApi } from 'creditcoin-js/lib/types'; -import { mnemonicGenerate } from '@polkadot/util-crypto'; -import { extractFee, describeIf } from '../utils'; -import { POINT_01_CTC } from 'creditcoin-js'; - -describeIf((global as any).CREDITCOIN_EXECUTE_SETUP_AUTHORITY, 'burn', () => { - let ccApi: CreditcoinApi; - let sudoSigner: KeyringPair; - - const testingData = testData( - (global as any).CREDITCOIN_ETHEREUM_CHAIN as Blockchain, - (global as any).CREDITCOIN_CREATE_WALLET, - ); - const { keyring } = testingData; - - const ONE_CTC = new BN((1 * CREDO_PER_CTC).toString(), 10); - const BURN_AMOUNT = new BN(POINT_01_CTC.toString(), 10); - - beforeAll(async () => { - ccApi = await creditcoinApi((global as any).CREDITCOIN_API_URL); - sudoSigner = (global as any).CREDITCOIN_CREATE_SIGNER(keyring, 'sudo'); - }); - - afterAll(async () => await ccApi.api.disconnect()); - - it('burn_all works as expected', async (): Promise => { - const wallet = keyring.addFromMnemonic(mnemonicGenerate(12)); - - const { api } = ccApi; - - await api.tx.sudo - .sudo(api.tx.balances.setBalance(wallet.address, ONE_CTC, 0)) - .signAndSend(sudoSigner, { nonce: -1 }); - await forElapsedBlocks(api); - - const starting = await api.derive.balances.all(wallet.address); - - expect(starting.freeBalance.isZero()).toBe(false); - - await api.tx.creditcoin.burnAll(wallet.address).signAndSend(wallet); - await forElapsedBlocks(api); - - const ending = await api.derive.balances.all(wallet.address); - expect(ending.freeBalance.isZero()).toBe(true); - }, 100_000); - - it('burn works as expected', async (): Promise => { - const burner = keyring.addFromMnemonic(mnemonicGenerate(12)); - - const { api } = ccApi; - - await api.tx.sudo - .sudo(api.tx.balances.setBalance(burner.address, ONE_CTC, 0)) - .signAndSend(sudoSigner, { nonce: -1 }); - await forElapsedBlocks(api); - - const starting = await api.derive.balances.all(burner.address); - expect(starting.freeBalance.isZero()).toBe(false); - - await api.tx.creditcoin.burn(BURN_AMOUNT, burner.address).signAndSend(burner); - await forElapsedBlocks(api); - - const ending = await api.derive.balances.all(burner.address); - expect(starting.freeBalance.gt(ending.freeBalance)).toBe(true); - }, 100_000); - - it('burn_all fee is min 0.01 CTC', async (): Promise => { - const burner = keyring.addFromMnemonic(mnemonicGenerate(12)); - - const { api } = ccApi; - - await api.tx.sudo - .sudo(api.tx.balances.setBalance(burner.address, ONE_CTC, 0)) - .signAndSend(sudoSigner, { nonce: -1 }); - await forElapsedBlocks(api); - - return new Promise((resolve, reject): void => { - const unsubscribe = api.tx.creditcoin - .burnAll(burner.address) - .signAndSend(burner, { nonce: -1 }, async ({ dispatchError, events, status }) => { - await extractFee(resolve, reject, unsubscribe, api, dispatchError, events, status); - }) - .catch((error) => reject(error)); - }).then((fee) => { - expect(fee).toBeGreaterThanOrEqual((global as any).CREDITCOIN_MINIMUM_TXN_FEE); - }); - }, 150_000); - - it('burn fee is min 0.01 CTC', async (): Promise => { - const burner = keyring.addFromMnemonic(mnemonicGenerate(12)); - - const { api } = ccApi; - - await api.tx.sudo - .sudo(api.tx.balances.setBalance(burner.address, ONE_CTC, 0)) - .signAndSend(sudoSigner, { nonce: -1 }); - await forElapsedBlocks(api); - - return new Promise((resolve, reject): void => { - const unsubscribe = api.tx.creditcoin - .burn(BURN_AMOUNT, burner.address) - .signAndSend(burner, { nonce: -1 }, async ({ dispatchError, events, status }) => { - await extractFee(resolve, reject, unsubscribe, api, dispatchError, events, status); - }) - .catch((error) => reject(error)); - }).then((fee) => { - expect(fee).toBeGreaterThanOrEqual((global as any).CREDITCOIN_MINIMUM_TXN_FEE); - }); - }, 150_000); -}); diff --git a/pallets/creditcoin/src/benchmarking.rs b/pallets/creditcoin/src/benchmarking.rs index aff39116ab..0fc422fe78 100644 --- a/pallets/creditcoin/src/benchmarking.rs +++ b/pallets/creditcoin/src/benchmarking.rs @@ -373,19 +373,6 @@ benchmarks! { let contract = BurnDetails::GCRE(address.value, tx_id); }: _( RawOrigin::Signed(collector), contract) - - burn_all { - let collector: T::AccountId = lender_account::(true); - let who = collector.clone(); - }: _(RawOrigin::Signed(who), collector) - - burn { - let collector: T::AccountId = lender_account::(true); - let who = collector.clone(); - let min = as Currency>::minimum_balance(); - as Currency>::make_free_balance_be(&collector, min * 1_000_000u32.into()); - let balance = min * 100u32.into(); - }: _(RawOrigin::Signed(who), balance, collector) } //impl_benchmark_test_suite!(Creditcoin, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/pallets/creditcoin/src/helpers.rs b/pallets/creditcoin/src/helpers.rs index ce1a688199..932d1de55e 100644 --- a/pallets/creditcoin/src/helpers.rs +++ b/pallets/creditcoin/src/helpers.rs @@ -257,27 +257,3 @@ pub fn blockchain_is_supported(blockchain: &Blockchain) -> bool { Blockchain::Other(_) => false, } } - -pub fn burn_and_settle( - who: T::AccountId, - amount: T::Balance, -) -> Result<(), PositiveImbalance> { - let imbalance: pallet_balances::PositiveImbalance = - >::burn(amount); - - as CurrencyT>::settle( - &who, - imbalance, - WithdrawReasons::TRANSFER, - AllowDeath, - ) -} - -pub fn can_burn_amount(who: T::AccountId, amount: T::Balance) -> bool { - let balance = as CurrencyT>::free_balance(&who); - - let res = balance.saturated_into::(); - let amount_128 = amount.saturated_into::(); - - res >= amount_128 -} diff --git a/pallets/creditcoin/src/lib.rs b/pallets/creditcoin/src/lib.rs index 4e3481d37d..c121e72fff 100644 --- a/pallets/creditcoin/src/lib.rs +++ b/pallets/creditcoin/src/lib.rs @@ -39,7 +39,6 @@ mod types; pub mod test_utils; use crate::types::{BurnId, BurnInfo}; -use helpers::{burn_and_settle, can_burn_amount}; use ocw::tasks::collect_coins::DeployedContract; pub use types::{ loan_terms, Address, AddressId, AskOrder, AskOrderId, AskTerms, BidOrder, BidOrderId, BidTerms, @@ -157,8 +156,6 @@ pub mod pallet { fn set_gate_contract() -> Weight; fn set_gate_faucet() -> Weight; fn request_collect_coins_v2() -> Weight; - fn burn_all() -> Weight; - fn burn() -> Weight; } #[pallet::pallet] diff --git a/pallets/creditcoin/src/tests.rs b/pallets/creditcoin/src/tests.rs index 6a819bb747..ba2851a464 100644 --- a/pallets/creditcoin/src/tests.rs +++ b/pallets/creditcoin/src/tests.rs @@ -2949,12 +2949,6 @@ fn exercise_weightinfo_functions() { let result = super::weights::WeightInfo::::request_collect_coins_v2(); assert!(result.ref_time() > 0); - - let result = super::weights::WeightInfo::::burn(); - assert!(result.ref_time() > 0); - - let result = super::weights::WeightInfo::::burn_all(); - assert!(result.ref_time() > 0); } #[test] @@ -3329,179 +3323,3 @@ fn gate_faucet_account_storage_should_return_none_when_not_set() { assert!(gate_faucet.is_none()); }); } - -#[test] -fn burn_all_should_error_when_not_signed() { - ExtBuilder::default().build_and_execute(|| { - System::set_block_number(1); - - let account_on_cc3: AccountId = AccountId::new([0; 32]); - - assert_noop!(Creditcoin::burn_all(Origin::none(), account_on_cc3), BadOrigin); - }); -} - -#[test] -fn burn_all_should_emit_event_and_update_storage_when_ok() { - let account_on_cc2: AccountId = AccountId::new([0; 32]); - let all_funds = 999_999_999; - - let mut ext = ExtBuilder::default(); - ext.fund(account_on_cc2.clone(), all_funds); - - ext.build_and_execute(|| { - System::set_block_number(1); - - let account_on_cc3: AccountId = AccountId::new([1; 32]); - - // no funds have been burned - assert_eq!(crate::BurnedFunds::::count(), 0); - - assert_ok!(Creditcoin::burn_all( - Origin::signed(account_on_cc2.clone()), - account_on_cc3.clone() - )); - assert_eq!(crate::BurnedFunds::::count(), 1); // burn has been registered - - let event = >::events().pop().expect("expected an event").event; - assert_matches!(event, crate::mock::RuntimeEvent::Creditcoin(crate::Event::Burned(burn_id))=>{ - let burn_info = crate::BurnedFunds::::try_get(burn_id).unwrap(); - assert_eq!(burn_info.account, account_on_cc2); - assert_eq!(burn_info.amount, all_funds); - assert_eq!(burn_info.collector, account_on_cc3); - }); - }); -} - -#[test] -fn burn_should_error_when_not_signed() { - ExtBuilder::default().build_and_execute(|| { - System::set_block_number(1); - - let account_on_cc3: AccountId = AccountId::new([1; 32]); - - assert_noop!(Creditcoin::burn(Origin::none(), 1_000_000, account_on_cc3), BadOrigin); - }); -} - -#[test] -fn burn_should_error_when_balance_is_less_than_requested_amount() { - let account_on_cc2: AccountId = AccountId::new([0; 32]); - let all_funds = 9_999; - - let mut ext = ExtBuilder::default(); - ext.fund(account_on_cc2.clone(), all_funds); - - ext.build_and_execute(|| { - System::set_block_number(1); - let account_on_cc3: AccountId = AccountId::new([1; 32]); - - // doesn't have enough funds on CC2 to burn that much - assert_noop!( - Creditcoin::burn(Origin::signed(account_on_cc2), 99_000_000, account_on_cc3), - crate::Error::::BurnInsufficientFunds - ); - }); -} - -#[test] -fn burn_should_error_when_requested_second_time_with_insufficient_amount() { - let account_on_cc2: AccountId = AccountId::new([0; 32]); - let all_funds = 999_999_999; - let some_funds = 899_999_999; - - let mut ext = ExtBuilder::default(); - ext.fund(account_on_cc2.clone(), all_funds); - - ext.build_offchain_and_execute_with_state(|_, _| { - System::set_block_number(1); - - let account_on_cc3: AccountId = AccountId::new([1; 32]); - - assert_ok!(Creditcoin::burn( - Origin::signed(account_on_cc2.clone()), - some_funds, - account_on_cc3.clone() - )); - - roll_by_with_ocw(10); - - // call again, some_funds > remaining balance - assert_noop!( - Creditcoin::burn(Origin::signed(account_on_cc2), some_funds, account_on_cc3), - crate::Error::::BurnInsufficientFunds - ); - }); -} - -#[test] -fn burn_should_work_when_requested_second_time_with_smaller_amount() { - let account_on_cc2: AccountId = AccountId::new([0; 32]); - let all_funds = 1_000_000; - let some_funds = 500_000; - - let mut ext = ExtBuilder::default(); - ext.fund(account_on_cc2.clone(), all_funds); - - ext.build_offchain_and_execute_with_state(|_, _| { - System::set_block_number(1); - - let account_on_cc3: AccountId = AccountId::new([1; 32]); - - assert_ok!(Creditcoin::burn( - Origin::signed(account_on_cc2.clone()), - some_funds, - account_on_cc3.clone() - )); - - roll_by_with_ocw(10); - - // call aagain, some_funds <= remaining balance - // actually some_funds == remaining balance in this example - assert_ok!(Creditcoin::burn( - Origin::signed(account_on_cc2.clone()), - some_funds, - account_on_cc3 - )); - }); -} - -#[test] -fn burn_should_emit_event_and_update_storage_when_ok() { - let account_on_cc2: AccountId = AccountId::new([0; 32]); - let all_funds = 999_999_999; - let some_funds = 899_999_999; - - let mut ext = ExtBuilder::default(); - ext.fund(account_on_cc2.clone(), all_funds); - - ext.build_and_execute(|| { - System::set_block_number(1); - - let account_on_cc3: AccountId = AccountId::new([1; 32]); - - // no funds have been burned - assert_eq!(crate::BurnedFunds::::count(), 0); - - assert_ok!(Creditcoin::burn( - Origin::signed(account_on_cc2.clone()), - some_funds, - account_on_cc3.clone() - )); - assert_eq!(crate::BurnedFunds::::count(), 1); // burn has been registered - - let event = >::events().pop().expect("expected an event").event; - assert_matches!(event, crate::mock::RuntimeEvent::Creditcoin(crate::Event::Burned(burn_id))=>{ - let burn_info = crate::BurnedFunds::::try_get(burn_id).unwrap(); - assert_eq!(burn_info.account, account_on_cc2); - assert_eq!(burn_info.amount, some_funds); - assert_eq!(burn_info.collector, account_on_cc3); - }); - - // remaining balance is exactly the difference between the 2 amounts - let remaining_balance = - as Currency>::free_balance(&account_on_cc2); - let remaining_balance = remaining_balance.saturated_into::(); - assert_eq!(remaining_balance, all_funds - some_funds); - }); -} diff --git a/pallets/creditcoin/src/weights.rs b/pallets/creditcoin/src/weights.rs index cd40602df3..44adfdbfb2 100644 --- a/pallets/creditcoin/src/weights.rs +++ b/pallets/creditcoin/src/weights.rs @@ -491,12 +491,4 @@ impl crate::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(1)) } - - fn burn_all() -> Weight { - Weight::from_parts(1, 1) - } - - fn burn() -> Weight { - Weight::from_parts(1, 1) - } }