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

Commit

Permalink
[PI-139]: Add a single function for backup in clientjs
Browse files Browse the repository at this point in the history
  • Loading branch information
arijoon committed Aug 4, 2023
1 parent a7a42f2 commit 9a8ebc1
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 15 deletions.
2 changes: 1 addition & 1 deletion env/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Add Web3 url
RPC_URL=https://ropsten.infura.io/v3/7bfab7398ae84af3b1b70c955cfd9491
RPC_URL=https://goerli.infura.io/v3/3d572ae9b01e45c789c3214696bb02d4
# Add GAS STATION API KEY
# Ethereum private key prefixed with 0x
PRIVATE_ETH_KEY=0x49e4d1e2aa7d026188251392dd2d335c176d846d8a894a8092c835f3b345e2ad
Expand Down
3 changes: 2 additions & 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.10",
"version": "5.2.0",
"main": "src/index.js",
"files": [
"src",
Expand Down Expand Up @@ -48,6 +48,7 @@
"@truffle/hdwallet-provider": "^2.0.13",
"env-cmd": "^10.1.0",
"jest": "^26.4.2",
"jest-environment-jsdom": "25",
"mustache": "^4.0.0",
"nock": "^13.0.4",
"solc": "^0.4.24"
Expand Down
22 changes: 22 additions & 0 deletions src/api/storeStarkL1Registration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Backup the Stark L1 registration payload for the currently logged in account
* @type {(dvf: ReturnType<import('../lib/dvf/bindApi')>,
* nonce: string,
* signature: string) => string}
*/
module.exports = async (dvf, tradingKey, nonce, signature) => {
const url = '/v1/trading/storeStarkL1Registration'

const { ethAddress } = await dvf.getUserConfig()

const l1RegistrationSignature = await dvf.stark.signRegistration(
tradingKey,
ethAddress
)

const data = {
l1RegistrationSignature
}

return dvf.postAuthenticated(url, nonce, signature, data)
}
47 changes: 47 additions & 0 deletions src/api/storeStarkL1Registration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const nock = require('nock')
const instance = require('./test/helpers/instance')
const mockGetConf = require('./test/fixtures/getConf')

describe('dvf.storeStarkL1Registration', () => {
const tradingKey = process.env.PRIVATE_STARK_KEY
const config = {
wallet: {
type: 'tradingKey',
meta: {
starkPrivateKey: tradingKey
}
}
}
/**
* @param {(dvf: Awaited<ReturnType<typeof instance>) => Promise<unknown>} fn
*/
const withInstance = (fn) => async () => fn(await instance(config))

beforeAll(async () => {
mockGetConf()
})

afterAll(() => {
nock.restore()
})

it('Generates and submits the registration payload', withInstance(async (dvf) => {
mockGetConf()

const expectedBody = {
l1RegistrationSignature: '0x025e160f8936b367f1aa10f4602dd54a31831de28cfc4263cbfd6b2fd3e9328602421b80ed0520b9e4d20620339ecd34093f04cec933bdebddc31e3bfcd32e5504de195d61296b6ac602ab5db8d190b90cd1e767fe9d47d4c9d96ab62cf7ad41'
}

const payloadValidator = jest.fn(body => {
expect(body).toMatchObject(expectedBody)
return true
})

nock(dvf.config.api)
.post('/v1/trading/storeStarkL1Registration', payloadValidator)
.reply(200)

await dvf.storeStarkL1Registration(tradingKey)
expect(payloadValidator).toBeCalled()
}))
})
4 changes: 2 additions & 2 deletions src/api/test/helpers/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const getWeb3 = require('../../../../examples/helpers/getWeb3')

const RhinofiClientFactory = require('../../../index')

module.exports = async () => {
module.exports = async (configOverride = {}) => {
const rpcUrl = process.env.RPC_URL
const privateKey = process.env.PRIVATE_ETH_KEY

const { web3 } = getWeb3(privateKey, rpcUrl)

const gasStationApiKey = process.env.ETH_GAS_STATION_KEY || ''

const config = { gasStationApiKey }
const config = { gasStationApiKey, ...configOverride }

// It's possible to overwrite the API address with the testnet address
// for example like this:
Expand Down
1 change: 1 addition & 0 deletions src/lib/dvf/bindApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ module.exports = () => {
dvf.walletFailedEvent = compose(require('../../api/walletFailedEvent'))
dvf.walletSuccessEvent = compose(require('../../api/walletSuccessEvent'))
dvf.topPerformersTokens = compose(require('../../api/topPerformersTokens'))
dvf.storeStarkL1Registration = compose(require('../../api/storeStarkL1Registration'))

dvf.ledger = {
deposit: compose(require('../../api/ledger/deposit')),
Expand Down
Loading

0 comments on commit 9a8ebc1

Please sign in to comment.