diff --git a/commands/deploy.js b/commands/deploy.js index 7fb026a..d534a9e 100644 --- a/commands/deploy.js +++ b/commands/deploy.js @@ -149,9 +149,13 @@ const handler = async function(argv) { // Check if website is already deployed if (refIndex !== 0) { console.log(archethic.nearestEndpoints) - isWebsiteUpdate = true; - const lastRefTx = await fetchLastRefTx(refAddress, archethic); - prevRefTxContent = JSON.parse(lastRefTx.data.content); + const lastHostingTx = await fetchLastHostingTx(refAddress, archethic); + if (lastHostingTx) { + prevRefTxContent = JSON.parse(lastHostingTx.data.content); + isWebsiteUpdate = true; + } else { + console.warn('No existing hosting transaction found. This will be considered as the initial hosting transaction.'); + } } // Convert directory structure into array of file content @@ -313,8 +317,7 @@ async function sendTransactions(transactions, index, endpoint) { }) } - -async function fetchLastRefTx(txnAddress, archethic) { +async function fetchLastHostingTx(txnAddress, archethic) { if (typeof txnAddress !== "string" && !(txnAddress instanceof Uint8Array)) { throw "'address' must be a string or Uint8Array"; } @@ -329,23 +332,68 @@ async function fetchLastRefTx(txnAddress, archethic) { txnAddress = uint8ArrayToHex(txnAddress); } - const query =` + const lastTxQuery = ` query { lastTransaction( address: "${txnAddress}" - ){ - data{ + ) { + previousAddress + type + data { content } + } + }`; + + const lastTxResult = await archethic.network.rawGraphQLQuery(lastTxQuery); + + if (!lastTxResult.lastTransaction) { + return null; + } + + let txn = lastTxResult.lastTransaction; + let lastHostingTx = null; + + console.log(`Current transaction address: ${txnAddress}`); + console.log(`Previous transaction address: ${txn.previousAddress}`); + console.log(`Transaction type: ${txn.type}`); + + while (txn.previousAddress) { + txnAddress = txn.previousAddress; + + const txQuery = ` + query { + transaction( + address: "${txnAddress}" + ) { + previousAddress + type + data { + content + } } - }` - - return archethic.network.rawGraphQLQuery(query) - .then(r => { - if (r.lastTransaction) { - return r.lastTransaction - } - }) + }`; + + const result = await archethic.network.rawGraphQLQuery(txQuery); + + if (!result || !result.transaction) { + console.log(`Transaction not found for address: ${txnAddress}`); + break; + } + + txn = result.transaction; + + console.log(`Current transaction address: ${txnAddress}`); + console.log(`Previous transaction address: ${txn.previousAddress}`); + console.log(`Transaction type: ${txn.type}`); + + if (txn.type === "hosting") { + lastHostingTx = txn; + break; + } + } + + return lastHostingTx; } async function logUpdateInfo(aeweb) {