forked from defi-wonderland/smock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mocks can now edit internal variables (defi-wonderland#7)
* fix: mock created from mock factory * feat: editable storage * docs: updated docs
- Loading branch information
Showing
15 changed files
with
463 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { LoptVMManager } from '../types'; | ||
import { convertToStorageSlots, fromHexString, toFancyAddress } from '../utils'; | ||
|
||
export class EditableStorageLogic { | ||
private storageLayout: any; | ||
private contractAddress: string; | ||
private vmManager: LoptVMManager; | ||
|
||
constructor(storageLayout: any, vmManager: LoptVMManager, contractAddress: string) { | ||
this.storageLayout = storageLayout; | ||
this.vmManager = vmManager; | ||
this.contractAddress = contractAddress; | ||
} | ||
|
||
async setVariable(variableName: string, value: any) { | ||
if (value === undefined || value === null) return; | ||
|
||
const slots = convertToStorageSlots(this.storageLayout, variableName, value); | ||
|
||
for (const slot of slots) { | ||
await this.vmManager.putContractStorage( | ||
toFancyAddress(this.contractAddress), | ||
fromHexString(slot.hash.toLowerCase()), | ||
fromHexString(slot.value) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,33 @@ | ||
import { BigNumber } from 'ethers'; | ||
import { isPojo } from './serdes'; | ||
|
||
const timeWords = [null, 'once', 'twice', 'thrice']; | ||
export function humanizeTimes(count: number) { | ||
return timeWords[count] || `${count || 0} times`; | ||
} | ||
|
||
/** | ||
* Custom object flatten | ||
* | ||
* @param obj Object to flatten. | ||
* @param prefix Current object prefix (used recursively). | ||
* @param result Current result (used recursively). | ||
* @returns Flattened object. | ||
*/ | ||
export function flatten(obj: any, prefix: string = '', result: any = {}): Object { | ||
return Object.entries(obj).reduce((acc, [key, val]) => { | ||
const subKey = `${prefix}${key}`; | ||
|
||
if (BigNumber.isBigNumber(val) || typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean') { | ||
result[subKey] = val; | ||
} else if (Array.isArray(val)) { | ||
val.forEach((valItem, index) => flatten(valItem, !prefix ? `${index}` : `${prefix}${index}.`, acc)); | ||
} else if (isPojo(val)) { | ||
flatten(val, `${subKey}.`, acc); | ||
} else { | ||
throw new Error('Cannot flatten unsupported object type'); | ||
} | ||
|
||
return acc; | ||
}, result); | ||
} |
Oops, something went wrong.