diff --git a/.env b/.env index 2eef41ad..ede56aeb 100644 --- a/.env +++ b/.env @@ -2,14 +2,15 @@ NODE_MODE=deployed DATABASE_URL_LOCAL=mongodb://localhost:27017/V2-graphQL DATABASE_URL_ONLINE=mongodb+srv://admin:scytalelabs@cluster0-9yop5.mongodb.net/V2-graphQL-backend-demo?retryWrites=true&w=majority -FACTORY_CONTRACT=BE48B5C9cfb0901A57db92fCB1b97d31Bfe536439efdC54a94f4C8bd1508Fc3C -PAIR_CONTRACT=Cfc208B9cd2fb35918D621D4002e378579382131d9D3011ff5A47aF36e603b2F -PAIR_CONTRACT_PACKAGE=1A89d4cDC6735c6c68D206b1C6c5c5c62ED96954F893A028Fa33E54C556B99AB +FACTORY_CONTRACT=e135359661bd6F4D0A2f827e0e25F0B1E32bb44b538676c3aECBe543F93fc54F +FACTORY_CONTRACT_PACKAGE=9e00eE28318CDD9Ea3fc83eEad5EbEE916A798565d930b14638f196E725d537a +PAIR_CONTRACT=5B4C9719f6dac2F551bF0100Ff85B1469A1Ffd0F3b03Fd44125845404dF892a9 +PAIR_CONTRACT_PACKAGE=9b0C92894Bb7E9C0E657B2ba505C69263e435082Ac37dF982827061B86966B24 CALLEE_CONTRACT=fbfeda8b97f056f526f20c2fc2b486d9bdbfb3e46b9a164527e57c0c86e68612 -TOKEN0_CONTRACT=E3F80775fddda7872185705BAAcc07aae66cFa5a07D5445D64D0C01642428D2d -TOKEN0_CONTRACT_PACKAGE=De0Ea05d65072DE515d6400f3Cf0FD2169A45938902C56B109BBAe62c4677955 -TOKEN1_CONTRACT=f3c553Bed5D57B87FB1e7fDe02eC2a0355d64c0faD2fE872cB6DcC115197c1fe -TOKEN1_CONTRACT_PACKAGE=555282981d63c7fD7b66A4cDd747Ee30f9cdfC024b3bd2d918314e8b9A25b25C +TOKEN0_CONTRACT=20831b61f3052df217EF0e441F4D7f365757Ae81Cb4C932408621cb7bf20b1c2 +TOKEN0_CONTRACT_PACKAGE=4DBe7c61F20d8FF91d8de2A70919235e2DF92d8C6212ffDc06989Ae59dA19059 +TOKEN1_CONTRACT=ba446757a580880faCa3B9FB52a014AB3d61C88C8B175609c44e362749D871E8 +TOKEN1_CONTRACT_PACKAGE=ec9518af5E73C169B20979cf44Dd582E69FD0b9044Fb60B041c438c56e778Ff1 GRAPHQL=http://localhost:3000/graphql CHAIN_NAME=casper-test @@ -80,13 +81,13 @@ TOKEN_NAME=uniswap-router TOKEN_SYMBOL=DRAG TOKEN_META=origin fire,lifetime infinite -FACTORY=BE48B5C9cfb0901A57db92fCB1b97d31Bfe536439efdC54a94f4C8bd1508Fc3C +FACTORY=e135359661bd6F4D0A2f827e0e25F0B1E32bb44b538676c3aECBe543F93fc54F WCSPR=bA8f6a73B6D548E2CAe2d572aA2F824BC46A3C06a3BBA10e3d12B015931D3f85 LIBRARY=e0Dae7571C3dc5C957a36422c56BA0ccb8AECEC833F35e32874b75A8Ea8Ac4Db CONTRACT_NAME=UniSwapRouter -CONTRACT_HASH=10c1405f84E629D0678620BaFCbaf0EDdE598e8717007B33A401458224c46dE7 -PACKAGE_HASH=C8B976d8d1e122B1baEc32dd06c9DAA89f5277C1eB11ADd2414a23a273741258 +CONTRACT_HASH=28b10e242d640B1a549Be1Ed5597E81019C7C6F4C90c277ed605406FF47E5134 +PACKAGE_HASH=17100D7fbb04140e9739fB5ba0D71AD7a00b6f370316329Ee0553CF83783693b INSTALL_PAYMENT_AMOUNT=200000000000 MINT_ONE_PAYMENT_AMOUNT=2000000000 MINT_COPIES_PAYMENT_AMOUNT=100000000000 @@ -97,12 +98,12 @@ MINT_COPIES_COUNT=20 MINT_MANY_META_SIZE=5 MINT_MANY_META_COUNT=5 -TO = E3F80775fddda7872185705BAAcc07aae66cFa5a07D5445D64D0C01642428D2d -DEADLINE = 1639403428465 +TO = 20831b61f3052df217EF0e441F4D7f365757Ae81Cb4C932408621cb7bf20b1c2 +DEADLINE = 1739598100811 # add_liquidity -TOKEN_A = E3F80775fddda7872185705BAAcc07aae66cFa5a07D5445D64D0C01642428D2d -TOKEN_B = f3c553Bed5D57B87FB1e7fDe02eC2a0355d64c0faD2fE872cB6DcC115197c1fe +TOKEN_A = 20831b61f3052df217EF0e441F4D7f365757Ae81Cb4C932408621cb7bf20b1c2 +TOKEN_B = ba446757a580880faCa3B9FB52a014AB3d61C88C8B175609c44e362749D871E8 AMOUNT_A_DESIRED = 500 AMOUNT_B_DESIRED = 600 AMOUNT_A_MIN = 250 diff --git a/JsClients/ERC20/keys/public_key.pem b/JsClients/ERC20/keys/public_key.pem index 82ad859f..917c8f67 100644 --- a/JsClients/ERC20/keys/public_key.pem +++ b/JsClients/ERC20/keys/public_key.pem @@ -1,3 +1,3 @@ -----BEGIN PUBLIC KEY----- -MCowBQYDK2VwAyEAs0KvzrC+3c3b48+M5hiTCBzTc1HVJZtYE+7n/r9r+dU= +MCowBQYDK2VwAyEA0g+BbrV8KHj7DLeH4ES/gjY2JXQLiZFW/sq0aq1hlgk= -----END PUBLIC KEY----- diff --git a/JsClients/ERC20/keys/public_key_hex b/JsClients/ERC20/keys/public_key_hex index a4a43f97..9a12d354 100644 --- a/JsClients/ERC20/keys/public_key_hex +++ b/JsClients/ERC20/keys/public_key_hex @@ -1 +1 @@ -01b342afceb0beddcddbe3cf8ce61893081cd37351d5259b5813eee7febf6bf9d5 \ No newline at end of file +01d20f816eb57c2878fb0cb787e044bf82363625740b899156fecab46aad619609 \ No newline at end of file diff --git a/JsClients/ERC20/keys/secret_key.pem b/JsClients/ERC20/keys/secret_key.pem index 7115f864..04128501 100644 --- a/JsClients/ERC20/keys/secret_key.pem +++ b/JsClients/ERC20/keys/secret_key.pem @@ -1,3 +1,3 @@ -----BEGIN PRIVATE KEY----- -MC4CAQAwBQYDK2VwBCIEIEAEVlLDzYh9hwBwLu3H2fsQVUrEZKJs+UblNZ9N0WhS +MC4CAQAwBQYDK2VwBCIEICzpVj2BiY8Hffjf6n5IM711VLDzBg3XBGzyw8myD6N6 -----END PRIVATE KEY----- diff --git a/JsClients/ERC20/test/install.ts b/JsClients/ERC20/test/install.ts index e255c423..b1dd1453 100644 --- a/JsClients/ERC20/test/install.ts +++ b/JsClients/ERC20/test/install.ts @@ -67,7 +67,7 @@ const test = async () => { `${ERC20_CONTRACT_NAME!}_package_hash` ); - console.log(`... Contract Hash: ${packageHash}`); + console.log(`... Package Hash: ${packageHash}`); }; //test(); diff --git a/JsClients/ERC20/test/installed.ts b/JsClients/ERC20/test/installed.ts index 36681f4c..c2c0eef7 100644 --- a/JsClients/ERC20/test/installed.ts +++ b/JsClients/ERC20/test/installed.ts @@ -207,16 +207,16 @@ const test = async () => { // // console.log(`... Total supply: ${totalSupply}`); //approve - // const approveDeployHash = await erc20.approve( - // ROUTERKEYS, - // PACKAGE_HASH!, - // AMOUNT_A_DESIRED!, - // APPROVE_PAYMENT_AMOUNT! - // ); - // console.log("... Approve deploy hash: ", approveDeployHash); + const approveDeployHash = await erc20.approve( + ROUTERKEYS, + PACKAGE_HASH!, + AMOUNT_B_DESIRED!, + APPROVE_PAYMENT_AMOUNT! + ); + console.log("... Approve deploy hash: ", approveDeployHash); - // await getDeploy(NODE_ADDRESS!, approveDeployHash); - // console.log("... Token approved successfully"); + await getDeploy(NODE_ADDRESS!, approveDeployHash); + console.log("... Token approved successfully"); // // //transfer // // const transferDeployHash = await erc20.transfer( diff --git a/JsClients/FACTORY/keys/public_key.pem b/JsClients/FACTORY/keys/public_key.pem index 75fb062f..b8b70afd 100644 --- a/JsClients/FACTORY/keys/public_key.pem +++ b/JsClients/FACTORY/keys/public_key.pem @@ -1,3 +1,3 @@ -----BEGIN PUBLIC KEY----- -MCowBQYDK2VwAyEA4Uucv/tdlBPn+RuHu0L5ihGGJlO2MwC85wbbjmCC1so= +MCowBQYDK2VwAyEAstkU0Ajtt7Fcn1TUQ8kTPQCuMMslDBVjPEjBvgEAWZk= -----END PUBLIC KEY----- diff --git a/JsClients/FACTORY/keys/public_key_hex b/JsClients/FACTORY/keys/public_key_hex index 22ca4807..b87c24b6 100644 --- a/JsClients/FACTORY/keys/public_key_hex +++ b/JsClients/FACTORY/keys/public_key_hex @@ -1 +1 @@ -01e14b9cbffb5d9413e7f91b87bb42f98a11862653b63300bce706db8e6082d6ca \ No newline at end of file +01b2d914d008edb7b15c9f54d443c9133d00ae30cb250c15633c48c1be01005999 \ No newline at end of file diff --git a/JsClients/FACTORY/keys/secret_key.pem b/JsClients/FACTORY/keys/secret_key.pem index 846426d7..50f6ba8b 100644 --- a/JsClients/FACTORY/keys/secret_key.pem +++ b/JsClients/FACTORY/keys/secret_key.pem @@ -1,3 +1,3 @@ -----BEGIN PRIVATE KEY----- -MC4CAQAwBQYDK2VwBCIEIBptGPvy3geNeR1+Ses6LnNP9+dYYRtSy9I2jnr2N+V7 +MC4CAQAwBQYDK2VwBCIEIC5xV975frbrQNoTnw8WWSGZ6ZD08J94q9Ag6fvqGKmD -----END PRIVATE KEY----- diff --git a/JsClients/FACTORY/test/install.ts b/JsClients/FACTORY/test/install.ts index c3f67e48..12397100 100644 --- a/JsClients/FACTORY/test/install.ts +++ b/JsClients/FACTORY/test/install.ts @@ -56,6 +56,14 @@ const test = async () => { ); console.log(`... Contract Hash: ${contractHash}`); + + const packageHash = await utils.getAccountNamedKeyValue( + accountInfo, + `${FACTORY_CONTRACT_NAME!}_package_hash` + ); + + console.log(`... Package Hash: ${packageHash}`); + }; //test(); diff --git a/JsClients/PAIR/keys/public_key.pem b/JsClients/PAIR/keys/public_key.pem index 3a73e3ff..5efade20 100644 --- a/JsClients/PAIR/keys/public_key.pem +++ b/JsClients/PAIR/keys/public_key.pem @@ -1,3 +1,3 @@ -----BEGIN PUBLIC KEY----- -MCowBQYDK2VwAyEAWCo4o5U2zNgRQan9mjlKj0AfjdVcyN1tigR0wnRMPTs= +MCowBQYDK2VwAyEAD1kE+NGCqK8ZWlbdHdZpydKs3WjAvRCjJsoK7AyLFCU= -----END PUBLIC KEY----- diff --git a/JsClients/PAIR/keys/public_key_hex b/JsClients/PAIR/keys/public_key_hex index b6701a0f..aea141e5 100644 --- a/JsClients/PAIR/keys/public_key_hex +++ b/JsClients/PAIR/keys/public_key_hex @@ -1 +1 @@ -01582a38a39536ccd81141a9fd9a394a8f401f8dd55cc8dd6d8a0474c2744c3d3b \ No newline at end of file +010f5904f8d182a8af195a56dd1dd669c9d2acdd68c0bd10a326ca0aec0c8b1425 \ No newline at end of file diff --git a/JsClients/PAIR/keys/secret_key.pem b/JsClients/PAIR/keys/secret_key.pem index c8ad94bd..0699d422 100644 --- a/JsClients/PAIR/keys/secret_key.pem +++ b/JsClients/PAIR/keys/secret_key.pem @@ -1,3 +1,3 @@ -----BEGIN PRIVATE KEY----- -MC4CAQAwBQYDK2VwBCIEIFCrU4+IUFtE+Clin5T6gCH05T11RteCOhbzNRzezBl9 +MC4CAQAwBQYDK2VwBCIEIKLUePR5NlWUtM+/OGb5iQm4AkH8n2tlkUtUzSvRAVwA -----END PRIVATE KEY----- diff --git a/JsClients/PAIR/src/pair.ts b/JsClients/PAIR/src/pair.ts index bceda9e2..80df7b9d 100644 --- a/JsClients/PAIR/src/pair.ts +++ b/JsClients/PAIR/src/pair.ts @@ -445,6 +445,36 @@ class PAIRClient { throw Error("Invalid Deploy"); } } + public async erc20Mint_router( + keys: Keys.AsymmetricKey, + to: RecipientType, + amount: string, + paymentAmount: string + ) { + + const runtimeArgs = RuntimeArgs.fromMap({ + to:utils.createRecipientAddress(to), + amount: CLValueBuilder.u256(amount), + }); + + + const deployHash = await contractCall({ + chainName: this.chainName, + contractHash: this.contractHash, + entryPoint: "erc20_mint", + keys, + nodeAddress: this.nodeAddress, + paymentAmount, + runtimeArgs, + }); + + if (deployHash !== null) { + this.addPendingDeploy(PAIREvents.Transfer, deployHash); + return deployHash; + } else { + throw Error("Invalid Deploy"); + } + } public async erc20Mint( keys: Keys.AsymmetricKey, to: String, diff --git a/JsClients/PAIR/test/install.ts b/JsClients/PAIR/test/install.ts index edb45b9c..11b288c0 100644 --- a/JsClients/PAIR/test/install.ts +++ b/JsClients/PAIR/test/install.ts @@ -72,7 +72,7 @@ const test = async () => { ); console.log(`... Contract Hash: ${contractHash}`); - console.log(`... Contract Package Hash: ${packageHash}`); + console.log(`... Package Hash: ${packageHash}`); }; diff --git a/JsClients/PAIR/test/installed.ts b/JsClients/PAIR/test/installed.ts index e7889405..22803f62 100644 --- a/JsClients/PAIR/test/installed.ts +++ b/JsClients/PAIR/test/installed.ts @@ -38,7 +38,8 @@ const { PAIR_CONTRACT_PACKAGE, PAIR_CONTRACT_NAME, GRAPHQL, - PAIR_CONTRACT + PAIR_CONTRACT, + MASTER_KEY_PAIR_PATH } = process.env; // const TOKEN_META = new Map(parseTokenMeta(process.env.TOKEN_META!)); @@ -47,7 +48,10 @@ const KEYS = Keys.Ed25519.parseKeyFiles( `${PAIR_MASTER_KEY_PAIR_PATH}/public_key.pem`, `${PAIR_MASTER_KEY_PAIR_PATH}/secret_key.pem` ); - +const ROUTERKEYS = Keys.Ed25519.parseKeyFiles( + `${MASTER_KEY_PAIR_PATH}/public_key.pem`, + `${MASTER_KEY_PAIR_PATH}/secret_key.pem` +); const pair = new PAIRClient( NODE_ADDRESS!, CHAIN_NAME!, @@ -330,12 +334,12 @@ const test = async () => { await pair.setContractHash(PAIR_CONTRACT!); //name - const name = await pair.name(); - console.log(`... Contract name: ${name}`); + // const name = await pair.name(); + // console.log(`... Contract name: ${name}`); - //symbol - const symbol = await pair.symbol(); - console.log(`... Contract symbol: ${symbol}`); + // //symbol + // const symbol = await pair.symbol(); + // console.log(`... Contract symbol: ${symbol}`); //initialize // const initializeDeployHash = await pair.initialize( @@ -398,9 +402,9 @@ const test = async () => { // console.log(`... Total supply: ${totalSupply}`); // //balanceof - // let balance = await pair.balanceOf(KEYS.publicKey); - // console.log(`... Balance of account ${KEYS.publicKey.toAccountHashStr()}`); - // console.log(`... Balance: ${balance}`); + let balance = await pair.balanceOf(PAIR_CONTRACT_PACKAGE!); + //console.log(`... Balance of account ${KEYS.publicKey.toAccountHashStr()}`); + console.log(`... Balance: ${balance}`); // //balanceof // let nonce = await pair.nonce(KEYS.publicKey); @@ -426,8 +430,20 @@ const test = async () => { // //erc20mint // const erc20mint1DeployHash = await pair.erc20Mint( // KEYS, - // TOKEN1_CONTRACT!, - // "1000"!, + // PAIR_CONTRACT_PACKAGE!, + // "2000", + // MINT_PAYMENT_AMOUNT! + // ); + // console.log("... Mint deploy hash: ", erc20mint1DeployHash); + + // await getDeploy(NODE_ADDRESS!, erc20mint1DeployHash); + // console.log("... Token minted successfully"); + + //erc20mint + // const erc20mint1DeployHash = await pair.erc20Mint_router( + // KEYS, + // ROUTERKEYS.publicKey!, + // "2000", // MINT_PAYMENT_AMOUNT! // ); // console.log("... Mint deploy hash: ", erc20mint1DeployHash); @@ -513,7 +529,7 @@ const test = async () => { //erc20mint // const erc20MintToken0DeployHash = await pair.erc20MintMethod( // KEYS, - // TOKEN0_CONTRACT!, + // PAIR_CONTRACT_PACKAGE!, // "1000"!, // MINT_PAYMENT_AMOUNT! // ); @@ -577,7 +593,7 @@ const test = async () => { }; -//test(); +test(); export const balanceOf = async (contractHash:string, key:string) => { diff --git a/JsClients/ROUTER/keys/public_key.pem b/JsClients/ROUTER/keys/public_key.pem index f8820b36..997762f4 100644 --- a/JsClients/ROUTER/keys/public_key.pem +++ b/JsClients/ROUTER/keys/public_key.pem @@ -1,3 +1,3 @@ -----BEGIN PUBLIC KEY----- -MCowBQYDK2VwAyEAisk1o1Ahn6RwyosEG9ryFSjKyUPlZdBlAK1atB5zc54= +MCowBQYDK2VwAyEAz2L03zDo9qgBL8DwJwWFKkHTNy7tj2w/nvRG8CUISLs= -----END PUBLIC KEY----- diff --git a/JsClients/ROUTER/keys/public_key_hex b/JsClients/ROUTER/keys/public_key_hex index 22ec15d6..d2416696 100644 --- a/JsClients/ROUTER/keys/public_key_hex +++ b/JsClients/ROUTER/keys/public_key_hex @@ -1 +1 @@ -018ac935a350219fa470ca8b041bdaf21528cac943e565d06500ad5ab41e73739e \ No newline at end of file +01cf62f4df30e8f6a8012fc0f02705852a41d3372eed8f6c3f9ef446f0250848bb \ No newline at end of file diff --git a/JsClients/ROUTER/keys/secret_key.pem b/JsClients/ROUTER/keys/secret_key.pem index b1d32bdc..a963c974 100644 --- a/JsClients/ROUTER/keys/secret_key.pem +++ b/JsClients/ROUTER/keys/secret_key.pem @@ -1,3 +1,3 @@ -----BEGIN PRIVATE KEY----- -MC4CAQAwBQYDK2VwBCIEIG6psKFljoJdMK/HvjRVt50w7+DrrivkvHf7ICB9KM3s +MC4CAQAwBQYDK2VwBCIEINSB9zUWCLJXgm2b5Z06zwzZek9651oGKGEC63nLgqdn -----END PRIVATE KEY----- diff --git a/JsClients/ROUTER/test/installed.ts b/JsClients/ROUTER/test/installed.ts index 556d604f..791ba2b8 100644 --- a/JsClients/ROUTER/test/installed.ts +++ b/JsClients/ROUTER/test/installed.ts @@ -528,4 +528,4 @@ const test = async () => { }; -//test(); +test(); diff --git a/app.js b/app.js index a4d1f296..5e95e63d 100644 --- a/app.js +++ b/app.js @@ -64,7 +64,7 @@ app.use((req, res, next) => { app.get("/", (req, res) => { res.json({ msg: "Uniswap V2 GraphQL Server" }); }); -app.use('/listener', listenerRouter); +app.use('/', listenerRouter); app.use('/graphql', graphqlHTTP({ schema: schema, diff --git a/graphql/mutations.js b/graphql/mutations.js index b2c73cfd..cf9c215d 100644 --- a/graphql/mutations.js +++ b/graphql/mutations.js @@ -1,3 +1,4 @@ +require("dotenv").config(); const { GraphQLString, GraphQLObjectType, @@ -6,7 +7,8 @@ const { GraphQLBoolean, } = require("graphql"); -require("dotenv").config(); +var {request} = require('graphql-request'); + const Response = require("../models/response"); const UniswapFactory = require("../models/uniswapFactory"); @@ -23,6 +25,7 @@ let pairHourData = require("../models/pairHourData"); let uniswapDayData = require("../models/uniswapDayData"); let token0DayData = require("../models/tokenDayData"); let token1DayData = require("../models/tokenDayData"); +let eventsModel = require("../models/events"); const {responseType} = require("./types/response"); @@ -61,6 +64,13 @@ const { getTrackedLiquidityUSD, } = require("./pricing"); +function splitdata(data) +{ + var temp=data.split('('); + var result=temp[1].split(')'); + return result[0]; +} + const handleNewPair = { type: responseType, description: "Handle New Pair", @@ -69,6 +79,7 @@ const handleNewPair = { token1: { type:GraphQLString }, pair: { type: GraphQLString }, all_pairs_length: { type: GraphQLInt }, + deployHash: { type: GraphQLString }, timeStamp: { type: GraphQLString }, blockHash: { type: GraphQLString } }, @@ -192,17 +203,262 @@ const handleNewPair = { await pair.save(); await factory.save(); - let response = await Response.findOne({ id: "1" }); - if(response=== null) + console.log("deployHash: ",args.deployHash); + var eventsData=await eventsModel.find({deployHash:args.deployHash}); + if(eventsData.length == 0) { - // create new response - response = new Response({ - id: "1", - result: true - }); - await response.save(); + console.log("No event found in the database at this pair created event deployHash."); + let response = await Response.findOne({ id: "1" }); + if(response=== null) + { + // create new response + response = new Response({ + id: "1", + result: true + }); + await response.save(); + } + return response; + } + else + { + const eventslength=eventsData.length; + for (var i=0;i console.log(data)) - .catch(error => console.error(error)); + {token0:token0, token1:token1, pair: pair, all_pairs_length: allpairslength, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "HandleNewPair Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } else if(eventName=="approve") { @@ -184,6 +183,10 @@ router.route("/geteventsdata").post(async function (req, res, next) { console.log(newData[2][0].data + " = " + newData[2][1].data); console.log(newData[3][0].data + " = " + newData[3][1].data); console.log(newData[4][0].data + " = " + newData[4][1].data); + return res.status(200).json({ + success: true, + message: "Approved Event emitted." + }); } else if(eventName=="erc20_transfer") { @@ -198,7 +201,7 @@ router.route("/geteventsdata").post(async function (req, res, next) { var flag=0; var temp=(newData[3][1].data).split('('); console.log("temp[0]: ",temp[0]); - if(temp[0] == "Key::Account(") + if(temp[0] == "Key::Account") { flag=1; } @@ -212,24 +215,87 @@ router.route("/geteventsdata").post(async function (req, res, next) { if(flag==0) { - var pairsresult=await pairsModel.findOne({id:"pair"}); - var to_contractHash=pairsresult.data[to]; + var pairsresult=await hashesofpairsModel.find({}); + if(pairsresult==null) + { + console.log("there are no contract and package hash found in the database."); + return res.status(400).json({ + success: false, + message: "there are no contract and package hash found in the database." + }); + } + var to_contractHash=null; + for(var i=0;i console.log(data)) - .catch(error => console.error(error)); + else + { + var pairData=await pairModel.findOne({id:to_contractHash}); + + if(pairData == null) + { + + var newevent = new eventsModel({ + deployHash : deployHash, + timestamp : timestamp, + block_hash : block_hash, + pairContractHash:to_contractHash.toLowerCase(), + eventName : eventName, + eventsdata:newData + }); + await eventsModel.create(newevent); + + console.log("pair did not created against this pair package hash, event has been saved to database."); + return res.status(200).json({ + success: true, + message: "pair did not created against this pair package hash, event has been saved to database." + }); + } + else + { + request(process.env.GRAPHQL, + `mutation handleTransfer( $from: String!, $to: String!, $value: Int!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ + handleTransfer( from: $from, to: $to, value: $value, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { + result + } + + }`, + {from:from, to: to, value: value, pairAddress: to_contractHash, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "handleTransfer Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } + } + } + else + { + return res.status(200).json({ + success: true, + message: "handleTransfer Mutation did not called." + }); + } } else if(eventName=="transfer") @@ -252,17 +318,47 @@ router.route("/geteventsdata").post(async function (req, res, next) { console.log("to: ", to); console.log("value: ",value); console.log("pair: ", pair); - - request(process.env.GRAPHQL, - `mutation handleTransfer( $from: String!, $to: String!, $value: Int!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ - handleTransfer( from: $from, to: $to, value: $value, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { - result - } - - }`, - {from:from, to: to, value: value, pairAddress: pair, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) - .then(data => console.log(data)) - .catch(error => console.error(error)); + + var pairData=await pairModel.findOne({id:pair}); + if(pairData==null) + { + var newevent = new eventsModel({ + deployHash : deployHash, + timestamp : timestamp, + block_hash : block_hash, + eventName : eventName, + eventsdata:newData + }); + await eventsModel.create(newevent); + + console.log("pair did not created against this pair package hash, event has been saved to database."); + return res.status(200).json({ + success: true, + message: "pair did not created against this pair package hash, event has been saved to database." + }); + } + else + { + request(process.env.GRAPHQL, + `mutation handleTransfer( $from: String!, $to: String!, $value: Int!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ + handleTransfer( from: $from, to: $to, value: $value, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { + result + } + + }`, + {from:from, to: to, value: value, pairAddress: pair, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "handleTransfer Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } + } else if (eventName=="mint") { @@ -284,17 +380,47 @@ router.route("/geteventsdata").post(async function (req, res, next) { console.log("amount1: ", amount1); console.log("pair: ",pair); console.log("sender: ", sender); - - request(process.env.GRAPHQL, - `mutation handleMint( $amount0: Int!, $amount1: Int!, $sender: String!,$logIndex: Int!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ - handleMint( amount0: $amount0, amount1: $amount1, sender: $sender, logIndex: $logIndex, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { - result - } - - }`, - {amount0:amount0, amount1: amount1, sender: sender,logIndex:0, pairAddress: pair, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) - .then(data => console.log(data)) - .catch(error => console.error(error)); + + var pairData=await pairModel.findOne({id:pair}); + if(pairData==null) + { + var newevent = new eventsModel({ + deployHash : deployHash, + timestamp : timestamp, + block_hash : block_hash, + eventName : eventName, + eventsdata:newData + }); + await eventsModel.create(newevent); + + console.log("pair did not created against this pair package hash, event has been saved to database."); + return res.status(200).json({ + success: true, + message: "pair did not created against this pair package hash, event has been saved to database." + }); + } + else + { + request(process.env.GRAPHQL, + `mutation handleMint( $amount0: Int!, $amount1: Int!, $sender: String!,$logIndex: Int!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ + handleMint( amount0: $amount0, amount1: $amount1, sender: $sender, logIndex: $logIndex, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { + result + } + + }`, + {amount0:amount0, amount1: amount1, sender: sender,logIndex:0, pairAddress: pair, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "handleMint Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } + } else if (eventName=="burn") { @@ -319,8 +445,28 @@ router.route("/geteventsdata").post(async function (req, res, next) { console.log("pair: ",pair); console.log("sender: ", sender); console.log("to: ", to); - - request(process.env.GRAPHQL, + + var pairData=await pairModel.findOne({id:pair}); + if(pairData==null) + { + var newevent = new eventsModel({ + deployHash : deployHash, + timestamp : timestamp, + block_hash : block_hash, + eventName : eventName, + eventsdata:newData + }); + await eventsModel.create(newevent); + + console.log("pair did not created against this pair package hash, event has been saved to database."); + return res.status(200).json({ + success: true, + message: "pair did not created against this pair package hash, event has been saved to database." + }); + } + else + { + request(process.env.GRAPHQL, `mutation handleBurn( $amount0: Int!, $amount1: Int!, $sender: String!,$logIndex: Int!,$to: String!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ handleBurn( amount0: $amount0, amount1: $amount1, sender: $sender, logIndex: $logIndex, to:$to, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { result @@ -328,8 +474,17 @@ router.route("/geteventsdata").post(async function (req, res, next) { }`, {amount0:amount0, amount1: amount1, sender: sender,logIndex:0, to:to,pairAddress: pair, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) - .then(data => console.log(data)) - .catch(error => console.error(error)); + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "handleBurn Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } } else if (eventName=="sync") { @@ -349,8 +504,27 @@ router.route("/geteventsdata").post(async function (req, res, next) { console.log("reserve1: ", reserve1); console.log("pair: ",pair); - - request(process.env.GRAPHQL, + var pairData=await pairModel.findOne({id:pair}); + if(pairData==null) + { + var newevent = new eventsModel({ + deployHash : deployHash, + timestamp : timestamp, + block_hash : block_hash, + eventName : eventName, + eventsdata:newData + }); + await eventsModel.create(newevent); + + console.log("pair did not created against this pair package hash, event has been saved to database."); + return res.status(200).json({ + success: true, + message: "pair did not created against this pair package hash, event has been saved to database." + }); + } + else + { + request(process.env.GRAPHQL, `mutation handleSync( $reserve0: Int!, $reserve1: Int!, $pairAddress: String!){ handleSync( reserve0: $reserve0, reserve1: $reserve1, pairAddress: $pairAddress) { result @@ -358,8 +532,17 @@ router.route("/geteventsdata").post(async function (req, res, next) { }`, {reserve0:reserve0, reserve1: reserve1, pairAddress: pair}) - .then(data => console.log(data)) - .catch(error => console.error(error)); + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "handleSync Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } } else if (eventName=="swap") { @@ -393,8 +576,28 @@ router.route("/geteventsdata").post(async function (req, res, next) { console.log("pair: ",pair); console.log("sender: ", sender); console.log("to: ", to); - - request(process.env.GRAPHQL, + var pairData=await pairModel.findOne({id:pair}); + if(pairData==null) + { + var newevent = new eventsModel({ + deployHash : deployHash, + timestamp : timestamp, + block_hash : block_hash, + eventName : eventName, + eventsdata:newData + }); + await eventsModel.create(newevent); + + console.log("pair did not created against this pair package hash, event has been saved to database."); + return res.status(200).json({ + success: true, + message: "pair did not created against this pair package hash, event has been saved to database." + }); + } + else + { + + request(process.env.GRAPHQL, `mutation handleSwap( $amount0In: Int!, $amount1In: Int!, $amount0Out: Int!, $amount1Out: Int!, $to: String!,$from: String!,$sender: String!,$logIndex: Int!, $pairAddress: String!, $deployHash: String!, $timeStamp: String!, $blockHash: String!){ handleSwap( amount0In: $amount0In, amount1In: $amount1In, amount0Out: $amount0Out, amount1Out: $amount1Out, to:$to, from:$from,sender: $sender,logIndex: $logIndex, pairAddress: $pairAddress, deployHash: $deployHash, timeStamp: $timeStamp, blockHash: $blockHash) { result @@ -402,15 +605,19 @@ router.route("/geteventsdata").post(async function (req, res, next) { }`, {amount0In:amount0In, amount1In: amount1In,amount0Out:amount0Out, amount1Out: amount1Out,to:to,from:from, sender: sender,logIndex:0,pairAddress: pair, deployHash:deployHash,timeStamp:timestamp.toString(), blockHash:block_hash}) - .then(data => console.log(data)) - .catch(error => console.error(error)); + .then(function (response) { + console.log(response); + return res.status(200).json({ + success: true, + message: "handleSwap Mutation called." + }); + }) + .catch(function (error) { + console.log(error); + }); + } } - return res.status(200).json({ - success: true, - message: "Mutation called according to received event." - }); - } catch (error) { console.log("error (try-catch) : " + error); return res.status(500).json({