Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #476 from AztecProtocol/fix-sdk-data
Browse files Browse the repository at this point in the history
Fix sdk data
  • Loading branch information
Joe Andrews authored Feb 13, 2020
2 parents 30c0375 + ccb3911 commit 5417dd0
Show file tree
Hide file tree
Showing 27 changed files with 282 additions and 215 deletions.
85 changes: 23 additions & 62 deletions packages/extension/demo/1_apis-usage/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@
}

async function approveAllowance() {
allowanceStatus.clear();

const allowanceInput = document.getElementById('erc20-allowance-value');
const value = parseInt(allowanceInput.value);
if (!value) {
allowanceStatus.error('× Allowance value must be larger than 0');
return;
}

allowanceStatus.clear();

const registryAddress = window.aztec.web3.getAddress('AccountRegistry');
const erc20Address = asset.linkedTokenAddress;
await window.aztec.web3
Expand All @@ -115,18 +115,17 @@
}

async function deposit() {
const numberOfOutputNotes = parseInt(document.getElementById('deposit-output-number').value, 10);
const toAddress = document.getElementById('deposit-to-address').value;
const depositInput = document.getElementById('deposit-value');
const value = parseInt(depositInput.value, 10);

depositStatus.clear();

const numberOfOutputNotes = document.getElementById('deposit-output-number').value;
const toAddress = document.getElementById('deposit-to-address').value;
const amount = document.getElementById('deposit-value').value;

try {
const resp = await asset.deposit(
[
{
amount: value,
amount,
to: toAddress,
},
],
Expand All @@ -136,63 +135,49 @@
);
console.log('>> deposit response', resp);
refreshAssetBalances();
depositInput.value = '';
} catch (error) {
console.error(error);
depositStatus.error(error.message);
}
}

async function withdraw() {
const withdrawInput = document.getElementById('withdraw-value');
const toAddress = document.getElementById('withdraw-to-address').value;
const numberOfInputNotes = parseInt(document.getElementById('withdraw-input-number').value, 10);
const value = parseInt(withdrawInput.value, 10);

withdrawStatus.clear();

const account = window.aztec.web3.getAccount();
const amount = document.getElementById('withdraw-value').value;
const toAddress = document.getElementById('withdraw-to-address').value;
const numberOfInputNotes = document.getElementById('withdraw-input-number').value;

try {
const resp = await asset.withdraw(
value,
amount,
{
to: toAddress,
numberOfInputNotes,
},
);
console.log('>> withdraw response', resp);
refreshAssetBalances();
withdrawInput.value = '';
} catch (error) {
console.error(error);
withdrawStatus.error(error.message);
}
}

