Skip to content

Commit

Permalink
Merge pull request #311 from EYBlockchain/david/wallet-tests
Browse files Browse the repository at this point in the history
Wallet unit tests
  • Loading branch information
Westlad authored Dec 2, 2021
2 parents f8b263a + e458acc commit f8f7d66
Show file tree
Hide file tree
Showing 50 changed files with 1,670 additions and 11,927 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/*
**/migrations/*
**/doc/*
cli/build/*
wallet/src/index.js
wallet/src/index.js
wallet/cli/*
35 changes: 35 additions & 0 deletions .github/workflows/check-PRs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ jobs:
name: ganache-test-logs
path: ./ganache-test.log

wallet-test:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: '14.17.0'

- name: Start Containers
run: |
docker-compose build
./start-nightfall -wt -s &
- name: wait 1000s for Containers startup and setup completion
run: sleep 1000

- name: Retrieve Wallet test logs
run: docker logs $(docker ps -aqf "name=wallet-test") > wallet-test.log

- name: debug logs - after container startup
if: always()
run: results=$(cat wallet-test.log | grep PASSED); cat wallet-test.log; if [ ! -z "${results}" ]; then exit 0; else exit 1; fi

- name: If integration test failed, shutdown the Containers
if: failure()
run: docker-compose -f docker-compose.yml -f docker-compose.ganache.yml -f docker-compose.wallet-test.yml down -v

- name: If integration test failed, upload logs files as artifacts
if: failure()
uses: actions/upload-artifact@master
with:
name: wallet-test-logs
path: ./wallet-test.log


test-gas:
name: check gas for 32 transactions per block
runs-on: ubuntu-20.04
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.DS_Store
node_modules
cli/build
.*swp


# wallet
wallet/build
wallet/cli
__pycache__
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ npm start
npm run start-ropsten
```

- When the wallet starts, you will have the option to enter your private key on connecting with metamask wallet installed in your browser. If you select the latter, you need to have previously configured your metamask wallet to operate with Nightfall's deployment on localhost
- When the wallet starts, connect metamask wallet installed in your browser. You need to have previously configured your metamask wallet to operate with Nightfall's deployment on localhost

More information can be found [here](https://github.com/EYBlockchain/nightfall_3/wallet/README.md)

Expand All @@ -158,11 +158,11 @@ More information can be found [here](https://github.com/EYBlockchain/nightfall_3

### Limitations
- You cannot run the wallet and a separate version of the SDK (CLI for example) in parallel as nonces will get mixed.
- If you select Metamask as your wallet, you need to reset the nonce every time you restart Nightfall, as Metamask will keep previous nonce whereas ganache has reset it. If nonce is not reset, you will see an error message after signing the transaction. To reset the nonce in metamask:
- You need to reset the nonce every time you restart Nightfall, as Metamask will keep previous nonce whereas ganache has reset it. If nonce is not reset, you will see an error message after signing the transaction. To reset the nonce in metamask:
1. Open Metamask in browser
2. Settings->Advance->Reset Account

- Transactions with ERC721 tokens do not work. This is because there is not way yet to recover the token Id.
- Transactions with ERC721 and ERC1155 tokens do not work. This is because there is not way yet to recover the token Id.
- Direct transactions are not implemented
- Instant withdraw is selected when doing a withdraw only. Once submitted the instant withdraw request,the wallet requests a simple withdraw and inmediatelly after converts this withdraw into an instant withdraw. Wallet will attempt to send the instant withdraw request up to 10 times, once every 10 seconds. It is likely that during this period, you need to request a simpler transaction (deposit, withdraw or transfer) so that the original withdraw is processed by the processor and the instant withdraw can be carried out.
- Tested with node version v14.18.0
Expand Down
8 changes: 7 additions & 1 deletion challenger
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#! /bin/bash

/usr/bin/env node cli/src/challenger.mjs
## Usage
# ./challenger [OPTIONAL ARGUMENTS]
# OPTIONAL_ARGUMENTS:
# --environment : test environment (such us localhost, docker, testnet). If no argument given
# challenger is launched in localhost environment

/usr/bin/env node cli/src/challenger.mjs "${@:1}"
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const SUPPORTED_ENVIRONMENTS = {
optimistWsUrl: 'ws://localhost:8082',
web3WsUrl: 'ws://localhost:8546',
},
docker: {
name: 'Docker',
chainId: 4378921,
clientApiUrl: 'http://client1',
optimistApiUrl: 'http://optimist1',
optimistWsUrl: 'ws://optimist1:8080',
web3WsUrl: 'ws://blockchain1:8546',
},
};

const ContractNames = {
Expand Down
3 changes: 2 additions & 1 deletion cli/lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import Nf3 from './nf3.mjs';
import * as Constants from './constants.mjs';
import * as Tokens from './tokens.mjs';
import * as Units from './units.mjs';
import * as Environment from './environment.mjs';

export { Nf3, Constants, Tokens, Units };
export { Nf3, Constants, Tokens, Units, Environment };
20 changes: 12 additions & 8 deletions cli/lib/nf3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,18 @@ class Nf3 {
@returns {Promise} Resolves into the Ethereum transaction receipt.
*/
async deposit(ercAddress, tokenType, value, tokenId, fee = this.defaultFee) {
await approve(
ercAddress,
this.ethereumAddress,
this.shieldContractAddress,
tokenType,
value,
this.web3,
);
try {
await approve(
ercAddress,
this.ethereumAddress,
this.shieldContractAddress,
tokenType,
value,
this.web3,
);
} catch (err) {
throw new Error(err);
}

const res = await axios.post(`${this.clientBaseUrl}/deposit`, {
ercAddress,
Expand Down
9 changes: 4 additions & 5 deletions cli/lib/tokens.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function approve(ercAddress, ownerAddress, spenderAddress, tokenType, valu
}

default:
throw new Error('Unknown token type');
throw new Error('Unknown token type', tokenType);
}
return Promise.resolve();
}
Expand All @@ -68,7 +68,7 @@ async function getDecimals(ercAddress, tokenType, provider) {
}

default:
throw new Error('Unknown token type');
throw new Error('Unknown token type', tokenType);
}
}

