riskmarket-sdk 1.0.0

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.

Potentially problematic release.


This version of riskmarket-sdk might be problematic. Click here for more details.

@@ -0,0 +1,75 @@
1
+ import { ethers } from 'ethers';
2
+ /**
3
+ * Utility class with helper functions
4
+ */
5
+ export declare class Utils {
6
+ /**
7
+ * Validate Ethereum address
8
+ */
9
+ static isValidAddress(address: string): boolean;
10
+ /**
11
+ * Validate and checksum an address
12
+ */
13
+ static validateAddress(address: string): string;
14
+ /**
15
+ * Format token amount from wei to human-readable format
16
+ */
17
+ static formatAmount(amount: string | bigint, decimals?: number): string;
18
+ /**
19
+ * Parse human-readable amount to wei
20
+ */
21
+ static parseAmount(amount: string, decimals?: number): string;
22
+ /**
23
+ * Validate amount is positive and not zero
24
+ */
25
+ static validateAmount(amount: string): void;
26
+ /**
27
+ * Calculate deadline timestamp (current time + minutes)
28
+ */
29
+ static getDeadline(minutes?: number): number;
30
+ /**
31
+ * Calculate percentage difference
32
+ */
33
+ static calculatePercentageChange(oldValue: string, newValue: string): string;
34
+ /**
35
+ * Estimate gas with buffer (adds 20% buffer)
36
+ */
37
+ static addGasBuffer(gasEstimate: bigint | string, bufferPercent?: number): string;
38
+ /**
39
+ * Parse error message from contract call
40
+ */
41
+ static parseContractError(error: any): string;
42
+ /**
43
+ * Wait for transaction confirmation
44
+ */
45
+ static waitForTransaction(txHash: string, provider: ethers.Provider, confirmations?: number): Promise<ethers.TransactionReceipt | null>;
46
+ /**
47
+ * Get current gas prices
48
+ */
49
+ static getGasPrices(provider: ethers.Provider): Promise<{
50
+ gasPrice?: bigint;
51
+ maxFeePerGas?: bigint;
52
+ maxPriorityFeePerGas?: bigint;
53
+ }>;
54
+ /**
55
+ * Convert chain ID to network name
56
+ */
57
+ static getNetworkName(chainId: number): string;
58
+ /**
59
+ * Sleep/delay utility
60
+ */
61
+ static sleep(ms: number): Promise<void>;
62
+ /**
63
+ * Retry logic for API calls
64
+ */
65
+ static retry<T>(fn: () => Promise<T>, maxRetries?: number, delayMs?: number): Promise<T>;
66
+ /**
67
+ * Format transaction hash with ellipsis
68
+ */
69
+ static shortenHash(hash: string, startLength?: number, endLength?: number): string;
70
+ /**
71
+ * Check if a transaction was successful
72
+ */
73
+ static isTransactionSuccessful(receipt: ethers.TransactionReceipt | null): boolean;
74
+ }
75
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/utils/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC;;GAEG;AACH,qBAAa,KAAK;IAChB;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQ/C;;OAEG;IACH,MAAM,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAU/C;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,GAAE,MAAW,GAAG,MAAM;IAW3E;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAW,GAAG,MAAM;IAWjE;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAU3C;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,OAAO,GAAE,MAAW,GAAG,MAAM;IAIhD;;OAEG;IACH,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAU5E;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,aAAa,GAAE,MAAW,GAAG,MAAM;IAMrF;;OAEG;IACH,MAAM,CAAC,kBAAkB,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM;IAuB7C;;OAEG;WACU,kBAAkB,CAC7B,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,CAAC,QAAQ,EACzB,aAAa,GAAE,MAAU,GACxB,OAAO,CAAC,MAAM,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAa5C;;OAEG;WACU,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC;QAC5D,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,oBAAoB,CAAC,EAAE,MAAM,CAAC;KAC/B,CAAC;IAiBF;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAa9C;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvC;;OAEG;WACU,KAAK,CAAC,CAAC,EAClB,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,UAAU,GAAE,MAAU,EACtB,OAAO,GAAE,MAAa,GACrB,OAAO,CAAC,CAAC,CAAC;IAiBb;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,GAAE,MAAU,EAAE,SAAS,GAAE,MAAU,GAAG,MAAM;IAOxF;;OAEG;IACH,MAAM,CAAC,uBAAuB,CAAC,OAAO,EAAE,MAAM,CAAC,kBAAkB,GAAG,IAAI,GAAG,OAAO;CAGnF"}
@@ -0,0 +1,185 @@
1
+ import { ethers } from 'ethers';
2
+ import { SDKError, ErrorType } from '../types';
3
+ /**
4
+ * Utility class with helper functions
5
+ */
6
+ export class Utils {
7
+ /**
8
+ * Validate Ethereum address
9
+ */
10
+ static isValidAddress(address) {
11
+ try {
12
+ return ethers.isAddress(address);
13
+ }
14
+ catch {
15
+ return false;
16
+ }
17
+ }
18
+ /**
19
+ * Validate and checksum an address
20
+ */
21
+ static validateAddress(address) {
22
+ if (!this.isValidAddress(address)) {
23
+ throw new SDKError(`Invalid Ethereum address: ${address}`, ErrorType.VALIDATION_ERROR);
24
+ }
25
+ return ethers.getAddress(address);
26
+ }
27
+ /**
28
+ * Format token amount from wei to human-readable format
29
+ */
30
+ static formatAmount(amount, decimals = 18) {
31
+ try {
32
+ return ethers.formatUnits(amount, decimals);
33
+ }
34
+ catch (error) {
35
+ throw new SDKError(`Failed to format amount: ${error}`, ErrorType.VALIDATION_ERROR);
36
+ }
37
+ }
38
+ /**
39
+ * Parse human-readable amount to wei
40
+ */
41
+ static parseAmount(amount, decimals = 18) {
42
+ try {
43
+ return ethers.parseUnits(amount, decimals).toString();
44
+ }
45
+ catch (error) {
46
+ throw new SDKError(`Failed to parse amount: ${error}`, ErrorType.VALIDATION_ERROR);
47
+ }
48
+ }
49
+ /**
50
+ * Validate amount is positive and not zero
51
+ */
52
+ static validateAmount(amount) {
53
+ const amountBN = BigInt(amount);
54
+ if (amountBN <= 0n) {
55
+ throw new SDKError('Amount must be greater than zero', ErrorType.VALIDATION_ERROR);
56
+ }
57
+ }
58
+ /**
59
+ * Calculate deadline timestamp (current time + minutes)
60
+ */
61
+ static getDeadline(minutes = 20) {
62
+ return Math.floor(Date.now() / 1000) + minutes * 60;
63
+ }
64
+ /**
65
+ * Calculate percentage difference
66
+ */
67
+ static calculatePercentageChange(oldValue, newValue) {
68
+ const old = parseFloat(oldValue);
69
+ const newVal = parseFloat(newValue);
70
+ if (old === 0)
71
+ return '0';
72
+ const change = ((newVal - old) / old) * 100;
73
+ return change.toFixed(2);
74
+ }
75
+ /**
76
+ * Estimate gas with buffer (adds 20% buffer)
77
+ */
78
+ static addGasBuffer(gasEstimate, bufferPercent = 20) {
79
+ const estimate = BigInt(gasEstimate);
80
+ const buffer = estimate * BigInt(bufferPercent) / 100n;
81
+ return (estimate + buffer).toString();
82
+ }
83
+ /**
84
+ * Parse error message from contract call
85
+ */
86
+ static parseContractError(error) {
87
+ // Check for common error patterns
88
+ if (error.reason) {
89
+ return error.reason;
90
+ }
91
+ if (error.message) {
92
+ // Extract revert reason if present
93
+ const revertMatch = error.message.match(/reason="([^"]*)"/);
94
+ if (revertMatch) {
95
+ return revertMatch[1];
96
+ }
97
+ return error.message;
98
+ }
99
+ if (error.data?.message) {
100
+ return error.data.message;
101
+ }
102
+ return 'Unknown error occurred';
103
+ }
104
+ /**
105
+ * Wait for transaction confirmation
106
+ */
107
+ static async waitForTransaction(txHash, provider, confirmations = 1) {
108
+ try {
109
+ const receipt = await provider.waitForTransaction(txHash, confirmations);
110
+ return receipt;
111
+ }
112
+ catch (error) {
113
+ throw new SDKError(`Transaction failed: ${this.parseContractError(error)}`, ErrorType.TRANSACTION_ERROR, { txHash });
114
+ }
115
+ }
116
+ /**
117
+ * Get current gas prices
118
+ */
119
+ static async getGasPrices(provider) {
120
+ try {
121
+ const feeData = await provider.getFeeData();
122
+ return {
123
+ gasPrice: feeData.gasPrice || undefined,
124
+ maxFeePerGas: feeData.maxFeePerGas || undefined,
125
+ maxPriorityFeePerGas: feeData.maxPriorityFeePerGas || undefined,
126
+ };
127
+ }
128
+ catch (error) {
129
+ throw new SDKError(`Failed to fetch gas prices: ${error}`, ErrorType.NETWORK_ERROR);
130
+ }
131
+ }
132
+ /**
133
+ * Convert chain ID to network name
134
+ */
135
+ static getNetworkName(chainId) {
136
+ const networks = {
137
+ 1: 'Ethereum Mainnet',
138
+ 5: 'Goerli',
139
+ 11155111: 'Sepolia',
140
+ 137: 'Polygon',
141
+ 42161: 'Arbitrum One',
142
+ 10: 'Optimism',
143
+ };
144
+ return networks[chainId] || `Unknown Network (${chainId})`;
145
+ }
146
+ /**
147
+ * Sleep/delay utility
148
+ */
149
+ static sleep(ms) {
150
+ return new Promise(resolve => setTimeout(resolve, ms));
151
+ }
152
+ /**
153
+ * Retry logic for API calls
154
+ */
155
+ static async retry(fn, maxRetries = 3, delayMs = 1000) {
156
+ let lastError;
157
+ for (let i = 0; i < maxRetries; i++) {
158
+ try {
159
+ return await fn();
160
+ }
161
+ catch (error) {
162
+ lastError = error;
163
+ if (i < maxRetries - 1) {
164
+ await this.sleep(delayMs * (i + 1));
165
+ }
166
+ }
167
+ }
168
+ throw lastError;
169
+ }
170
+ /**
171
+ * Format transaction hash with ellipsis
172
+ */
173
+ static shortenHash(hash, startLength = 6, endLength = 4) {
174
+ if (hash.length <= startLength + endLength) {
175
+ return hash;
176
+ }
177
+ return `${hash.slice(0, startLength)}...${hash.slice(-endLength)}`;
178
+ }
179
+ /**
180
+ * Check if a transaction was successful
181
+ */
182
+ static isTransactionSuccessful(receipt) {
183
+ return receipt?.status === 1;
184
+ }
185
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "repository": {
3
+ "type": "git",
4
+ "url": "https://github.com/AmanUllah2/raingames-sdk"
5
+ },
6
+ "name": "riskmarket-sdk",
7
+ "version": "1.0.0",
8
+ "type": "module",
9
+ "description": "SDK for Risk market Protocol",
10
+ "main": "dist/index.js",
11
+ "module": "dist/index.esm.js",
12
+ "types": "dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc && rollup -c",
18
+ "dev": "tsc --watch",
19
+ "test": "jest",
20
+ "lint": "eslint src --ext .ts",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "keywords": [
24
+ "blockchain",
25
+ "ethereum",
26
+ "web3",
27
+ "sdk"
28
+ ],
29
+ "author": "Quecko Inc",
30
+ "license": "MIT",
31
+ "devDependencies": {
32
+ "@rollup/plugin-commonjs": "^25.0.7",
33
+ "@rollup/plugin-node-resolve": "^15.2.3",
34
+ "@rollup/plugin-typescript": "^11.1.6",
35
+ "@types/node": "^20.11.5",
36
+ "@typescript-eslint/eslint-plugin": "^6.19.1",
37
+ "@typescript-eslint/parser": "^6.19.1",
38
+ "eslint": "^8.56.0",
39
+ "jest": "^29.7.0",
40
+ "rollup": "^4.9.6",
41
+ "tslib": "^2.6.2",
42
+ "typescript": "^5.3.3"
43
+ },
44
+ "dependencies": {
45
+ "ethers": "^6.10.0",
46
+ "axios": "^1.6.5"
47
+ },
48
+ "peerDependencies": {
49
+ "ethers": "^6.0.0"
50
+ }
51
+ }