rango-sdk-basic 0.1.22 → 0.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/lib/dist/index.d.ts +3 -0
  2. package/lib/dist/index.d.ts.map +1 -0
  3. package/lib/dist/index.js +8 -0
  4. package/lib/dist/rango-sdk-basic.cjs.development.js +1074 -0
  5. package/lib/dist/rango-sdk-basic.cjs.development.js.map +1 -0
  6. package/lib/dist/rango-sdk-basic.cjs.production.min.js +2 -0
  7. package/lib/dist/rango-sdk-basic.cjs.production.min.js.map +1 -0
  8. package/lib/dist/rango-sdk-basic.esm.js +1065 -0
  9. package/lib/dist/rango-sdk-basic.esm.js.map +1 -0
  10. package/lib/dist/services/client.d.ts +25 -0
  11. package/lib/dist/services/client.d.ts.map +1 -0
  12. package/lib/dist/services/executor.d.ts +8 -0
  13. package/lib/dist/services/executor.d.ts.map +1 -0
  14. package/lib/dist/services/index.d.ts +2 -0
  15. package/lib/dist/services/index.d.ts.map +1 -0
  16. package/lib/dist/types/api/balance.d.ts +2 -0
  17. package/lib/dist/types/api/balance.d.ts.map +1 -0
  18. package/lib/dist/types/api/common.d.ts +4 -0
  19. package/lib/dist/types/api/common.d.ts.map +1 -0
  20. package/lib/dist/types/api/meta.d.ts +3 -0
  21. package/lib/dist/types/api/meta.d.ts.map +1 -0
  22. package/lib/dist/types/api/routing.d.ts +2 -0
  23. package/lib/dist/types/api/routing.d.ts.map +1 -0
  24. package/lib/dist/types/api/transactions.d.ts +2 -0
  25. package/lib/dist/types/api/transactions.d.ts.map +1 -0
  26. package/lib/dist/types/api/txs/cosmos.d.ts +2 -0
  27. package/lib/dist/types/api/txs/cosmos.d.ts.map +1 -0
  28. package/lib/dist/types/api/txs/evm.d.ts +2 -0
  29. package/lib/dist/types/api/txs/evm.d.ts.map +1 -0
  30. package/lib/dist/types/api/txs/index.d.ts +4 -0
  31. package/lib/dist/types/api/txs/index.d.ts.map +1 -0
  32. package/lib/dist/types/api/txs/transfer.d.ts +2 -0
  33. package/lib/dist/types/api/txs/transfer.d.ts.map +1 -0
  34. package/lib/dist/types/configs.d.ts +5 -0
  35. package/lib/dist/types/configs.d.ts.map +1 -0
  36. package/lib/dist/types/index.d.ts +9 -0
  37. package/lib/dist/types/index.d.ts.map +1 -0
  38. package/lib/dist/types/utils/errors.d.ts +23 -0
  39. package/lib/dist/types/utils/errors.d.ts.map +1 -0
  40. package/lib/dist/utils/errors.d.ts +2 -0
  41. package/lib/dist/utils/errors.d.ts.map +1 -0
  42. package/lib/dist/utils/promise.d.ts +2 -0
  43. package/lib/dist/utils/promise.d.ts.map +1 -0
  44. package/package.json +4 -3
  45. package/src/index.ts +2 -0
  46. package/src/services/client.ts +253 -0
  47. package/src/services/executor.ts +140 -0
  48. package/src/services/index.ts +1 -0
  49. package/src/types/api/balance.ts +1 -0
  50. package/src/types/api/common.ts +8 -0
  51. package/src/types/api/meta.ts +2 -0
  52. package/src/types/api/routing.ts +1 -0
  53. package/src/types/api/transactions.ts +1 -0
  54. package/src/types/api/txs/cosmos.ts +1 -0
  55. package/src/types/api/txs/evm.ts +1 -0
  56. package/src/types/api/txs/index.ts +3 -0
  57. package/src/types/api/txs/transfer.ts +1 -0
  58. package/src/types/configs.ts +5 -0
  59. package/src/types/index.ts +8 -0
  60. package/src/types/utils/errors.ts +22 -0
  61. package/src/utils/errors.ts +29 -0
  62. package/src/utils/promise.ts +3 -0
