Closed nvergez closed 2 weeks ago
What about addKvs
? Also, do you have concrete use cases where you need to add kvs?
Yes addKvs
why not!
For example I used a lot the helper giveTokensTo
at the start of specific tests where the wallet is already created during a beforeEach
Why not directly adding the tokens?
In order to confirm the use case, could you provide a sample code of your use case? Give a configuration where having such helper would have helped.
A use case:
I have a Minter smart contract that allows users to mint NFTs. I have a staking smart contract that allows user to stake the NFTs and get rewards. I have a pool rewards smart contract that owns all the rewards. A user can mint an NFT using his staking rewards (without even claiming them).
I want to test only my minter smart contract, so I need to "mock" the staking contract and pool rewards to be able to test the mint logic:
test("Mint 1 NFT with only rewards", async () => {
const rewardsAmount = 1_000_000n;
await stakingContract.setAccount({
...(await stakingContract.getAccountWithoutKvs()),
kvs: [
await stakingContract.getAccountKvs(),
e.kvs.Mapper("rewards_by_user", e.Usize(1)).Value(e.U(rewardsAmount)),
],
});
await poolContract.setAccount({
...(await poolContract.getAccountWithoutKvs()),
kvs: [
await poolContract.getAccountKvs(),
e.kvs.Esdts([
{
id: tokenId,
amount: rewardsAmount,
},
]),
],
});
// Mint
// Assert everything needed
});
I could just add those Kvs at the initialization of the contract, but it won't let me easily strictly assert my kvs in each test.
Design chosen:
We add the following methods to Contract
and Wallet
classes, and the equivalent to World
and Proxy
classes:
addKvs(kvs: EncodableKvs);
addEsdts(esdts: Array<EncodableEsdt>);
addMappers(mappers: Array<EncodableMapper>);
To be able to add Kvs to an existing account, we need to do something like this:
which is not very readable nor intuitive.
Or create this kind of helper function:
We could add an
appendKvs
method to theProxy
andWorld
classes:And to the
Contract
andWallet
classes:We can discuss about the
options
parameter. But I think it's important to let people choose if they want to overwrite the existing Kvs or not. I think the default behavior should be to overwrite the existing Kvs.We can also discuss about the naming:
appendKvs
,appendAccountKvs
,appendAccount
, ...Example usage: