Skip to content

Commit

Permalink
Merge pull request #20 from HyunggyuJang/refactor/without-external-ca…
Browse files Browse the repository at this point in the history
…ll-pair-for

Make pair_for method internal call
  • Loading branch information
HyunggyuJang authored Oct 24, 2022
2 parents 0267ec8 + 577e066 commit af0bf97
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('ROUTER', () => {
const match = tokenA.contract.address.toString() == token0Address.output.toString()
const token0 = match ? tokenA : tokenB
const token1 = match ? tokenB : tokenA
const router_contract = await setupContract('router_contract', 'new', factory_contract.contract.address)
const router_contract = await setupContract('router_contract', 'new', factory_contract.contract.address, pair_code_hash)

return {
wallet,
Expand Down
3 changes: 2 additions & 1 deletion uniswap-v2/contracts/router/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pub mod router {

impl RouterContract {
#[ink(constructor)]
pub fn new(factory: AccountId) -> Self {
pub fn new(factory: AccountId, pair_code_hash: Hash) -> Self {
ink_lang::codegen::initialize_contract(|instance: &mut Self| {
instance.router.factory = factory;
instance.router.pair_code_hash = pair_code_hash;
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions uniswap-v2/logics/impls/router/data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ink_env::Hash;
use openbrush::traits::AccountId;

pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data);
Expand All @@ -6,4 +7,5 @@ pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data);
#[openbrush::upgradeable_storage(STORAGE_KEY)]
pub struct Data {
pub factory: AccountId,
pub pair_code_hash: Hash,
}
18 changes: 13 additions & 5 deletions uniswap-v2/logics/impls/router/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use crate::{
router::*,
},
};
use ink_env::hash::Blake2x256;
use ink_prelude::{
vec,
vec::Vec,
Expand Down Expand Up @@ -428,17 +429,24 @@ impl<T: Storage<data::Data>> Router for T {
/// Original Uniswap Library pairFor function calculate pair contract address without making cross contract calls.
/// Please refer /~https://github.com/Uniswap/v2-periphery/blob/master/contracts/libraries/UniswapV2Library.sol#L18
///
/// In this contract, use cross contract call to get pair contract address.
/// In this contract, use precomputed address like Uniswap's, as ink!'s deployment is done via create2-like one by default.
/// Please refer /~https://github.com/paritytech/substrate/blob/493b58bd4a475080d428ce47193ee9ea9757a808/frame/contracts/src/lib.rs#L178
/// for how contract's address is calculated.
default fn _pair_for(
&self,
factory: AccountId,
token_a: AccountId,
token_b: AccountId,
) -> Result<AccountId, RouterError> {
let (token_0, token_1) = self._sort_tokens(token_a, token_b)?;
let pair =
FactoryRef::get_pair(&factory, token_0, token_1).ok_or(RouterError::PairNotFound)?;
Ok(pair)
let tokens = self._sort_tokens(token_a, token_b)?;
let salt = &Self::env().hash_encoded::<Blake2x256, _>(&tokens)[..4];
let input: Vec<_> = AsRef::<[u8]>::as_ref(&factory)
.iter()
.chain(self.data().pair_code_hash.as_ref())
.chain(salt)
.cloned()
.collect();
Ok(Self::env().hash_bytes::<Blake2x256>(&input).into())
}

default fn _sort_tokens(
Expand Down

0 comments on commit af0bf97

Please sign in to comment.