| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941 |
- // SPDX-License-Identifier: Apache-2.0
- use std::ops::Mul;
- use contract_transcode::ContractMessageTranscoder;
- use hex::FromHex;
- use parity_scale_codec::{Decode, DecodeAll, Encode, Input};
- use rand::Rng;
- use sp_core::{
- crypto::{AccountId32, Ss58Codec},
- hexdisplay::AsBytesRef,
- keccak_256, U256,
- };
- use crate::{Contract, DeployContract, Execution, ReadContract, WriteContract, API};
- #[ignore = "trapped when transfer liquidity"]
- #[tokio::test]
- async fn mint() -> anyhow::Result<()> {
- let api = API::new().await?;
- let w = MockWorld::init(&api).await?;
- let min_liquidity: U256 = U256::from(1000_u32);
- w.token_0
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = keccak_256(b"transfer(address,uint)")[..4].to_vec();
- w.pair.address.clone().unwrap().encode_to(&mut s);
- U256::from(10_u8).pow(18_u8.into()).encode_to(&mut s);
- s
- },
- )
- .await?;
- w.token_1
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = keccak_256(b"transfer(address,uint)")[..4].to_vec();
- w.pair.address.clone().unwrap().encode_to(&mut s);
- U256::from(10_u8)
- .pow(18_u8.into())
- .mul(U256::from(4_u8))
- .encode_to(&mut s);
- s
- },
- )
- .await?;
- let expected_liquidity = U256::from(10_u8).pow(18_u8.into()).mul(U256::from(2_u8));
- w.pair
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "mint",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await?;
- let total_supply = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(expected_liquidity, total_supply);
- let balance = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(balance, expected_liquidity - min_liquidity);
- let balance_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(balance_0, U256::from(10_u8).pow(18_u8.into()));
- let balance_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(
- balance_1,
- U256::from(10_u8).pow(18_u8.into()).mul(U256::from(4_u8))
- );
- let (r0, r1, block) = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("getReserves", []).unwrap(),
- )
- .await
- .and_then(|v| <(u128, u128, u32)>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(U256::from(r0), U256::from(10_u8).pow(18_u8.into()));
- assert_eq!(
- U256::from(r1),
- U256::from(10_u8).pow(18_u8.into()).mul(U256::from(4_u8))
- );
- Ok(())
- }
- #[ignore = "trapped when adding liquidity"]
- #[tokio::test]
- async fn swap_token0() -> anyhow::Result<()> {
- let api = API::new().await?;
- let token0_amount = U256::from(10_u8)
- .pow(U256::from(18_u8))
- .mul(U256::from(5_u8));
- let token1_amount = U256::from(10_u8).pow(U256::from(19_u8));
- let w = MockWorld::init(&api).await?;
- let min_liquidity: U256 = U256::from(1000_u32);
- w.add_liquitity(&api, &token0_amount, &token1_amount)
- .await?;
- let swap_amount = U256::from(10_u8).pow(18_u8.into());
- let expected_output = U256::from_dec_str("1662497915624478906")?;
- w.token_0
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = t
- .encode::<_, String>(
- "transfer",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap();
- swap_amount.encode_to(&mut s);
- s
- },
- )
- .await?;
- w.pair
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = t.encode::<_, String>("swap", []).unwrap();
- (
- U256::zero(),
- expected_output,
- sp_keyring::AccountKeyring::Alice.to_account_id(),
- "",
- )
- .encode_to(&mut s);
- s
- },
- )
- .await?;
- let out = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("getReserves", []).unwrap(),
- )
- .await
- .and_then(|v| {
- let t = &w.pair.transcoder;
- <(u128, u128, u32)>::decode(&mut &v[..]).map_err(Into::into)
- })?;
- assert_eq!(U256::from(out.0), token0_amount + swap_amount);
- assert_eq!(U256::from(out.1), token1_amount - expected_output);
- let bal_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(bal_0, token0_amount + swap_amount);
- let bal_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(bal_1, token1_amount - expected_output);
- let supply_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let supply_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let wallet_balance_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let wallet_balance_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(wallet_balance_0, supply_0 - token0_amount - swap_amount,);
- assert_eq!(wallet_balance_1, supply_1 - token1_amount + expected_output);
- Ok(())
- }
- #[ignore = "trapped when adding liquidity"]
- #[tokio::test]
- async fn swap_token1() -> anyhow::Result<()> {
- let api = API::new().await?;
- let token0_amount = U256::from(10_u8)
- .pow(U256::from(18_u8))
- .mul(U256::from(5_u8));
- let token1_amount = U256::from(10_u8).pow(U256::from(19_u8));
- let w = MockWorld::init(&api).await?;
- let min_liquidity: U256 = U256::from(1000_u32);
- w.add_liquitity(&api, &token0_amount, &token1_amount)
- .await?;
- let swap_amount = U256::from(10_u8).pow(18_u8.into());
- let expected_output = U256::from_dec_str("453305446940074565")?;
- w.token_1
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = t
- .encode::<_, String>(
- "transfer",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap();
- swap_amount.encode_to(&mut s);
- s
- },
- )
- .await?;
- w.pair
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = t.encode::<_, String>("swap", []).unwrap();
- (
- expected_output,
- U256::zero(),
- sp_keyring::AccountKeyring::Alice.to_account_id(),
- "",
- )
- .encode_to(&mut s);
- s
- },
- )
- .await?;
- let out = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("getReserves", []).unwrap(),
- )
- .await
- .and_then(|v| <(u128, u128, u32)>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(U256::from(out.0), token0_amount - expected_output);
- assert_eq!(U256::from(out.1), token1_amount + swap_amount);
- let bal_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(bal_0, token0_amount - expected_output);
- let bal_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(bal_1, token1_amount + swap_amount);
- let supply_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let supply_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let wallet_balance_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let wallet_balance_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(wallet_balance_0, supply_0 - token0_amount + expected_output);
- assert_eq!(wallet_balance_1, supply_1 - token1_amount - swap_amount);
- Ok(())
- }
- #[ignore = "trapped when adding liquidity"]
- #[tokio::test]
- async fn burn() -> anyhow::Result<()> {
- let api = API::new().await?;
- let w = MockWorld::init(&api).await?;
- let token_amount = U256::from(10_u8).pow(18_u8.into()).mul(U256::from(3_u8));
- w.add_liquitity(&api, &token_amount, &token_amount).await?;
- w.pair
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = t
- .encode::<_, String>(
- "transfer",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap();
- (token_amount - U256::from(1000_u32)).encode_to(&mut s);
- s
- },
- )
- .await?;
- w.pair
- .call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "burn",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await?;
- let wallet_balance_0 = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(wallet_balance_0, U256::zero());
- let pair_supply = w
- .pair
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(pair_supply, U256::from(1000_u32));
- let pair_balance_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(pair_balance_0, U256::from(1000_u32));
- let pair_balance_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(w.pair.address.as_ref().unwrap())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(pair_balance_1, U256::from(1000_u32));
- let supply_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let supply_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("totalSupply", []).unwrap(),
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let wallet_balance_0 = w
- .token_0
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- let wallet_balance_1 = w
- .token_1
- .try_call(
- &api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "balanceOf",
- [format!(
- "0x{}",
- hex::encode(sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <U256>::decode(&mut &v[..]).map_err(Into::into))?;
- assert_eq!(wallet_balance_0, supply_0 - U256::from(1000_u32));
- assert_eq!(wallet_balance_1, supply_1 - U256::from(1000_u32));
- Ok(())
- }
- struct MockWorld {
- factory: Contract,
- pair: Contract,
- token_0: Contract,
- token_1: Contract,
- }
- impl MockWorld {
- async fn init(api: &API) -> anyhow::Result<Self> {
- let mut factory = Contract::new("./contracts/UniswapV2Factory.contract")?;
- factory
- .deploy(
- api,
- sp_keyring::AccountKeyring::Alice,
- 10_u128.pow(16),
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "new",
- [format!(
- "0x{}",
- hex::encode(&sp_keyring::AccountKeyring::Alice.to_account_id())
- )],
- )
- .unwrap()
- },
- )
- .await?;
- let mut pair = Contract::new("./contracts/UniswapV2Pair.contract")?;
- factory
- .upload_code(api, sp_keyring::AccountKeyring::Alice)
- .await?;
- let mut token_a = Contract::new("./contracts/UniswapV2ERC20.contract")?;
- token_a
- .deploy(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut selector = t.encode::<_, String>("new", []).unwrap();
- U256::from(10_u8).pow(22_u8.into()).encode_to(&mut selector);
- selector
- },
- )
- .await?;
- let mut token_b = Contract::new("./contracts/UniswapV2ERC20.contract")?;
- token_b
- .deploy(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut selector = t.encode::<_, String>("new", []).unwrap();
- U256::from(10_u8).pow(22_u8.into()).encode_to(&mut selector);
- selector
- },
- )
- .await?;
- factory
- .call(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "createPair",
- [
- format!("0x{}", hex::encode(token_a.address.as_ref().unwrap())),
- format!("0x{}", hex::encode(token_b.address.as_ref().unwrap())),
- ],
- )
- .unwrap()
- },
- )
- .await?;
- let pair_addr = factory
- .try_call(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- t.encode::<_, String>(
- "getPair",
- [
- format!("0x{}", hex::encode(token_a.address.as_ref().unwrap())),
- format!("0x{}", hex::encode(token_b.address.as_ref().unwrap())),
- ],
- )
- .unwrap()
- },
- )
- .await
- .and_then(|v| <AccountId32>::decode(&mut &v[..]).map_err(Into::into))?;
- pair = pair.from_addr(pair_addr)?;
- let token_0_addr = pair
- .try_call(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| t.encode::<_, String>("token0", []).unwrap(),
- )
- .await
- .and_then(|v| <AccountId32>::decode(&mut &v[..]).map_err(Into::into))?;
- let (token_0, token_1) = if *token_a.address.as_ref().unwrap() == token_0_addr {
- (token_a, token_b)
- } else {
- (token_b, token_a)
- };
- Ok(Self {
- factory,
- pair,
- token_0,
- token_1,
- })
- }
- async fn add_liquitity(
- &self,
- api: &API,
- amount_a: &U256,
- amount_b: &U256,
- ) -> anyhow::Result<()> {
- self.token_0
- .call(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = keccak_256(b"transfer(address,uint)")[..4].to_vec();
- self.pair.address.clone().unwrap().encode_to(&mut s);
- amount_a.encode_to(&mut s);
- s
- },
- )
- .await?;
- self.token_1
- .call(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = keccak_256(b"transfer(address,uint)")[..4].to_vec();
- self.pair.address.clone().unwrap().encode_to(&mut s);
- amount_b.encode_to(&mut s);
- s
- },
- )
- .await?;
- self.pair
- .call(
- api,
- sp_keyring::AccountKeyring::Alice,
- 0,
- &|t: &ContractMessageTranscoder| {
- let mut s = keccak_256(b"mint(address)")[..4].to_vec();
- sp_keyring::AccountKeyring::Alice
- .to_account_id()
- .encode_to(&mut s);
- s
- },
- )
- .await?;
- Ok(())
- }
- }
|