Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from LayerXcom/feature/crosschain-atomic-swap-…
Browse files Browse the repository at this point in the history
…cordapp

feature/crosschain atomic swap cordapp
  • Loading branch information
etaroid authored Apr 14, 2020
2 parents c99a0ad + f78cd66 commit cf94f59
Show file tree
Hide file tree
Showing 65 changed files with 7,073 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/runConfigurations/cross_chain_atomic_swap_cordapp.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/runConfigurations/flow_ethereum_event_watch_test.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
56 changes: 56 additions & 0 deletions atomic-swap-ethereum-env/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"extends" : [
"standard",
"plugin:promise/recommended"
],
"plugins": [
"mocha-no-only",
"promise"
],
"env": {
"node" : true,
"mocha" : true
},
"globals" : {
"artifacts": false,
"contract": false,
"assert": false,
"web3": false
},
"rules": {

// Strict mode
"strict": ["error", "global"],

// Code style
"camelcase": ["error", {"properties": "always"}],
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": ["error", {"before": false, "after": true}],
"dot-notation": ["error", {"allowKeywords": true, "allowPattern": ""}],
"eol-last": ["error", "always"],
"eqeqeq": ["error", "smart"],
"generator-star-spacing": ["error", "before"],
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"max-len": ["error", 170, 2],
"no-debugger": "off",
"no-dupe-args": "error",
"no-dupe-keys": "error",
"no-mixed-spaces-and-tabs": ["error", "smart-tabs"],
"no-redeclare": ["error", {"builtinGlobals": true}],
"no-trailing-spaces": ["error", { "skipBlankLines": false }],
"no-undef": "error",
"no-use-before-define": "off",
"no-var": "error",
"object-curly-spacing": ["error", "always"],
"prefer-const": "error",
"quotes": ["error", "single"],
"semi": ["error", "always"],
"space-before-function-paren": ["error", "always"],

"mocha-no-only/mocha-no-only": ["error"],

"promise/always-return": "off",
"promise/avoid-new": "off"
}
}
2 changes: 2 additions & 0 deletions atomic-swap-ethereum-env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
6 changes: 6 additions & 0 deletions atomic-swap-ethereum-env/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "solhint:default",
"rules": {
"max-line-length": ["error", 150]
}
}
67 changes: 67 additions & 0 deletions atomic-swap-ethereum-env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Atomic Swap Ethereum Environment
This manages ethereum environments for [Cross-Chain Atomic Swap Cordapp](../cross-chain-atomic-swap-cordapp).
You can deploy the [Settlement](contracts/Settlement.sol) contract.

## Dependencies
- Node.js v12 (Required)
- yarn (Optional but recommended)

## Usage
Install npm dependencies
```
yarn
```

Run local ethereum network
```
yarn network
```

Deploy SimpleStorage smart contract
```
yarn truffle migrate --network local
```

### Ethereum addresses and private keys
**DO NOT USE IN PRODUCTION**

```
Available Accounts
==================
(0) 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 (100 ETH) Participant C (Security Issuer)
(1) 0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0 (100 ETH) Participant A
(2) 0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b (100 ETH) Participant B
(3) 0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d (100 ETH) Notary
(4) 0xd03ea8624C8C5987235048901fB614fDcA89b117 (100 ETH)
(5) 0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC (100 ETH)
(6) 0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9 (100 ETH)
(7) 0x28a8746e75304c0780E011BEd21C72cD78cd535E (100 ETH)
(8) 0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E (100 ETH)
(9) 0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e (100 ETH)
Private Keys
==================
(0) 0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d Participant C (Security Issuer)
(1) 0x6cbed15c793ce57650b9877cf6fa156fbef513c4e6134f022a85b1ffdd59b2a1 Participant A
(2) 0x6370fd033278c143179d81c5526140625662b8daa446c22ee2d73db3707e620c Participant B
(3) 0x646f1ce2fdad0e6deeeb5c7e8e5543bdde65e86029e2fd9fc169899c440a7913 Notary
(4) 0xadd53f9a7e588d003326d1cbf9e4a43c061aadd9bc938c843a79e7b4fd2ad743
(5) 0x395df67f0c2d2d9fe1ad08d1bc8b6627011959b79c53d7dd6a3536a33ab8a4fd
(6) 0xe485d098507f54e7733a205420dfddbe58db035fa577fc294ebd14db90767a52
(7) 0xa453611d9419d0e56f499079478fd72c37b251a94bfde4d19872c44cf65386e3
(8) 0x829e924fdf021ba3dbbc4225edfece9aca04b929d6e75613329ca6f1d31c0bb4
(9) 0xb0057716d5917badaf911b193b12b910811c1497b5bada8d7711f758981c3773
```

### Truffle console
Log in to truffle console
```
yarn truffle console --network local
```

