Skip to content

Commit

Permalink
some more smaller refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghel committed Feb 19, 2024
1 parent 563f27e commit fd5a01d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
49 changes: 24 additions & 25 deletions action/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -188049,35 +188043,29 @@ 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
if (!identity) {
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`);
Expand All @@ -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)];
Expand Down
19 changes: 13 additions & 6 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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<string> {
// 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
Expand Down

0 comments on commit fd5a01d

Please sign in to comment.