{"id":21471433,"url":"https://github.com/max-tonny8/via_protocol","last_synced_at":"2025-03-27T20:19:35.417Z","repository":{"id":250665214,"uuid":"808667004","full_name":"max-tonny8/via_protocol","owner":"max-tonny8","description":"A small blazing fast wrapper over the VIA Router API for on-chain and cross-chain swaps.","archived":false,"fork":false,"pushed_at":"2024-05-31T14:52:00.000Z","size":51,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T22:41:17.544Z","etag":null,"topics":["ethereum","ethers","sdk","solana","wallet"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/max-tonny8.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-31T14:51:25.000Z","updated_at":"2024-11-28T18:44:52.000Z","dependencies_parsed_at":"2024-07-29T08:45:15.782Z","dependency_job_id":null,"html_url":"https://github.com/max-tonny8/via_protocol","commit_stats":null,"previous_names":["tonny0831/via_protocol","max-tonny8/via_protocol"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/max-tonny8%2Fvia_protocol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/max-tonny8%2Fvia_protocol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/max-tonny8%2Fvia_protocol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/max-tonny8%2Fvia_protocol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/max-tonny8","download_url":"https://codeload.github.com/max-tonny8/via_protocol/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245916741,"owners_count":20693400,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ethereum","ethers","sdk","solana","wallet"],"created_at":"2024-11-23T09:34:38.903Z","updated_at":"2025-03-27T20:19:35.393Z","avatar_url":"https://github.com/max-tonny8.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003ca href=\"https://via.exchange\" target=\"_blank\"\u003e\u003cimg alt=\"Via Protocol is the most advanced cross-chain aggregation protocol\" src=\"https://user-images.githubusercontent.com/55061526/182078399-edbc98a1-cc9b-47e0-a475-8f4ea8b27753.png\" width=\"100%\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n# VIA SDK typescript\n\nA small blazing fast wrapper over the VIA Router API for on-chain and cross-chain swaps.\nOur API allows you to find the best route for moving funds between chains!\n\n### Feature List\n\n|feature|remark|method|\n|--|--|--|\n|get routes|Swaps/bridges cheapest routes|getRoutes|\n|check allowance|Allowance to check whether it's needed or not to approve|getAllowanceStatus|\n|build approval transaction|The transaction that approves the VIA contract to spend your token|buildApprovalTx|\n|build transaction|The transaction that will perform a swap/bridge according to the route|buildTx|\n|check transaction|Check the status of the transaction|checkTx|\n\n\n### Installation\n```\nnpm i @viaprotocol/router-sdk\n```\n\n\n### Usage\n\nFirst you need to initialize a VIA Client instance:\n\n``` js\nimport {Via} from '@viaprotocol/router-sdk';\n\nconst DEFAULT_API_KEY = 'e3db93a3-ae1c-41e5-8229-b8c1ecef5583';\nconst cli = new Via({apiKey: DEFAULT_API_KEY, url: 'https://router-api.via.exchange', timeout: 30000});\n```\n\n\u003e ⚠️ Default API key has 1 RPS rate limit per IP. [Contact us](mailto:mbelyaev@via.exchange) if you need your personal API key with much higher limits.\n\n#### Get the best routes\n\n``` js\nconst pagesNum = await cli.routesPages(); // cache me!\nconst baseParams = {\n    fromChainId: 1,\n    fromTokenAddress: '0x0000000000000000000000000000000000000000',\n    fromAmount: Math.pow(10, 18),\n    toChainId: 56,\n    toTokenAddress: '0x0000000000000000000000000000000000000000',\n    fromAddress: '0x856cc59aaE47997a1C8D5472Fc8dfef27821235d', // might be null\n    multiTx: false, // whether to return routes with multiple user transactions\n    limit: 1,\n};\nconst params = [...Array(pagesNum)].map(\n    (_, i) =\u003e ({\n        ...baseParams,\n        offset: i+1\n    })\n);\n\nconst routes = await Promise.allSettled(\n    params.map(i =\u003e cli.getRoutes(i))\n);\n```\nRequest parameters description\n|Parameter|Description|\n|--|--|\n|fromChainId|Source chain id|\n|fromTokenAddress|Source token address|\n|fromAmount|Amount|\n|toChainId|Target chain id|\n|toTokenAddress|Target token address|\n|fromAddress|Sender address|\n|multiTx|whether to return routes with multiple user transactions|\n|offset|Pagination offset|\n|limit|Pagination limit|\n\nPagination is needed because the request time for a specific page is faster than for all pages at once\n\n#### Get allowance status\n\nYou must approve the contract to spend your token.\nYou can get route_id from the route you like received above in the code snippet.\n\n``` js\nconst firstNonEmptyPage = routes.find(i =\u003e i.value.routes.length \u003e 0).value;\nconst route = firstNonEmptyPage.routes[0];\nconst routeId = route.routeId;\nconst chainId = fromChainId;\nconst owner = \"YOUR_WALLET_ADDRESS\";\nconst tokenAddress = fromTokenAddress;\nconst numAction = 0;  // number of action in routes\n\nconst allowanceStatus = await cli.getAllowanceStatus(\n    {owner, routeId, numAction}\n);\n```\n\nBuild approval transaction.\nReturns the transaction that approves the VIA API to spend your token.\n\n``` js\nconst amount = fromAmount;\n\ntx = await cli.buildApprovalTx(\n    {routeId, owner, numAction}\n)\n```\n\nNow you can build the transaction that will perform a crosschain swap according to the route.\n\n``` js\n// amount out minimal, you can get it from get_routes as to_token_amount\noutput = firstNonEmptyPage.routes[0].toTokenAmount\n\ntx = await cli.buildTx(\n    {\n        routeId,\n        fromAddress,\n        receiveAddress,\n        numAction\n    }\n)\n```\n\nIf you want to know the status of the transaction, then you need to tell us that you started it\n\n``` js\nawait cli.startRoute(\n    {\n        fromAddress: fromAddress,\n        toAddress: receiveAddress,\n        routeId: route.routeId,\n        txHash: txHash\n    }\n)\n```\n\nAnd start action (you can start from the second action because startRoute also handles first action)\n\n``` js\nawait cli.startAction(\n    {\n        actionUuid: route.actions[numAction].uuid,\n        initialTxHash: actionTxHash\n    }\n)\n```\n\n#### You can see the status of the transaction\n``` js\nconst txStatus = await cli.checkTx(\n    {\n        actionUuid: route.actions[numAction].uuid\n    }\n)\n```\n\n#### You can use websocket\n``` js\nconst v = new Via({apiKey: DEFAULT_API_KEY});\nconst wsProvider = v.getRoutesViaWs({\n    fromAddress: '0xD75183E452d6915356814454D2D64Df149853D38',\n    fromAmount: 148875000000000000,\n    fromChainId: 56,\n    toChainId: 56,\n    fromTokenAddress: '0x0000000000000000000000000000000000000000',\n    toTokenAddress: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',\n    multiTx: true\n});\n\nwsProvider.onopen = function open() {\n    console.log('connected');\n};\n\nwsProvider.onclose = function close() {\n    console.log('disconnected');\n};\n\nwsProvider.onmessage = function incoming(data) {\n    const res = JSON.parse(data.data as string);\n    let route: IRoute[];\n    let status: IRouteFetchStatus;\n    if (Array.isArray(res)) {\n        route = res;\n        console.log(route);\n    } else {\n        status = res.status;\n        console.log(status);\n\n        if (status.finished === status.all){\n            wsProvider.close()\n        }\n    }\n};\n```\n\nResponse parameters description\n|Parameter|Description|\n|--|--|\n|retry|Time to retry in ms|\n|event|Status of the transaction|\n|data.started|Started time of the transaction on source chain|\n|data.finished|Finished time of the transaction on source chain|\n|data.txHash|Hash of the destination transaction|\n|data.actualAmount|Received amount|\n\n\n### Multi-tx\n\nWith via.exchange it is possible to execute routes in several steps. This will help expand the number of available routes and, as a result, help you find the most profitable routes!\n\n\n``` js\nconst pagesNum = await cli.routesPages(); // cache me!\nconst baseParams = {\n    fromChainId: 1,\n    fromTokenAddress: '0x0000000000000000000000000000000000000000',\n    fromAmount: Math.pow(10, 18),\n    toChainId: 56,\n    toTokenAddress: '0x0000000000000000000000000000000000000000',\n    fromAddress: '0x856cc59aaE47997a1C8D5472Fc8dfef27821235d', // might be null\n    multiTx: true, // !!\n    limit: 1,\n};\nconst params = [...Array(pagesNum)].map(\n    (_, i) =\u003e ({\n        ...baseParams,\n        offset: i+1\n    })\n);\n\nconst routes = await Promise.allSettled(\n    params.map(i =\u003e cli.getRoutes(i))\n);\n```\n\nNow all you need is to sequentially execute transactions for different numAction\n\n``` js\nconst actionCount = firstNonEmptyPage.routes[0].length;\n\nfor (let numAction = 0; i \u003c specified_len; i++) {\n    tx = await cli.buildTx(\n        {\n            routeId,\n            fromAddress,\n            receiveAddress,\n            numAction\n        }\n    );\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmax-tonny8%2Fvia_protocol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmax-tonny8%2Fvia_protocol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmax-tonny8%2Fvia_protocol/lists"}