# Handling Nested Smart Contract Dependencies in TypeScript SDK Calls

**URL:** <https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590>\
**Category:** TypeScript SDK\
**Created:** [November 9, 2023, 1:09pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590 "2023-11-09T13:09:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sway](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sway/32/103_2.png) [@sway](https://forum.fuel.network/u/sway)\
**Post date:** [November 9, 2023, 1:09pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/1 "2023-11-09T13:09:43Z")

</div>

Hey everyone, I’m in the process of building a project that involves a system of 5 interconnected smart contracts. Previously, when I worked on a lending protocol project, I had to instantiate the dependent Oracle contract in my frontend code to make function calls, like so:

```ts
// Example code snippet from a previous project
const { addressInput, provider } = this.rootStore.accountStore;
if (addressInput == null || provider == null) return;
const { priceOracle } = this.rootStore.settingsStore.currentVersionConfig;
const oracle = new Contract(priceOracle, OracleAbi__factory.abi, provider);
const { value } = await marketContract.functions
  .available_to_borrow(addressInput)
  .addContracts([oracle])
  .simulate();

```

My question is, for a system with multiple layers of contract dependencies, is it necessary to instantiate and pass all related contracts when making calls through the TypeScript SDK? Or is there a streamlined way to manage these nested dependencies? Would love to get insights into how others are handling this scenario.

I have found [this doc](https://docs.fuel.network/docs/fuels-ts/program/multicallinvocationscope/#addcontracts), but it doesn’t say much

---

<div class="post-metadata">

**Author:** ![sandusky](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sandusky/32/1313_2.png) [@sandusky](https://forum.fuel.network/u/sandusky)\
**Post date:** [November 9, 2023, 1:44pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/2 "2023-11-09T13:44:44Z")

</div>

Hi there @sway!  
This [example](https://github.com/FuelLabs/fuels-ts/blob/master/apps/docs-snippets/src/guide/contracts/multicalls.test.ts#L50) might be helpful.

```auto
  const { minGasPrice } = provider.getGasConfig();

    const { value: results } = await counterContract
      .multiCall([
        counterContract.functions.get_count(),
        counterContract.functions.increment_count(2),
        counterContract.functions.increment_count(4),
      ])
      .txParams({ gasPrice: minGasPrice })
      .call();

    const initialValue = new BN(results[0]).toNumber();
    const incrementedValue1 = new BN(results[1]).toNumber();
    const incrementedValue2 = new BN(results[2]).toNumber();

    expect(incrementedValue1).toEqual(initialValue + 2);
    expect(incrementedValue2).toEqual(incrementedValue1 + 4);
    // #endregion multicall-1

```

Let me know if this helps!

---

<div class="post-metadata">

**Author:** ![anderson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/anderson/32/320_2.png) [@anderson](https://forum.fuel.network/u/anderson)\
**Post date:** [November 9, 2023, 2:04pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/3 "2023-11-09T14:04:13Z")

</div>

@sway You’ll also find more examples in this [page](https://docs.fuel.network/docs/fuels-ts/contracts/multicalls/).

---

<div class="post-metadata">

**Author:** ![sway](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sway/32/103_2.png) [@sway](https://forum.fuel.network/u/sway)\
**Post date:** [November 13, 2023, 2:19pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/4 "2023-11-13T14:19:00Z")

</div>

Hi all, I think there was a bit of confusion regarding my earlier query. To clarify, I’m not focusing on multi-call scenarios but rather on the correct approach to including dependent smart contracts in a function call via TypeScript SDK.

In a previous project, as illustrated in my example, I had to use the `addContract` method, passing the oracle instance to make the function call successfully. Without this, the requests would fail. Here’s the relevant snippet for reference:

```javascript
// Example from a previous project
const { addressInput, provider } = this.rootStore.accountStore;
if (addressInput == null || provider == null) return;
const { priceOracle } = this.rootStore.settingsStore.currentVersionConfig;
const oracle = new Contract(priceOracle, OracleAbi__factory.abi, provider);
const { value } = await marketContract.functions
  .available_to_borrow(addressInput)
  .addContracts([oracle])
  .simulate();

```

My current project involves a system of 5 interlinked smart contracts, where one contract might call functions of others in a nested manner (e.g., Contract 1 interacts with Contracts 2 and 3, and Contract 3 might call Contract 4). My question is: In such a setup with nested dependencies, do I need to pass instances of all related smart contracts when making a call? How should I handle these dependencies in TypeScript SDK to ensure successful transaction simulations? Any insights or best practices would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![sandusky](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sandusky/32/1313_2.png) [@sandusky](https://forum.fuel.network/u/sandusky)\
**Post date:** [November 26, 2023, 5:38pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/5 "2023-11-26T17:38:45Z")

</div>

Hi there!

Sorry for the delay, I was OOO during devconnect.  
Thanks for providing more context on your question.

> In such a setup with nested dependencies, do I need to pass instances of all related smart contracts when making a call?

Yes, every instance that is going to be invocated must be added to the call context.

> How should I handle these dependencies in TypeScript SDK to ensure successful transaction simulations?

[This example](https://github.com/FuelLabs/fuels-ts/blob/master/apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts) might be useful

> [https://github.com/FuelLabs/fuels-ts/blob/master/apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts](https://github.com/FuelLabs/fuels-ts/blob/master/apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts)

No insights on my side, however, I’ve requested some help from the Fuels-TS team. Expect more information soon.

Let me know if this solves your question 🌴

---

<div class="post-metadata">

**Author:** ![anderson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/anderson/32/320_2.png) [@anderson](https://forum.fuel.network/u/anderson)\
**Post date:** [November 27, 2023, 11:27am UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/6 "2023-11-27T11:27:11Z")

</div>

@sway Yes, every time you call a function on a contract that interacts with _ **other** _ Contracts, you’ll have to reference that _ **other** _ contract just as you did in your last example.

You could have individual files, one per contract, exporting a ready-to-use Contract instance. Then, whenever you’d do a call, you could import the related contract and pass it along.

```ts
import { marketContract } from './market.ts';
import { oracleContract } from './oracle.ts';

const { value } = await marketContract.functions
  .available_to_borrow(addressInput)
  .addContracts([oracleContract])
  .simulate();

```

---

<div class="post-metadata">

**Author:** ![sway](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sway/32/103_2.png) [@sway](https://forum.fuel.network/u/sway)\
**Post date:** [November 27, 2023, 6:16pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/7 "2023-11-27T18:16:20Z")

</div>

thanks, We have checked in real code already)

---

<div class="post-metadata">

**Author:** ![sandusky](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sandusky/32/1313_2.png) [@sandusky](https://forum.fuel.network/u/sandusky)\
**Post date:** [December 5, 2023, 10:04pm UTC](https://forum.fuel.network/t/handling-nested-smart-contract-dependencies-in-typescript-sdk-calls/3590/8 "2023-12-05T22:04:42Z")

</div>


