@rhino.fi/client-js
Version:
66 lines (57 loc) • 1.7 kB
JavaScript
const generateRandomNonceV2 = require('./generateRandomNonceV2')
const { Joi } = require('@rhino.fi/dvf-utils')
const validateWithJoi = require('../validators/validateWithJoi')
const getSafeQuantizedAmountOrThrow = require(
'./token/getSafeQuantizedAmountOrThrow',
)
const getTokenAddressFromTokenInfoOrThrow = require(
'./token/getTokenAddressFromTokenInfoOrThrow',
)
const R = require('ramda')
const schema = Joi.object({
chain: Joi.string(),
token: Joi.string(),
amount: Joi.amount(),
nonce: Joi
.number()
.integer()
.min(0)
// Will be auto-generated if not provided.
.optional(),
recipientEthAddress: Joi.string().optional(),
})
const validateArg0 = validateWithJoi(schema)('INVALID_METHOD_ARGUMENT')({
context: `bridgedWithdrawal`,
})
const removeUndefined = R.reject(R.isNil)
module.exports = async (dvf, data, authNonce, signature) => {
const { token, chain, amount, nonce, recipientEthAddress } = validateArg0(
data,
)
const tokenInfo = dvf.token.getTokenInfoOrThrow(token)
// To make it fail if token is not supported
getTokenAddressFromTokenInfoOrThrow(tokenInfo, chain)
const quantisedAmount = getSafeQuantizedAmountOrThrow(amount, tokenInfo)
const { vaultId, starkKey } = await dvf.getVaultIdAndStarkKey(
{
token,
targetEthAddress: dvf.config.DVF.deversifiAddress,
},
authNonce,
signature,
)
const { tx } = await dvf.createTransferPayload({
token,
amount,
recipientPublicKey: starkKey,
recipientVaultId: vaultId,
})
return removeUndefined({
chain,
token,
amount: quantisedAmount,
tx,
nonce: nonce || generateRandomNonceV2(),
recipientEthAddress,
})
}