From 1ee399f3d9f7f2188bacbb76ea04bbd8e5b071ea Mon Sep 17 00:00:00 2001 From: Leila Wang Date: Sat, 1 Feb 2020 17:24:59 +0000 Subject: [PATCH] feat(sdk): check apiKey quota to render view of proper steps --- .../TransactionSendingService/index.js | 6 +- .../makeApproveFunction.js | 41 +++++++++++ .../services/ConnectionService/index.js | 70 +++++++++++++------ .../client/services/MetaMaskService/index.js | 45 ++---------- .../src/client/utils/getApiKeyApproval.js | 44 ++++++++++++ .../src/client/utils/getApiKeyQuota.js | 40 +++++++++++ packages/extension/src/config/constants.js | 1 - packages/extension/src/config/urls.js | 4 ++ packages/extension/src/ui/App.jsx | 7 -- .../src/ui/components/Route/index.jsx | 6 -- .../extension/src/ui/helpers/getGSNConfig.js | 19 +++++ .../src/ui/pages/CreateNoteFromBalance.jsx | 18 ++--- packages/extension/src/ui/pages/Deposit.jsx | 16 ++--- packages/extension/src/ui/pages/Register.jsx | 37 +++++----- .../src/ui/pages/RegisterAddress.jsx | 23 +++--- packages/extension/src/ui/pages/Send.jsx | 29 ++++---- packages/extension/src/ui/pages/Withdraw.jsx | 26 ++++--- .../extension/src/utils/approveGSNFunction.js | 47 ------------- packages/extension/src/utils/checkQuota.js | 24 ------- packages/extension/src/utils/getGSNConfig.js | 13 ---- 20 files changed, 276 insertions(+), 240 deletions(-) create mode 100644 packages/extension/src/background/services/TransactionSendingService/makeApproveFunction.js create mode 100644 packages/extension/src/client/utils/getApiKeyApproval.js create mode 100644 packages/extension/src/client/utils/getApiKeyQuota.js create mode 100644 packages/extension/src/ui/helpers/getGSNConfig.js delete mode 100644 packages/extension/src/utils/approveGSNFunction.js delete mode 100644 packages/extension/src/utils/checkQuota.js delete mode 100644 packages/extension/src/utils/getGSNConfig.js diff --git a/packages/extension/src/background/services/TransactionSendingService/index.js b/packages/extension/src/background/services/TransactionSendingService/index.js index 21b2aa856..c68c2ebd8 100644 --- a/packages/extension/src/background/services/TransactionSendingService/index.js +++ b/packages/extension/src/background/services/TransactionSendingService/index.js @@ -3,8 +3,8 @@ import { } from '@openzeppelin/gsn-provider'; import Web3Service from '~/helpers/Web3Service'; import retrieveSigningInfo from '~/utils/retrieveSigningInfo'; -import approveFunction from '~/utils/approveGSNFunction'; import { getProviderUrl } from '~/utils/network'; +import makeApproveFunction from './makeApproveFunction'; const sendTransaction = async (query, connection) => { const { @@ -25,7 +25,7 @@ const sendTransaction = async (query, connection) => { const gsnProvider = new GSNProvider(providerUrl, { pollInterval: 1 * 1000, signKey: signingInfo.privateKey, - approveFunction: approveFunction(query, connection), + approveFunction: makeApproveFunction(query, connection), }); const receipt = await Web3Service .useContract(contract) @@ -53,8 +53,6 @@ const sendTransaction = async (query, connection) => { } }; -// TODO change this to use the gas station network - export default { sendTransaction, }; diff --git a/packages/extension/src/background/services/TransactionSendingService/makeApproveFunction.js b/packages/extension/src/background/services/TransactionSendingService/makeApproveFunction.js new file mode 100644 index 000000000..cd3552bde --- /dev/null +++ b/packages/extension/src/background/services/TransactionSendingService/makeApproveFunction.js @@ -0,0 +1,41 @@ +import ClientActionService from '~/background/services/ClientActionService'; +import { + actionRequestEvent, +} from '~/config/event'; + +export default function makeApproveFunction(query, connection) { + return async ({ + from, + to, + encodedFunctionCall, + txFee, + gasPrice, + gas, + nonce, + relayerAddress, + relayHubAddress, + }) => { + const params = { + from, + to, + encodedFunctionCall, + txFee, + gasPrice, + gas, + nonce, + relayerAddress, + relayHubAddress, + }; + + const response = await ClientActionService.triggerClientAction({ + ...query, + data: { + type: actionRequestEvent, + action: 'apiKeyApproval', + params, + }, + }, connection); + + return response.data.approvalData; + }; +} diff --git a/packages/extension/src/client/services/ConnectionService/index.js b/packages/extension/src/client/services/ConnectionService/index.js index d9ef1c4bc..bf5ae08be 100644 --- a/packages/extension/src/client/services/ConnectionService/index.js +++ b/packages/extension/src/client/services/ConnectionService/index.js @@ -1,5 +1,4 @@ import { - mergeMap, filter, tap, take, @@ -7,7 +6,6 @@ import { import { Subject, fromEvent, - from, } from 'rxjs'; import { randomId, @@ -33,6 +31,8 @@ import { import MetaMaskService from '~/client/services/MetaMaskService'; import ApiError from '~/client/utils/ApiError'; import getSiteData from '~/client/utils/getSiteData'; +import getApiKeyQuota from '~/client/utils/getApiKeyQuota'; +import getApiKeyApproval from '~/client/utils/getApiKeyApproval'; import backgroundFrame from './backgroundFrame'; class ConnectionService { @@ -42,6 +42,7 @@ class ConnectionService { } setInitialVars() { + this.apiKey = ''; this.port = null; this.MessageSubject = null; this.messages$ = null; @@ -71,6 +72,11 @@ class ConnectionService { } async openConnection(clientProfile) { + const { + apiKey, + } = clientProfile; + this.apiKey = apiKey; + const frame = await backgroundFrame.init(); const backgroundResponse = fromEvent(window, 'message') @@ -116,28 +122,52 @@ class ConnectionService { case subscriptionResponseEvent: this.handleReceiveSubscription(data.response); break; + case actionRequestEvent: + this.handleClientActionRequest(data); + break; + case clientResponseEvent: + this.handleReceiveResponse(data); + break; default: - this.MessageSubject.next(data); } }; + } - this.messages$.pipe( - filter(({ type }) => type === actionRequestEvent), - mergeMap(data => from(MetaMaskService(data))), - tap(({ - requestId, - responseId, - response, - }) => this.port.postMessage({ - type: actionResponseEvent, - origin: window.location.origin, - clientId: this.clientId, - sender: 'WEB_CLIENT', - requestId, - responseId, - data: response, - })), - ).subscribe(); + handleReceiveResponse(data) { + this.MessageSubject.next(data); + } + + async handleClientActionRequest(data) { + const { + requestId, + responseId, + data: { + action, + params, + }, + } = data; + let response; + + switch (action) { + case 'apiKeyQuota': + response = await getApiKeyQuota(this.apiKey); + break; + case 'apiKeyApproval': + response = await getApiKeyApproval(this.apiKey, params); + break; + default: + response = await MetaMaskService(action, params); + } + + this.port.postMessage({ + type: actionResponseEvent, + origin: window.location.origin, + clientId: this.clientId, + sender: 'WEB_CLIENT', + requestId, + responseId, + data: response, + }); } async postToBackground({ diff --git a/packages/extension/src/client/services/MetaMaskService/index.js b/packages/extension/src/client/services/MetaMaskService/index.js index afbaf5cc9..038114a56 100644 --- a/packages/extension/src/client/services/MetaMaskService/index.js +++ b/packages/extension/src/client/services/MetaMaskService/index.js @@ -1,14 +1,10 @@ import ethSigUtil from 'eth-sig-util'; import * as ethUtil from 'ethereumjs-util'; import Web3Service from '~/client/services/Web3Service'; -import { - SIGNING_PROVIDER, -} from '~/config/constants'; import registerExtension, { generateTypedData } from './registerExtension'; import signNote from './signNote'; import signProof from './signProof'; - const handleAction = async (action, params) => { let response = {}; const { address } = Web3Service.account; @@ -30,32 +26,6 @@ const handleAction = async (action, params) => { .send(...data); break; } - case 'gsn.sign.transaction': { - const { - apiKey, - networkId, - ...data - } = params; - const result = await window.fetch(`${SIGNING_PROVIDER}/Stage/${networkId}/${apiKey}`, { - method: 'POST', // *GET, POST, PUT, DELETE, etc. - mode: 'cors', // no-cors, *cors, same-origin - cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached - credentials: 'same-origin', // include, *same-origin, omit - headers: { - 'Content-Type': 'application/json', - // 'Content-Type': 'application/x-www-form-urlencoded', - }, - body: JSON.stringify({ data }), // body data type must match "Content-Type" header - }); - const { - data: { - approvalData, - }, - } = await result.json(); - - response = { approvalData }; - break; - } case 'metamask.register.extension': { const eip712Data = registerExtension(params); const method = 'eth_signTypedData_v4'; @@ -137,14 +107,10 @@ const handleAction = async (action, params) => { return response; }; -export default async function MetaMaskService(query) { +export default async function MetaMaskService(action, params) { let response; let error; try { - const { - action, - params, - } = query.data; response = await handleAction(action, params); ({ error, @@ -154,11 +120,8 @@ export default async function MetaMaskService(query) { } return { - ...query, - response: { - ...response, - error, - success: !error, - }, + ...response, + error, + success: !error, }; } diff --git a/packages/extension/src/client/utils/getApiKeyApproval.js b/packages/extension/src/client/utils/getApiKeyApproval.js new file mode 100644 index 000000000..19924032e --- /dev/null +++ b/packages/extension/src/client/utils/getApiKeyApproval.js @@ -0,0 +1,44 @@ +import urls from '~/config/urls'; +import { + warnLog, +} from '~/utils/log'; +import { + formatStrPattern, +} from '~/utils/format'; +import Web3Service from '~/client/services/Web3Service'; + +const urlPattern = urls.apiKeyQuota; + +export default async function getApiKeyApproval(apiKey, data) { + const { + networkId, + } = Web3Service; + const url = formatStrPattern(urlPattern, { + apiKey, + networkId, + }); + + let approvalData = null; + try { + const result = await fetch(url, { + method: 'POST', + mode: 'cors', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ data }), + }); + ({ + data: { + approvalData, + }, + } = await result.json()); + } catch (e) { + warnLog('Failed to get apiKey approval.', e); + } + + return { + approvalData, + }; +} diff --git a/packages/extension/src/client/utils/getApiKeyQuota.js b/packages/extension/src/client/utils/getApiKeyQuota.js new file mode 100644 index 000000000..bcff3abc9 --- /dev/null +++ b/packages/extension/src/client/utils/getApiKeyQuota.js @@ -0,0 +1,40 @@ +import urls from '~/config/urls'; +import { + warnLog, +} from '~/utils/log'; +import { + formatStrPattern, +} from '~/utils/format'; +import Web3Service from '~/client/services/Web3Service'; + +const urlPattern = urls.apiKeyQuota; + +export default async function getApiKeyQuota(apiKey) { + const { + networkId, + } = Web3Service; + const url = formatStrPattern(urlPattern, { + apiKey, + networkId, + }); + + let hasFreeTransactions = false; + try { + const result = await fetch(url, { + method: 'GET', + mode: 'cors', + cache: 'no-cache', + }); + ({ + data: { + hasFreeTransactions, + }, + } = await result.json()); + } catch (e) { + warnLog('Failed to get apiKey quota.', e); + } + + return { + hasFreeTransactions: hasFreeTransactions || false, + }; +} diff --git a/packages/extension/src/config/constants.js b/packages/extension/src/config/constants.js index e3b204919..ee25645f2 100644 --- a/packages/extension/src/config/constants.js +++ b/packages/extension/src/config/constants.js @@ -13,7 +13,6 @@ export const AZTEC_JS_METADATA_PREFIX_LENGTH = 130; export const START_EVENTS_SYNCING_BLOCK = 0; export const INFURA_API_KEY = '09c4eed231c840d5ace14ba5389a1a7c'; -export const SIGNING_PROVIDER = 'https://bv9t4hwozi.execute-api.us-east-1.amazonaws.com'; export const NOTE_STATUS = { CREATED: 'CREATED', diff --git a/packages/extension/src/config/urls.js b/packages/extension/src/config/urls.js index 27810c062..e74e160dc 100644 --- a/packages/extension/src/config/urls.js +++ b/packages/extension/src/config/urls.js @@ -1,8 +1,11 @@ +const apiKeyQuota = 'https://bv9t4hwozi.execute-api.us-east-1.amazonaws.com/Stage/{networkId}/{apiKey}'; + const production = { origin: 'https://sdk.aztecprotocol.com', public: 'https://sdk.aztecprotocol.com/{version}/sdk/public', background: 'https://sdk.aztecprotocol.com/{version}/sdk/background.html', ui: 'https://sdk.aztecprotocol.com/{version}/sdk/ui.html', + apiKeyQuota, }; const development = { @@ -10,6 +13,7 @@ const development = { public: 'http://localhost:5555/sdk/public', background: 'http://localhost:5555/sdk/public/background.html', ui: 'http://localhost:5555/sdk/public/ui.html', + apiKeyQuota, }; const config = process.env.NODE_ENV === 'production' diff --git a/packages/extension/src/ui/App.jsx b/packages/extension/src/ui/App.jsx index 17529d73a..ef90376a8 100644 --- a/packages/extension/src/ui/App.jsx +++ b/packages/extension/src/ui/App.jsx @@ -19,7 +19,6 @@ import routes from '~uiModules/config/routes'; import actions from '~/ui/config/actions'; import getAuthRoute from '~/ui/utils/getAuthRoute'; import ensureMinPendingTime from '~/ui/utils/ensureMinPendingTime'; -import getGsnConfig from '~/utils/getGSNConfig'; import './styles/guacamole.css'; import './styles/ui.scss'; @@ -35,7 +34,6 @@ class App extends PureComponent { this.state = { loading: !mock, currentAccount: null, - gsnConfig: {}, action: null, nextRoute: '', }; @@ -114,8 +112,6 @@ class App extends PureComponent { const startTime = Date.now(); - const gsnConfig = await getGsnConfig(); - const { type, data: actionData, @@ -160,7 +156,6 @@ class App extends PureComponent { nextRoute: route, action: actionData, currentAccount, - gsnConfig, }; const minDelayTime = Math.max(0, 2000 - (Date.now() - startTime)); @@ -185,7 +180,6 @@ class App extends PureComponent { const { currentAccount, action, - gsnConfig, } = this.state; Object.keys(config).forEach((subName) => { @@ -218,7 +212,6 @@ class App extends PureComponent { key={path} name={name} path={path} - gsnConfig={gsnConfig} currentAccount={currentAccount} action={action} Component={Component} diff --git a/packages/extension/src/ui/components/Route/index.jsx b/packages/extension/src/ui/components/Route/index.jsx index 31ad3ed6f..e89bc5b7c 100644 --- a/packages/extension/src/ui/components/Route/index.jsx +++ b/packages/extension/src/ui/components/Route/index.jsx @@ -3,16 +3,12 @@ import PropTypes from 'prop-types'; import { Route, } from 'react-router-dom'; -import { - gsnConfigShape, -} from '~/ui/config/propTypes'; const CustomRoute = ({ path, currentAccount, action, Component, - gsnConfig, goToPage, }) => ( ( @@ -36,7 +31,6 @@ CustomRoute.propTypes = { }).isRequired, action: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types Component: PropTypes.func.isRequired, - gsnConfig: gsnConfigShape.isRequired, goToPage: PropTypes.func.isRequired, }; diff --git a/packages/extension/src/ui/helpers/getGSNConfig.js b/packages/extension/src/ui/helpers/getGSNConfig.js new file mode 100644 index 000000000..0e455fb22 --- /dev/null +++ b/packages/extension/src/ui/helpers/getGSNConfig.js @@ -0,0 +1,19 @@ +import Web3Service from '~/helpers/Web3Service'; +import ConnectionService from '~/ui/services/ConnectionService'; + +export default async function getGsnConfig() { + const response = await ConnectionService.post({ + action: 'apiKeyQuota', + }); + const { + hasFreeTransactions, + } = response || {}; + + const isGSNAvailable = hasFreeTransactions; + const proxyContract = Web3Service.getAddress('AccountRegistry'); + + return { + isGSNAvailable, + proxyContract, + }; +} diff --git a/packages/extension/src/ui/pages/CreateNoteFromBalance.jsx b/packages/extension/src/ui/pages/CreateNoteFromBalance.jsx index 9c8505dcf..2162647b7 100644 --- a/packages/extension/src/ui/pages/CreateNoteFromBalance.jsx +++ b/packages/extension/src/ui/pages/CreateNoteFromBalance.jsx @@ -5,7 +5,6 @@ import { valueOf, } from '~/utils/note'; import { - gsnConfigShape, inputAmountType, } from '~/ui/config/propTypes'; import { @@ -15,6 +14,7 @@ import parseInputAmount from '~/ui/utils/parseInputAmount'; import apis from '~uiModules/apis'; import makeAsset from '~/ui/utils/makeAsset'; import returnAndClose from '~/ui/helpers/returnAndClose'; +import getGSNConfig from '~/ui/helpers/getGSNConfig'; import StepsHandler from '~/ui/views/handlers/StepsHandler'; import CreateNoteFromBalanceContent from '~/ui/views/CreateNoteFromBalanceContent'; import createNoteFromBalanceSteps from '~/ui/steps/createNoteFromBalance'; @@ -45,18 +45,19 @@ const CreateNoteFromBalance = ({ numberOfInputNotes: customNumberOfInputNotes, numberOfOutputNotes: customNumberOfOutputNotes, userAccess, - gsnConfig, }) => { const { address: currentAddress, } = currentAccount; - const { - isGSNAvailable, - proxyContract, - } = gsnConfig; - const steps = createNoteFromBalanceSteps[isGSNAvailable ? 'gsn' : 'metamask']; const fetchInitialData = async () => { + const gsnConfig = await getGSNConfig(); + const { + isGSNAvailable, + proxyContract, + } = gsnConfig; + const steps = createNoteFromBalanceSteps[isGSNAvailable ? 'gsn' : 'metamask']; + const asset = await makeAsset(assetAddress); const userAccessAccounts = await apis.account.batchGetExtensionAccount(userAccess); const sender = isGSNAvailable ? proxyContract : currentAddress; @@ -75,6 +76,7 @@ const CreateNoteFromBalance = ({ ]; return { + steps, assetAddress, currentAddress, asset, @@ -92,7 +94,6 @@ const CreateNoteFromBalance = ({ return ( { - const { - isGSNAvailable, - proxyContract, - } = gsnConfig; const { address: currentAddress, } = currentAccount; + const fetchInitialData = async () => { + const gsnConfig = await getGSNConfig(); + const { + isGSNAvailable, + proxyContract, + } = gsnConfig; const asset = await makeAsset(assetAddress); const parsedTransactions = parseInputTransactions(transactions); const amount = parsedTransactions.reduce((sum, tx) => sum + tx.amount, 0); @@ -100,7 +101,6 @@ Deposit.propTypes = { transactions: PropTypes.arrayOf(inputTransactionShape).isRequired, numberOfOutputNotes: PropTypes.number, userAccess: PropTypes.arrayOf(PropTypes.string), - gsnConfig: gsnConfigShape.isRequired, }; Deposit.defaultProps = { diff --git a/packages/extension/src/ui/pages/Register.jsx b/packages/extension/src/ui/pages/Register.jsx index 416ba6a1d..e18f15490 100644 --- a/packages/extension/src/ui/pages/Register.jsx +++ b/packages/extension/src/ui/pages/Register.jsx @@ -1,10 +1,8 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { - gsnConfigShape, -} from '~/ui/config/propTypes'; import ConnectionService from '~uiModules/services/ConnectionService'; -import returnAndClose from '~uiModules/helpers/returnAndClose'; +import returnAndClose from '~/ui/helpers/returnAndClose'; +import getGSNConfig from '~/ui/helpers/getGSNConfig'; import StepsHandler from '~/ui/views/handlers/StepsHandler'; import RegisterContent from '~/ui/views/RegisterContent'; import registerSteps from '~/ui/steps/register'; @@ -12,16 +10,24 @@ import registerSteps from '~/ui/steps/register'; const Register = ({ currentAccount, domainRegistered, - gsnConfig, goToPage, }) => { - const { - isGSNAvailable, - } = gsnConfig; - const { - address, - } = currentAccount; - const steps = registerSteps[isGSNAvailable ? 'gsn' : 'metamask']; + const fetchInitialData = async () => { + const gsnConfig = await getGSNConfig(); + const { + isGSNAvailable, + } = gsnConfig; + const { + address, + } = currentAccount; + const steps = registerSteps[isGSNAvailable ? 'gsn' : 'metamask']; + + return { + steps, + address, + isGSNAvailable, + }; + }; const handleClose = async (data) => { if (domainRegistered) { @@ -34,11 +40,7 @@ const Register = ({ return ( @@ -50,7 +52,6 @@ Register.propTypes = { address: PropTypes.string.isRequired, }).isRequired, domainRegistered: PropTypes.bool.isRequired, - gsnConfig: gsnConfigShape.isRequired, goToPage: PropTypes.func.isRequired, }; diff --git a/packages/extension/src/ui/pages/RegisterAddress.jsx b/packages/extension/src/ui/pages/RegisterAddress.jsx index c8240f024..bb74435b8 100644 --- a/packages/extension/src/ui/pages/RegisterAddress.jsx +++ b/packages/extension/src/ui/pages/RegisterAddress.jsx @@ -1,10 +1,8 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { - gsnConfigShape, -} from '~/ui/config/propTypes'; -import ConnectionService from '~uiModules/services/ConnectionService'; -import returnAndClose from '~uiModules/helpers/returnAndClose'; +import ConnectionService from '~/ui/services/ConnectionService'; +import returnAndClose from '~/ui/helpers/returnAndClose'; +import getGSNConfig from '~/ui/helpers/getGSNConfig'; import StepsHandler from '~/ui/views/handlers/StepsHandler'; import RegisterContent from '~/ui/views/RegisterContent'; import registerSteps from '~/ui/steps/register'; @@ -13,15 +11,15 @@ import apis from '~uiModules/apis'; const RegisterAddress = ({ currentAccount, domainRegistered, - gsnConfig, goToPage, }) => { - const { - isGSNAvailable, - } = gsnConfig; - const steps = registerSteps[isGSNAvailable ? 'gsn' : 'metamask'].slice(1); - const fetchInitialData = async () => { + const gsnConfig = await getGSNConfig(); + const { + isGSNAvailable, + } = gsnConfig; + const steps = registerSteps[isGSNAvailable ? 'gsn' : 'metamask'].slice(1); + const { keyStore, pwDerivedKey, @@ -30,6 +28,7 @@ const RegisterAddress = ({ return { ...currentAccount, + steps, keyStore, pwDerivedKey, AZTECaddress, @@ -48,7 +47,6 @@ const RegisterAddress = ({ return ( { - const { - address: currentAddress, - } = currentAccount; - const { - isGSNAvailable, - proxyContract, - } = gsnConfig; - const steps = isGSNAvailable ? sendSteps.gsn : sendSteps.metamask; - const sender = isGSNAvailable ? proxyContract : currentAddress; - const fetchInitialData = async () => { + const gsnConfig = await getGSNConfig(); + const { + isGSNAvailable, + proxyContract, + } = gsnConfig; + const { + address: currentAddress, + } = currentAccount; + const steps = isGSNAvailable ? sendSteps.gsn : sendSteps.metamask; + const sender = isGSNAvailable ? proxyContract : currentAddress; + const asset = await makeAsset(assetAddress); const parsedTransactions = parseInputTransactions(transactions); const amount = parsedTransactions.reduce((sum, tx) => sum + tx.amount, 0); const userAccessAccounts = await apis.account.batchGetExtensionAccount(userAccess); return { + steps, assetAddress, currentAddress, asset, @@ -55,7 +56,6 @@ const Send = ({ return ( @@ -71,7 +71,6 @@ Send.propTypes = { numberOfInputNotes: PropTypes.number, numberOfOutputNotes: PropTypes.number, userAccess: PropTypes.arrayOf(PropTypes.string), - gsnConfig: gsnConfigShape.isRequired, }; Send.defaultProps = { diff --git a/packages/extension/src/ui/pages/Withdraw.jsx b/packages/extension/src/ui/pages/Withdraw.jsx index c2aef0923..017aaa103 100644 --- a/packages/extension/src/ui/pages/Withdraw.jsx +++ b/packages/extension/src/ui/pages/Withdraw.jsx @@ -1,12 +1,12 @@ import React from 'react'; import PropTypes from 'prop-types'; import { - gsnConfigShape, inputAmountType, } from '~/ui/config/propTypes'; import { emptyIntValue, } from '~/ui/config/settings'; +import getGSNConfig from '~/ui/helpers/getGSNConfig'; import parseInputAmount from '~/ui/utils/parseInputAmount'; import makeAsset from '~/ui/utils/makeAsset'; import StepsHandler from '~/ui/views/handlers/StepsHandler'; @@ -19,22 +19,22 @@ const Withdraw = ({ amount, to, numberOfInputNotes, - gsnConfig, }) => { - const { - address: currentAddress, - } = currentAccount; - const { - isGSNAvailable, - proxyContract, - } = gsnConfig; - const steps = isGSNAvailable ? withdrawSteps.gsn : withdrawSteps.metamask; - const sender = isGSNAvailable ? proxyContract : currentAddress; - const fetchInitialData = async () => { + const { + address: currentAddress, + } = currentAccount; + const gsnConfig = await getGSNConfig(); + const { + isGSNAvailable, + proxyContract, + } = gsnConfig; + const steps = isGSNAvailable ? withdrawSteps.gsn : withdrawSteps.metamask; + const sender = isGSNAvailable ? proxyContract : currentAddress; const asset = await makeAsset(assetAddress); return { + steps, assetAddress, asset, currentAddress, @@ -48,7 +48,6 @@ const Withdraw = ({ return ( @@ -63,7 +62,6 @@ Withdraw.propTypes = { amount: inputAmountType.isRequired, to: PropTypes.string.isRequired, numberOfInputNotes: PropTypes.number, - gsnConfig: gsnConfigShape.isRequired, }; Withdraw.defaultProps = { diff --git a/packages/extension/src/utils/approveGSNFunction.js b/packages/extension/src/utils/approveGSNFunction.js deleted file mode 100644 index 93395c3fa..000000000 --- a/packages/extension/src/utils/approveGSNFunction.js +++ /dev/null @@ -1,47 +0,0 @@ -import { get } from '~/utils/storage'; -import ClientActionService from '~/background/services/ClientActionService'; -import { - actionRequestEvent, -} from '~/config/event'; - - -export default (query, connection) => async ({ - from, - to, - encodedFunctionCall, - txFee, - gasPrice, - gas, - nonce, - relayerAddress, - relayHubAddress, -}) => { - const apiKey = await get('apiKey') || 'test1234'; - const networkId = await get('networkId'); - - const params = { - from, - to, - encodedFunctionCall, - txFee, - gasPrice, - gas, - nonce, - relayerAddress, - relayHubAddress, - apiKey, - networkId, - }; - - - const response = await ClientActionService.triggerClientAction({ - ...query, - data: { - type: actionRequestEvent, - action: 'gsn.sign.transaction', - params, - }, - }, connection); - - return response.data.approvalData; -}; diff --git a/packages/extension/src/utils/checkQuota.js b/packages/extension/src/utils/checkQuota.js deleted file mode 100644 index 2607887fd..000000000 --- a/packages/extension/src/utils/checkQuota.js +++ /dev/null @@ -1,24 +0,0 @@ -import { get } from '~/utils/storage'; -import { - SIGNING_PROVIDER, -} from '~/config/constants'; - -export default async () => { - const apiKey = await get('apiKey'); - const networkId = await get('networkId'); - let hasFreeTransactions = false; - // we need to check the quota - if (apiKey) { - const lambdaResponse = await window.fetch(`${SIGNING_PROVIDER}/Stage/${networkId}/${apiKey}`, { - method: 'GET', // *GET, POST, PUT, DELETE, etc. - mode: 'cors', // no-cors, *cors, same-origin - cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached - credentials: 'same-origin', // include, *same-origin, omit - }); - - const { data } = await lambdaResponse.json(); - ({ hasFreeTransactions } = data); - } - - return !!apiKey && hasFreeTransactions; -}; diff --git a/packages/extension/src/utils/getGSNConfig.js b/packages/extension/src/utils/getGSNConfig.js deleted file mode 100644 index 3b4beb0d8..000000000 --- a/packages/extension/src/utils/getGSNConfig.js +++ /dev/null @@ -1,13 +0,0 @@ -import Web3Service from '~/helpers/Web3Service'; - -export default async function getGsnConfig() { - // TODO: check apiKey - const isGSNAvailable = Web3Service.networkId > 0; - - const proxyContract = Web3Service.getAddress('AccountRegistry'); - - return { - isGSNAvailable, - proxyContract, - }; -}