Skip to content

Commit

Permalink
chore(release): 2.2.0 (#141)
Browse files Browse the repository at this point in the history
* feat: add getVariable function

Co-authored-by: Antonis Kogias <tonykogias@gmail.com>
  • Loading branch information
0xGorilla and tonykogias committed Jul 13, 2022
1 parent 66f6c6f commit 466d944
Show file tree
Hide file tree
Showing 11 changed files with 663 additions and 7 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [2.2.0](https://github.com/defi-wonderland/smock/compare/v2.1.0...v2.2.0) (2022-07-12)


### Features

* add getVariable function ([ce79653](https://github.com/defi-wonderland/smock/commit/ce79653d8de40ab3e9bbd537bc002eaa1edd49f9))
* add getVariable function ([ebdd00a](https://github.com/defi-wonderland/smock/commit/ebdd00a4bf8907013d6d8fa9b8f4c601e703535c))
* return hardhat errors on factory failures ([#125](https://github.com/defi-wonderland/smock/issues/125)) ([3b626a3](https://github.com/defi-wonderland/smock/commit/3b626a3891c07ba224d2f12386bae95522843e2b))


### Bug Fixes

* add warning in docs ([71e5a52](https://github.com/defi-wonderland/smock/commit/71e5a52353ac69a3bcc919f2942cbfa4096e1857))
* bug with negative ints ([ed30ea7](https://github.com/defi-wonderland/smock/commit/ed30ea7bb0758d01eb7bce102560aa4c962d27cd))
* delegated calls support ([#132](https://github.com/defi-wonderland/smock/issues/132)) ([812c335](https://github.com/defi-wonderland/smock/commit/812c3354d77603d8a6db172f5297c8e49625a98b))
* duplicated type ([#138](https://github.com/defi-wonderland/smock/issues/138)) ([823b0ea](https://github.com/defi-wonderland/smock/commit/823b0ea7ed422636e4e763576b6975a422728522))
* more fixes ([3650a85](https://github.com/defi-wonderland/smock/commit/3650a856be773d3a5db69c255b45366f04cb9ace))
* resolve some comments & support nested mappings ([5d594d1](https://github.com/defi-wonderland/smock/commit/5d594d1a832c1158552572492b1501d890d3e41b))
* split each type logic into functions ([a50af26](https://github.com/defi-wonderland/smock/commit/a50af2611ac7868f1ed0716cc2db15e79b949a8d))
* typo ([6ace20e](https://github.com/defi-wonderland/smock/commit/6ace20e79a5b5c48fbf7f3662b0118970e683447))

### [2.1.1](https://github.com/defi-wonderland/smock/compare/v2.1.0...v2.1.1) (2022-07-07)


Expand Down
25 changes: 24 additions & 1 deletion docs/source/mocks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,27 @@ Setting the value of multiple variables
[myKey]: 1234
}
})
Getting the value of an internal variable
********************

.. warning::
This is an experimental feature and it does not support multidimensional or packed arrays

.. code-block:: typescript
const myUint256 = await myMock.getVariable('myUint256VariableName');
Getting the value of an internal mapping given the value's key
#######################################

.. code-block:: typescript
const myMappingValue = await myMock.getVariable('myMappingVariableName', [mappingKey]);
Getting the value of an internal nested mapping given the value's keys
#######################################

.. code-block:: typescript
const myMappingValue = await myMock.getVariable('myMappingVariableName', [mappingKeyA, mappingKeyB]);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@defi-wonderland/smock",
"version": "2.1.1",
"version": "2.2.0",
"description": "The Solidity mocking library",
"keywords": [
"ethereum",
Expand Down
3 changes: 3 additions & 0 deletions src/factories/smock-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Observable } from 'rxjs';
import { distinct, filter, map, share, withLatestFrom } from 'rxjs/operators';
import { EditableStorageLogic as EditableStorage } from '../logic/editable-storage-logic';
import { ProgrammableFunctionLogic, SafeProgrammableContract } from '../logic/programmable-function-logic';
import { ReadableStorageLogic as ReadableStorage } from '../logic/readable-storage-logic';
import { ObservableVM } from '../observable-vm';
import { Sandbox } from '../sandbox';
import { ContractCall, FakeContract, MockContractFactory, ProgrammableContractFunction, ProgrammedReturnValue } from '../types';
Expand Down Expand Up @@ -51,8 +52,10 @@ function mockifyContractFactory<T extends ContractFactory>(

// attach to every internal variable, all the editable logic
const editableStorage = new EditableStorage(await getStorageLayout(contractName), vm.getManager(), mock.address);
const readableStorage = new ReadableStorage(await getStorageLayout(contractName), vm.getManager(), mock.address);
mock.setVariable = editableStorage.setVariable.bind(editableStorage);
mock.setVariables = editableStorage.setVariables.bind(editableStorage);
mock.getVariable = readableStorage.getVariable.bind(readableStorage);

// We attach a wallet to the contract so that users can send transactions *from* a watchablecontract.
mock.wallet = await impersonate(mock.address);
Expand Down
40 changes: 40 additions & 0 deletions src/logic/readable-storage-logic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { SmockVMManager } from '../types';
import { fromHexString, remove0x, toFancyAddress, toHexString } from '../utils';
import {
decodeVariable,
getVariableStorageSlots,
SolidityStorageLayout,
StorageSlotKeyTypePair,
StorageSlotKeyValuePair,
} from '../utils/storage';

export class ReadableStorageLogic {
private storageLayout: SolidityStorageLayout;
private contractAddress: string;
private vmManager: SmockVMManager;

constructor(storageLayout: SolidityStorageLayout, vmManager: SmockVMManager, contractAddress: string) {
this.storageLayout = storageLayout;
this.vmManager = vmManager;
this.contractAddress = contractAddress;
}

async getVariable(variableName: string, mappingKeys?: string[] | number[]): Promise<unknown> {
const slots: StorageSlotKeyTypePair[] = await getVariableStorageSlots(
this.storageLayout,
variableName,
this.vmManager,
this.contractAddress,
mappingKeys
);
const slotValueTypePairs: StorageSlotKeyValuePair[] = await Promise.all(
slots.map(async (slotKeyPair) => ({
...slotKeyPair,
value: remove0x(
toHexString(await this.vmManager.getContractStorage(toFancyAddress(this.contractAddress), fromHexString(slotKeyPair.key)))
),
}))
);
return decodeVariable(slotValueTypePairs);
}
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Provider } from '@ethersproject/abstract-provider';
import { Signer } from '@ethersproject/abstract-signer';
import { BaseContract, ContractFactory, ethers } from 'ethers';
import { EditableStorageLogic } from './logic/editable-storage-logic';
import { ReadableStorageLogic } from './logic/readable-storage-logic';
import { WatchableFunctionLogic } from './logic/watchable-function-logic';

type Abi = ReadonlyArray<
Expand Down Expand Up @@ -72,6 +73,7 @@ export type MockContract<T extends BaseContract = BaseContract> = SmockContractB
connect: (...args: Parameters<T['connect']>) => MockContract<T>;
setVariable: EditableStorageLogic['setVariable'];
setVariables: EditableStorageLogic['setVariables'];
getVariable: ReadableStorageLogic['getVariable'];
} & {
[Property in keyof T['functions']]: ProgrammableContractFunction;
};
Expand Down
18 changes: 18 additions & 0 deletions src/utils/hex-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,21 @@ function bitnot(bi: BigInt) {
.join('');
return BigInt('0b' + prefix + bin) + BigInt(1);
}

/**
* XOR operation between 2 Buffers
* Source: https://github.com/crypto-browserify/buffer-xor/blob/master/index.js
* @param a Buffer to XOR
* @param b Buffer is the mask
* @returns hex representation of the big number
*/
export function xor(a: Buffer, b: Buffer) {
var length = Math.max(a.length, b.length);
var buffer = Buffer.allocUnsafe(length);

for (var i = 0; i < length; ++i) {
buffer[i] = a[i] ^ b[i];
}

return buffer;
}

0 comments on commit 466d944

Please sign in to comment.