Skip to content

Commit

Permalink
Merge pull request #357 from oraclesorg/separate-files-and-zip
Browse files Browse the repository at this point in the history
(sync) bridge <- separate-files-and-zip
  • Loading branch information
vbaranov committed Nov 9, 2017
2 parents 5bf9c8b + 35b5dc8 commit d9160bc
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 134 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"clipboard": "^1.7.1",
"install": "^0.10.1",
"jquery": "^3.2.1",
"jszip": "^3.1.4",
"mobx": "^3.3.0",
"mobx-react": "^4.3.3",
"npm": "^5.3.0",
Expand Down
74 changes: 52 additions & 22 deletions src/components/stepFour/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import {
setReleaseAgentRecursive,
updateJoinedCrowdsalesRecursive,
transferOwnership,
setReservedTokensListMultiple,
setLastCrowdsale
setReservedTokensListMultiple
} from './utils'
import {download, handleContractsForFile, handleTokenForFile, handleCrowdsaleForFile, handlePricingStrategyForFile, handleFinalizeAgentForFile, handleConstantForFile, scrollToBottom } from './utils'
import { download, handleContractsForFile, handleConstantForFile, handlerForFile, scrollToBottom } from './utils'
import { noMetaMaskAlert, noContractDataAlert } from '../../utils/alerts'
import { defaultState, FILE_CONTENTS, DOWNLOAD_NAME, DOWNLOAD_TYPE, TOAST } from '../../utils/constants'
import { defaultState, FILE_CONTENTS, DOWNLOAD_TYPE, TOAST } from '../../utils/constants'
import { getOldState, toFixed, floorToDecimals, toast } from '../../utils/utils'
import { getEncodedABIClientSide } from '../../utils/microservices'
import { stepTwo } from '../stepTwo'
Expand All @@ -25,6 +24,7 @@ import { DisplayTextArea } from '../Common/DisplayTextArea'
import { Loader } from '../Common/Loader'
import { NAVIGATION_STEPS, TRUNC_TO_DECIMALS } from '../../utils/constants'
import { copy } from '../../utils/copy';
import JSZip from 'jszip'
const { PUBLISH } = NAVIGATION_STEPS

export class stepFour extends stepTwo {
Expand Down Expand Up @@ -142,32 +142,62 @@ export class stepFour extends stepTwo {
});
}*/