Expand All @@ -94,12 +94,11 @@ async function getERCInfo(ercAddress, ethereumAddress, provider, options) {
const abi = getAbi(TOKEN_TYPE.ERC20);
const ercContract = new provider.eth.Contract(abi, ercAddress);
let balance = await ercContract.methods.balanceOf(ethereumAddress).call();
const decimals = await getDecimals(ercAddress, TOKEN_TYPE.ERC20, provider);
if (toEth) {
const decimals = await getDecimals(ercAddress, TOKEN_TYPE.ERC20, provider);
balance = fromBaseUnit(balance, decimals);
return { balance, decimals, tokenType: TOKEN_TYPE.ERC20 };
}
return { balance, tokenType: TOKEN_TYPE.ERC20 };
return { balance, decimals, tokenType: TOKEN_TYPE.ERC20 };
} catch {
try {
const abi = getAbi(TOKEN_TYPE.ERC1155);
Expand Down
57 changes: 55 additions & 2 deletions cli/package-lock.json

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

3 changes: 2 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"async-mutex": "^0.3.2",
"axios": "^0.21.4",
"bip39": "^3.0.4",
"chalk": "^4.1.2",
"clear": "^0.1.0",
"cli-table": "^0.3.6",
Expand All @@ -19,7 +20,7 @@
"inquirer": "^8.1.4",
"web3": "^1.5.2",
"ws": "^8.2.2",
"bip39": "^3.0.4"
"yargs": "^17.2.1"
},
"devDependencies": {
"@babel/cli": "^7.15.7",
Expand Down
24 changes: 15 additions & 9 deletions cli/src/challenger.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ Module that runs up as a challenger
*/
import { Command } from 'commander/esm.mjs';
import clear from 'clear';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import Nf3 from '../lib/nf3.mjs';
import { setEnvironment, getCurrentEnvironment } from '../lib/environment.mjs';

const defaultKey = '0xd42905d0582c476c4b74757be6576ec323d715a0c7dcff231b6348b7ab0190eb';
const defaultMnemonic =
Expand All @@ -14,19 +17,22 @@ program.option('-h', '--help', 'Help');
if (program.opts().help) console.log('-k | --key input an Ethereum signing key to use');
const ethereumSigningKey = program.opts().key || defaultKey;

const argv = yargs(hideBin(process.argv)).parse();
const { environment } = argv;
/**
Does the preliminary setup and starts listening on the websocket
@param {string} testEnvironment - Environment where propose is launched ('Testnet','Localhost','Docker')
*/
async function startChallenger() {
async function startChallenger(testEnvironment) {
clear();
console.log('Starting Challenger...');
const nf3 = new Nf3(
'http://localhost:8080',
'http://localhost:8081',
'ws://localhost:8082',
'ws://localhost:8546',
ethereumSigningKey,
);
if (typeof testEnvironment !== 'undefined') {
setEnvironment(testEnvironment);
} else {
setEnvironment('Localhost');
}
const nf3Env = getCurrentEnvironment().currentEnvironment;
const nf3 = new Nf3(nf3Env.web3WsUrl, ethereumSigningKey, nf3Env);
await nf3.init(defaultMnemonic);
if (await nf3.healthcheck('optimist')) console.log('Healthcheck passed');
else throw new Error('Healthcheck failed');
Expand All @@ -36,4 +42,4 @@ async function startChallenger() {
console.log('Listening for incoming events');
}

startChallenger();
startChallenger(environment);
Loading

0 comments on commit f8f7d66

Please sign in to comment.