Skip to content
This repository was archived by the owner on Sep 8, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 45 additions & 44 deletions src/bluefinClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@ import {
Order,
OrderSigner,
Transaction,
ADJUST_MARGIN,
MARGIN_TYPE,
ORDER_SIDE,
ORDER_STATUS,
ORDER_TYPE,
TIME_IN_FORCE,
OnboardingSigner,
} from "@firefly-exchange/library-sui";
import {
Connection,
Ed25519Keypair,
JsonRpcProvider,
Keypair,
RawSigner,
Secp256k1Keypair,
SignerWithProvider,
} from "@mysten/sui.js";
import {
AdjustLeverageResponse,
AuthorizeHashResponse,
Expand Down Expand Up @@ -58,27 +74,9 @@ import { SERVICE_URLS } from "./exchange/apiUrls";
import { Sockets } from "./exchange/sockets";
import { ExtendedNetwork, Networks } from "./constants";
import { WebSockets } from "./exchange/WebSocket";
import {
Connection,
Ed25519Keypair,
JsonRpcProvider,
Keypair,
RawSigner,
Secp256k1Keypair,
SignerWithProvider,
} from "@mysten/sui.js";
import {
ADJUST_MARGIN,
MARGIN_TYPE,
ORDER_SIDE,
ORDER_STATUS,
ORDER_TYPE,
TIME_IN_FORCE,
} from "@firefly-exchange/library-sui";
import { generateRandomNumber, readFile } from "../utils/utils";
import { ContractCalls } from "./exchange/contractService";
import { ResponseSchema } from "./exchange/contractErrorHandling.service";
import { OnboardingSigner } from "@firefly-exchange/library-sui";

// import { Contract } from "ethers";

Expand All @@ -88,16 +86,21 @@ export class BluefinClient {
private orderSigner: OrderSigner | undefined;

private apiService: APIService;

public sockets: Sockets;

public webSockets: WebSockets | undefined;

public kmsSigner: AwsKmsSigner | undefined;

public marketSymbols: string[] = []; // to save array market symbols [DOT-PERP, SOL-PERP]

private walletAddress = ""; // to save user's public address when connecting from UI

private signer: RawSigner | undefined; // to save signer when connecting from UI

private contractCalls: ContractCalls | undefined;

private provider: any | undefined; // to save raw web3 provider when connecting from UI

private isTermAccepted = false;
Expand All @@ -106,6 +109,7 @@ export class BluefinClient {

// the number of decimals supported by USDC contract
private MarginTokenPrecision = 6;

/**
* initializes the class instance
* @param _isTermAccepted boolean indicating if exchange terms and conditions are accepted
Expand All @@ -120,8 +124,9 @@ export class BluefinClient {
_scheme?: any
) {
this.network = _network;

this.provider = new JsonRpcProvider(
new Connection({ fullnode: _network.rpc })
new Connection({ fullnode: _network.url })
);

this.apiService = new APIService(this.network.apiGateway);
Expand All @@ -132,8 +137,8 @@ export class BluefinClient {
}

this.isTermAccepted = _isTermAccepted;
//if input is string then its seed phrase else it should be AwsKmsSigner object
if (_account && _scheme && typeof _account == "string") {
// if input is string then its seed phrase else it should be AwsKmsSigner object
if (_account && _scheme && typeof _account === "string") {
this.initializeWithSeed(_account, _scheme);
} else if (
_account &&
Expand Down Expand Up @@ -171,7 +176,7 @@ export class BluefinClient {
initializeWithKMS = async (awsKmsSigner: AwsKmsSigner): Promise<void> => {
try {
this.kmsSigner = awsKmsSigner;
//fetching public address of the account
// fetching public address of the account
this.walletAddress = await this.kmsSigner.getAddress();
} catch (err) {
console.log(err);
Expand Down Expand Up @@ -246,6 +251,7 @@ export class BluefinClient {
}
return this.signer;
};

/**
* @description
* Gets the RPC Provider of the client
Expand Down Expand Up @@ -369,6 +375,7 @@ export class BluefinClient {

return response;
};

/**
* @description
* Creates signature for cancelling orders
Expand Down Expand Up @@ -464,26 +471,25 @@ export class BluefinClient {
if (amount) {
const coin =
await this.contractCalls.onChainCalls.getUSDCoinHavingBalance({
amount: amount,
amount,
address: await this.signer.getAddress(),
currencyID: this.contractCalls.onChainCalls.getCurrencyID(),
limit: limit,
cursor: cursor,
limit,
cursor,
});
if (coin) {
coin.balance = usdcToBaseNumber(coin.balance);
}
return coin;
} else {
const coins = await this.contractCalls.onChainCalls.getUSDCCoins({
address: await this.signer.getAddress(),
});
coins.data.forEach((coin) => {
coin.balance = usdcToBaseNumber(coin.balance);
});

return coins;
}
const coins = await this.contractCalls.onChainCalls.getUSDCCoins({
address: await this.signer.getAddress(),
});
coins.data.forEach((coin) => {
coin.balance = usdcToBaseNumber(coin.balance);
});

return coins;
};

/**
Expand Down Expand Up @@ -530,9 +536,8 @@ export class BluefinClient {
});
if (Transaction.getStatus(txResponse) == "success") {
return true;
} else {
return false;
}
return false;
};

/**
Expand Down Expand Up @@ -589,7 +594,7 @@ export class BluefinClient {
if (amount && !coinID) {
coin = (
await this.contractCalls.onChainCalls.getUSDCoinHavingBalance({
amount: amount,
amount,
})
)?.coinObjectId;
}
Expand All @@ -598,9 +603,8 @@ export class BluefinClient {
amount,
coin
);
} else {
throw Error(`User has no coin with amount ${amount} to deposit`);
}
throw Error(`User has no coin with amount ${amount} to deposit`);
};

/**
Expand All @@ -614,9 +618,8 @@ export class BluefinClient {
return await this.contractCalls.withdrawFromMarginBankContractCall(
amount
);
} else {
return await this.contractCalls.withdrawAllFromMarginBankContractCall();
}
return await this.contractCalls.withdrawAllFromMarginBankContractCall();
};

/**
Expand Down Expand Up @@ -1063,9 +1066,7 @@ export class BluefinClient {
isBuy: params.side === ORDER_SIDE.BUY,
quantity: toBigNumber(params.quantity),
leverage: toBigNumber(params.leverage || 1),
maker: parentAddress
? parentAddress
: this.getPublicAddress().toLocaleLowerCase(),
maker: parentAddress || this.getPublicAddress().toLocaleLowerCase(),
reduceOnly: params.reduceOnly || false,
expiration: toBigNumber(
params.expiration || Math.floor(expiration.getTime() / 1000)
Expand Down
22 changes: 15 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Network } from "@firefly-exchange/library-sui";
//import { Network } from "@firefly-exchange/library-sui";

export const Networks = {
TESTNET_SUI: {
name: "testnet",
rpc: "https://fullnode.devnet.sui.io:443",
url: "https://fullnode.devnet.sui.io:443",
apiGateway: "https://dapi.api.arbitrum-staging.firefly.exchange",
dmsURL: "https://api.arbitrum-staging.firefly.exchange/dead-man-switch",
socketURL: "wss://dapi.api.arbitrum-staging.firefly.exchange",
Expand All @@ -13,7 +13,7 @@ export const Networks = {
},
PRODUCTION_SUI: {
name: "production",
rpc: "https://arb1.arbitrum.io/rpc/",
url: "https://arb1.arbitrum.io/rpc/",
apiGateway: "https://dapi.api.arbitrum-prod.firefly.exchange",
socketURL: "wss://dapi.api.arbitrum-prod.firefly.exchange",
dmsURL: "https://api.arbitrum-prod.firefly.exchange/dead-man-switch",
Expand All @@ -23,7 +23,7 @@ export const Networks = {
},
LOCAL_SUI: {
name: "local",
rpc: "http://127.0.0.1:9000",
url: "http://127.0.0.1:9000",
apiGateway: "https://dapi.api.arbitrum-staging.firefly.exchange",
dmsURL: "https://api.arbitrum-staging.firefly.exchange/dead-man-switch",
socketURL: "wss://dapi.api.arbitrum-staging.firefly.exchange",
Expand All @@ -38,11 +38,19 @@ export const SUI_NETWROK = "sui";

export const EXTRA_FEES = 10000;

export interface Network {
name?: string;
rpc?: string;
faucet?: string;
url?: string;
}

//adding this here as it's temporary support for socket.io
// adding this here as it's temporary support for socket.io
export interface ExtendedNetwork extends Network {
apiGateway: string;
socketURL: string;
onboardingUrl: string;
apiGateway?: string; // making it optional for backward compatibility
socketURL?: string;
onboardingUrl?: string;
webSocketURL: string;
dmsURL?: string;
}