From 9170962185c1bd84728b64201a7a0ac113fe519a Mon Sep 17 00:00:00 2001 From: jonybang Date: Sun, 14 Jul 2019 17:59:15 +0200 Subject: [PATCH] cyb transfer page and errors handling --- .../Cabinet/CyberDCabinet/CyberDCabinet.html | 3 +- .../CyberDCabinet/LinkContent/LinkContent.ts | 40 ++++++----- .../SaveAndLinkContent/SaveAndLinkContent.ts | 47 ++++++------ .../TransferCyb/TransferCyb.html | 31 ++++++++ .../CyberDCabinet/TransferCyb/TransferCyb.ts | 52 ++++++++++++++ src/popup/router/routes.ts | 6 ++ src/services/cyberd.ts | 71 ++++++++++++++++++- 7 files changed, 211 insertions(+), 39 deletions(-) create mode 100644 src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.html create mode 100644 src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.ts diff --git a/src/popup/pages/Cabinet/CyberDCabinet/CyberDCabinet.html b/src/popup/pages/Cabinet/CyberDCabinet/CyberDCabinet.html index d88a9aa..994cfee 100644 --- a/src/popup/pages/Cabinet/CyberDCabinet/CyberDCabinet.html +++ b/src/popup/pages/Cabinet/CyberDCabinet/CyberDCabinet.html @@ -14,7 +14,8 @@

