-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (53 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// src/index.js
const Web3 = require("web3");
const {
ZWeb3,
Contracts,
ProxyAdminProject
} = require("@openzeppelin/upgrades");
const { setupLoader } = require("@openzeppelin/contract-loader");
async function main() {
// Set up web3 object, connected to the local development network, initialize the Upgrades library
const web3 = new Web3("http://localhost:8545");
ZWeb3.initialize(web3.currentProvider);
const loader = setupLoader({ provider: web3 }).web3;
//Fetch the default account
const from = await ZWeb3.defaultAccount();
//creating a new project, to manage our upgradeable contracts.
const project = new ProxyAdminProject("MyProject", null, null, {
from,
gas: 1e6,
gasPrice: 1e9
});
//Using this project, we can now create an instance of any contract.
//The project will take care of deploying it in such a way it can be upgraded later.
const TodoList1 = Contracts.getFromLocal("TodoList1");
const instance = await project.createProxy(TodoList1);
const address1 = instance.options.address;
console.log("Proxy Contract Address 1: ", address1);
const todoList1 = loader.fromArtifact("TodoList1", address1);
// Send a transaction to add a new item in the TodoList1
await todoList1.methods
.addItem("go to class")
.send({ from: from, gas: 100000, gasPrice: 1e6 });
// Call the getListItem() function to fetch the added item from TodoList1
var item = await todoList1.methods.getListItem(0).call();
console.log("TodoList1: List Item 0: ", item);
//After deploying the contract, you can upgrade it to a new version of
//the code using the upgradeProxy method, and providing the instance address.
const TodoList2 = Contracts.getFromLocal("TodoList2");
const updatedInstance = await project.upgradeProxy(address1, TodoList2);
const address2 = updatedInstance.options.address;
console.log("Proxy Contract Address 2: ", address2);
const todoList2 = loader.fromArtifact("TodoList2", address2);
// Send a transaction to add a new item in the TodoList2
await todoList2.methods
.addItem("code")
.send({ from: from, gas: 100000, gasPrice: 1e6 });
// Call the getListItem() function to fetch the added items from TodoList2
var item0 = await todoList2.methods.getListItem(0).call();
var item1 = await todoList2.methods.getListItem(1).call();
console.log("TodoList2: List Item 0: ", item0);
console.log("TodoList2: List Item 1: ", item1);
}
main();