Skip to content

Commit d46ad7b

Browse files
Addressing the comments on the PR as per the conversations
Signed-off-by: Dhinakaran Vinayagamurthy <[email protected]>
1 parent f18cc39 commit d46ad7b

File tree

7 files changed

+129
-119
lines changed

7 files changed

+129
-119
lines changed

core/network/besu/contracts/interop/manageAsset.sol

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ contract InteroperationBaseClassERC20 {
2222
uint8 status;
2323
}
2424

25-
uint8 public constant UNUSED = 0;
26-
uint8 public constant LOCKED = 1;
27-
uint8 public constant CLAIMED = 2;
28-
uint8 public constant UNLOCKED = 3;
25+
uint8 constant UNUSED = 0;
26+
uint8 constant LOCKED = 1;
2927

30-
event NewLock(
28+
event Lock(
3129
address indexed sender,
3230
address indexed receiver,
3331
address assetContract,
@@ -98,7 +96,7 @@ contract InteroperationBaseClassERC20 {
9896
LOCKED
9997
);
10098

101-
emit NewLock(
99+
emit Lock(
102100
sender,
103101
receiver,
104102
assetContract,
@@ -119,10 +117,9 @@ contract InteroperationBaseClassERC20 {
119117

120118
// Check the validity of the claim
121119
require(c.status == LOCKED, "lockContract is not active");
122-
require(c.expirationTime > block.timestamp, "lockContract has expired");
120+
require(block.timestamp < c.expirationTime, "lockContract has expired");
123121
require(c.hashLock == sha256(abi.encodePacked(preimage)),"Invalid preimage, its hash does not equal the hashLock");
124122

125-
c.status = CLAIMED;
126123
bool transferStatus = ERC20(c.assetContract).transfer(c.receiver, c.amount);
127124
require(transferStatus == true, "ERC20 transfer failed from the lockContract to the receiver");
128125

@@ -138,7 +135,7 @@ contract InteroperationBaseClassERC20 {
138135
}
139136

140137

141-
// Unlocking and reclaiming a locked asset for the sender after the expiration time. Can be called by anyone.
138+
// Unlocking and reclaiming a locked asset for the sender after the expiration time. Can be called by anyone, not just the sender.
142139
function unlockFungibleAsset(bytes32 lockContractId)
143140
external
144141
returns (bool)
@@ -150,7 +147,6 @@ contract InteroperationBaseClassERC20 {
150147
require(c.sender != address(0), "Sender address is invalid");
151148
require(block.timestamp >= c.expirationTime, "Lock contract has expired");
152149

153-
c.status = UNLOCKED;
154150
bool transferStatus = ERC20(c.assetContract).transfer(c.sender, c.amount);
155151
require(transferStatus == true, "ERC20 transfer failed from the lockContract back to the sender");
156152

@@ -163,4 +159,20 @@ contract InteroperationBaseClassERC20 {
163159
return true;
164160
}
165161

162+
// Function to check if there is an active contract with the input lockContractId.
163+
function isFungibleAssetLocked(bytes32 lockContractId)
164+
external
165+
returns (bool)
166+
{
167+
LockContract storage c = lockContracts[lockContractId];
168+
169+
bool lockContractStatus;
170+
if ( c.status == LOCKED ){
171+
lockContractStatus = true;
172+
} else {
173+
lockContractStatus = false;
174+
}
175+
176+
return lockContractStatus;
177+
}
166178
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
node_modules
3+
contracts/manageAsset.sol
4+
package-lock.json

samples/besu/simpleasset/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
.PHONY: deploy-contracts
12
deploy-contracts: deploy-contract-network1 deploy-contract-network2
23

4+
.PHONY: deploy-contract-network1
35
deploy-contract-network1:
46
BESU_NETWORK_HOST=localhost BESU_NETWORK_PORT=8545 npm run deploy-contract
57

8+
.PHONY: deploy-contract-network2
69
deploy-contract-network2:
710
BESU_NETWORK_HOST=localhost BESU_NETWORK_PORT=9544 npm run deploy-contract
11+
12+
.PHONY: clean-run
13+
clean-run:
14+
npm run clean-run
15+
16+
.PHONY: clean
17+
clean:
18+
npm run clean

samples/besu/simpleasset/app/AssetExchangeERC20.js

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ async function getContractInstance(provider, pathToJson){
3232
var contractName = contract(JSON.parse(jsonFileContents))
3333
contractName.setProvider(provider)
3434
var instance = await contractName.deployed().catch(function () {
35-
console.log("Failed getting the contractName!");
36-
})
35+
console.log("Failed getting the contractName!");
36+
})
3737

3838
return instance
3939
}
@@ -42,32 +42,32 @@ async function getContractInstance(provider, pathToJson){
4242
// Initialization of the parameters
4343
async function init(provider1, provider2, contractOwner1, contractOwner2, Alice1, Bob2, tokenSupply, senderInitialBalance) {
4444

45-
interopContract1 = await getContractInstance(provider1,'../build/contracts/InteroperationBaseClassERC20.json').catch(function () {
46-
console.log("Failed getting interopContract1!");
47-
})
48-
interopContract2 = await getContractInstance(provider2,'../build/contracts/InteroperationBaseClassERC20.json').catch(function () {
49-
console.log("Failed getting interopContract2!");
50-
})
45+
interopContract1 = await getContractInstance(provider1,'../build/contracts/InteroperationBaseClassERC20.json').catch(function () {
46+
console.log("Failed getting interopContract1!");
47+
})
48+
interopContract2 = await getContractInstance(provider2,'../build/contracts/InteroperationBaseClassERC20.json').catch(function () {
49+
console.log("Failed getting interopContract2!");
50+
})
5151

5252

5353
AliceERC20 = await getContractInstance(provider1, '../build/contracts/AliceERC20.json').catch(function () {
54-
console.log("Failed getting AliceERC20 token contract!");
55-
})
54+
console.log("Failed getting AliceERC20 token contract!");
55+
})
5656
BobERC20 = await getContractInstance(provider2, '../build/contracts/BobERC20.json').catch(function () {
57-
console.log("Failed getting BobERC20 token contract!");
58-
})
59-
60-
// Issue AliceERC20 tokens to Alice in Network 1 and BobERC20 tokens to Bob in Network 2
61-
// SUFFICIENT TO RUN THIS ONCE. Comment it out after the first run unless more tokens have to issued to Alice and/or Bob.
62-
///*
63-
await AliceERC20.transfer(Alice1, senderInitialBalance, {from: contractOwner1}).catch(function () {
64-
console.log("AliceERC20 transfer threw an error; Probably the token supply is used up!");
65-
})
66-
await BobERC20.transfer(Bob2, senderInitialBalance, {from: contractOwner2}).catch(function () {
67-
console.log("BobERC20 transfer threw an error; Probably the token supply is used up!");
68-
})
69-
//*/
70-
57+
console.log("Failed getting BobERC20 token contract!");
58+
})
59+
60+
// Issue AliceERC20 tokens to Alice in Network 1 and BobERC20 tokens
61+
// to Bob in Network 2. A minimal number of tokens equal to the
62+
// number of token being exchanged is issued to Alice and Bob to
63+
// ensure that the exchange in this test application does not fail
64+
// due to insufficient funds.
65+
await AliceERC20.transfer(Alice1, senderInitialBalance, {from: contractOwner1}).catch(function () {
66+
console.log("AliceERC20 transfer threw an error; Probably the token supply is used up!");
67+
})
68+
await BobERC20.transfer(Bob2, senderInitialBalance, {from: contractOwner2}).catch(function () {
69+
console.log("BobERC20 transfer threw an error; Probably the token supply is used up!");
70+
})
7171
}
7272

7373

@@ -77,11 +77,12 @@ async function init(provider1, provider2, contractOwner1, contractOwner2, Alice1
7777
// used when locking it
7878
async function claimToken(interopContract, lockContractId, recipient, preimage) {
7979
console.log("\n Claiming %s using preimage: %o", lockContractId, preimage)
80-
var claimStatus = await interopContract.claimFungibleAsset(lockContractId, preimage, {
81-
from: recipient
82-
}).catch(function () {
83-
console.log("claimFungibleAsset threw an error");
84-
})
80+
var claimStatus = await interopContract.claimFungibleAsset(lockContractId, preimage, {
81+
from: recipient
82+
}).catch(function () {
83+
console.log("claimFungibleAsset threw an error");
84+
claimStatus = false
85+
})
8586

8687
return claimStatus
8788
}
@@ -90,11 +91,12 @@ async function claimToken(interopContract, lockContractId, recipient, preimage)
9091
// Unlock function of a locked fungible asset/token
9192
async function unlockToken(interopContract, lockContractId, sender) {
9293

93-
var unlockStatus = await interopContract.unlockFungibleAsset(lockContractId, {
94+
var unlockStatus = await interopContract.unlockFungibleAsset(lockContractId, {
9495
from: sender
95-
}).catch(function () {
96+
}).catch(function () {
9697
console.log("unlockFungibleAsset threw an error");
97-
})
98+
unlockStatus = false
99+
})
98100

99101
return unlockStatus
100102
}
@@ -105,13 +107,15 @@ async function unlockToken(interopContract, lockContractId, sender) {
105107
// for 'recipient' using the contract constructs of 'interopContract'
106108
// with hashLock and timeLock providing the conditions for claiming/unlocking
107109
async function lockToken(sender, recipient, tokenContract, tokenAmount, interopContract, hashLock, timeLock) {
108-
// initiator of the swap has to first designate the swap contract as a spender of his/her money
109-
// with allowance matching the swap amount
110-
await tokenContract.approve(interopContract.address, tokenAmount, {from: sender}).catch(function () {
111-
console.log("Token approval failed!!!");
112-
})
110+
111+
// initiator of the swap has to first designate the swap contract as a spender of his/her money
112+
// with allowance matching the swap amount
113+
await tokenContract.approve(interopContract.address, tokenAmount, {from: sender}).catch(function () {
114+
console.log("Token approval failed!!!");
115+
return false
116+
})
113117

114-
return interopContract.lockFungibleAsset(
118+
var lockStatus = interopContract.lockFungibleAsset(
115119
recipient,
116120
tokenContract.address,
117121
tokenAmount,
@@ -120,12 +124,19 @@ async function lockToken(sender, recipient, tokenContract, tokenAmount, interopC
120124
{
121125
from: sender
122126
}
123-
)
127+
).catch(function () {
128+
console.log("lockFungibleAsset threw an error");
129+
lockStatus = false
130+
})
131+
132+
return lockStatus
124133
}
125134

126135

127-
// A function to obtain and print the account balances of a pair of accounts in the two participating networks.
128-
// Designed for printing the account balances of the sender and recipient at various stages of the exchange.
136+
// A function to obtain and print the account balances of a pair
137+
// of accounts in the two participating networks.
138+
// Designed for printing the account balances of the sender and
139+
// recipient at various stages of the exchange.
129140
async function getBalances(Alice1, Bob1, Alice2, Bob2) {
130141

131142
var AliceAliceERC20Balance = await AliceERC20.balanceOf(Alice1)
@@ -173,37 +184,46 @@ async function main() {
173184

174185
// Initialization
175186
const tokenSupply = 1000
176-
const senderInitialBalance=100
187+
const tokenAmount = 10 // Number of tokens to be exchanged
188+
const senderInitialBalance = tokenAmount
177189

178190
await init(provider1, provider2, contractOwner1, contractOwner2, Alice1, Bob2, tokenSupply, senderInitialBalance)
179191

180192
console.log("\n Balances after init():")
181193
await getBalances(Alice1, Bob1, Alice2, Bob2)
182194

183-
//hashPair = newSecretHashPair()
184195
preimage = crypto.randomBytes(32) // to sample a preimage for the hash
185196
hash = crypto.createHash('sha256').update(preimage).digest()
186197
console.log("Hash: ", hash)
187198

188-
let tokenAmount=10
189199
let timeOut = 15
190-
let timeLockSeconds = Math.floor(Date.now() / 1000) + 2*timeOut
200+
let timeLockSeconds = Math.floor(Date.now() / 1000) + 2*timeOut
191201

192202
// Creating a HTLC contract for Alice locking her AliceERC20 tokens for Bob
193203
let lockTx1 = await lockToken(Alice1, Bob1, AliceERC20, tokenAmount, interopContract1, hash, timeLockSeconds)
194-
let lockContractId1 = lockTx1.logs[0].args.lockContractId
204+
if (!lockTx1) {
205+
console.log("\n !!! Locking of Alice's tokens failed in Netowrk 1. Aborting here !!!")
206+
return
207+
}
208+
let lockContractId1 = lockTx1.logs[0].args.lockContractId
195209
console.log("lockContractID1: ", lockContractId1)
196210

197211
console.log("\n Balances after Alice locks her tokens in Network 1:")
198212
await getBalances(Alice1, Bob1, Alice2, Bob2)
199213

200-
// After he observes this lockContract created in Network 1, Bob creates a similar contract
201-
// in Network 2 using the same hash to transfer the agreed upon amount of BobERC20 to Alice.
202-
// Bob sets the timeLock such that he will have enough time to claim Alice's tokens in
203-
// Network 1 after she claims Bob's tokens in Network 2.
204-
timeLockSeconds = Math.floor(Date.now() / 1000) + timeOut
205-
let lockTx2 = await lockToken(Bob2, Alice2, BobERC20, tokenAmount, interopContract2, hash, timeLockSeconds)
206-
let lockContractId2 = lockTx2.logs[0].args.lockContractId
214+
// After he observes this lockContract created in Network 1,
215+
// Bob creates a similar contract in Network 2 using the same
216+
// hash to transfer the agreed upon amount of BobERC20 to Alice.
217+
// Bob sets the timeLock such that he will have enough time to
218+
// claim Alice's tokens in Network 1 after she claims Bob's
219+
// tokens in Network 2.
220+
timeLockSeconds = Math.floor(Date.now() / 1000) + timeOut
221+
let lockTx2 = await lockToken(Bob2, Alice2, BobERC20, tokenAmount, interopContract2, hash, timeLockSeconds)
222+
if (!lockTx2) {
223+
console.log("\n !!! Locking of Bob's tokens failed in Netowrk 2. Aborting here !!!")
224+
return
225+
}
226+
let lockContractId2 = lockTx2.logs[0].args.lockContractId
207227
console.log("lockContractID2: ", lockContractId2)
208228

209229
console.log("\n Balances after creating Bob locks his tokens in Network 2:")

samples/besu/simpleasset/get-network-details.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ async function main() {
5757
str = "\t\t\tfrom: \""+accounts[0]+"\","
5858
}
5959

60+
if(!str.startsWith("}")) {
61+
str = str + "\n"
62+
}
63+
6064
truffleConfigFileString += str
61-
truffleConfigFileString += "\n"
6265
}
6366

6467
// Writing the updated content to truffle-config

samples/besu/simpleasset/package.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "sample-besu-app",
33
"version": "0.1.0",
44
"description": "Sample Besu Application for Demonstrating Interoperation With Other Networks",
5-
"main": "truffle.js",
65
"license": "Apache-2.0",
76
"repository": "hyperledger-labs/weaver-dlt-interoperability/samples/besu/simpleasset",
87
"bugs": {
@@ -13,13 +12,9 @@
1312
"test": "test"
1413
},
1514
"scripts": {
16-
"ganache-start": "ganache-cli --networkId 4447 --port 7545",
17-
"lint": "solium -d contracts/",
1815
"copy-interop-contracts": "cp ../../../core/network/besu/contracts/interop/manageAsset.sol contracts/",
1916
"update-truffle-config": "node get-network-details.js",
2017
"deploy-contract": "npm run copy-interop-contracts && npm install && npm run update-truffle-config && truffle compile && truffle migrate",
21-
"ethereum-htlc": "truffle compile",
22-
"test": "truffle test",
2318
"clean-run": "rm -rf package-lock.json build",
2419
"clean": "rm -rf node_modules package-lock.json build"
2520
},
@@ -41,9 +36,6 @@
4136
"solc": "^0.8.8"
4237
},
4338
"devDependencies": {
44-
"eslint": "^7.5.0",
45-
"ethlint": "^1.2.5",
46-
"truffle": "^5.4.15",
47-
"truffle-assertions": "^0.9.2"
39+
"truffle": "^5.4.15"
4840
}
4941
}

0 commit comments

Comments
 (0)