Get ether balances
```
web3.eth.getBalance('0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0') // Participant A
web3.eth.getBalance('0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b') // Participant B
web3.eth.getBalance(Settlement.address) // Settlement contract
```
19 changes: 19 additions & 0 deletions atomic-swap-ethereum-env/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity >=0.4.21 <0.7.0;


contract Migrations {
address public owner;
uint256 public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint256 completed) public restricted {
last_completed_migration = completed;
}
}
94 changes: 94 additions & 0 deletions atomic-swap-ethereum-env/contracts/Settlement.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
pragma solidity >=0.4.21 <0.7.0;
pragma experimental ABIEncoderV2;

import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

contract Settlement is Ownable {
enum SwapStatus {
Nonexistent,
Locked,
Unlocked
}

struct SwapDetail {
address payable transferFromAddress;
address payable transferToAddress;
uint256 weiAmount;
uint256 securityAmount;
SwapStatus status;
}

address cordaNotary;
mapping(string => SwapDetail) public swapIdToDetailMap;
uint256 settlementId;

event Locked(
uint256 settlementId,
string swapId,
bytes encodedSwapDetail
);

event Unlocked(
uint256 settlementId,
string swapId,
bytes encodedSwapDetail
);

constructor(address _cordaNotary) public {
cordaNotary = _cordaNotary;
settlementId = 1;
}

modifier onlyCordaNotary {
require(msg.sender == cordaNotary, "caller is not the corda notary");
_;
}

function lock(
string memory _swapId,
address payable _transferFromAddress,
address payable _transferToAddress,
uint256 _weiAmount,
uint256 _securityAmount
) public payable {
require(msg.sender == _transferFromAddress, "msg.sender is not _transferFromAddress");
require(msg.value == _weiAmount, "msg.value is not equivalent to _weiAmount");

SwapDetail storage swapDetail = swapIdToDetailMap[_swapId];
swapDetail.transferFromAddress = _transferFromAddress;
swapDetail.transferToAddress = _transferToAddress;
swapDetail.weiAmount = _weiAmount;
swapDetail.securityAmount = _securityAmount;
swapDetail.status = SwapStatus.Locked;

bytes memory encodedSwapDetail = abi.encode(swapDetail);

emit Locked(
settlementId++,
_swapId,
encodedSwapDetail
);
}

function unlock(
string memory _swapId
) public onlyCordaNotary {
SwapDetail storage swapDetail = swapIdToDetailMap[_swapId];

require(swapDetail.status != SwapStatus.Nonexistent, "swapDetail does not exist");
require(swapDetail.status == SwapStatus.Locked, "swapDetail.status is not Locked");

// Try sending wei to targetAddress.
require(swapDetail.transferToAddress.send(swapDetail.weiAmount));

swapDetail.status = SwapStatus.Unlocked;

bytes memory encodedSwapDetail = abi.encode(swapDetail);

emit Unlocked(
settlementId++,
_swapId,
encodedSwapDetail
);
}
}
5 changes: 5 additions & 0 deletions atomic-swap-ethereum-env/migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Migrations = artifacts.require('Migrations');

module.exports = deployer => {
deployer.deploy(Migrations);
};
6 changes: 6 additions & 0 deletions atomic-swap-ethereum-env/migrations/2_settlement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Settlement = artifacts.require('Settlement');

module.exports = async (deployer, network, accounts) => {
const notaryAddress = accounts[3];
await deployer.deploy(Settlement, notaryAddress);
};
37 changes: 37 additions & 0 deletions atomic-swap-ethereum-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "atomic-swap-ethereum-env",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "truffle test",
"network": "ganache-cli -i 5777 -b 1 -l 100000000 -m 'myth like bonus scare over problem client lizard pioneer submit female collect'",
"lint": "yarn lint:sol && yarn lint:js",
"lint:sol": "solhint --max-warnings 0 \"contracts/**/*.sol\"",
"lint:js": "eslint migrations/*.js test/**/*.js scripts/*.js",
"format": "yarn format:sol && yarn format:js",
"format:sol": "prettier --write **/*.sol",
"format:js": "eslint migrations/*.js test/**/*.js scripts/*.js --fix"
},
"author": "LayerX Inc.",
"license": "Apache-2.0",
"dependencies": {
"openzeppelin-solidity": "2.3.0",
"ganache-cli": "^6.9.1",
"truffle": "^5.1.14"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-bn": "^0.2.0",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-mocha-no-only": "^1.1.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"prettier-plugin-solidity": "^1.0.0-alpha.44",
"solhint": "^3.0.0-rc.5",
"truffle-assertions": "^0.9.2"
}
}
10 changes: 10 additions & 0 deletions atomic-swap-ethereum-env/scripts/getContractAddressAndCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Settlement = artifacts.require('Settlement');

module.exports = async done => {
const settlement = await Settlement.deployed();
const address = settlement.address;
const code = await web3.eth.getCode(address);
console.log(address);
console.log(code);
done();
};
Loading

0 comments on commit cf94f59

Please sign in to comment.