UNPKG

@rhino.fi/client-js

Version:
59 lines (47 loc) 1.6 kB
const post = require('../lib/dvf/post-generic') const DVFError = require('../lib/dvf/DVFError') const validateAssertions = require('../lib/validators/validateAssertions') const { Joi } = require('@rhino.fi/dvf-utils') const schema = Joi.object({ amount: Joi.amount().required(), // number or number string }) module.exports = async (dvf, token, amount, starkPrivateKey) => { validateAssertions(dvf, { amount, token, starkPrivateKey }) const { value } = schema.validate({ amount }) amount = value.amount // TODO: assess and replace all validations with custom Joi const currency = dvf.token.getTokenInfo(token) const quantisedAmount = dvf.token.toQuantizedAmount(token, amount) const tempVaultId = dvf.config.DVF.tempStarkVaultId const nonce = dvf.util.generateRandomNonce() const starkTokenId = currency.starkTokenId let starkVaultId = currency.starkVaultId const { starkPublicKey, starkKeyPair } = await dvf.stark.createKeyPair( starkPrivateKey, ) // This should be in hours expireTime = Math.floor(Date.now() / (1000 * 3600)) + parseInt(dvf.config.defaultStarkExpiry) const { starkMessage } = dvf.stark.createTransferMsg( quantisedAmount, nonce, starkVaultId, starkTokenId, tempVaultId, `0x${starkPublicKey.x}`, expireTime, ) const starkSignature = dvf.stark.sign(starkKeyPair, starkMessage) const url = '/v1/trading/w/withdraw' const data = { token, amount, nonce, starkPublicKey, starkSignature, starkVaultId, expireTime, } // console.log({ data }) return post(dvf, url, data) }