Skip to content

Commit 5174956

Browse files
committed
allow users to specify fee for deposits/withdrawals
1 parent 4ed1e88 commit 5174956

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/client/client.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,12 +2786,12 @@ export class Client {
27862786
}
27872787
}
27882788

2789-
public depositToTradingContract(quantity: CurrencyAmount) {
2790-
return this.transferToTradingContract(quantity, MovementTypeDeposit)
2789+
public depositToTradingContract(quantity: CurrencyAmount, feeLevel: 'low' | 'medium' | 'high' = 'medium') {
2790+
return this.transferToTradingContract(quantity, MovementTypeDeposit, feeLevel)
27912791
}
27922792

2793-
public withdrawFromTradingContract(quantity: CurrencyAmount) {
2794-
return this.transferToTradingContract(quantity, MovementTypeWithdrawal)
2793+
public withdrawFromTradingContract(quantity: CurrencyAmount, feeLevel: 'low' | 'medium' | 'high' = 'medium') {
2794+
return this.transferToTradingContract(quantity, MovementTypeWithdrawal, feeLevel)
27952795
}
27962796

27972797
private async prepareMovement(
@@ -2819,10 +2819,11 @@ export class Client {
28192819

28202820
private transferToTradingContract(
28212821
quantity: CurrencyAmount,
2822-
movementType: typeof MovementTypeDeposit | typeof MovementTypeWithdrawal
2822+
movementType: typeof MovementTypeDeposit | typeof MovementTypeWithdrawal,
2823+
feeLevel: 'low' | 'medium' | 'high' = 'medium'
28232824
): Promievent<{ txId: string; movementId: string }> {
28242825
const promise = new Promievent((resolve, reject) =>
2825-
this._transferToTradingContract(quantity, movementType, (...args) =>
2826+
this._transferToTradingContract(quantity, movementType, feeLevel, (...args) =>
28262827
promise.emit(...args)
28272828
)
28282829
.then(resolve)
@@ -2840,9 +2841,9 @@ export class Client {
28402841
private async _transferToTradingContract(
28412842
quantity: CurrencyAmount,
28422843
movementType: typeof MovementTypeDeposit | typeof MovementTypeWithdrawal,
2844+
feeLevel: 'low' | 'medium' | 'high',
28432845
emit: Promievent<any>['emit']
28442846
): Promise<{ txId: string; movementId: string }> {
2845-
this.requireMPC()
28462847
if (this.assetData == null) {
28472848
throw new Error('Asset data null')
28482849
}
@@ -2851,23 +2852,39 @@ export class Client {
28512852
}
28522853
const assetData = this.assetData[quantity.currency]
28532854
const blockchain = assetData.blockchain
2854-
const childKey = this.apiKey.child_keys[
2855-
BLOCKCHAIN_TO_BIP44[blockchain.toUpperCase() as Blockchain]
2856-
]
2855+
let address
2856+
try {
2857+
const childKey = this.apiKey.child_keys[
2858+
BLOCKCHAIN_TO_BIP44[blockchain.toUpperCase() as Blockchain]
2859+
]
2860+
address = childKey.address
2861+
} catch(e) {
2862+
address = this.nashCoreConfig.wallets[blockchain].address
2863+
}
2864+
28572865
const blockchainFees = await this.getBlockchainFees(
28582866
blockchain.toUpperCase() as Blockchain
28592867
)
28602868

2861-
const address = childKey.address
28622869
const bnAmount = new BigNumber(quantity.amount)
28632870

2871+
let gasPrice = blockchainFees.priceMedium
2872+
switch (feeLevel) {
2873+
case 'low':
2874+
gasPrice = blockchainFees.priceLow
2875+
break;
2876+
case 'high':
2877+
gasPrice = blockchainFees.priceHigh
2878+
break;
2879+
}
2880+
28642881
let preparedMovement: PrepareMovementData['prepareMovement']
28652882
let movementAmount = bnAmount
28662883
const prepareAMovement = async () => {
28672884
const params = {
28682885
address,
28692886
backendGeneratedPayload: true,
2870-
gasPrice: blockchainFees.priceMedium,
2887+
gasPrice,
28712888
quantity: {
28722889
amount: bnAmount.toFormat(
28732890
8,
@@ -2898,7 +2915,7 @@ export class Client {
28982915
while (true) {
28992916
signedAddMovementPayload = await this.signPayload({
29002917
payload: {
2901-
address: childKey.address,
2918+
address,
29022919
backendGeneratedPayload: true,
29032920
nonce: preparedMovement.nonce,
29042921
quantity: {

0 commit comments

Comments
 (0)