Skip to content

Commit

Permalink
feat: add swap-endpoint migration (#298)
Browse files Browse the repository at this point in the history
* feat: add swap-endpoint migration

* refactor: rename to blockchainRpcEndpoint

* refactor: rename to blockchainRpcEndpoint
  • Loading branch information
Cafe137 authored Dec 19, 2022
1 parent 09b1f23 commit 6dbf2a6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
17 changes: 11 additions & 6 deletions src/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { Contract, providers, Wallet } from 'ethers'
import { bzzContractInterface } from './contract'

export async function sendNativeTransaction(privateKey: string, to: string, value: string, jsonRpcProvider: string) {
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
export async function sendNativeTransaction(
privateKey: string,
to: string,
value: string,
blockchainRpcEndpoint: string,
) {
const signer = await makeReadySigner(privateKey, blockchainRpcEndpoint)
const gasPrice = await signer.getGasPrice()
const transaction = await signer.sendTransaction({ to, value, gasPrice })
const receipt = await transaction.wait(1)

return { transaction, receipt }
}

export async function sendBzzTransaction(privateKey: string, to: string, value: string, jsonRpcProvider: string) {
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
export async function sendBzzTransaction(privateKey: string, to: string, value: string, blockchainRpcEndpoint: string) {
const signer = await makeReadySigner(privateKey, blockchainRpcEndpoint)
const gasPrice = await signer.getGasPrice()
const bzz = new Contract('0xdBF3Ea6F5beE45c02255B2c26a16F300502F68da', bzzContractInterface, signer)
const transaction = await bzz.transfer(to, value, { gasPrice })
Expand All @@ -20,8 +25,8 @@ export async function sendBzzTransaction(privateKey: string, to: string, value:
return { transaction, receipt }
}

async function makeReadySigner(privateKey: string, jsonRpcProvider: string) {
const provider = new providers.JsonRpcProvider(jsonRpcProvider, 100)
async function makeReadySigner(privateKey: string, blockchainRpcEndpoint: string) {
const provider = new providers.JsonRpcProvider(blockchainRpcEndpoint, 100)
await provider.ready
const signer = new Wallet(privateKey, provider)

Expand Down
14 changes: 3 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import * as Sentry from '@sentry/electron'
import { dialog, app } from 'electron'
import updater from 'update-electron-app'

import {
configYamlExists,
getDesktopVersionFromFile,
readConfigYaml,
writeConfigYaml,
writeDesktopVersionFile,
} from './config'
import { getDesktopVersionFromFile, writeDesktopVersionFile } from './config'
import { openDashboardInBrowser } from './browser'
import { runDownloader } from './downloader'
import { runElectronTray } from './electron'
Expand All @@ -26,11 +20,9 @@ import { ensureApiKey } from './api-key'
// @ts-ignore
import squirrelInstallingExecution from 'electron-squirrel-startup'
import { initSplash, Splash } from './splash'
import { runMigrations } from './migration'

// TODO: remove this after 1.0.0 release
// this is a migration path for pioneers
// who helped testing the early versions
if (configYamlExists() && !readConfigYaml().password) writeConfigYaml({ password: 'Test' })
runMigrations()

if (squirrelInstallingExecution) {
app.quit()
Expand Down
20 changes: 20 additions & 0 deletions src/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { configYamlExists, readConfigYaml, writeConfigYaml } from './config'

export function runMigrations() {
if (!configYamlExists()) {
return
}

const config = readConfigYaml()

// TODO: remove this after 1.0.0 release
// this is a migration path for pioneers
// who helped testing the early versions
if (!config.password) {
writeConfigYaml({ password: 'Test' })
}

if (config['swap-endpoint'] && !config['blockchain-rpc-endpoint']) {
writeConfigYaml({ 'blockchain-rpc-endpoint': config['swap-endpoint'] })
}
}
12 changes: 6 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { URL } from 'url'
import PACKAGE_JSON from '../package.json'
import { getApiKey } from './api-key'
import { sendBzzTransaction, sendNativeTransaction } from './blockchain'
import { readConfigYaml, readWalletPasswordOrThrow, writeConfigYaml } from './config'
import { runLauncher } from './launcher'
import { BeeManager } from './lifecycle'
import { logger, readBeeDesktopLogs, readBeeLogs, subscribeLogServerRequests } from './logger'
Expand All @@ -21,7 +22,6 @@ import { port } from './port'
import { getStatus } from './status'
import { swap } from './swap'
import { bufferRequest } from './utility'
import { readConfigYaml, writeConfigYaml, readWalletPasswordOrThrow } from './config'

const UI_DIST = path.join(__dirname, '..', '..', 'ui')

Expand Down Expand Up @@ -163,19 +163,19 @@ export function runServer() {
})
router.post('/gift-wallet/:address', async context => {
const config = readConfigYaml()
const swapEndpoint = Reflect.get(config, 'swap-endpoint')
const blockchainRpcEndpoint = Reflect.get(config, 'blockchain-rpc-endpoint') as string
const privateKeyString = await getPrivateKey()
const { address } = context.params
await sendBzzTransaction(privateKeyString, address, '50000000000000000', swapEndpoint)
await sendNativeTransaction(privateKeyString, address, '1000000000000000000', swapEndpoint)
await sendBzzTransaction(privateKeyString, address, '50000000000000000', blockchainRpcEndpoint)
await sendNativeTransaction(privateKeyString, address, '1000000000000000000', blockchainRpcEndpoint)
context.body = { success: true }
})
router.post('/swap', async context => {
const config = readConfigYaml()
const swapEndpoint = Reflect.get(config, 'swap-endpoint')
const blockchainRpcEndpoint = Reflect.get(config, 'swap-endpoint') as string
const privateKeyString = await getPrivateKey()
try {
await swap(privateKeyString, context.request.body.dai, '10000', swapEndpoint)
await swap(privateKeyString, context.request.body.dai, '10000', blockchainRpcEndpoint)
context.body = { success: true }
} catch (error) {
logger.error(error)
Expand Down
4 changes: 2 additions & 2 deletions src/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export async function swap(
privateKey: string,
xdai: string,
minimumBzz: string,
jsonRpcProvider: string,
blockchainRpcEndpoint: string,
): Promise<string[]> {
const provider = new providers.JsonRpcProvider(jsonRpcProvider, 100)
const provider = new providers.JsonRpcProvider(blockchainRpcEndpoint, 100)
const signer = new ethers.Wallet(privateKey, provider)
const gasLimit = 1000000
const contract = new Contract('0x1C232F01118CB8B424793ae03F870aa7D0ac7f77', contractInterface, signer)
Expand Down

0 comments on commit 6dbf2a6

Please sign in to comment.