diff --git a/action/index.cjs b/action/index.cjs index bcfc736..8a9e8d7 100644 --- a/action/index.cjs +++ b/action/index.cjs @@ -188026,13 +188026,7 @@ const robot = (app) => { console.log(`Names of changed files: ${printableFilenames}. original owners=${originalOwners}. new owners: ${newOwners}`); const allOwners = []; const allOwnersToCheck = [mainOwner, ...extraOwners]; - let apiUrl = 'https://next-api.multiversx.com'; - if (network === 'devnet') { - apiUrl = 'https://devnet-api.multiversx.com'; - } - else if (network === 'testnet') { - apiUrl = 'https://testnet-api.multiversx.com'; - } + const apiUrl = getApiUrl(); for (const owner of allOwnersToCheck) { if (new out_1.Address(owner).isContractAddress()) { const ownerResult = await axios_1.default.get(`${apiUrl}/accounts/${owner}?extract=ownerAddress`); @@ -188049,21 +188043,21 @@ const robot = (app) => { return ''; } const account = identity; - let apiUrl = 'https://next-api.multiversx.com'; - if (network === 'devnet') { - apiUrl = 'https://devnet-api.multiversx.com'; - } - else if (network === 'testnet') { - apiUrl = 'https://testnet-api.multiversx.com'; - } - const ownerResult = await axios_1.default.get(`${apiUrl}/accounts/${account}?extract=ownerAddress`); - const accountOwner = ownerResult.data; + const accountOwner = await getAccountOwnerFromApi(account); if (new out_1.Address(accountOwner).isContractAddress()) { - const ownerResult = await axios_1.default.get(`${apiUrl}/tokens/${accountOwner}?extract=ownerAddress`); - return ownerResult.data; + const newOwner = getAccountOwnerFromApi(accountOwner); + return newOwner; } return accountOwner; } + async function getAccountOwnerFromApi(address) { + const apiUrl = getApiUrl(); + const accountOwnerResponse = await axios_1.default.get(`${apiUrl}/accounts/${address}?extract=owner`); + if (accountOwnerResponse && accountOwnerResponse.data) { + return accountOwnerResponse.data; + } + return ''; + } async function getTokenOwner() { // since the token owner can be changed at protocol level at any time, it's enough to check the ownership of the token, // without checking any previous owners @@ -188071,13 +188065,7 @@ const robot = (app) => { return ''; } const token = identity; - let apiUrl = 'https://next-api.multiversx.com'; - if (network === 'devnet') { - apiUrl = 'https://devnet-api.multiversx.com'; - } - else if (network === 'testnet') { - apiUrl = 'https://testnet-api.multiversx.com'; - } + const apiUrl = getApiUrl(); const tokenOwner = await getTokenOwnerFromApi(token, apiUrl); if (new out_1.Address(tokenOwner).isContractAddress()) { const ownerResult = await axios_1.default.get(`${apiUrl}/tokens/${token}?extract=ownerAddress`); @@ -188092,6 +188080,17 @@ const robot = (app) => { } return ''; } + function getApiUrl() { + switch (network) { + case 'mainnet': + return 'https://next-api.multiversx.com'; + case 'devnet': + return 'https://devnet-api.multiversx.com'; + case 'testnet': + return 'https://testnet-api.multiversx.com'; + } + throw new Error(`Invalid network: ${network}`); + } function getDistinctNetworks(fileNames) { const networks = fileNames.map(fileName => getNetwork(fileName)).filter(x => x !== undefined); return [...new Set(networks)]; diff --git a/src/bot.ts b/src/bot.ts index c2e2120..7c4c89d 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -83,18 +83,25 @@ export const robot = (app: Probot) => { } const account = identity; - const apiUrl = getApiUrl(); - - const ownerResult = await axios.get(`${apiUrl}/accounts/${account}?extract=ownerAddress`); - const accountOwner = ownerResult.data; + const accountOwner = await getAccountOwnerFromApi(account); if (new Address(accountOwner).isContractAddress()) { - const ownerResult = await axios.get(`${apiUrl}/tokens/${accountOwner}?extract=ownerAddress`); - return ownerResult.data; + const newOwner = getAccountOwnerFromApi(accountOwner); + return newOwner; } return accountOwner; } + async function getAccountOwnerFromApi(address: string): Promise { + const apiUrl = getApiUrl(); + const accountOwnerResponse = await axios.get(`${apiUrl}/accounts/${address}?extract=owner`); + if (accountOwnerResponse && accountOwnerResponse.data) { + return accountOwnerResponse.data; + } + + return ''; + } + async function getTokenOwner(): Promise { // since the token owner can be changed at protocol level at any time, it's enough to check the ownership of the token, // without checking any previous owners