Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api for get balance ( external users ) #410

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions controllers/external.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
const { externalUpdateStatforUser } = require('../helpers/common')
const Big = require('big.js')
const upload = multer({ dest: 'uploads/' });

const {
getWeb3Instance,
formatTokenBalance,
getNativeBalance,
} = require('../web3/wallets')
const { Constants, TronConstant } = require('../conf/const')
const {
getInstagramUserName,
findBountyOracle,
Expand Down Expand Up @@ -720,12 +725,13 @@
try {
let campaign = req.body
const user = await UserExternalWallet.findOne({ walletId: req.address })

campaign.idNode = user.UserId
campaign.createdAt = Date.now()
campaign.updatedAt = Date.now()
campaign.type = 'draft'
console.log({campaign});
let draft = await Campaigns.create(campaign)
console.log({draft});
return responseHandler.makeResponseData(res, 200, 'success', draft)
} catch (err) {
return responseHandler.makeResponseError(
Expand Down Expand Up @@ -1397,8 +1403,8 @@
? JSON.parse(req.query.idWallet)
: req.query.idWallet
if (idWallet) {
let userId = await UserExternalWallet.findOne({walletId: idWallet});
var idNode = userId.UserId;
let user = await UserExternalWallet.findOne({walletId: idWallet});

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
var idNode = user.UserId;
strangerDraft = await Campaigns.distinct('_id', {
idNode: { $ne: idNode },
hash: { $exists: false },
Expand All @@ -1411,7 +1417,6 @@
let query = sortOutPublic(req, idNode, strangerDraft)

let count = await Campaigns.countDocuments()
console.log({count});
let tri = [['draft', 'apply', 'inProgress', 'finished'], '$type']
let campaigns = await Campaigns.aggregate([
{
Expand Down Expand Up @@ -1462,3 +1467,49 @@
)
}
}


exports.getBalanceUserExternal = async (req, res) => {
try {
const { network, walletAddress, smartContract, isNative } = req.body

// Initialize the appropriate Web3 instance based on the network
const web3Instance = await getWeb3Instance(network)

if (!isNative) {
const contract = new web3Instance.eth.Contract(
Constants.token.abi,
smartContract
)
const [balance, decimals] = await Promise.all([
contract.methods.balanceOf(walletAddress).call(),
contract.methods.decimals().call(),
])
const balanceFormatted = formatTokenBalance(balance, decimals)
return responseHandler.makeResponseData(
res,
200,
'success',
balanceFormatted
)
} else {
const balance = await getNativeBalance(
web3Instance,
walletAddress,
network
)
return responseHandler.makeResponseData(
res,
200,
'success',
balance
)
}
} catch (err) {
return responseHandler.makeResponseError(
res,
500,
err.message ? err.message : err.error
)
}
}
5 changes: 3 additions & 2 deletions routes/external.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
checkHarvest,
externalAnswer,
externalGains,
campaigns
campaigns,
getBalanceUserExternal
} = require('../controllers/external.controller');

const {
Expand All @@ -51,8 +52,8 @@


router.post('/create-user', createUserFromExternalWallet)
router.get('/campaigns', campaigns)

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.
This route handler performs
a database access
, but is not rate-limited.
This route handler performs
a database access
, but is not rate-limited.

router.post('/getBalance', getBalanceUserExternal)
router.post('/createCampaign', verifySignatureMiddleware, externalSaveCampaign)
// DONE
router.get('/socialAccounts', verifySignatureMiddleware , externalSocialAccounts)
Expand Down
Loading