- Link + Link + Send CYB
diff --git a/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts b/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts index eb47416..2822a97 100644 --- a/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts +++ b/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts @@ -15,25 +15,33 @@ export default { async link() { const keywordHashes = await addIpfsContentArray(this.resultKeywords); - const results = await pIteration.mapSeries(keywordHashes, async keywordHash => { - return CyberD.link( - { - address: this.currentAccount.address, - privateKey: await AppWallet.decryptByPassword(this.currentAccount.encryptedPrivateKey), - }, - keywordHash, - this.resultContentHash - ); - }); + try { + const results = await pIteration.mapSeries(keywordHashes, async keywordHash => { + return CyberD.link( + { + address: this.currentAccount.address, + privateKey: await AppWallet.decryptByPassword(this.currentAccount.encryptedPrivateKey), + }, + keywordHash, + this.resultContentHash + ); + }); - console.log('link results', results); + console.log('link results', results); - this.$notify({ - type: 'success', - text: 'Successfully linked', - }); + this.$notify({ + type: 'success', + text: 'Successfully linked', + }); - this.$router.push({ name: 'cabinet-cyberd' }); + this.$router.push({ name: 'cabinet-cyberd' }); + } catch (e) { + this.$notify({ + type: 'error', + title: e && e.message ? e.message : e || 'Unknown error', + text: e && e.data ? e.data : '', + }); + } }, }, computed: { diff --git a/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts b/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts index 494e91c..b96bccc 100644 --- a/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts +++ b/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts @@ -21,29 +21,36 @@ export default { mimeType: this.$route.query.mimeType, }); - if (this.linkKeywords) { - const keywordHashes = await addIpfsContentArray(this.resultKeywords); + try { + if (this.linkKeywords) { + const keywordHashes = await addIpfsContentArray(this.resultKeywords); - const results = await pIteration.mapSeries(keywordHashes, async keywordHash => { - return CyberD.link( - { - address: this.currentAccount.address, - privateKey: await AppWallet.decryptByPassword(this.currentAccount.encryptedPrivateKey), - }, - keywordHash, - this.resultContentHash - ); - }); - - console.log('link results', results); - } + const results = await pIteration.mapSeries(keywordHashes, async keywordHash => { + return CyberD.link( + { + address: this.currentAccount.address, + privateKey: await AppWallet.decryptByPassword(this.currentAccount.encryptedPrivateKey), + }, + keywordHash, + this.resultContentHash + ); + }); - this.$notify({ - type: 'success', - text: this.linkKeywords ? 'Successfully saved and linked' : 'Successfully saved', - }); + console.log('link results', results); + } - this.$router.push({ name: 'cabinet-cyberd' }); + this.$notify({ + type: 'success', + text: this.linkKeywords ? 'Successfully saved and linked' : 'Successfully saved', + }); + this.$router.push({ name: 'cabinet-cyberd' }); + } catch (e) { + this.$notify({ + type: 'error', + title: e && e.message ? e.message : e || 'Unknown error', + text: e && e.data ? e.data : '', + }); + } }, }, computed: { diff --git a/src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.html b/src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.html new file mode 100644 index 0000000..d7ca956 --- /dev/null +++ b/src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.html @@ -0,0 +1,31 @@ +
+
+ < Back +
+ +

Send CYB

+ +
+

To address:

+ +
+ +
+

Amount: (GCYB)

+ +
+ + + + + + + + + + + +
+ +
+
diff --git a/src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.ts b/src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.ts new file mode 100644 index 0000000..17ae088 --- /dev/null +++ b/src/popup/pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb.ts @@ -0,0 +1,52 @@ +import { AppWallet, StorageVars } from '../../../../../services/data'; +import { CyberD } from '../../../../../services/cyberd'; +import ContentDetails from '../../../../directives/ContentDetails/ContentDetails'; + +export default { + template: require('./TransferCyb.html'), + components: { ContentDetails }, + created() { + this.inputKeywordsStr = this.keywordsStr; + }, + methods: { + async transfer() { + try { + const result = await CyberD.transfer( + { + address: this.currentAccount.address, + privateKey: await AppWallet.decryptByPassword(this.currentAccount.encryptedPrivateKey), + }, + this.addressTo, + this.amount + ); + console.log('transfer result', result); + this.$notify({ + type: 'success', + text: 'Successfully transfered', + }); + + this.$router.push({ name: 'cabinet-cyberd' }); + } catch (e) { + this.$notify({ + type: 'error', + title: e && e.message ? e.message : e || 'Unknown error', + text: e && e.data ? e.data : '', + }); + } + }, + }, + computed: { + currentAccount() { + return this.$store.state[StorageVars.Account]; + }, + disableTransfer() { + return !this.addressTo || !this.amount; + }, + }, + data() { + return { + addressTo: '', + amount: null, + }; + }, +}; diff --git a/src/popup/router/routes.ts b/src/popup/router/routes.ts index b7ecf94..e4abdcd 100644 --- a/src/popup/router/routes.ts +++ b/src/popup/router/routes.ts @@ -17,6 +17,7 @@ import LinkContent from '../pages/Cabinet/CyberDCabinet/LinkContent/LinkContent' import Backup from '../pages/Cabinet/Backup/Backup'; import AskRestoreBackup from '../pages/NewWallet/AskRestoreBackup/AskRestoreBackup'; import LogIn from '../pages/NewWallet/LogIn/LogIn'; +import TransferCyb from '../pages/Cabinet/CyberDCabinet/TransferCyb/TransferCyb'; export default [ { @@ -48,6 +49,11 @@ export default [ name: 'cabinet-cyberd-link', component: LinkContent, }, + { + path: '/cyberd/transfer', + name: 'cabinet-cyberd-transfer', + component: TransferCyb, + }, { path: '/cyberd/save-and-link', name: 'cabinet-cyberd-save-and-link', diff --git a/src/services/cyberd.ts b/src/services/cyberd.ts index 9986d9c..f0ecfd5 100644 --- a/src/services/cyberd.ts +++ b/src/services/cyberd.ts @@ -2,6 +2,7 @@ import EthData from '@galtproject/frontend-core/libs/EthData'; const cyberjsBuilder = require('@litvintech/cyberjs/builder'); const cyberjsCodec = require('@litvintech/cyberjs/codec'); +const cyberjsConstants = require('@litvintech/cyberjs/constants'); const axios = require('axios'); const node = 'http://88.198.36.117:26657'; const indexedNode = 'http://88.198.36.117:26657'; @@ -100,7 +101,73 @@ export class CyberD { method: 'get', url: `${node}/submit_signed_link?data="${signedSendHex}"`, }) - .then(response => response.data) - .catch(error => console.error('Cannot send', error)); + .then(res => { + if (!res.data) { + throw new Error('Empty data'); + } + if (res.data.error) { + throw res.data.error; + } + return res.data; + }) + .catch(error => { + console.error('Link error', error); + throw error; + }); + } + + static async transfer(txOptions, addressTo, gAmount) { + const chainId = await this.getNetworkId(); + const addressInfo = await axios({ + method: 'get', + url: `${node}/account?address="${txOptions.address}"`, + }); + + if (!addressInfo.data.result) { + return console.error('error: addressInfo.data.result undefined'); + } + const account = addressInfo.data.result.account; + if (!account) { + return console.error('error: addressInfo.data.result.account undefined'); + } + + const acc = { + address: account.address, + chain_id: chainId, + account_number: parseInt(account.account_number, 10), + sequence: parseInt(account.sequence, 10), + }; + + const amount = parseFloat(gAmount) * 10 ** 9; + + const sendRequest = { + acc, + amount, + from: account.address, + // to: cyberjsCodec.bech32.toBech32(cyberjsConstants.CyberdNetConfig.PREFIX_BECH32_ACCADDR, addressTo), + to: addressTo, + type: 'send', + }; + + const txRequest = cyberjsBuilder.buildAndSignTxRequest(sendRequest, txOptions.privateKey, chainId); + const signedSendHex = cyberjsCodec.hex.stringToHex(JSON.stringify(txRequest)); + + return axios({ + method: 'get', + url: `${node}/submit_signed_send?data="${signedSendHex}"`, + }) + .then(res => { + if (!res.data) { + throw new Error('Empty data'); + } + if (res.data.error) { + throw res.data.error; + } + return res.data; + }) + .catch(error => { + console.error('Transfer error', error); + throw error; + }); } }