From 6e32d1c2ad670ea3c44377126a21ba99183124e0 Mon Sep 17 00:00:00 2001 From: Raul Bernal Date: Fri, 18 Oct 2024 09:21:23 +0200 Subject: [PATCH 1/3] Update api-cosmos.php Apply endpoint checking previously the SDK version --- api-cosmos.php | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/api-cosmos.php b/api-cosmos.php index 96166a0..c5b6d42 100644 --- a/api-cosmos.php +++ b/api-cosmos.php @@ -201,14 +201,37 @@ echo '{ "return": "canceled" }'; -} elseif ( isset($_GET['check']) && isset( $_GET['order_id'] ) ) { +} elseif (isset($_GET['check']) && isset($_GET['order_id'])) { + + $orderId = sanitize_text_field($_GET['order_id']); + $getLcdPay = wc_get_order_item_meta($orderId, '_cosmos_lcd_pay', true); + + // Obtain node information to determine the Cosmos SDK version + $nodeInfoJson = file_get_contents($getLcdPay.'/cosmos/base/tendermint/v1beta1/node_info'); + $nodeInfo = json_decode($nodeInfoJson, true); + + // Check if the node information was obtained successfully + if ($nodeInfo !== null && isset($nodeInfo['application_version']['cosmos_sdk_version'])) { + // Extract the Cosmos SDK version and remove the 'v' prefix if it exists + $cosmosSdkVersion = ltrim($nodeInfo['application_version']['cosmos_sdk_version'], 'v'); + + // Decide which endpoint to use based on the SDK version + if (version_compare($cosmosSdkVersion, '0.50.0', '>=')) { + // Use the endpoint for SDK v0.50.x + $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); + } else { + // Use the endpoint for SDK v0.47.x + $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?events=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); + } + } else { + // If unable to determine the SDK version, default to the endpoint for SDK v0.50.x + $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); + } + + $obj = json_decode($json); + + echo $json; +} else { + echo 'Incorrect parameters'; +} - $orderId = sanitize_text_field( $_GET['order_id'] ); - $getLcdPay = wc_get_order_item_meta( $orderId , '_cosmos_lcd_pay', true ); - $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?events=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); - $obj = json_decode($json); - - echo $json; -} else - echo 'Bad parameters'; - From e115d129605fad83f6b2c544c5fcdb153ee95c73 Mon Sep 17 00:00:00 2001 From: Raul Bernal Date: Thu, 7 Nov 2024 13:38:21 +0100 Subject: [PATCH 2/3] Check version & fix URL param order --- api-cosmos.php | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/api-cosmos.php b/api-cosmos.php index c5b6d42..8870e82 100644 --- a/api-cosmos.php +++ b/api-cosmos.php @@ -206,28 +206,41 @@ $orderId = sanitize_text_field($_GET['order_id']); $getLcdPay = wc_get_order_item_meta($orderId, '_cosmos_lcd_pay', true); - // Obtain node information to determine the Cosmos SDK version - $nodeInfoJson = file_get_contents($getLcdPay.'/cosmos/base/tendermint/v1beta1/node_info'); + // Get info from LCD Node to get the Tendermint/CometBFT version + $nodeInfoJson = file_get_contents($getLcdPay . '/cosmos/base/tendermint/v1beta1/node_info'); $nodeInfo = json_decode($nodeInfoJson, true); - // Check if the node information was obtained successfully - if ($nodeInfo !== null && isset($nodeInfo['application_version']['cosmos_sdk_version'])) { - // Extract the Cosmos SDK version and remove the 'v' prefix if it exists - $cosmosSdkVersion = ltrim($nodeInfo['application_version']['cosmos_sdk_version'], 'v'); - - // Decide which endpoint to use based on the SDK version - if (version_compare($cosmosSdkVersion, '0.50.0', '>=')) { - // Use the endpoint for SDK v0.50.x - $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); + if ($nodeInfo !== null && isset($nodeInfo['default_node_info']['version'])) { + // Get the string + $versionString = $nodeInfo['default_node_info']['version']; + // Split the string '.' => "v0" "38" "11" + $versionParts = explode('.', $versionString); + + if (isset($versionParts[1])) { + // Get the minor version and convert to integer + $minorVersion = intval($versionParts[1]); + + // Check the minor version and apply the proper endpoint fix + if ($minorVersion >= 38) { + $paramName = 'query'; + } else { + $paramName = 'events'; + } } else { - // Use the endpoint for SDK v0.47.x - $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?events=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); + // If something fails getting the version.. use "query" as default + $paramName = 'query'; } } else { - // If unable to determine the SDK version, default to the endpoint for SDK v0.50.x - $json = file_get_contents($getLcdPay.'/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&order_by=ORDER_BY_DESC&pagination.limit=10'); + // If something fails querying the node.. use "query" as default + $paramName = 'query'; } + // Build the proper Query + // BCNA: https://lcd.bitcanna.io/cosmos/tx/v1beta1/txs?events=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=0&limit=10 + // OSMOSIS: https://rest.cosmos.directory/osmosis/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=2&limit=10 + // COSMOS: https://rest.cosmos.directory/cosmoshub/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=2&limit=10 + $json = file_get_contents($getLcdPay . '/cosmos/tx/v1beta1/txs?' . $paramName . '=message.sender=%27' . $addrWallet . '%27&&pagination.limit=10&order_by=2&limit=10'); + $obj = json_decode($json); echo $json; From a9950c06a85d67af22b071119002e18d0862ef8b Mon Sep 17 00:00:00 2001 From: Raul Bernal Date: Thu, 7 Nov 2024 14:13:15 +0100 Subject: [PATCH 3/3] fix URL to check --- api-cosmos.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api-cosmos.php b/api-cosmos.php index 8870e82..2fdede4 100644 --- a/api-cosmos.php +++ b/api-cosmos.php @@ -239,7 +239,7 @@ // BCNA: https://lcd.bitcanna.io/cosmos/tx/v1beta1/txs?events=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=0&limit=10 // OSMOSIS: https://rest.cosmos.directory/osmosis/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=2&limit=10 // COSMOS: https://rest.cosmos.directory/cosmoshub/cosmos/tx/v1beta1/txs?query=message.action=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=2&limit=10 - $json = file_get_contents($getLcdPay . '/cosmos/tx/v1beta1/txs?' . $paramName . '=message.sender=%27' . $addrWallet . '%27&&pagination.limit=10&order_by=2&limit=10'); + $json = file_get_contents($getLcdPay . '/cosmos/tx/v1beta1/txs?' . $paramName . '=message.sender=%27/cosmos.bank.v1beta1.MsgSend%27&pagination.limit=10&order_by=2&limit=10'); $obj = json_decode($json);