async function send() {
let numberOfInputNotes = document.getElementById('send-input-number').value.trim();
numberOfInputNotes = numberOfInputNotes === ''
? undefined
: parseInt(numberOfInputNotes);
let numberOfOutputNotes = document.getElementById('send-output-number').value.trim();
numberOfOutputNotes = numberOfOutputNotes === ''
? undefined
: parseInt(numberOfOutputNotes);
const valueInput = document.getElementById('send-value');
const address = document.getElementById('send-address').value.trim();
const value = parseInt(valueInput.value.trim(), 10);

sendStatus.clear();

const account = window.aztec.web3.getAccount();
const numberOfInputNotes = document.getElementById('send-input-number').value;
const numberOfOutputNotes = document.getElementById('send-output-number').value;
const amount = document.getElementById('send-value').value;
const address = document.getElementById('send-address').value;

try {
const resp = await asset.send(
[
{
to: address,
amount: value,
amount,
},
],
{
Expand All @@ -202,7 +187,6 @@
);
console.log('>> send response', resp);
refreshAssetBalances();
valueInput.value = '';
} catch (error) {
console.error(error);
sendStatus.error(error.message);
Expand All @@ -212,16 +196,9 @@
async function createNoteFromBalance() {
createStatus.clear();

let numberOfInputNotes = document.getElementById('create-input-number').value.trim();
numberOfInputNotes = numberOfInputNotes === ''
? undefined
: parseInt(numberOfInputNotes);
let numberOfOutputNotes = document.getElementById('create-output-number').value.trim();
numberOfOutputNotes = numberOfOutputNotes === ''
? undefined
: parseInt(numberOfOutputNotes);
const valueInput = document.getElementById('create-amount');
const value = parseInt(valueInput.value.trim());
const numberOfInputNotes = document.getElementById('create-input-number').value;
const numberOfOutputNotes = document.getElementById('create-output-number').value;
const value = document.getElementById('create-amount').value;
const userAccess = [];
for (let i = 0; i < 10; i += 1) {
const elem = document.getElementById(`create-access-${i}`);
Expand All @@ -231,8 +208,6 @@
}
}

const account = window.aztec.web3.getAccount();

try {
const resp = await asset.createNoteFromBalance(
value,
Expand All @@ -242,10 +217,8 @@
numberOfOutputNotes,
},
);

console.log('>> create note from balance response', resp);
refreshAssetBalances();
valueInput.value = '';
} catch (error) {
console.error(error);
createStatus.error(error.message);
Expand All @@ -255,22 +228,10 @@
async function fetchNotesFromBalance() {
fetchStatus.clear();

let equalTo = document.getElementById('fetch-eq-value').value.trim();
equalTo = equalTo === ''
? undefined
: parseInt(equalTo);
let greaterThan = document.getElementById('fetch-gt-value').value.trim();
greaterThan = greaterThan === ''
? undefined
: parseInt(greaterThan);
let lessThan = document.getElementById('fetch-lt-value').value.trim();
lessThan = lessThan === ''
? undefined
: parseInt(lessThan);
let numberOfNotes = document.getElementById('fetch-count-value').value.trim();
numberOfNotes = numberOfNotes === ''
? undefined
: parseInt(numberOfNotes);
const equalTo = document.getElementById('fetch-eq-value').value;
const greaterThan = document.getElementById('fetch-gt-value').value;
const lessThan = document.getElementById('fetch-lt-value').value;
const numberOfNotes = document.getElementById('fetch-count-value').value;

let notes;
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/demo/1_apis-usage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
function handleReload(type, value) {
let message;
if (type === 'accountChanged') {
message = `Switching account to ${value}...`;
message = `Switching account to ${value.address}...`;
} else if (type === 'networkChanged') {
message = `Switching network to ${value}...`;
message = `Switching network to ${value.name} (${value.id})...`;
} else {
message = 'Signing in to AZTEC account...';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ export default function validateParameters(query, args) {

const errorMsg = schema.validate(args);
if (errorMsg) {
return argsError('input.invalid', {
message: errorMsg,
});
const errorResp = argsError('input.invalid');
return {
...errorResp,
error: {
...errorResp.error,
message: errorMsg,
},
};
}

return null;
Expand Down
5 changes: 3 additions & 2 deletions packages/extension/src/background/utils/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Connection {
webClientId,
});

await this.uiFrame.ensureCreated();
const frame = await this.uiFrame.ensureCreated();

this.openUi({
requestId,
Expand All @@ -141,13 +141,14 @@ class Connection {
},
});

this.uiFrame.frame.contentWindow.postMessage({
frame.contentWindow.postMessage({
type: sendActionEvent,
action: {
...action,
data: args,
},
}, '*');

loadingElem.style.display = 'none';
uiContainer.style.display = 'block';
this.uiFrame.open();
Expand Down
37 changes: 22 additions & 15 deletions packages/extension/src/client/Aztec/ApiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@ export default class ApiManager {
this.enableProfileChangeListener = null;

Web3Service.bindProfileChange((changedType, newTypeValue) => {
let objValue = newTypeValue;
switch (changedType) {
case 'accountChanged':
objValue = {
address: newTypeValue,
};
break;
case 'networkChanged':
case 'chainChanged':
objValue = {
id: newTypeValue,
name: getNetworkName(Web3Service.networkId),
};
break;
default:
let objValue = newTypeValue || null;
if (objValue) {
switch (changedType) {
case 'accountChanged':
objValue = {
address: newTypeValue,
};
break;
case 'networkChanged':
case 'chainChanged':
if (newTypeValue === 'loading') {
objValue = null;
} else {
objValue = {
id: newTypeValue,
name: getNetworkName(newTypeValue),
};
}
break;
default:
}
}

this.eventListeners.notify('profileChanged', changedType, objValue);
});
}
Expand Down
30 changes: 16 additions & 14 deletions packages/extension/src/client/apis/ZkAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Web3Service from '~/client/services/Web3Service';
import ConnectionService from '~/client/services/ConnectionService';
import ContractError from '~/client/utils/ContractError';
import ApiError from '~/client/utils/ApiError';
import parseInputTransactions from '~/client/utils/parseInputTransactions';
import parseInputInteger from '~/client/utils/parseInputInteger';
import SubscriptionManager from './SubscriptionManager';

const dataProperties = [
Expand Down Expand Up @@ -197,8 +199,8 @@ export default class ZkAsset {
{
proofType: 'DEPOSIT_PROOF',
assetAddress: this.address,
transactions,
numberOfOutputNotes,
transactions: parseInputTransactions(transactions),
numberOfOutputNotes: parseInputInteger(numberOfOutputNotes),
userAccess,
},
);
Expand Down Expand Up @@ -238,9 +240,9 @@ export default class ZkAsset {
{
proofType: 'WITHDRAW_PROOF',
assetAddress: this.address,
amount,
amount: parseInputInteger(amount),
to: to || address,
numberOfInputNotes,
numberOfInputNotes: parseInputInteger(numberOfInputNotes),
},
);
};
Expand Down Expand Up @@ -274,9 +276,9 @@ export default class ZkAsset {
{
proofType: 'TRANSFER_PROOF',
assetAddress: this.address,
transactions,
numberOfInputNotes,
numberOfOutputNotes,
transactions: parseInputTransactions(transactions),
numberOfInputNotes: parseInputInteger(numberOfInputNotes),
numberOfOutputNotes: parseInputInteger(numberOfOutputNotes),
userAccess,
},
);
Expand Down Expand Up @@ -395,10 +397,10 @@ export default class ZkAsset {
{
proofType: 'CREATE_NOTE_FROM_BALANCE_PROOF',
assetAddress: this.address,
amount,
amount: parseInputInteger(amount),
userAccess,
numberOfInputNotes,
numberOfOutputNotes,
numberOfInputNotes: parseInputInteger(numberOfInputNotes),
numberOfOutputNotes: parseInputInteger(numberOfOutputNotes),
},
) || {};

Expand Down Expand Up @@ -429,10 +431,10 @@ export default class ZkAsset {
'fetchNotesFromBalance',
{
assetAddress: this.address,
greaterThan,
lessThan,
equalTo,
numberOfNotes,
greaterThan: parseInputInteger(greaterThan),
lessThan: parseInputInteger(lessThan),
equalTo: parseInputInteger(equalTo),
numberOfNotes: parseInputInteger(numberOfNotes),
},
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ConnectionService {
constructor() {
this.clientId = randomId();
this.setInitialVars();
backgroundFrame.init();
}

setInitialVars() {
Expand Down Expand Up @@ -77,7 +78,7 @@ class ConnectionService {
} = clientProfile;
this.apiKey = apiKey;

const frame = await backgroundFrame.init();
const frame = await backgroundFrame.ensureCreated();

const backgroundResponse = fromEvent(window, 'message')
.pipe(
Expand Down
4 changes: 0 additions & 4 deletions packages/extension/src/client/services/Web3Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class ClientWeb3Service extends Web3Service {

subscribeProfileListeners() {
if (window.ethereum) {
// TODO - to be removed
// https://metamask.github.io/metamask-docs/API_Reference/Ethereum_Provider#ethereum.autorefreshonnetworkchange-(to-be-removed)
window.ethereum.autoRefreshOnNetworkChange = false;

window.ethereum.on('accountsChanged', (accounts) => {
this.eventListeners.notify('profile', 'accountChanged', accounts[0]);
});
Expand Down
Loading

0 comments on commit 5417dd0

Please sign in to comment.