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

Improvment for custum CallBuilder implementation #10

Merged
merged 14 commits into from
Aug 4, 2024

Conversation

atoumbre
Copy link
Contributor

We add some methods and functions that allow the implementation of custom method calls for CallBuilder and give more flexibility to interact with underlying structures like the Simulator and ManifestBuilders

TestEngine

  • Add with_simulator to TestEngine for direction action on the underlying simulator
  • Add new_with_custom_genesis function to instantiate TestEngine with a CustonGenesis

CallBuilder

  • Add with_test_engine to be able to have access to the test_engine while building a call
  • Add with_manifest_builder to be able to perform custom actions on the underlying ManifestBuilder

We all so made some adjustments to the Environments enum to have the ability to set the method parameters consistently.

Environnements

  • Merge of Fungible and FungibleAll as well as NonFungible and NonFungibleAll.
  • Dedicated ProofOf type for proof.

There are also some small features added like print tx fee to std output and tx output title.

example:

pub trait LendingMethods<'a> {

  fn weft_take_flash_loan(self, flash_loan: IndexMap<&str, Decimal>) -> CallBuilder<'a>;

  fn weft_repay_flash_loan(self, flash_loan: EnvVec, loan_terms: Fungible<&'static str>) -> CallBuilder<'a>;

}

impl<'a> LendingMethods<'a> for CallBuilder<'a> {

  fn weft_take_flash_loan(mut self, flash_loan: IndexMap<&str, Decimal>) -> Self {
    let flash_loan_map = flash_loan
      .into_iter()
      .map(|(k, v)| (self.with_test_engine(|cb| cb.get_resource(k)), v))
      .collect::<IndexMap<_, _>>();

    self
      .call_from(LENDING_POOL, TAKE_FLASH_LOAN, env_args!(flash_loan_map))
      .with_log_title(TAKE_FLASH_LOAN)
      .with_log_tx_fee()
  }

  fn weft_repay_flash_loan(self, payment: EnvVec, loan_terms: Fungible<&'static str>) -> CallBuilder<'a> {
    self
      .call_from(LENDING_POOL, REPAY_FLASH_LOAN, env_args!(payment, loan_terms))
      .with_log_title(REPAY_FLASH_LOAN)
      .with_log_tx_fee()
  }

}

#[test]
pub fn valid_flash_loan() {

  test_engine
    .weft_take_flash_loan(indexmap![
    "xrd" =>  dec!(1000),
    "usdc"  => dec!(100)
    ])
    .withdraw("usdc", dec!(50))
    .withdraw("xrd", dec!(50))
    .weft_repay_flash_loan(
      env_vec![Fungible::FromWorkTop("xrd", dec!(1000))],
      Fungible::FromWorkTop("pool_loan_term", Decimal::ONE),
    )
    .weft_repay_flash_loan(
      env_vec![Fungible::FromWorkTop("usdc", dec!(100))],
      Fungible::FromWorkTop("pool_loan_term", Decimal::ONE),
    )
    .weft_repay_flash_loan(
      env_vec![
        Fungible::FromWorkTop("xrd", Decimal::ONE),
        Fungible::FromWorkTop("usdc", Decimal::ONE)
      ],
      Fungible::FromWorkTop("pool_loan_term", Decimal::ONE),
    )
    .output("output/flash_loan", "flash_loan")
    .execute_and_expect_success();

}

@arthurvinci arthurvinci merged commit 0a238c0 into BeakerTools:develop Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants