Skip to content

Commit

Permalink
Merge pull request #386 from EYBlockchain/david/wallet-erc1155
Browse files Browse the repository at this point in the history
David/wallet erc1155
  • Loading branch information
ChaitanyaKonda authored Jan 21, 2022
2 parents 6e61909 + 355304f commit b169a3b
Show file tree
Hide file tree
Showing 37 changed files with 1,917 additions and 652 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/check-PRs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ jobs:
docker-compose build
./start-nightfall -wt -s &
- name: wait 1500s for Containers startup and setup completion
run: sleep 1500
- name: wait 2500s for Containers startup and test completion
run: sleep 2500

- name: Retrieve Wallet test logs
run: docker logs $(docker ps -aqf "name=wallet-test") > wallet-test.log
run: docker logs $(docker ps -aqf "name=wallet-test") > wallet-test.log; cat 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
run: if [ -z "$(cat wallet-test.log | grep FAILED)" ]; then exit 0; else exit 1; fi

- name: If integration test failed, shutdown the Containers
if: failure()
Expand Down
55 changes: 42 additions & 13 deletions cli/lib/nf3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Nf3 {
@returns {Promise}
*/
async init(mnemonic) {
this.setWeb3Provider();
await this.setWeb3Provider();
this.shieldContractAddress = await this.getContractAddress('Shield');
this.proposersContractAddress = await this.getContractAddress('Proposers');
this.challengesContractAddress = await this.getContractAddress('Challenges');
Expand Down Expand Up @@ -258,11 +258,14 @@ class Nf3 {
tokenType,
value,
this.web3,
!!this.ethereumSigningKey,
);
} catch (err) {
throw new Error(err);
}
if (txDataToSign) await this.submitTransaction(txDataToSign, ercAddress, 0);
if (txDataToSign) {
await this.submitTransaction(txDataToSign, ercAddress, 0);
}
const res = await axios.post(`${this.clientBaseUrl}/deposit`, {
ercAddress,
tokenId,
Expand Down Expand Up @@ -704,12 +707,20 @@ class Nf3 {
Returns the balance of tokens held in layer 2
@method
@async
@param {Array} ercList - list of erc contract addresses to filter.
@param {Boolean} filterByCompressedPkd - flag to indicate if request is filtered
ones compressed pkd
@returns {Promise} This promise resolves into an object whose properties are the
addresses of the ERC contracts of the tokens held by this account in Layer 2. The
value of each propery is the number of tokens originating from that contract.
*/
async getLayer2Balances() {
const res = await axios.get(`${this.clientBaseUrl}/commitment/balance`);
async getLayer2Balances(ercList, filterByCompressedPkd) {
const res = await axios.get(`${this.clientBaseUrl}/commitment/balance`, {
params: {
compressedPkd: filterByCompressedPkd === true ? this.zkpKeys.compressedPkd : null,
ercList,
},
});
return res.data.balance;
}

Expand All @@ -723,9 +734,11 @@ class Nf3 {
value of each propery is the number of tokens originating from that contract.
*/
async getLayer2BalancesDetails(ercList) {
const res = await axios.post(`${this.clientBaseUrl}/commitment/balance-details`, {
compressedPkd: this.zkpKeys.compressedPkd,
ercList,
const res = await axios.get(`${this.clientBaseUrl}/commitment/balance-details`, {
params: {
compressedPkd: this.zkpKeys.compressedPkd,
ercList,
},
});
return res.data.balance;
}
Expand All @@ -734,26 +747,42 @@ class Nf3 {
Returns the balance of tokens held in layer 2
@method
@async
@param {Array} ercList - list of erc contract addresses to filter.
@param {Boolean} filterByCompressedPkd - flag to indicate if request is filtered
ones compressed pkd
@returns {Promise} This promise resolves into an object whose properties are the
addresses of the ERC contracts of the tokens held by this account in Layer 2. The
value of each propery is the number of tokens pending deposit from that contract.
*/
async getLayer2PendingDepositBalances() {
const res = await axios.get(`${this.clientBaseUrl}/commitment/pending-deposit`);
async getLayer2PendingDepositBalances(ercList, filterByCompressedPkd) {
const res = await axios.get(`${this.clientBaseUrl}/commitment/pending-deposit`, {
params: {
compressedPkd: filterByCompressedPkd === true ? this.zkpKeys.compressedPkd : null,
ercList,
},
});
return res.data.balance;
}

/**
Returns the balance of tokens held in layer 2
@method
@async
@param {Array} ercList - list of erc contract addresses to filter.
@param {Boolean} filterByCompressedPkd - flag to indicate if request is filtered
ones compressed pkd
@returns {Promise} This promise resolves into an object whose properties are the
addresses of the ERC contracts of the tokens held by this account in Layer 2. The
value of each propery is the number of tokens pending spent (transfer & withdraw)
from that contract.
*/
async getLayer2PendingSpentBalances() {
const res = await axios.get(`${this.clientBaseUrl}/commitment/pending-spent`);
async getLayer2PendingSpentBalances(ercList, filterByCompressedPkd) {
const res = await axios.get(`${this.clientBaseUrl}/commitment/pending-spent`, {
params: {
compressedPkd: filterByCompressedPkd === true ? this.zkpKeys.compressedPkd : null,
ercList,
},
});
return res.data.balance;
}

Expand Down Expand Up @@ -786,12 +815,12 @@ class Nf3 {
/**
Set a Web3 Provider URL
*/
setWeb3Provider() {
async setWeb3Provider() {
this.web3 = new Web3(this.web3WsUrl, { transactionBlockTimeout: 200 }); // set a longer timeout
if (typeof window !== 'undefined') {
if (window.ethereum && this.ethereumSigningKey === '') {
this.web3 = new Web3(window.ethereum);
window.ethereum.request({ method: 'eth_accounts' });
await window.ethereum.request({ method: 'eth_requestAccounts' });
} else {
// Metamask not available
throw new Error('No Web3 provider found');
Expand Down
20 changes: 16 additions & 4 deletions cli/lib/tokens.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ Sends an approve transaction to an ERC20/ERC721/ERC1155 contract for a certain a
* @param {object} provider - web3 provider
* @returns {Promise} transaction
*/
async function approve(ercAddress, ownerAddress, spenderAddress, tokenType, value, provider) {
async function approve(
ercAddress,
ownerAddress,
spenderAddress,
tokenType,
value,
provider,
encodeABI,
) {
const abi = getAbi(tokenType);
const ercContract = new provider.eth.Contract(abi, ercAddress);

Expand All @@ -23,7 +31,7 @@ async function approve(ercAddress, ownerAddress, spenderAddress, tokenType, valu
const allowanceBN = new Web3.utils.BN(allowance);
const valueBN = new Web3.utils.BN(value);
if (allowanceBN.lt(valueBN)) {
if (process.env.USER_ETHEREUM_SIGNING_KEY)
if (process.env.USER_ETHEREUM_SIGNING_KEY || encodeABI)
return ercContract.methods.approve(spenderAddress, APPROVE_AMOUNT).encodeABI();
await ercContract.methods
.approve(spenderAddress, APPROVE_AMOUNT)
Expand All @@ -35,7 +43,7 @@ async function approve(ercAddress, ownerAddress, spenderAddress, tokenType, valu
case TOKEN_TYPE.ERC721:
case TOKEN_TYPE.ERC1155: {
if (!(await ercContract.methods.isApprovedForAll(ownerAddress, spenderAddress).call())) {
if (process.env.USER_ETHEREUM_SIGNING_KEY)
if (process.env.USER_ETHEREUM_SIGNING_KEY || encodeABI)
return ercContract.methods.setApprovalForAll(spenderAddress, true).encodeABI();
await ercContract.methods
.setApprovalForAll(spenderAddress, true)
Expand Down Expand Up @@ -191,7 +199,11 @@ async function getERCInfo(ercAddress, ethereumAddress, provider, options) {

await Promise.all(
tokenIdsEvents.map(async Id => {
const amount = await ercContract.methods.balanceOf(ethereumAddress, Id).call();
let amount = await ercContract.methods.balanceOf(ethereumAddress, Id).call();
if (toEth) {
decimals = await getDecimals(ercAddress, TOKEN_TYPE.ERC1155, provider);
amount = fromBaseUnit(amount, decimals);
}
tokenIds.push({ tokenId: Id, amount });
}),
);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function printBalances(balances, type) {
for (const compressedPkd in balances) {
const table = new Table({ head: ['ERC Contract Address', `${type} Layer 2 Balance`] });
Object.keys(balances[compressedPkd]).forEach(ercAddress =>
table.push({ [ercAddress]: balances[compressedPkd][ercAddress] }),
table.push({ [ercAddress]: balances[compressedPkd][ercAddress][0] }),
);
console.log(chalk.yellow(`${type} Balances of user ${compressedPkd}`));
console.log(table.toString());
Expand Down
2 changes: 2 additions & 0 deletions cli/src/liquidity-provider.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function startProvider(testEnvironment) {
else throw new Error('Healthcheck failed');
const erc20Address = await nf3.getContractAddress('ERC20Mock');

// Aprove ERC20 contract
await approve(
erc20Address,
nf3.ethereumAddress,
Expand All @@ -51,6 +52,7 @@ async function startProvider(testEnvironment) {
APPROVE_AMOUNT,
nf3.web3,
);

// set up a listener to service requests for an instant withdrawal
const emitter = await nf3.getInstantWithdrawalRequestedEmitter();
emitter.on('data', async (withdrawTransactionHash, paidBy, amount) => {
Expand Down
2 changes: 1 addition & 1 deletion doc/lib/nf3.mjs.html
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ <h1 class="page-title">Source: nf3.mjs</h1>
value of each propery is the number of tokens originating from that contract.
*/
async getLayer2Balances() {
const res = await axios.get(`${this.clientBaseUrl}/commitment/balance`);
const res = await axios.post(`${this.clientBaseUrl}/commitment/balance`);
return res.data.balance;
}

Expand Down
16 changes: 11 additions & 5 deletions nightfall-client/src/routes/commitment.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ router.get('/salt', async (req, res, next) => {
router.get('/balance', async (req, res, next) => {
logger.debug('commitment/balance endpoint received GET');
try {
const balance = await getWalletBalance();
const { compressedPkd, ercList } = req.query;
logger.debug(`Details requested with compressedPkd ${compressedPkd} and ercList ${ercList}`);
const balance = await getWalletBalance(compressedPkd, ercList);
res.json({ balance });
} catch (err) {
logger.error(err);
next(err);
}
});

router.post('/balance-details', async (req, res, next) => {
router.get('/balance-details', async (req, res, next) => {
logger.debug('commitment/balance details endpoint received GET');
try {
const { compressedPkd, ercList } = req.body;
const { compressedPkd, ercList } = req.query;
const balance = await getWalletBalanceDetails(compressedPkd, ercList);
res.json({ balance });
} catch (err) {
Expand All @@ -56,7 +58,9 @@ router.post('/balance-details', async (req, res, next) => {
router.get('/pending-deposit', async (req, res, next) => {
logger.debug('commitment/pending-deposit endpoint received GET');
try {
const balance = await getWalletPendingDepositBalance();
const { compressedPkd, ercList } = req.query;
logger.debug(`Details requested with compressedPkd ${compressedPkd} and ercList ${ercList}`);
const balance = await getWalletPendingDepositBalance(compressedPkd, ercList);
res.json({ balance });
} catch (err) {
logger.error(err);
Expand All @@ -67,7 +71,9 @@ router.get('/pending-deposit', async (req, res, next) => {
router.get('/pending-spent', async (req, res, next) => {
logger.debug('commitment/pending-spent endpoint received GET');
try {
const balance = await getWalletPendingSpentBalance();
const { compressedPkd, ercList } = req.query;
logger.debug(`Details requested with compressedPkd ${compressedPkd} and ercList ${ercList}`);
const balance = await getWalletPendingSpentBalance(compressedPkd, ercList);
res.json({ balance });
} catch (err) {
logger.error(err);
Expand Down
Loading

0 comments on commit b169a3b

Please sign in to comment.