Merge pull request #60 from Instadapp/check-if-public-function-is-payable

check if public or external function is payable
This commit is contained in:
Thrilok kumar 2021-07-26 22:33:20 +05:30 committed by GitHub
commit 54fb216bba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -180,10 +180,20 @@ const parseCode = async (connector) => {
func = [] func = []
} }
} }
funcs = funcs const allPublicFuncs = funcs
.filter(({ raw }) => { .filter(({ raw }) => {
if ((raw.includes('external') || raw.includes('public')) && return raw.includes('external') || raw.includes('public')
raw.includes('returns')) { })
.map(f => {
const name = f.raw.split('(')[0].split('function')[1].trim()
return {
...f,
name
}
})
funcs = allPublicFuncs
.filter(({ raw }) => {
if (raw.includes('returns')) {
const returns = raw.split('returns')[1].split('(')[1].split(')')[0] const returns = raw.split('returns')[1].split('(')[1].split(')')[0]
return returns.includes('string') && returns.includes('bytes') return returns.includes('string') && returns.includes('bytes')
} }
@ -193,11 +203,9 @@ const parseCode = async (connector) => {
const args = f.raw.split('(')[1].split(')')[0].split(',') const args = f.raw.split('(')[1].split(')')[0].split(',')
.map(arg => arg.trim()) .map(arg => arg.trim())
.filter(arg => arg !== '') .filter(arg => arg !== '')
const name = f.raw.split('(')[0].split('function')[1].trim()
return { return {
...f, ...f,
args, args
name
} }
}) })
const eventsPath = `${connector.path}/events.sol` const eventsPath = `${connector.path}/events.sol`
@ -229,7 +237,8 @@ const parseCode = async (connector) => {
eventsFirstLines, eventsFirstLines,
mainEvents, mainEvents,
mainEventsLines, mainEventsLines,
funcs funcs,
allPublicFuncs
} }
} catch (error) { } catch (error) {
return Promise.reject(error) return Promise.reject(error)
@ -262,6 +271,21 @@ const checkComments = async (connector) => {
} }
} }
const checkPublicFuncs = async (connector) => {
try {
const errors = []
for (let i1 = 0; i1 < connector.allPublicFuncs.length; i1++) {
const { raw, firstLine, name } = connector.allPublicFuncs[i1]
if (!raw.includes('payable')) {
errors.push(`public function ${name} is not payable at ${connector.path}/main.sol:${firstLine}`)
}
}
return errors
} catch (error) {
return Promise.reject(error)
}
}
const checkName = async (connector) => { const checkName = async (connector) => {
try { try {
const strs = connector.code.split('\n') const strs = connector.code.split('\n')
@ -313,12 +337,14 @@ async function checkMain () {
const commentsErrors = await checkComments(connectors[index]) const commentsErrors = await checkComments(connectors[index])
const nameErrors = await checkName(connectors[index]) const nameErrors = await checkName(connectors[index])
const headCommentsErrors = await checkHeadComments(connectors[index]) const headCommentsErrors = await checkHeadComments(connectors[index])
const publicFuncsErrors = await checkPublicFuncs(connectors[index])
errors.push(...forbiddenErrors) errors.push(...forbiddenErrors)
errors.push(...eventsErrors) errors.push(...eventsErrors)
errors.push(...commentsErrors) errors.push(...commentsErrors)
errors.push(...nameErrors) errors.push(...nameErrors)
errors.push(...headCommentsErrors) errors.push(...headCommentsErrors)
errors.push(...publicFuncsErrors)
warnings.push(...eventsWarnings) warnings.push(...eventsWarnings)
} }
if (errors.length) { if (errors.length) {