Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

fix: fixing eslint warning; Obtaining swaps configs by note prefix #122

Merged
merged 7 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions api/storage/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1641,16 +1641,8 @@ native-fetch@^3.0.0:
resolved "https://registry.yarnpkg.com/native-fetch/-/native-fetch-3.0.0.tgz#06ccdd70e79e171c365c75117959cf4fe14a09bb"
integrity sha512-G3Z7vx0IFb/FQ4JxvtqGABsOTIqRWvgQz6e+erkB+JJD6LrszQtMozEHI4EkmgZQvnGHrpLVzUWk7t4sJCIkVw==

node-fetch@2.6.7, node-fetch@^2.6.1:
node-fetch@2.6.7, node-fetch@^2.6.1, "node-fetch@https://registry.npmjs.org/@achingbrain/node-fetch/-/node-fetch-2.6.7.tgz":
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

"node-fetch@https://registry.npmjs.org/@achingbrain/node-fetch/-/node-fetch-2.6.7.tgz":
version "2.6.7"
uid "1b5d62978f2ed07b99444f64f0df39f960a6d34d"
resolved "https://registry.npmjs.org/@achingbrain/node-fetch/-/node-fetch-2.6.7.tgz#1b5d62978f2ed07b99444f64f0df39f960a6d34d"

node-forge@^1.2.1:
Expand Down Expand Up @@ -2115,11 +2107,6 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==

trim-newlines@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
Expand Down Expand Up @@ -2264,19 +2251,6 @@ web3.storage@4.3.0:
throttled-queue "^2.1.2"
uint8arrays "^3.0.0"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

which-boxed-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"
Expand Down
28 changes: 25 additions & 3 deletions api/swaps/get-swap-configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@

import json
from http.server import BaseHTTPRequestHandler
from time import sleep
from urllib import parse

import requests
from algoworld_contracts import contracts
from requests.adapters import HTTPAdapter, Retry

import api_utils.utils as common

retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS"],
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http_client = requests.Session()
http_client.mount("https://", adapter)
http_client.mount("http://", adapter)