handleContentByParent(content, docData) {
switch(content.parent) {
case 'token':
return handleTokenForFile(content, docData, this.state)
handleContentByParent(content, index = 0) {
const { parent } = content

switch (parent) {
case 'crowdsale':
return handleCrowdsaleForFile(content, docData, this.state)
case 'contracts':
return handleContractsForFile(content, docData, this.state)
case 'pricingStrategy':
return handlePricingStrategyForFile(content, docData, this.state)
case 'finalizeAgent':
return handleFinalizeAgentForFile(content, docData, this.state)
return handlerForFile(content, this.state[parent][0])
case 'token':
return handlerForFile(content, this.state[parent])
case 'contracts':
return handleContractsForFile(content, this.state, index)
case 'none':
return handleConstantForFile(content, docData)
return handleConstantForFile(content)
}
}

downloadCrowdsaleInfo() {
var docData = { data: '' }
FILE_CONTENTS.forEach(content => {
this.handleContentByParent(content, docData)
downloadCrowdsaleInfo = () => {
const zip = new JSZip()
const commonHeader = FILE_CONTENTS.common.map(content => this.handleContentByParent(content))
const contractsKeys = FILE_CONTENTS.files.order
const NEW_LINE = '\n\n'
const orderNumber = order => order.toString().padStart(3, '0');
let prefix = 1

contractsKeys.forEach(key => {
if (this.state.contracts.hasOwnProperty(key)) {
const { txt, sol, name } = FILE_CONTENTS.files[key]
const { abiConstructor } = this.state.contracts[key]

const tiersCount = Array.isArray(abiConstructor) ? abiConstructor.length : 1

for (let tier = 0; tier < tiersCount; tier++) {
const suffix = tiersCount > 1 ? `_${tier + 1}` : ''
const solFilename = `${orderNumber(prefix++)}_${name}${suffix}`
const txtFilename = `${orderNumber(prefix++)}_${name}${suffix}`

zip.file(
`${solFilename}.sol`,
this.handleContentByParent(sol)
)
zip.file(
`${txtFilename}.txt`,
commonHeader.concat(txt.map(content => this.handleContentByParent(content, tier))).join(NEW_LINE)
)
}
}
})
console.debug('docDAta', docData.data)
const tokenAddr = this.state.contracts ? this.state.contracts.token.addr : '';
return getDownloadName(tokenAddr)
.then(downloadName => download(docData.data, downloadName, DOWNLOAD_TYPE));

zip.generateAsync({ type: DOWNLOAD_TYPE.blob })
.then(content => {
const tokenAddr = this.state.contracts ? this.state.contracts.token.addr : '';

getDownloadName(tokenAddr)
.then(downloadName => download({ zip: content, filename: downloadName }))
})
}

deploySafeMathLibrary = () => {
Expand Down
138 changes: 66 additions & 72 deletions src/components/stepFour/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,91 +308,85 @@ export function setReleaseAgentRecursive (i, web3, abi, addr, finalizeAgentAddrs
})
}

export const handleTokenForFile = (content, docData, state) => {
const title = content.value
const fileContent = title + state.token[content.field]
docData.data += fileContent + '\n\n'
export const handlerForFile = (content, type) => {
let suffix = ''
let checkIfTime = content.field == "startTime" || content.field == "endTime"
if (checkIfTime) {
let timezoneOffset = (new Date()).getTimezoneOffset()/60
let operator = timezoneOffset > 0 ? "-" : "+"
suffix = " (GMT " + operator + " " + Math.abs(timezoneOffset) + ")"
}
return `${content.value}${type[content.field]}` + suffix
}

export const handleCrowdsaleForFile = (content, docData, state) => {
const title = content.value
const fileContent = title + state.crowdsale[0][content.field]
docData.data += fileContent + '\n\n'
export const handleConstantForFile = content => {
return `${content.value}${content.fileValue}`
}

export const handlePricingStrategyForFile = (content, docData, state) => {
const title = content.value
const fileContent = title + state.pricingStrategy[0][content.field]
docData.data += fileContent + '\n\n'
}
export const handleContractsForFile = (content, state, index) => {
const title = content.value
const { field } = content
let fileContent = ''

export const handleFinalizeAgentForFile = (content, docData, state) => {
const title = content.value
const fileContent = title + state.finalizeAgent[0][content.field]
docData.data += fileContent + '\n\n'
}
if (field !== 'src' && field !== 'abi' && field !== 'addr') {
const contractField = state.contracts[content.child][field]
let fileBody

export const handleContractsForFile = (content, docData, state) => {
const title = content.value
if(content.field !== 'src' && content.field !== 'abi' && content.field !== 'addr') {
let fileBody
if ( Object.prototype.toString.call( state.contracts[content.child][content.field] ) === '[object Array]' ) {
for (let i = 0; i < state.contracts[content.child][content.field].length; i++) {
fileBody = state.contracts[content.child][content.field][i]

if (!fileBody) return
let fileContent = title + " for " + state.crowdsale[i].tier + ":**** \n \n" + fileBody
docData.data += fileContent + '\n\n'
}
} else {
fileBody = state.contracts[content.child][content.field]
if (!fileBody) return
let fileContent = title + ":**** \n \n" + fileBody
docData.data += fileContent + '\n\n'
}
} else {
addSrcToFile(content, docData, state)
if (Array.isArray(contractField)) {
fileBody = contractField[index]

if (!!fileBody) {
fileContent = title + ' for ' + state.crowdsale[index].tier + ':**** \n\n' + fileBody
}
} else if (!!contractField) {
fileContent = title + ':**** \n\n' + contractField
}
}
} else {
fileContent = addSrcToFile(content, state, index)
}

export const handleConstantForFile = (content, docData) => {
const title = content.value
const fileContent = title + content.fileValue
docData.data += fileContent + '\n\n'
return fileContent
}

const addSrcToFile = (content, docData, state) => {
const title = content.value

if ( Object.prototype.toString.call( state.contracts[content.child][content.field] ) === '[object Array]' && content.field !== 'abi') {
for (let i = 0; i < state.contracts[content.child][content.field].length; i++) {
const body = state.contracts[content.child][content.field][i]
const text = title + " for " + state.crowdsale[i].tier + ": " + body
docData.data += text + '\n\n'
}
const addSrcToFile = (content, state, index) => {
const title = content.value
const { field } = content
const contractField = state.contracts[content.child][field]
let fileContent = ''

if (Array.isArray(contractField) && field !== 'abi') {
fileContent = title + ' for ' + state.crowdsale[index].tier + ': ' + contractField[index]
} else {
if (field !== 'src') {
const body = field === 'abi' ? JSON.stringify(contractField) : contractField
fileContent = title + body
} else {
const body = content.field === 'abi' ? JSON.stringify(state.contracts[content.child][content.field]) : state.contracts[content.child][content.field]
const text = title + body
docData.data += text + '\n\n'
fileContent = contractField
}
}

return fileContent
}

export const download = (data, filename, type) => {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
export const download = ({ data = {}, filename = '', type = '', zip = '' }) => {
let file = !zip ? new Blob([data], { type: type }) : zip

if (window.navigator.msSaveOrOpenBlob) { // IE10+
window.navigator.msSaveOrOpenBlob(file, filename)
} else { // Others
let a = document.createElement('a')
let url = URL.createObjectURL(file)

a.href = url
a.download = filename
document.body.appendChild(a)
a.click()

setTimeout(function () {
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}, 0)
}
}

export function scrollToBottom() {
Expand All @@ -412,7 +406,7 @@ export function getDownloadName (tokenAddress) {

return networkName;
})
.then((networkName) => `${DOWNLOAD_NAME}_${networkName}_${tokenAddress}.txt`);
.then((networkName) => `${DOWNLOAD_NAME}_${networkName}_${tokenAddress}`);

resolve(whenNetworkName);
});
Expand Down
Loading

0 comments on commit d9160bc

Please sign in to comment.