Skip to content

Commit

Permalink
Feat: Implement early logics and tests for the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
pisuthd committed Nov 22, 2020
1 parent cb81f28 commit f358264
Show file tree
Hide file tree
Showing 12 changed files with 3,936 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
dist/
33 changes: 30 additions & 3 deletions package.json
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"
}
}
26 changes: 26 additions & 0 deletions rollup.config.js
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'),
}),
],
}
26 changes: 26 additions & 0 deletions src/constants.ts
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"
}
]
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Kulap } from "./kulap"
103 changes: 103 additions & 0 deletions src/kulap.ts
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);
}

}
34 changes: 34 additions & 0 deletions src/types.ts
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
}
6 changes: 6 additions & 0 deletions src/types/Calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface ICalculator {
a: number;
b: number;
}

export { ICalculator }
38 changes: 38 additions & 0 deletions test.json
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"
}
}
}
42 changes: 42 additions & 0 deletions tests/index.test.ts
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)
});




});
17 changes: 17 additions & 0 deletions tsconfig.json
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"
]
}
Loading

0 comments on commit f358264

Please sign in to comment.