def compileSwapProxy(cfg: common.SwapProxyConfig):
swapper = contracts.get_swapper_proxy_teal(
Expand All @@ -32,6 +45,16 @@ def compileSwapProxy(cfg: common.SwapProxyConfig):
return response


def retry(fun, max_tries=10):
for i in range(max_tries):
try:
sleep(0.3)
fun()
break
except Exception:
continue


class handler(BaseHTTPRequestHandler):
def do_GET(self):
s = self.path
Expand Down Expand Up @@ -64,10 +87,9 @@ def do_GET(self):
config_file_url = common.get_decoded_note_from_txn(swap_config_txn)

if "ipfs" in config_file_url:
config_file_url = f'https://{config_file_url.split("ipfs://")[1]}.ipfs.cf-ipfs.com/aw_swaps.json'
config_file_url = f'https://{config_file_url.split("ipfs://")[1]}.ipfs.dweb.link/aw_swaps.json'
try:
configFileResponse = requests.get(config_file_url).json()

configFileResponse = http_client.get(config_file_url).json()
configFile = json.dumps(
[
config
Expand Down
4 changes: 4 additions & 0 deletions e2e/pages/public-swaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ test.describe(`Public Swaps`, () => {
await page.locator(`id=${NAV_BAR_ICON_HOME_BTN_ID}`).click();
await page.locator(`id=${NAV_BAR_SETTINGS_BTN_ID}`).click();
await page.locator(`id=${NAV_BAR_SETTINGS_MENU_ITEM_ID(`Logout`)}`).click();
await page.waitForTimeout(1000 * 5);

await page.goto(`${AW_SWAPPER_BASE_URL}/public-swaps`);
});

test(`Anonymous user should find Bob's public swap`, async () => {
await page.waitForTimeout(1000 * 5);
await page.locator(`id=${PUBLIC_SWAPS_SEARCH_FIELD_ID}`).fill(BOB_ADDRESS);
await page.locator(`id=${PUBLIC_SWAPS_SEARCH_BUTTON_ID}`).click();
await page.waitForTimeout(1000 * 5);
await page.locator(`id=${PUBLIC_SWAP_OPEN_SWAP_BUTTON_ID}`).click();
await page.waitForTimeout(5000);
const pages = context.pages();
Expand All @@ -172,6 +175,7 @@ test.describe(`Public Swaps`, () => {
await expect(page.locator(`id=${CONNECT_WALLET_DIALOG_ID}`)).toBeVisible();
await page.locator(`div[role="button"]:has-text("Mnemonic")`).click();
await expect(page.locator(`id=${MNEMONIC_DIALOG_ID}`)).toBeVisible();
await page.waitForTimeout(5000);
await page
.locator(`id=${MNEMONIC_DIALOG_TEXT_INPUT_ID}`)
.fill(process.env.E2E_TESTS_ALICE_MNEMONIC ?? ``);
Expand Down
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const moduleExports = {
pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
images: {
domains: [
'cf-ipfs.com',
'*.cf-ipfs.com',
'dweb.link',
'*.dweb.link',
'images.unsplash.com',
'vitals.vercel-insights.com',
'google-analytics.com',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"algorand-walletconnect-qrcode-modal": "1.7.4",
"algosdk": "1.19.0",
"axios": "0.27.2",
"axios-retry": "3.3.1",
"next": "12.2.4",
"next-pwa": "5.5.5",
"nextjs-google-analytics": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const ALGOEXPLORER_URL = (chain: ChainType) => {
: `https://testnet.algoexplorer.io`;
};

export const EMPTY_ASSET_IMAGE_URL = `https://cf-ipfs.com/ipfs/QmXrsy5TddTiwDCXqGc2yzNowKs7WhCJfQ17rvHuArfnQp`;
export const EMPTY_ASSET_IMAGE_URL = `https://dweb.link/ipfs/QmXrsy5TddTiwDCXqGc2yzNowKs7WhCJfQ17rvHuArfnQp`;
export const LATEST_SWAP_PROXY_VERSION = `0.0.2`;
export const ALL_SWAP_PROXY_VERSIONS = [LATEST_SWAP_PROXY_VERSION, `0.0.1`];
export const AWVT_ASSET_INDEX = (chain: ChainType) => {
Expand Down
2 changes: 0 additions & 2 deletions src/components/Tables/AssetsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import {
} from '@mui/material';
import formatAmount from '@/utils/formatAmount';
import getAssetBalance from '@/utils/api/assets/getAssetBalance';
import { ChainType } from '@/models/Chain';
import { useAppSelector } from '@/redux/store/hooks';
import { ConnectingAirportsOutlined } from '@mui/icons-material';

const columns: GridColDef[] = [
{
Expand Down
1 change: 0 additions & 1 deletion src/pages/swap/[proxy]/[escrow].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ const PerformSwap = () => {
const zeroBalanceAssets = swapAssets.filter((asset) => {
return asset.amount === 0;
});

return zeroBalanceAssets.length > 0;
}, [swapAssets]);

Expand Down
15 changes: 13 additions & 2 deletions src/utils/api/accounts/getPublicSwapCreators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { ChainType } from '@/models/Chain';
import filterAsync from '@/utils/filterAsync';
import axios from 'axios';
import getSwapConfigurationsForAccount from './getSwapConfigurationsForAccount';

export default async function getPublicSwapCreators(
assetId: number,
Expand Down Expand Up @@ -64,9 +65,19 @@ export default async function getPublicSwapCreators(
)
: false;

return (
hasAwvtOptedIn && account.address !== AWVT_CREATOR_ADDRESS(chain)
const hasPrereqs =
hasAwvtOptedIn && account.address !== AWVT_CREATOR_ADDRESS(chain);

if (!hasPrereqs) {
return false;
}

const swapConfigs = await getSwapConfigurationsForAccount(
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved
chain,
account.address,
);

return swapConfigs.length > 0;
} catch (e) {
return false;
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/api/assets/assetsToRawString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { Asset } from '@/models/Asset';
const assetsToRowString = (assets: Asset[], offering = true) => {
let response = ``;

let index = 1;
for (const asset of assets) {
response += `${asset.name} x${
offering ? asset.offeringAmount : asset.requestingAmount
}\n`;
index += 1;
}

return response;
Expand Down
57 changes: 57 additions & 0 deletions src/utils/api/swaps/createSaveSwapConfigTxns.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ChainType } from '@/models/Chain';
import createSaveSwapConfigTxns from './createSaveSwapConfigTxns';

import { generateAccount } from 'algosdk';

import { LogicSigAccount, encodeAddress } from 'algosdk';
import getLogicSign from '../accounts/getLogicSignature';
import { TransactionToSign, TransactionToSignType } from '@/models/Transaction';
import { dummyContract } from './__utils__/testUtils';

describe(`createSaveSwapConfigTxns`, () => {
it(`generates txns correctly`, async () => {
const dummyAccount = generateAccount();
const dummyLogicSig = getLogicSign(dummyContract) as LogicSigAccount;

const fundingFee = 10_000;
const dummyCid = `test`;
const txns = await createSaveSwapConfigTxns(
ChainType.TestNet,
dummyAccount.addr,
dummyLogicSig,
fundingFee,
dummyCid,
);

// First txn
expect((txns[0] as TransactionToSign).signer).toBe(undefined);
expect((txns[0] as TransactionToSign).type).toBe(
TransactionToSignType.UserFeeTransaction,
);
expect(
encodeAddress((txns[0] as TransactionToSign).transaction.from.publicKey),
).toBe(dummyAccount.addr);
expect(
encodeAddress((txns[0] as TransactionToSign).transaction.to.publicKey),
).toBe(dummyLogicSig.address());
expect((txns[0] as TransactionToSign).transaction.amount).toBe(fundingFee);

// Second txn
expect((txns[1] as TransactionToSign).signer).toBe(dummyLogicSig);
expect((txns[1] as TransactionToSign).type).toBe(
TransactionToSignType.LsigTransaction,
);
expect(
encodeAddress((txns[1] as TransactionToSign).transaction.from.publicKey),
).toBe(dummyLogicSig.address());
expect(
encodeAddress((txns[1] as TransactionToSign).transaction.to.publicKey),
).toBe(dummyLogicSig.address());
expect(
Buffer.from(
(txns[1] as TransactionToSign).transaction.note as any,
`base64`,
).toString(`utf8`),
).toBe(`ipfs://test`);
});
});
33 changes: 19 additions & 14 deletions src/utils/api/swaps/loadSwapConfigurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import { SwapConfiguration } from '@/models/Swap';
import axios from 'axios';
import accountExists from '../accounts/accountExists';
import { indexerForChain } from '../algorand';
import axiosRetry from 'axios-retry';

// Exponential back-off retry delay between requests
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay });

export default async function loadSwapConfigurations(
chain: ChainType,
Expand All @@ -33,28 +37,29 @@ export default async function loadSwapConfigurations(
return [] as SwapConfiguration[];
}

const paymentTxns = await client.lookupAccountTransactions(proxyAddress).do();
const ipfsPrefixBase64 = Buffer.from(`ipfs://`).toString(`base64`);
const paymentTxns = await client
.lookupAccountTransactions(proxyAddress)
.notePrefix(ipfsPrefixBase64)
.do();

if (paymentTxns.transactions.length === 0) {
return [] as SwapConfiguration[];
}

const swapConfigTxn = paymentTxns.transactions[0];

const configFileUrl = Buffer.from(swapConfigTxn.note, `base64`).toString(
`utf-8`,
);

if (configFileUrl.includes(`ipfs`)) {
const configFile = await axios
.get(
`https://${
configFileUrl.split(`ipfs://`)[1]
}.ipfs.cf-ipfs.com/aw_swaps.json`,
)
.then((res) => res.data);

return configFile as SwapConfiguration[];
} else {
return [] as SwapConfiguration[];
}
const configFile = await axios
.get(
`https://${
configFileUrl.split(`ipfs://`)[1]
}.ipfs.dweb.link/aw_swaps.json`,
)
.then((res) => res.data);

return configFile as SwapConfiguration[];
}
2 changes: 1 addition & 1 deletion src/utils/ipfsToProxyUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe(`ipfsToProxyUrl()`, () => {
);

expect(url).toEqual(
`https://cf-ipfs.com/ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu`,
`https://dweb.link/ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu`,
);
});
});
2 changes: 1 addition & 1 deletion src/utils/ipfsToProxyUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

export const ipfsToProxyUrl = (ipfsUrl: string) => {
return ipfsUrl.includes(`ipfs://`)
? `https://cf-ipfs.com/ipfs/${ipfsUrl.split(`ipfs://`)[1]}`
? `https://dweb.link/ipfs/${ipfsUrl.split(`ipfs://`)[1]}`
: ipfsUrl;
};
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@
core-js-pure "^3.20.2"
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.17.2", "@babel/runtime@^7.18.3", "@babel/runtime@^7.18.6", "@babel/runtime@^7.18.9", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.15.4", "@babel/runtime@^7.17.2", "@babel/runtime@^7.18.3", "@babel/runtime@^7.18.6", "@babel/runtime@^7.18.9", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.9.tgz#b4fcfce55db3d2e5e080d2490f608a3b9f407f4a"
integrity sha512-lkqXDcvlFT5rvEjiu6+QYO+1GXrEHRo2LOtS7E4GtX5ESIZOgepqsZBVIj6Pv+a6zqsya9VCgiK1KAK4BvJDAw==
Expand Down Expand Up @@ -2733,6 +2733,14 @@ axe-core@^4.4.3:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.3.tgz#11c74d23d5013c0fa5d183796729bc3482bd2f6f"
integrity sha512-32+ub6kkdhhWick/UjvEwRchgoetXqTK14INLqbGm5U2TzBkBNF3nQtLYm8ovxSkQWArjEQvftCKryjZaATu3w==

axios-retry@3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.3.1.tgz#47624646138aedefbad2ac32f226f4ee94b6dcab"
integrity sha512-RohAUQTDxBSWLFEnoIG/6bvmy8l3TfpkclgStjl5MDCMBDgapAWCmr1r/9harQfWC8bzLC8job6UcL1A1Yc+/Q==
dependencies:
"@babel/runtime" "^7.15.4"
is-retry-allowed "^2.2.0"

axios@0.27.2:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
Expand Down Expand Up @@ -4958,6 +4966,11 @@ is-regexp@^1.0.0:
resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-1.0.0.tgz#fd2d883545c46bac5a633e7b9a09e87fa2cb5069"
integrity sha512-7zjFAPO4/gwyQAAgRRmqeEeyIICSdmCqa3tsVHMdBzaXXRiqopZL4Cyghg/XulGWrtABTpbnYYzzIRffLkP4oA==

is-retry-allowed@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz#88f34cbd236e043e71b6932d09b0c65fb7b4d71d"
integrity sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg==

is-shared-array-buffer@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-shared-array-buffer/-/is-shared-array-buffer-1.0.2.tgz#8f259c573b60b6a32d4058a1a07430c0a7344c79"
Expand Down