Skip to content

Commit

Permalink
Introduce the set_gate_contract() extrinsic
Browse files Browse the repository at this point in the history
in preparation for request_collect_coins_v2
  • Loading branch information
Zachary Frederick authored and atodorov committed Aug 25, 2023
1 parent 77bf581 commit 59a7323
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 27 deletions.
2 changes: 1 addition & 1 deletion integration-tests/src/test/collect-coins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('CollectCoins', (): void => {
collector = (global as any).CREDITCOIN_CREATE_SIGNER(keyring, 'lender');

/* eslint-disable @typescript-eslint/naming-convention */
const contract = api.createType('PalletCreditcoinOcwTasksCollectCoinsGCreContract', {
const contract = api.createType('PalletCreditcoinOcwTasksCollectCoinsDeployedContract', {
address: (global as any).CREDITCOIN_CTC_CONTRACT_ADDRESS,
chain: blockchain,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('SetCollectCoinsContract', (): void => {
const { api } = ccApi;

/* eslint-disable @typescript-eslint/naming-convention */
const contract = api.createType('PalletCreditcoinOcwTasksCollectCoinsGCreContract', {
const contract = api.createType('PalletCreditcoinOcwTasksCollectCoinsDeployedContract', {
address: '0xa3EE21C306A700E682AbCdfe9BaA6A08F3820419',
chain: testingData.blockchain,
});
Expand Down
7 changes: 6 additions & 1 deletion pallets/creditcoin/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ benchmarks! {

set_collect_coins_contract {
let root = RawOrigin::Root;
let contract = GCreContract::default();
let contract = DeployedContract::default();
}: _(root, contract)

register_address_v2 {
Expand All @@ -348,6 +348,11 @@ benchmarks! {
let signature = ecdsa_sign(ktypeid, &pkey, &message).expect("ecdsa signature");
let proof = OwnershipProof::EthSign(signature);
}: _(RawOrigin::Signed(who), Blockchain::Ethereum, address, proof)

set_gate_contract {
let root = RawOrigin::Root;
let contract = DeployedContract::default();
}: _(root, contract)
}

//impl_benchmark_test_suite!(Creditcoin, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
25 changes: 22 additions & 3 deletions pallets/creditcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod migrations;
pub mod ocw;
mod types;

use ocw::tasks::collect_coins::GCreContract;
use ocw::tasks::collect_coins::DeployedContract;
pub use types::{
loan_terms, Address, AddressId, AskOrder, AskOrderId, AskTerms, BidOrder, BidOrderId, BidTerms,
Blockchain, CollectedCoinsId, CollectedCoinsStruct, DealOrder, DealOrderId, Duration,
Expand Down Expand Up @@ -144,6 +144,7 @@ pub mod pallet {
fn remove_authority() -> Weight;
fn set_collect_coins_contract() -> Weight;
fn register_address_v2() -> Weight;
fn set_gate_contract() -> Weight;
}

#[pallet::pallet]
Expand Down Expand Up @@ -229,7 +230,11 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn collect_coins_contract)]
pub type CollectCoinsContract<T: Config> = StorageValue<_, GCreContract, ValueQuery>;
pub type CollectCoinsContract<T: Config> = StorageValue<_, DeployedContract, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn gate_contract)]
pub type GATEConract<T: Config> = StorageValue<_, DeployedContract, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down Expand Up @@ -1323,7 +1328,7 @@ pub mod pallet {
#[pallet::weight(<T as Config>::WeightInfo::set_collect_coins_contract())]
pub fn set_collect_coins_contract(
origin: OriginFor<T>,
contract: GCreContract,
contract: DeployedContract,
) -> DispatchResult {
ensure_root(origin)?;
CollectCoinsContract::<T>::put(contract);
Expand Down Expand Up @@ -1401,5 +1406,19 @@ pub mod pallet {
},
}
}

/// Set the onchain details for the Gluwa GATE Contract, including its address and the blockchain where it is deployed.
/// This extrinsic expects the caller to have root permissions.
#[transactional]
#[pallet::call_index(23)]
#[pallet::weight(<T as Config>::WeightInfo::set_gate_contract())]
pub fn set_gate_contract(
origin: OriginFor<T>,
contract: DeployedContract,
) -> DispatchResult {
ensure_root(origin)?;
GATEConract::<T>::put(contract);
Ok(())
}
}
}
34 changes: 17 additions & 17 deletions pallets/creditcoin/src/ocw/tasks/collect_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ use sp_runtime::SaturatedConversion;
use sp_std::prelude::*;

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct GCreContract {
pub struct DeployedContract {
pub address: sp_core::H160,
pub chain: Blockchain,
}

impl GCreContract {
impl DeployedContract {
const DEFAULT_CHAIN: Blockchain = Blockchain::Ethereum;
}

impl Default for GCreContract {
impl Default for DeployedContract {
fn default() -> Self {
let contract_chain: Blockchain = GCreContract::DEFAULT_CHAIN;
let contract_chain: Blockchain = DeployedContract::DEFAULT_CHAIN;
let contract_address: H160 =
sp_core::H160(hex!("a3EE21C306A700E682AbCdfe9BaA6A08F3820419"));
Self { address: contract_address, chain: contract_chain }
}
}

impl GCreContract {
impl DeployedContract {
///exchange has been deprecated, use burn instead
fn burn_vested_cc_abi() -> Function {
#[allow(deprecated)]
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn validate_collect_coins(
return Err(VerificationFailureCause::MissingSender.into());
}

let transfer_fn = GCreContract::burn_vested_cc_abi();
let transfer_fn = DeployedContract::burn_vested_cc_abi();
ensure!(!transaction.is_input_empty(), VerificationFailureCause::EmptyInput);

{
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<T: CreditcoinConfig> Pallet<T> {
u_cc: &UnverifiedCollectedCoins,
) -> VerificationResult<T::Balance> {
log::debug!("verifying OCW Collect Coins");
let UnverifiedCollectedCoins { to, tx_id, contract: GCreContract { address, chain } } =
let UnverifiedCollectedCoins { to, tx_id, contract: DeployedContract { address, chain } } =
u_cc;
let rpc_url = &chain.rpc_url()?;
let tx = ocw::eth_get_transaction(tx_id, rpc_url)?;
Expand All @@ -139,9 +139,9 @@ impl<T: CreditcoinConfig> Pallet<T> {

#[cfg(any(test, feature = "runtime-benchmarks"))]
pub(crate) mod testing_constants {
use super::{Blockchain, GCreContract};
use super::{Blockchain, DeployedContract};

pub const CHAIN: Blockchain = GCreContract::DEFAULT_CHAIN;
pub const CHAIN: Blockchain = DeployedContract::DEFAULT_CHAIN;
}

#[cfg(test)]
Expand Down Expand Up @@ -200,7 +200,7 @@ pub(crate) mod tests {
});

pub(crate) static RPC_RESPONSE_AMOUNT: Lazy<sp_core::U256> = Lazy::new(|| {
let transfer_fn = GCreContract::burn_vested_cc_abi();
let transfer_fn = DeployedContract::burn_vested_cc_abi();

let inputs = transfer_fn.decode_input(&(INPUT.0)[4..]).unwrap();

Expand Down Expand Up @@ -282,7 +282,7 @@ pub(crate) mod tests {
receipt: EthTransactionReceipt { status: Some(1u64.into()), ..Default::default() },
transaction,
eth_tip: (base_height + ETH_CONFIRMATIONS),
contract_address: GCreContract::default().address,
contract_address: DeployedContract::default().address,
}
}
}
Expand Down Expand Up @@ -1008,7 +1008,7 @@ pub(crate) mod tests {
let acct_pubkey = ext.generate_authority();
let _auth = AccountId::from(acct_pubkey.into_account().0);
ext.build_and_execute(|| {
let contract = GCreContract {
let contract = DeployedContract {
address: sp_core::H160(hex!("aaaaabbbbbcccccdddddeeeeefffff08F3820419")),
chain: Blockchain::Rinkeby,
};
Expand All @@ -1018,7 +1018,7 @@ pub(crate) mod tests {
));
let from_storage = Creditcoin::<Test>::collect_coins_contract();
assert_eq!(contract, from_storage);
assert_ne!(from_storage, GCreContract::default());
assert_ne!(from_storage, DeployedContract::default());

let (acc, ..) = generate_address_with_proof("somebody");

Expand All @@ -1039,7 +1039,7 @@ pub(crate) mod tests {

#[test]
fn gcrecontract_value_query_is_default() {
let contract = GCreContract::default();
let contract = DeployedContract::default();
let ext = ExtBuilder::default();
ext.build_and_execute(|| {
let value_query = Creditcoin::<Test>::collect_coins_contract();
Expand Down Expand Up @@ -1093,7 +1093,7 @@ pub(crate) mod tests {
let cc = UnverifiedCollectedCoins {
to: addr,
tx_id: TX_HASH.hex_to_address(),
contract: GCreContract::default(),
contract: DeployedContract::default(),
};
assert_matches!(
Creditcoin::<Test>::verify_collect_coins_ocw(&cc),
Expand All @@ -1117,7 +1117,7 @@ pub(crate) mod tests {
let cc = UnverifiedCollectedCoins {
to: addr,
tx_id: TX_HASH.hex_to_address(),
contract: GCreContract::default(),
contract: DeployedContract::default(),
};

let id = TaskV2::<Test>::to_id(&cc);
Expand Down Expand Up @@ -1155,7 +1155,7 @@ pub(crate) mod tests {
let cc = UnverifiedCollectedCoins {
to: addr,
tx_id: TX_HASH.hex_to_address(),
contract: GCreContract::default(),
contract: DeployedContract::default(),
};

let id = TaskV2::<Test>::to_id(&cc);
Expand Down
45 changes: 45 additions & 0 deletions pallets/creditcoin/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
non_paying_error, EVMAddress, PublicToAddress,
},
mock::{RuntimeOrigin as Origin, *},
ocw::tasks::collect_coins::DeployedContract,
types::{DoubleMapExt, OwnershipProof},
AddressId, AskOrder, AskOrderId, BidOrder, BidOrderId, Blockchain, DealOrder, DealOrderId,
DealOrders, Duration, ExternalAddress, ExternalAmount, Guid, Id, LegacySighash, LoanTerms,
Expand Down Expand Up @@ -3236,3 +3237,47 @@ fn register_address_v2_should_error_with_unsupported_blockchain() {
);
});
}

use hex_literal::hex;

#[test]
fn set_burn_gate_contract_should_return_default_goerli_contract_when_not_set() {
ExtBuilder::default().build_and_execute(|| {
let contract: DeployedContract = Creditcoin::gate_contract();

assert_eq!(
contract.address,
sp_core::H160(hex!("a3EE21C306A700E682AbCdfe9BaA6A08F3820419"))
);

assert_eq!(contract.chain, Blockchain::Ethereum);
});
}

#[test]
fn set_burn_gate_contract_fails_with_non_root() {
ExtBuilder::default().build_and_execute(|| {
let acct: AccountId = AccountId::new([0; 32]);
let gate_contract = DeployedContract::default();

assert_noop!(Creditcoin::set_gate_contract(Origin::signed(acct), gate_contract), BadOrigin);
});
}

#[test]
fn set_burn_gate_contract_passes_and_storage_is_updated() {
ExtBuilder::default().build_and_execute(|| {
let fake_address =
sp_core::H160([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);

let gate_contract =
DeployedContract { address: fake_address, chain: Blockchain::Luniverse };

assert_ok!(Creditcoin::set_gate_contract(RawOrigin::Root.into(), gate_contract));

let stored_contract = Creditcoin::gate_contract();

assert_eq!(stored_contract.address, fake_address);
assert_eq!(stored_contract.chain, Blockchain::Luniverse);
});
}
2 changes: 1 addition & 1 deletion pallets/creditcoin/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use collect_coins::{
pub use loan_terms::*;
pub use transfer::*;

use crate::ocw::tasks::collect_coins::GCreContract;
use crate::ocw::tasks::collect_coins::DeployedContract;
use crate::ocw::VerificationFailureCause;
use crate::ocw::VerificationResult;
use extend::ext;
Expand Down
6 changes: 3 additions & 3 deletions pallets/creditcoin/src/types/collect_coins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::{
AddressId, Blockchain, ExternalAddress, ExternalTxId, GCreContract, SystemConfig,
AddressId, Blockchain, DeployedContract, ExternalAddress, ExternalTxId, SystemConfig,
};
use frame_support::RuntimeDebug;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
Expand All @@ -16,15 +16,15 @@ pub struct CollectedCoins<Hash, Balance> {
pub struct UnverifiedCollectedCoins {
pub to: ExternalAddress,
pub tx_id: ExternalTxId,
pub contract: GCreContract,
pub contract: DeployedContract,
}

impl UnverifiedCollectedCoins {
pub fn into_output<T>(self, amount: T::Balance) -> CollectedCoins<T::Hash, T::Balance>
where
T: Config,
{
let Self { to, tx_id, contract: GCreContract { chain, .. } } = self;
let Self { to, tx_id, contract: DeployedContract { chain, .. } } = self;
let to = crate::AddressId::new::<T>(&chain, to.as_slice());
CollectedCoins { amount, to, tx_id }
}
Expand Down
4 changes: 4 additions & 0 deletions pallets/creditcoin/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,8 @@ impl<T: frame_system::Config> crate::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}

fn set_gate_contract() -> Weight {
Weight::from_parts(0,0)
}
}

0 comments on commit 59a7323

Please sign in to comment.