Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-rosianu committed Mar 8, 2024
1 parent 293d3da commit 5e9ee38
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 68 deletions.
62 changes: 30 additions & 32 deletions action/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -188034,8 +188034,10 @@ const robot = (app) => {
return [];
}
if (new out_1.Address(owner).isContractAddress()) {
const ownerResult = await axios_1.default.get(`${apiUrl}/accounts/${owner}?extract=ownerAddress`);
allOwners.push(ownerResult.data);
const ownerResult = await fetchStringValueFromApi(apiUrl, "accounts", owner, "ownerAddress");
if (ownerResult) {
allOwners.push(ownerResult);
}
}
else {
allOwners.push(owner);
Expand All @@ -188044,17 +188046,23 @@ const robot = (app) => {
return [...new Set(allOwners)];
}
async function isProviderAlreadyBranded(apiUrl, identity, provider) {
const providerInfo = await axios_1.default.get(`${apiUrl}/providers/${provider}`);
if (!providerInfo) {
return { existing: false };
try {
const providerInfo = await axios_1.default.get(`${apiUrl}/providers/${provider}`);
if (!providerInfo) {
return { existing: false };
}
if (!providerInfo.data?.identity) {
return { existing: false };
}
return {
existing: true,
isUpdate: identity === providerInfo.data?.identity,
};
}
if (!providerInfo.data?.identity) {
catch (error) {
console.error(`API error while fetching the provider data for address ${provider}: ${error}`);
return { existing: false };
}
return {
existing: true,
isUpdate: identity === providerInfo.data?.identity,
};
}
async function getAccountOwner(account) {
const accountOwner = account;
Expand All @@ -188065,43 +188073,33 @@ const robot = (app) => {
}
async function getAccountOwnerFromApi(address) {
const apiUrl = getApiUrl();
const accountOwnerResponse = await axios_1.default.get(`${apiUrl}/accounts/${address}?extract=ownerAddress`);
if (accountOwnerResponse && accountOwnerResponse.data) {
return accountOwnerResponse.data;
}
return '';
return await fetchStringValueFromApi(apiUrl, "accounts", address, "ownerAddress");
}
async function getTokenOwner(token) {
// 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
const apiUrl = getApiUrl();
const tokenOwner = await getTokenOwnerFromApi(token, apiUrl);
if (new out_1.Address(tokenOwner).isContractAddress()) {
const ownerResult = await axios_1.default.get(`${apiUrl}/accounts/${tokenOwner}?extract=ownerAddress`);
return ownerResult.data;
return await fetchStringValueFromApi(apiUrl, "accounts", tokenOwner, "ownerAddress");
}
return tokenOwner;
}
async function getTokenOwnerFromApi(token, apiUrl) {
try {
const tokenOwnerResponse = await axios_1.default.get(`${apiUrl}/tokens/${token}?extract=owner`);
if (tokenOwnerResponse && tokenOwnerResponse.data) {
return tokenOwnerResponse.data;
}
return '';
}
catch (error) {
console.log(`Cannot find token owner on the 'tokens' endpoint. Will try the 'collections'`);
return await fetchStringValueFromApi(apiUrl, "tokens", token, "owner") ||
await fetchStringValueFromApi(apiUrl, "collections", token, "owner");
}
async function fetchStringValueFromApi(apiUrl, endpoint, query, extract) {
let requestUrl = `${apiUrl}/${endpoint}/${query}`;
if (extract) {
requestUrl += `?extract=${extract}`;
}
try {
const tokenOwnerResponse = await axios_1.default.get(`${apiUrl}/collections/${token}?extract=owner`);
if (tokenOwnerResponse && tokenOwnerResponse.data) {
return tokenOwnerResponse.data;
}
return '';
const response = await axios_1.default.get(requestUrl);
return response.data;
}
catch (error) {
await fail(`Cannot find token or collection with ID ${token}: ${error}`);
console.error(`Cannot query API at ${requestUrl}: ${error}`);
return '';
}
}
Expand Down
67 changes: 31 additions & 36 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ export const robot = (app: Probot) => {
return [];
}
if (new Address(owner).isContractAddress()) {
const ownerResult = await axios.get(`${apiUrl}/accounts/${owner}?extract=ownerAddress`);
allOwners.push(ownerResult.data);
const ownerResult = await fetchStringValueFromApi(apiUrl, "accounts", owner, "ownerAddress");
if (ownerResult) {
allOwners.push(ownerResult);
}
} else {
allOwners.push(owner);
}
Expand All @@ -86,19 +88,24 @@ export const robot = (app: Probot) => {
existing: boolean,
isUpdate?: boolean
}> {
const providerInfo = await axios.get(`${apiUrl}/providers/${provider}`);
if (!providerInfo) {
return { existing: false };
}
try {
const providerInfo = await axios.get(`${apiUrl}/providers/${provider}`);
if (!providerInfo) {
return { existing: false };
}

if (!providerInfo.data?.identity) {
if (!providerInfo.data?.identity) {
return { existing: false };
}

return {
existing: true,
isUpdate: identity === providerInfo.data?.identity,
};
} catch (error) {
console.error(`API error while fetching the provider data for address ${provider}: ${error}`);
return { existing: false };
}

return {
existing: true,
isUpdate: identity === providerInfo.data?.identity,
};
}

async function getAccountOwner(account: string): Promise<string> {
Expand All @@ -112,12 +119,7 @@ export const robot = (app: Probot) => {

async function getAccountOwnerFromApi(address: string): Promise<string> {
const apiUrl = getApiUrl();
const accountOwnerResponse = await axios.get(`${apiUrl}/accounts/${address}?extract=ownerAddress`);
if (accountOwnerResponse && accountOwnerResponse.data) {
return accountOwnerResponse.data;
}

return '';
return await fetchStringValueFromApi(apiUrl, "accounts", address, "ownerAddress");
}

async function getTokenOwner(token: string): Promise<string> {
Expand All @@ -127,34 +129,27 @@ export const robot = (app: Probot) => {

const tokenOwner = await getTokenOwnerFromApi(token, apiUrl);
if (new Address(tokenOwner).isContractAddress()) {
const ownerResult = await axios.get(`${apiUrl}/accounts/${tokenOwner}?extract=ownerAddress`);
return ownerResult.data;
return await fetchStringValueFromApi(apiUrl, "accounts", tokenOwner, "ownerAddress");
}

return tokenOwner;
}

async function getTokenOwnerFromApi(token: string, apiUrl: string): Promise<string> {
try {
const tokenOwnerResponse = await axios.get(`${apiUrl}/tokens/${token}?extract=owner`);
if (tokenOwnerResponse && tokenOwnerResponse.data) {
return tokenOwnerResponse.data;
}
return await fetchStringValueFromApi(apiUrl, "tokens", token, "owner") ||
await fetchStringValueFromApi(apiUrl, "collections", token, "owner");
}

return '';
} catch (error) {
console.log(`Cannot find token owner on the 'tokens' endpoint. Will try the 'collections'`);
async function fetchStringValueFromApi(apiUrl: string, endpoint: string, query: string, extract?: string): Promise<string> {
let requestUrl = `${apiUrl}/${endpoint}/${query}`;
if (extract) {
requestUrl += `?extract=${extract}`;
}

try {
const tokenOwnerResponse = await axios.get(`${apiUrl}/collections/${token}?extract=owner`);
if (tokenOwnerResponse && tokenOwnerResponse.data) {
return tokenOwnerResponse.data;
}

return '';
const response = await axios.get(requestUrl);
return response.data;
} catch (error) {
await fail(`Cannot find token or collection with ID ${token}: ${error}`);
console.error(`Cannot query API at ${requestUrl}: ${error}`);
return '';
}
}
Expand Down

0 comments on commit 5e9ee38

Please sign in to comment.