-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow_blockchain_2.js
198 lines (171 loc) · 6.81 KB
/
workflow_blockchain_2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// ECONNRESET error for hardhat network: The ECONNRESET error typically indicates that the TCP connection was abruptly closed by the remote server, or some network issue occurred.
// NOTE:
// - Keep breakpoint at step-1 in workflow_blockchain_1.js script and at step-2 in workflow_blockchain_2.js script.
// - While debugging, once a step is completed, pause for few seconds before executing next step so that our previous steps changes are recorded in the world state of the canonical chain in the underlying blockchain.
// NOTE for reverse nft transfer:
// - interchange hardhat network names (eth_local_net_1 and eth_local_net_2) for hre.network.name in Utils.getSigners() method in both workflow scripts (workflow_blockchain_1.js and workflow_blockchain_2.js).
// - interchange blockchain names (blockchain_1 and blockchain_2) and nft_minter names (NFTMinter and BridgeNFTMinter) in const b1 and const b2 variables in both scripts(workflow_blockchain_1.js and workflow_blockchain_2.js).
// - Comment step-0: Minting NFT tokens code.
// - interchange user1 and user2 in both workflow scripts (workflow_blockchain_1.js and workflow_blockchain_2.js).
// - Execute workflow_blockchain_1.js in blockchain_2 and workflow_blockchain_2.js in blockchain_1.
const hre = require("hardhat");
const fs = require("fs");
const path = require("path");
const { expect } = require("chai");
class Blockchain {
constructor(name, tokenName, nftMinterName, nftBridgeName) {
this.name = name;
this.tokenName = tokenName;
this.nftMinterName = nftMinterName;
this.nftBridgeName = nftBridgeName;
this.tokenInfo = null;
this.nftMinterInfo = null;
this.nftBridgeInfo = null;
this.token = null;
this.nftMinter = null;
this.nftBridge = null;
}
async setContracts() {
const folder_path = path.join(
__dirname,
"../dapp_contracts_info/",
this.name,
"/"
);
this.tokenInfo = JSON.parse(
fs.readFileSync(path.join(folder_path, `${this.tokenName}.json`), "utf8")
);
this.nftMinterInfo = JSON.parse(
fs.readFileSync(
path.join(folder_path, `${this.nftMinterName}.json`),
"utf8"
)
);
this.nftBridgeInfo = JSON.parse(
fs.readFileSync(
path.join(folder_path, `${this.nftBridgeName}.json`),
"utf8"
)
);
this.token = new hre.ethers.Contract(
this.tokenInfo.contractInstances[0].address,
this.tokenInfo.abi
);
this.nftMinter = new hre.ethers.Contract(
this.nftMinterInfo.contractInstances[0].address,
this.nftMinterInfo.abi
);
this.nftBridge = new hre.ethers.Contract(
this.nftBridgeInfo.contractInstances[0].address,
this.nftBridgeInfo.abi
);
}
}
class Utils {
static async getSigners() {
var owner = null,
user1 = null,
user2 = null;
if (hre.network.name === "eth_local_net_2") {
owner = new hre.ethers.Wallet(
hre.network.config.owner_private_key,
new hre.ethers.JsonRpcProvider(hre.network.config.url)
);
user1 = new hre.ethers.Wallet(
hre.network.config.user1_private_key,
new hre.ethers.JsonRpcProvider(hre.network.config.url)
);
user2 = new hre.ethers.Wallet(
hre.network.config.user2_private_key,
new hre.ethers.JsonRpcProvider(hre.network.config.url)
);
} else {
[owner, user1, user2] = await hre.ethers.getSigners();
}
return [owner, user1, user2];
}
}
async function main() {
const b2 = new Blockchain(
"blockchain_2",
"Token",
"BridgeNFTMinter",
"NFTBridge"
);
await b2.setContracts();
console.log("b2: ", b2);
[owner, user1, user2] = await Utils.getSigners();
// Moving NFT from one blockchain to another blockchain.
const user_account_1 = user1;
const user_account_2 = user2;
const tokenId = 3;
try {
// 2: check if tokenId exists
await b2.nftMinter.connect(owner).ownerOf(tokenId);
} catch (error) {
// 3: if tokenId doesn't exists
await b2.nftMinter.connect(owner).mint(owner, tokenId);
console.log(
"owner_tokens: ",
await b2.nftMinter.connect(owner).walletOfOwner(owner.address)
);
let account_address = await b2.nftMinter.connect(owner).ownerOf(tokenId);
expect(account_address).to.equal(owner.address);
// 4:
await b2.nftMinter.connect(owner).approve(b2.nftBridge.target, tokenId);
account_address = await b2.nftMinter.connect(owner).getApproved(tokenId);
expect(account_address).to.equal(b2.nftBridge.target);
// 5:
await b2.nftBridge.connect(owner).retainNFT(tokenId);
console.log(
"nftBridge_tokens: ",
await b2.nftMinter.connect(owner).walletOfOwner(b2.nftBridge.target)
);
account_address = await b2.nftMinter.connect(owner).ownerOf(tokenId);
expect(account_address).to.equal(b2.nftBridge.target);
let custodialNFT = await b2.nftBridge.connect(owner).custodialNFTs(tokenId);
console.log(`custodialNFT for tokenId : ${tokenId}`, custodialNFT);
expect(custodialNFT.tokenId).to.equal(tokenId);
expect(custodialNFT.holder).to.equal(owner);
}
// 6: if tokenId already exists, then it should belong to the bridgeNFT smart contract and stored in custodialNFTs.
let account_address = await b2.nftMinter.connect(owner).ownerOf(tokenId);
expect(account_address).to.equal(b2.nftBridge.target);
let custodialNFT = await b2.nftBridge.connect(owner).custodialNFTs(tokenId);
console.log(`custodialNFT for tokenId : ${tokenId}`, custodialNFT);
expect(custodialNFT.tokenId).to.equal(tokenId);
// 10:
await b2.nftBridge.connect(owner).releaseNFT(tokenId, user_account_1.address);
console.log(
"user_account_tokens: ",
await b2.nftMinter.connect(owner).walletOfOwner(user_account_1.address)
);
account_address = await b2.nftMinter.connect(owner).ownerOf(tokenId);
expect(account_address).to.equal(user_account_1.address);
custodialNFT = await b2.nftBridge.connect(owner).custodialNFTs(tokenId);
console.log(`custodialNFT for tokenId : ${tokenId}`, custodialNFT);
expect(custodialNFT.tokenId).to.equal(0);
expect(custodialNFT.holder).to.equal(hre.ethers.ZeroAddress);
// N-1: Transferring NFT token from one user to another.
await b2.nftMinter
.connect(user_account_1)
.transferFrom(user_account_1.address, user_account_2.address, tokenId);
account_address = await b2.nftMinter.connect(owner).ownerOf(tokenId);
expect(account_address).to.equal(user_account_2.address);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.log(
"\n--------------------------- ERROR --------------------------\n"
);
console.error(error);
console.log(
"\n------------------------------------------------------------\n"
);
console.log(
"ERROR NOTE:\n \
1) Make sure hardhat network is running."
);
process.exitCode = 1;
});