Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

[CHAIN-718] Use withdrawalQuotes for FW #498

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions examples/25.fastWithdrawalFee.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const rhinofiConfig = {
;(async () => {
const rhinofi = await RhinofiClientFactory(web3, rhinofiConfig)

// NOTE: fastWithdrawalFee method as been deprecated
const response = await rhinofi.fastWithdrawalFee('ETH')

logExampleResult(response)
Expand Down
1 change: 1 addition & 0 deletions examples/src/fastWithdrawalFee.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// NOTE: fastWithdrawalFee method as been deprecated
const response = await rhinofi.fastWithdrawalFee('ETH')

logExampleResult(response)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rhino.fi/client-js",
"version": "5.1.3",
"version": "5.1.4",
"main": "src/index.js",
"files": [
"src",
Expand Down
1 change: 1 addition & 0 deletions src/api/fastWithdrawalFee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { get } = require('request-promise')

// TODO: Deprecated
sjpbeale marked this conversation as resolved.
Show resolved Hide resolved
module.exports = async (dvf, token) => {
const url = dvf.config.api + '/v1/trading/r/fastWithdrawalFee'
return get(url, { json: true, qs: { token } })
Expand Down
25 changes: 25 additions & 0 deletions src/api/getWithdrawalQuote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { Joi } = require('@rhino.fi/dvf-utils')
const validateWithJoi = require('../lib/validators/validateWithJoi')

const schema = Joi.object({
token: Joi.string(),
amount: Joi.string(),
chain: Joi.string(),
type: Joi.string(),
recipient: Joi.string().optional(),
abortSignal: Joi.any().optional()
})

const validateInputs = validateWithJoi(schema)('INVALID_METHOD_ARGUMENT')({
context: 'getWithdrawalQuote'
})

module.exports = async (dvf, data, nonce, signature) => {
const { get } = dvf.request
const { abortSignal, ...requestData } = validateInputs(data)

return get('/v1/trading/withdrawalQuotes', nonce, signature, {
data: requestData,
signal: abortSignal
})
}
1 change: 1 addition & 0 deletions src/lib/dvf/bindApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ module.exports = () => {
dvf.fastWithdrawal = compose(require('../../api/fastWithdrawal'))
dvf.fastWithdrawalFee = compose(require('../../api/fastWithdrawalFee'))
dvf.fastWithdrawalMaxAmount = compose(require('../../api/fastWithdrawalMaxAmount'))
dvf.getWithdrawalQuote = compose(require('../../api/getWithdrawalQuote'))
dvf.getDeposits = compose(require('../../api/getDeposits'))
dvf.getBalance = compose(require('../../api/getBalance'))
dvf.getBalanceUsd = compose(require('../../api/getBalanceUsd'))
Expand Down
18 changes: 12 additions & 6 deletions src/lib/dvf/createFastWithdrawalPayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ const getValidTokenInfo = dvf => token => {
return tokenInfo
}

const getFeeQuantised = async (dvf, token) => dvf
.fastWithdrawalFee(token)
.then(res => toBN(res.feeQuantised))
const getFeeQuantised = (dvf) => async (token, amount, recipient) => dvf
.getWithdrawalQuote({
token,
amount,
chain: 'ETHEREUM',
type: 'FAST',
recipient
})
.then(res => toBN(res.fee))

const schema = Joi.object({
amount: Joi.amount(),
Expand Down Expand Up @@ -64,17 +70,17 @@ module.exports = async (dvf, withdrawalData) => {

const tokenInfo = getValidTokenInfo(dvf)(token)

const quantisedAmount = toQuantizedAmountBN(tokenInfo, amount)
const baseUnitsAmount = fromQuantizedToBaseUnitsBN(tokenInfo)(quantisedAmount)
const feeQuantised = await (
transactionFee
? toQuantizedAmountBN(tokenInfo, transactionFee)
: getFeeQuantised(dvf, token)
: getFeeQuantised(dvf)(token, quantisedAmount.toString(), recipientEthAddress)
)

const tokenContractAddress = token === 'ETH'
? address0
: tokenInfo.tokenAddressPerChain.ETHEREUM
const quantisedAmount = toQuantizedAmountBN(tokenInfo, amount)
const baseUnitsAmount = fromQuantizedToBaseUnitsBN(tokenInfo)(quantisedAmount)

const nonce = dvf.util.generateRandomNonce()

Expand Down
18 changes: 12 additions & 6 deletions src/lib/stark/ledger/createFastWithdrawalPayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ const getValidTokenInfo = dvf => token => {
return tokenInfo
}

const getFeeQuantised = async (dvf, token) => dvf
.fastWithdrawalFee(token)
.then(res => toBN(res.feeQuantised))
const getFeeQuantised = (dvf) => async (token, amount, recipient) => dvf
.getWithdrawalQuote({
token,
amount,
chain: 'ETHEREUM',
type: 'FAST',
recipient
})
.then(res => toBN(res.fee))

const schema = Joi.object({
amount: Joi.amount(),
Expand Down Expand Up @@ -73,17 +79,17 @@ module.exports = async (dvf, withdrawalData, path) => {
const tokenInfo = getValidTokenInfo(dvf)(token)
const transferQuantization = new BN(tokenInfo.quantization)

const quantisedAmount = toQuantizedAmountBN(tokenInfo, amount)
const baseUnitsAmount = fromQuantizedToBaseUnitsBN(tokenInfo)(quantisedAmount)
const feeQuantised = await (
transactionFee
? toQuantizedAmountBN(tokenInfo, transactionFee)
: getFeeQuantised(dvf, token)
: getFeeQuantised(dvf)(token, quantisedAmount.toString(), recipientEthAddress)
)

const tokenContractAddress = token === 'ETH'
? address0
: tokenInfo.tokenAddressPerChain.ETHEREUM
const quantisedAmount = toQuantizedAmountBN(tokenInfo, amount)
const baseUnitsAmount = fromQuantizedToBaseUnitsBN(tokenInfo)(quantisedAmount)

const nonce = dvf.util.generateRandomNonce()

Expand Down