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

Develop #204

Merged
merged 2 commits into from
Dec 15, 2022
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
63 changes: 36 additions & 27 deletions app/controllers/api/v1/community-member/multiSwap/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ module.exports = function (router: any) {

let address = null;
let sourceNetwork = null;
let destinationNetwork = null;
let swapTransaction = null;

if (!req.params.txId || !req.query.sourceNetworkId) {
return res.http400('txId & sourceNetworkId are required.');
if (!req.params.txId || !req.query.sourceNetworkId || !req.query.destinationNetworkId) {
return res.http400('txId & sourceNetworkId & destinationNetworkId are required.');
}

if (!mongoose.Types.ObjectId.isValid(req.query.sourceNetworkId)) {
return res.http400('Invalid sourceNetworkId');
}

if (!mongoose.Types.ObjectId.isValid(req.query.destinationNetworkId)) {
return res.http400('Invalid destinationNetworkId');
}

let oldSwapTransaction = await db.SwapAndWithdrawTransactions.findOne({receiveTransactionId: req.params.txId});
if(oldSwapTransaction){
return res.http200({
Expand All @@ -28,13 +33,15 @@ module.exports = function (router: any) {
}

sourceNetwork = await db.Networks.findOne({ _id: req.query.sourceNetworkId });

destinationNetwork = await db.Networks.findOne({ _id: req.query.destinationNetworkId });
console.log(sourceNetwork?._id)
console.log(destinationNetwork?._id)
if(sourceNetwork){
req.query.smartContractAddress = await smartContractHelper.getSmartContractAddressByNetworkIdAndTag(sourceNetwork._id, '#fiberFund');
}

if (address && req.query.smartContractAddress) {
let receipt = await swapTransactionHelper.getTransactionReceiptByTxIdUsingWeb3(sourceNetwork, req.params.txId, req.query.smartContractAddress);
let receipt = await swapTransactionHelper.getTransactionReceiptByTxIdUsingWeb3(sourceNetwork, req.params.txId);

if(receipt.code == 401){
return res.http400(await commonFunctions.getValueFromStringsPhrase(receipt.message), receipt.message);
Expand All @@ -45,15 +52,26 @@ module.exports = function (router: any) {
receipt = receipt.data;
if (receipt) {
receipt.sourceSmartContractAddress = req.query.smartContractAddress;
swapTransaction = await swapTransactionHelper.swapTransactionSummary(receipt, utils.expectedSchemaVersionV1_0);
console.log('come here')
swapTransaction = await swapTransactionHelper.swapTransactionSummary(sourceNetwork, receipt);
}

if(swapTransaction ){
swapTransaction.sourceNetwork = receipt.fromNetwork._id;
swapTransaction.destinationNetwork = receipt.toNetwork._id;
swapTransaction.destinationCabn = (await db.CurrencyAddressesByNetwork.findOne({tokenContractAddress: req.query.sourceTokenContractAddress}))._id;
swapTransaction.sourceCabn = (await db.CurrencyAddressesByNetwork.findOne({tokenContractAddress: req.query.destinationTokenContractAddress}))._id;
if(swapTransaction){

let sourceCabn = await db.CurrencyAddressesByNetwork.findOne({_id: req.query.sourceCabn});
let destinationCabn = await db.CurrencyAddressesByNetwork.findOne({_id: req.query.destinationCabn});
console.log('sourceCabn',sourceCabn);
console.log('destinationCabn',destinationCabn);
console.log('swapTransaction.sourceAmount',swapTransaction.sourceAmount);

if(swapTransaction.sourceAmount){
swapTransaction.sourceAmount = await swapUtilsHelper.amountToHuman_(sourceNetwork, sourceCabn, swapTransaction.sourceAmount);
}
console.log(sourceNetwork?._id)
console.log(destinationNetwork?._id)
swapTransaction.sourceNetwork = sourceNetwork?._id;
swapTransaction.destinationNetwork = destinationNetwork?._id;
swapTransaction.destinationCabn = destinationCabn?._id;
swapTransaction.sourceCabn = sourceCabn?._id;
swapTransaction.createdByUser = req.user._id;
swapTransaction.createdAt = new Date();
swapTransaction.updatedAt = new Date();
Expand All @@ -62,16 +80,6 @@ module.exports = function (router: any) {
swapAndWithdrawTransaction: swapTransaction
})
}


// console.log('swapTransaction', swapTransaction);
// let hash = signatureHelper.bridgeHashV1_0(swapTransaction, swap);
// console.log('final hash', hash);
// let msgHash = hash.replace('0x', '');
// let signatureResponse = await ecdsaHelper.sign('915c8bf73c84c0482beef48bb4bf782892d38d57d3c9af32de6af27a54d12c5a', msgHash);
// console.log(signatureResponse);
// let withdrawSigned = await contractHelper.withdrawSigned(address, swapTransaction, swap.toNetwork, '0x544db7e44d73761c00cfc4525c38f896bd20b69fb8487ba12f2a21af013502d565d02ade27351679f16c832cc1eaf6202a831ad2ecec47fbf2fbfe4a70bafcb11c');
// console.log(withdrawSigned);
}

return res.http400('Invalid txId, sourceNetwork or smartContractAddress');
Expand Down Expand Up @@ -224,7 +232,7 @@ module.exports = function (router: any) {
return res.http400('txId and withdrawTxId required.');
}

let oldSwapTransaction = await db.SwapAndWithdrawTransactions.findOne({receiveTransactionId: req.params.txId}).populate('sourceNetwork').populate('destinationNetwork');
let oldSwapTransaction = await db.SwapAndWithdrawTransactions.findOne({receiveTransactionId: req.params.txId}).populate('sourceNetwork').populate('destinationNetwork').populate('sourceCabn').populate('destinationCabn');

if (req.user) {
address = await db.Addresses.findOne({ user: req.user._id });
Expand All @@ -234,20 +242,21 @@ module.exports = function (router: any) {
destinationNetwork = oldSwapTransaction.destinationNetwork;

if(sourceNetwork){
req.query.smartContractAddress = await smartContractHelper.getSmartContractAddressByNetworkIdAndTag(sourceNetwork._id, '#fiberFund');
req.query.smartContractAddress = await smartContractHelper.getSmartContractAddressByNetworkIdAndTag(destinationNetwork._id, '#fiberFund');
}

if (address && sourceNetwork && oldSwapTransaction && req.query.smartContractAddress) {
let receipt = await swapTransactionHelper.getTransactionReceiptStatusByTxIdUsingWeb3(destinationNetwork, req.body.withdrawTxId, req.query.smartContractAddress);
if (address && sourceNetwork && oldSwapTransaction) {
let receipt = await swapTransactionHelper.getTransactionReceiptStatusByTxIdUsingWeb3(destinationNetwork, req.body.withdrawTxId);

if(receipt.code == 401){
return res.http400(await commonFunctions.getValueFromStringsPhrase(receipt.message), receipt.message);
}else if (receipt.code == 400){
return res.http400(receipt.message);
}
console.log(receipt);
receipt = receipt.data;

if(receipt.destinationAmount){
oldSwapTransaction.destinationAmount = await swapUtilsHelper.amountToHuman_(destinationNetwork, oldSwapTransaction.destinationCabn, receipt.destinationAmount);
}
if(receipt.status == 'swapWithdrawFailed'){
oldSwapTransaction.status = 'swapWithdrawFailed'
}else if(receipt.status == 'swapWithdrawCompleted'){
Expand Down
33 changes: 23 additions & 10 deletions app/controllers/api/v1/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = function (router: any) {
var matchFilter: any = {}
var filterOrList: any= []
var filterAndList: any= []
var filter = []
var filter: any = []
var innerFilter: any = []
let wbns = []
var sort: any = { "createdAt": -1 }

Expand All @@ -31,32 +32,44 @@ module.exports = function (router: any) {
sort = { [req.query.sortKey] : parseInt(req.query.sortOrder)}
}

if (req.query.isPagination != null && req.query.isPagination == 'false') {

filter = [
if (!req.query.networkId) {
innerFilter = [
{ $lookup: { from: 'networks', localField: 'network', foreignField: '_id', as: 'network' } },
{ "$unwind": { "path": "$network","preserveNullAndEmptyArrays": true}},
{ $lookup: { from: 'wallets', localField: 'wallet', foreignField: '_id', as: 'wallet' } },
{ "$unwind": { "path": "$wallet","preserveNullAndEmptyArrays": true}},
{ "$group": { _id: { wallet: "$wallet" } } },
{ "$project": { _id: 0, wallet: "$_id.wallet"} },
{ "$match": matchFilter },
{ "$sort": sort }
];

} else {

filter = [
}else {
innerFilter = [
{ $lookup: { from: 'networks', localField: 'network', foreignField: '_id', as: 'network' } },
{ "$unwind": { "path": "$network","preserveNullAndEmptyArrays": true}},
{ $lookup: { from: 'wallets', localField: 'wallet', foreignField: '_id', as: 'wallet' } },
{ "$unwind": { "path": "$wallet","preserveNullAndEmptyArrays": true}},
{ "$match": matchFilter },
{ "$sort": sort },
{ "$sort": sort }
];
}

if (req.query.isPagination != null && req.query.isPagination == 'false') {

filter = [
...innerFilter
];

} else {

filter = [
...innerFilter,
{ $skip: req.query.offset ? parseInt(req.query.offset) : 0 },
{ $limit: req.query.limit ? parseInt(req.query.limit) : 10 },
];

}

wbns = await db.WalletByNetwork.aggregate(filter);

return res.http200({
Expand Down
Loading