-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Implement early logics and tests for the SDK
- Loading branch information
Showing
12 changed files
with
3,936 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules/ | ||
node_modules/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
{ | ||
"name": "kulab-sdk", | ||
"name": "kulap-sdk", | ||
"version": "0.1.0", | ||
"description": "Javascript SDK for Kulab DEX", | ||
"main": "index.js", | ||
"description": "Javascript SDK for Kulap DEX", | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"watch": "rollup -cw", | ||
"test": "jest" | ||
}, | ||
"jest": { | ||
"transform": { | ||
".(ts|tsx)": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js" | ||
] | ||
}, | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/jest": "^26.0.15", | ||
"jest": "^26.6.3", | ||
"rollup": "^2.33.3", | ||
"rollup-plugin-typescript2": "^0.29.0", | ||
"ts-jest": "^26.4.4", | ||
"typescript": "^4.1.2" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.21.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import typescript from 'rollup-plugin-typescript2' | ||
|
||
import pkg from './package.json' | ||
|
||
export default { | ||
input: 'src/index.ts', | ||
output: [ | ||
{ | ||
file: pkg.main, | ||
format: 'cjs', | ||
}, | ||
{ | ||
file: pkg.module, | ||
format: 'es', | ||
}, | ||
], | ||
external: [ | ||
...Object.keys(pkg.dependencies || {}), | ||
...Object.keys(pkg.peerDependencies || {}), | ||
], | ||
plugins: [ | ||
typescript({ | ||
typescript: require('typescript'), | ||
}), | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Network } from "./types" | ||
|
||
export const API_URL = "https://api.kulap.io/v1/api/rate/best-rate/toAmount" | ||
|
||
export const SUPPORTED_TOKENS = [ | ||
{ | ||
"symbol": "ETH", | ||
"address": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" | ||
}, | ||
{ | ||
"symbol": "USDT", | ||
"address": "0xdAC17F958D2ee523a2206206994597C13D831ec7" | ||
}, | ||
{ | ||
"symbol": "DAI", | ||
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F" | ||
}, | ||
{ | ||
"symbol": "MAKER", | ||
"address": "0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2" | ||
}, | ||
{ | ||
"symbol": "USDC", | ||
"address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Kulap } from "./kulap" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import axios, { AxiosError } from 'axios'; | ||
import { Network, Configuration, APIError, TradeOptions } from "./types" | ||
import { SUPPORTED_TOKENS, API_URL } from "./constants" | ||
|
||
export class Kulap { | ||
|
||
config: Configuration | ||
|
||
constructor( | ||
accessKey: string, | ||
networkName: string = "mainnet" | ||
) { | ||
|
||
const network = Network[networkName as keyof typeof Network] | ||
|
||
if (!network) { | ||
throw new Error("Given network name is not valid.") | ||
} | ||
|
||
// TODO: Validate Access Key | ||
|
||
this.config = { | ||
accessKey, | ||
network | ||
} | ||
} | ||
|
||
getCurrentNetwork(): string { | ||
return this.config.network | ||
} | ||
|
||
tokensList(): Array<string> { | ||
const symbols = SUPPORTED_TOKENS.map(item => item.symbol) | ||
return symbols | ||
} | ||
|
||
async getRate( | ||
sourceToken: string, | ||
targetToken: string, | ||
amount: string | ||
): Promise<TradeOptions | APIError> { | ||
try { | ||
const { data } = await axios.get(API_URL, { | ||
params: { | ||
from: sourceToken, | ||
to: targetToken, | ||
fromAmount: amount, | ||
accessKey: this.config.accessKey | ||
} | ||
}) | ||
return data | ||
// return { | ||
// "FAST": { | ||
// "gasPrice": "46", | ||
// "gasLimit": 500000, | ||
// "trade": { | ||
// "routes": [ | ||
// 2 | ||
// ], | ||
// "fromAmount": "1000000000000000000", | ||
// "toAmount": "518950296471435895073", | ||
// "rate": "518.950296471435895073" | ||
// } | ||
// }, | ||
// "STD": { | ||
// "gasPrice": "28", | ||
// "gasLimit": 500000, | ||
// "trade": { | ||
// "routes": [ | ||
// 2 | ||
// ], | ||
// "fromAmount": "1000000000000000000", | ||
// "toAmount": "518950296471435895073", | ||
// "rate": "518.950296471435895073" | ||
// } | ||
// }, | ||
// "SLOW": { | ||
// "gasPrice": "27", | ||
// "gasLimit": 500000, | ||
// "trade": { | ||
// "routes": [ | ||
// 2 | ||
// ], | ||
// "fromAmount": "1000000000000000000", | ||
// "toAmount": "518950296471435895073", | ||
// "rate": "518.950296471435895073" | ||
// } | ||
// } | ||
// } | ||
} catch (e) { | ||
return this.handleAPIError(e) | ||
} | ||
} | ||
|
||
private handleAPIError(e: AxiosError): APIError { | ||
if (e.response) { | ||
const { data, status } = e.response!; | ||
return { status, message: data.error }; | ||
} | ||
return new Error(e.message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
export enum Network { | ||
mainnet = "mainnet", | ||
// kovan = "kovan" | ||
} | ||
|
||
export type Configuration = { | ||
network: Network | ||
accessKey: string | ||
} | ||
|
||
export type APIError = { | ||
message: string | ||
status?: number | ||
} | ||
|
||
type Trade = { | ||
routes: number[] | ||
fromAmount: string | ||
toAmount: string | ||
rate: string | ||
} | ||
|
||
type TradeOption = { | ||
gasPrice : string | ||
gasLimit: number | ||
trade : Trade | ||
} | ||
|
||
export type TradeOptions = { | ||
"FAST": TradeOption, | ||
"STD": TradeOption, | ||
"SLOW": TradeOption | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface ICalculator { | ||
a: number; | ||
b: number; | ||
} | ||
|
||
export { ICalculator } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"FAST": { | ||
"gasPrice": "46", | ||
"gasLimit": 500000, | ||
"trade": { | ||
"routes": [ | ||
2 | ||
], | ||
"fromAmount": "1000000000000000000", | ||
"toAmount": "518950296471435895073", | ||
"rate": "518.950296471435895073" | ||
} | ||
}, | ||
"STD": { | ||
"gasPrice": "28", | ||
"gasLimit": 500000, | ||
"trade": { | ||
"routes": [ | ||
2 | ||
], | ||
"fromAmount": "1000000000000000000", | ||
"toAmount": "518950296471435895073", | ||
"rate": "518.950296471435895073" | ||
} | ||
}, | ||
"SLOW": { | ||
"gasPrice": "27", | ||
"gasLimit": 500000, | ||
"trade": { | ||
"routes": [ | ||
2 | ||
], | ||
"fromAmount": "1000000000000000000", | ||
"toAmount": "518950296471435895073", | ||
"rate": "518.950296471435895073" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Kulap } from "../src" | ||
|
||
let kulapSDK : any | ||
|
||
describe("Kulap SDK", () => { | ||
|
||
beforeAll(() => { | ||
kulapSDK = new Kulap('access_key', "mainnet") | ||
}); | ||
|
||
test("Verify constructor creation is correct", async () => { | ||
const networkName = kulapSDK.getCurrentNetwork() | ||
expect(networkName).toBe("mainnet"); | ||
|
||
try { | ||
new Kulap('access_key', "wrong") | ||
} catch (error) { | ||
expect(error.message).toBe("Given network name is not valid."); | ||
} | ||
|
||
}); | ||
|
||
test("Verify supported tokens list is correct", async () => { | ||
const symbols = kulapSDK.tokensList() | ||
expect( symbols.indexOf("ETH") ).toBeGreaterThan(-1) | ||
expect( symbols.indexOf("DAI") ).toBeGreaterThan(-1) | ||
}); | ||
|
||
|
||
test("Verify returned rates and paths are received ", async () => { | ||
const baseToken = kulapSDK.tokensList()[0] | ||
const pairToken = kulapSDK.tokensList()[1] | ||
const amountIn = "1000000000000000000" | ||
const txOptions = await kulapSDK.getRate(baseToken, pairToken, amountIn) | ||
expect( Object.keys(txOptions).length).toBe(3) | ||
expect( txOptions.STD.trade.fromAmount ).toBe(amountIn) | ||
}); | ||
|
||
|
||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationDir": "./dist", | ||
"module": "es6", | ||
"noImplicitAny": true, | ||
"outDir": "./dist", | ||
"target": "es5", | ||
"esModuleInterop" : true | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
Oops, something went wrong.