|
1 | | -# erc-1167-minimal-proxy-contract-example |
| 1 | +# ERC-1167: Minimal Proxy Contract Example |
| 2 | + |
2 | 3 | An example of a contract that implements ERC-1167: Minimal Proxy |
| 4 | + |
| 5 | +__Setup commands__ |
| 6 | + |
| 7 | +For background information, these are the commands used to setup this project initially |
| 8 | + |
| 9 | +``` |
| 10 | +npm i -g truffle |
| 11 | +npm install @openzeppelin/upgrades |
| 12 | +truffle init |
| 13 | +truffle create migration store |
| 14 | +truffle create migration storeFactory |
| 15 | +``` |
| 16 | + |
| 17 | +__Try out this example__ |
| 18 | + |
| 19 | +To run this example on your local machine, you need to clone this repo(!) and then follow the below instructions. |
| 20 | + |
| 21 | +Start up a new truffle console session in your terminal using `truffle console` then, in the session, compile and deploy the Store and StoreFactory contracts. In order to test this out in the Tuffle Console we essentially need to: |
| 22 | + |
| 23 | +1. Compile the contracts |
| 24 | +1. Migrate the contracts |
| 25 | +1. Get an instance of the StoreFactory |
| 26 | +1. Verify the `impl` address for the implemtation of the Store Contract has been set correctly |
| 27 | +1. Check the deployed contract addresses using the `networks` command |
| 28 | +1. Now for the fun stuff! Lets clone the Store implementation using the StoreFactory contract. |
| 29 | +1. Fetch the result of the `ProxyCreated` event. This event is defined in the [OpenZeppelin ProxyFactory](https://github.com/OpenZeppelin/openzeppelin-sdk/blob/master/packages/lib/contracts/upgradeability/ProxyFactory.sol) contract that we are using here. Its fired any time we call the `deployMinimal` of the `ProxyFactory` and it includes one property: __proxy__ which is the address of the new cloned proxy contract. |
| 30 | +1. Get an instance of the proxy contract from the proxy address returned in the `ProxyCreated` event. |
| 31 | +1. Check the value of the store (it should be empty) |
| 32 | +1. Set the value of the store to something |
| 33 | +1. Check the value of the store again (it should be set to the new value) |
| 34 | +1. Now clone 2 more proxy contracts |
| 35 | +1. Repeat the above and set the value to different strings in each proxy contract |
| 36 | +1. Confirm all the value data strings are different and set correctly! |
| 37 | + |
| 38 | +``` |
| 39 | +compile |
| 40 | +migrate --reset |
| 41 | +sf = await StoreFactory.deployed() |
| 42 | +sf.impl() // This should be the same address as the deployed Store contract! |
| 43 | +networks // Lets verify the contract deployed addresses |
| 44 | +sf.cloneStore() // This returns the transaction receipt |
| 45 | +sf.getPastEvents('ProxyCreated') // This will return the event that contains the address of the cloned contract instance |
| 46 | +store1 = await Store.at('0x4644f030a4713387d9D02d1662Fb4e1a9A385dcb') // use the address in the returnValues 'proxy' of the ProxyCreated event |
| 47 | +store1.value() // returns '' |
| 48 | +store1.setValue('My First Store') |
| 49 | +store1.value() // returns 'My First Store' |
| 50 | +
|
| 51 | +// Lets create two more store proxies! |
| 52 | +
|
| 53 | +sf.cloneStore() |
| 54 | +sf.cloneStore() |
| 55 | +
|
| 56 | +sf.getPastEvents('ProxyCreated', {fromBlock: 0}) |
| 57 | +
|
| 58 | +store2 = await Store.at('0xE872f5BEaEAfA067Eba935f20f08F84F928354C5') |
| 59 | +store3 = await Store.at('0x8871Ba7F9002287c4A40c2cce036daB7a57D2742') |
| 60 | +
|
| 61 | +store2.setValue('My Second store') |
| 62 | +store3.setValue('My Third store') |
| 63 | +
|
| 64 | +// Confirm all values are set as expected! |
| 65 | +store1.value() // returns 'My First Store' |
| 66 | +store2.value() // returns 'My Second Store' |
| 67 | +store3.value() // returns 'My Third Store' |
| 68 | +``` |
| 69 | + |
| 70 | +Versions used: |
| 71 | + |
| 72 | +``` |
| 73 | +Truffle v5.1.32 (core: 5.1.32) |
| 74 | +Solidity v0.5.16 (solc-js) |
| 75 | +Node v12.18.1 |
| 76 | +Web3.js v1.2.1 |
| 77 | +``` |
0 commit comments