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

feat: add getVariable function #134

Merged
merged 9 commits into from
Jul 12, 2022

Conversation

tonykogias
Copy link
Contributor

Description
This commit introduces a new function for mock contracts. With getVariable you can view an internal variable's value.

Approach
A rough idea of how I approached this problem is:

  1. I get an array of slot keys using getVariableStorageSlots that represent the position of the variable's value.
  2. I get the values that are stored in those slots using the vmManager.getContractStorage function and providing the contract address and the appropriate slot key.
  3. I decoded the values using the decodeVariable function and providing all the values from the above step which results in the variable's value

Metadata

This commit introduces a new function for mock contracts. With
`getVariable` you can view an internal variable's value.
src/utils/storage.ts Outdated Show resolved Hide resolved
src/utils/storage.ts Outdated Show resolved Hide resolved
src/utils/storage.ts Outdated Show resolved Hide resolved
src/utils/storage.ts Outdated Show resolved Hide resolved
Copy link
Member

@wei3erHase wei3erHase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yo @tonykogias, i'm very pleased to be reading this PR 😍
although the very precise commenting, got quite lost on the deep TS/EVM logic, but the tests seem to be working fine, so congrats for that gud ser 👏
made a few comments, will probably give a further review w/ @0xGorilla before approving 🚀
looks like it was way harder than it initially appeared, but you definitely have found the spot

oh this fell, i guess it's yours, KING
{\__/}
( • . •)
/ > 👑

src/utils/storage.ts Outdated Show resolved Hide resolved
src/utils/storage.ts Outdated Show resolved Hide resolved
@tonykogias tonykogias marked this pull request as ready for review July 11, 2022 12:02
.. code-block:: typescript

const myMappingValue = await myMock.getVariable('myMappingVariableName', [mappingKeyA, mappingKeyB]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's please add a warning, mentioning the things that don't work. Will links to PRs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Member

@0xGorilla 0xGorilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely 🔥, well done

@0xGorilla 0xGorilla merged commit ce79653 into defi-wonderland:dev Jul 12, 2022
@ddnexus
Copy link

ddnexus commented Aug 2, 2022

This is great and in regular contract deployment it works, however if I try to use it with an OZ UUPS proxy I get:

getVariable is not a function

The contract must be deployed using the upgrades.deployProxy() from the OZ upgrades HH plugin, and I managed to get no complaints from typescript:

const ProxyMock = await smock.mock("MyContract");
const proxyMock = await upgrades.deployProxy(ProxyMock as MockContractFactory<MyContract__factory> & MyContract__factory, [...]) as MyContract & MockContract<MyContract>;

However that does not seem to help. What did I miss?

Is there any possibility to pass a contract instance or a contract factory instead a contractName to smock.moch... Like what we can do with the smock.fake?

Currently I need to use a preprocessor to conditionally add a bunch of accessors to the contract just to handle the private variables for unit testing. The getVariable/setVariable would get rid of all that mess!

Is there any work around I could try?

Thanks

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.

Add a way to view internal variables value
4 participants