capsule-corp-ternoa / ternoa-js

Ternoa JavaScript SDK to build on top of the Ternoa Chain ⚙️
https://docs.ternoa.network
Apache License 2.0
24 stars 10 forks source link

Same functions are called multiple times for a simple createNFT call #21

Closed markopoloparadox closed 2 years ago

markopoloparadox commented 2 years ago

@Leouarz @peshwar9 @ipapandinas This is going to be a long post but I hope that in the end you will realize that our current architecture is not optimal at all.

Let's start with the fact that the user is not dumb and that he will take all the necessary steps to ensure that when he calls something that it will succeed. With this in mind, we have the following counters: BalanceCheckCounter, DataCheckCounter, GetRawApiCounter.

We start with the balance and data check counters set to 1 (because of my previous statement) and raw api counter set to 0.

BalanceCheckCounter: 1
DataCheckCounter: 1
GetRawApiCounter: 0

createNft

What he calls next is the createNft function which looks like this: image

Inside the createNFT function we again do the same checks that we did before. Here we have one additional balance check (checkBalanceToMintNft) and two additional data checks (checkNftOffchainDataLimit, getCollectionData).

Besides that, checkBalanceToMintNft calls GetRawApi 2 times, checkNftOffchainDataLimit calls it once, and getCollectionData call it once too.

Current statistics:

BalanceCheckCounter: 2
DataCheckCounter: 3
GetRawApiCounter: 4

runTx

The runTx is a bit complicated because it has a lot of nested calls. image

But in a nutshell: createTxHex function calls GetRawApi 2 times, signTx calls it once.

Current statistics:

BalanceCheckCounter: 2
DataCheckCounter: 3
GetRawApiCounter: 7

submitTx

This is our last call and this function is simillar to runTx becuase it has a lot of nested call. image

But in a nutshell: checkFundsForTxFees function does the balance check once, three data checks and it calls GetRawApi 5 times. the top level functions call GetRawApi once

Total statistics:

BalanceCheckCounter: 3
DataCheckCounter: 6
GetRawApiCounter: 13

Question

Is it really worth it to have such a "simple" API?

Leouarz commented 2 years ago

I guess the data checks and the balance checks can be leverage to the user. The function should not be called blindy.

Concerning the getRawApi(), the function leverage the initialisation of the inner polkadot api instance to the sdk instead of the user. After the first getRawApi() call, the function only returns the api, not that different from self.rawApi. What we can do about that is either :

markopoloparadox commented 2 years ago

None of our functions should do any checks. Doing checks just makes things more complicated, slower and causes more problems.

Our API is a abstraction over the PolkadotJS API and it needs to be an zero-overhead one.

The zero-overhead principle is a C++ design principle that states: You don't pay for what you don't use. What you do use is just as efficient as what you could reasonably write by hand.