@@ -0,0 +1,140 @@
1
+ import { Signer } from 'ethers'
2
+ import { RangoClient } from './client'
3
+ import { EvmTransaction } from '../types/api/txs'
4
+ import {
5
+ StatusResponse,
6
+ SwapResponse,
7
+ TransactionStatus,
8
+ TransactionType,
9
+ } from '../types/api/transactions'
10
+ import { sleep } from '../utils/promise'
11
+
12
+ export function prepareEvmTransaction(
13
+ evmTx: EvmTransaction,
14
+ isApprove: boolean
15
+ ) {
16
+ const gasPrice =
17
+ !!evmTx.gasPrice && !evmTx.gasPrice.startsWith('0x')
18
+ ? '0x' + parseInt(evmTx.gasPrice).toString(16)
19
+ : null
20
+ const manipulatedTx = {
21
+ ...evmTx,
22
+ gasPrice,
23
+ }
24
+ let tx = {}
25
+ if (!!manipulatedTx.from) tx = { ...tx, from: manipulatedTx.from }
26
+ if (isApprove) {
27
+ if (!!manipulatedTx.approveTo) tx = { ...tx, to: manipulatedTx.approveTo }
28
+ if (!!manipulatedTx.approveData)
29
+ tx = { ...tx, data: manipulatedTx.approveData }
30
+ } else {
31
+ if (!!manipulatedTx.txTo) tx = { ...tx, to: manipulatedTx.txTo }
32
+ if (!!manipulatedTx.txData) tx = { ...tx, data: manipulatedTx.txData }
33
+ if (!!manipulatedTx.value) tx = { ...tx, value: manipulatedTx.value }
34
+ if (!!manipulatedTx.gasLimit)
35
+ tx = { ...tx, gasLimit: manipulatedTx.gasLimit }
36
+ if (!!manipulatedTx.gasPrice)
37
+ tx = { ...tx, gasPrice: manipulatedTx.gasPrice }
38
+ }
39
+ return tx
40
+ }
41
+
42
+ async function checkApprovalSync(
43
+ requestId: string,
44
+ txId: string,
45
+ rangoClient: RangoClient
46
+ ): Promise<boolean> {
47
+ while (true) {
48
+ try {
49
+ const approvalResponse = await rangoClient.isApproved(requestId, txId)
50
+ if (approvalResponse?.isApproved) return true
51
+ if (
52
+ !approvalResponse?.isApproved &&
53
+ approvalResponse?.txStatus === TransactionStatus.FAILED
54
+ )
55
+ return false
56
+ } catch (err) {
57
+ console.log('ignorinig error', { err })
58
+ }
59
+ await sleep(3_000)
60
+ }
61
+ }
62
+
63
+ export const checkTransactionStatusSync = async (
64
+ requestId: string,
65
+ txId: string,
66
+ rangoClient: RangoClient
67
+ ) => {
68
+ let txStatus: StatusResponse | undefined
69
+ while (true) {
70
+ try {
71
+ txStatus = await rangoClient.status({
72
+ requestId,
73
+ txId,
74
+ })
75
+ } catch (err) {
76
+ console.log('ignorinig error', { err })
77
+ }
78
+ if (!!txStatus) {
79
+ if (
80
+ !!txStatus.status &&
81
+ [TransactionStatus.FAILED, TransactionStatus.SUCCESS].includes(
82
+ txStatus.status
83
+ )
84
+ ) {
85
+ return txStatus
86
+ }
87
+ }
88
+ await sleep(3_000)
89
+ }
90
+ }
91
+
92
+ export const executeEvmRoute = async (
93
+ client: RangoClient,
94
+ signer: Signer,
95
+ route: SwapResponse
96
+ ): Promise<StatusResponse> => {
97
+ const { tx, requestId, error, resultType } = route
98
+ if (resultType != 'OK') throw new Error(resultType)
99
+ if (!!error || !tx)
100
+ throw new Error(error || 'Error creating the transaction.')
101
+ if (tx?.type !== TransactionType.EVM)
102
+ throw new Error('Non Evm transactions are not supported yet.')
103
+ const evmTransaction = tx as EvmTransaction
104
+ if (!evmTransaction) throw new Error('Transaction is null. Please try again!')
105
+ const txChainId = parseInt(evmTransaction.blockChain.chainId || '-1')
106
+ let signerChainId = await signer.getChainId()
107
+ if (signerChainId !== txChainId) {
108
+ throw new Error(
109
+ `Signer chainId ${signerChainId} doesn't match required chainId ${txChainId}.`
110
+ )
111
+ }
112
+ if (!!evmTransaction.approveTo && !!evmTransaction.approveData) {
113
+ const approveTxData = prepareEvmTransaction(evmTransaction, true)
114
+ const approveTx = await signer.sendTransaction(approveTxData)
115
+ approveTx.wait()
116
+ const isApproved = await checkApprovalSync(
117
+ requestId,
118
+ approveTx.hash,
119
+ client
120
+ )
121
+ if (!isApproved) throw new Error('Error in approve transaction.')
122
+ }
123
+ signerChainId = await signer.getChainId()
124
+ if (signerChainId !== txChainId) {
125
+ throw new Error(
126
+ `Signer chainId ${signerChainId} doesn't match required chainId ${txChainId}.`
127
+ )
128
+ }
129
+ const mainTxData = prepareEvmTransaction(evmTransaction, false)
130
+ const mainTx = await signer.sendTransaction(mainTxData)
131
+ mainTx.wait()
132
+ const status = await checkTransactionStatusSync(
133
+ requestId,
134
+ mainTx.hash,
135
+ client
136
+ )
137
+ if (status.status !== TransactionStatus.SUCCESS)
138
+ throw new Error(`Cross-chain swap failed. ${status.error || ''}`)
139
+ return status
140
+ }
@@ -0,0 +1 @@
1
+ export { RangoClient } from './client'
@@ -0,0 +1 @@
1
+ export * from 'rango-types/lib/api/basic/balance'
@@ -0,0 +1,8 @@
1
+ import { Asset } from 'rango-types/lib/api/basic'
2
+ export * from 'rango-types/lib/api/basic/common'
3
+
4
+ export function assetToString(asset: Asset): string {
5
+ if (!!asset.address)
6
+ return `${asset.blockchain}.${asset.symbol}--${asset.address}`
7
+ else return `${asset.blockchain}.${asset.symbol}`
8
+ }
@@ -0,0 +1,2 @@
1
+ export * from 'rango-types/lib/api/basic/meta'
2
+ export * from 'rango-types/lib/api/shared/type-gaurds'
@@ -0,0 +1 @@
1
+ export * from 'rango-types/lib/api/basic/routing'
@@ -0,0 +1 @@
1
+ export * from 'rango-types/lib/api/basic/transactions'
@@ -0,0 +1 @@
1
+ export * from 'rango-types/lib/api/basic/txs/cosmos'
@@ -0,0 +1 @@
1
+ export * from 'rango-types/lib/api/basic/txs/evm'
@@ -0,0 +1,3 @@
1
+ export * from './evm'
2
+ export * from './cosmos'
3
+ export * from './transfer'
@@ -0,0 +1 @@
1
+ export * from 'rango-types/lib/api/basic/txs/transfer'
@@ -0,0 +1,5 @@
1
+ import { GenericAbortSignal } from 'axios'
2
+
3
+ export interface RequestOptions {
4
+ signal?: GenericAbortSignal
5
+ }
@@ -0,0 +1,8 @@
1
+ export * from './api/balance'
2
+ export * from './api/common'
3
+ export * from './api/meta'
4
+ export * from './api/routing'
5
+ export * from './api/transactions'
6
+ export * from './api/txs'
7
+ export * from './utils/errors'
8
+ export * from './configs'
@@ -0,0 +1,22 @@
1
+ export const MetamaskErrorCodes = {
2
+ rpc: {
3
+ invalidInput: -32000,
4
+ resourceNotFound: -32001,
5
+ resourceUnavailable: -32002,
6
+ transactionRejected: -32003,
7
+ methodNotSupported: -32004,
8
+ limitExceeded: -32005,
9
+ parse: -32700,
10
+ invalidRequest: -32600,
11
+ methodNotFound: -32601,
12
+ invalidParams: -32602,
13
+ internal: -32603,
14
+ },
15
+ provider: {
16
+ userRejectedRequest: 4001,
17
+ unauthorized: 4100,
18
+ unsupportedMethod: 4200,
19
+ disconnected: 4900,
20
+ chainDisconnected: 4901,
21
+ },
22
+ }
@@ -0,0 +1,29 @@
1
+ import { getMessageFromCode } from 'eth-rpc-errors'
2
+ import { MetamaskErrorCodes } from '../types/utils/errors'
3
+
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ export const prettifyError = (error: any) => {
6
+ if (!error) return error
7
+ if (error.code && (error.code === 'ACTION_REJECTED' || error.code === 4001))
8
+ return new Error('Transaction Rejected')
9
+ if (error && typeof error.code === 'number') {
10
+ if (Object.values(MetamaskErrorCodes.provider).includes(error.code)) {
11
+ return new Error(getMessageFromCode(error.code))
12
+ }
13
+ if (Object.values(MetamaskErrorCodes.rpc).includes(error.code)) {
14
+ if (
15
+ error.code === MetamaskErrorCodes.rpc.internal &&
16
+ error.message?.includes('underpriced')
17
+ )
18
+ return new Error('Transaction underpriced')
19
+ if (
20
+ error.message?.includes('intrinsic gas too low') ||
21
+ error.message?.includes('out of gas')
22
+ )
23
+ return new Error('This Gas limit is low.')
24
+ return new Error(getMessageFromCode(error.code))
25
+ }
26
+ }
27
+ if (error.message) return new Error(error.message)
28
+ return error
29
+ }
@@ -0,0 +1,3 @@
1
+ export function sleep(ms: number) {
2
+ return new Promise((resolve) => setTimeout(resolve, ms))
3
+ }