feat: add light node upgrade top up methods (#372)
* feat: add top up * chore: remove console.log * build: add pseudo-missing dependency * feat: add missing top up components * fix: crypto route * feat(wip): add gift wallet logic * fix: fix gift wallet flows * feat: simplify flow without fund step * feat: add loading screens * fix: remove alert * fix: prepend http if needed * fix: fix bug that was reintroduced with merge * refactor: rename minusEther to minusBaseUnits * fix: remove unused setStartedAt * build: remove unused dependency
This commit is contained in:
+69
-4
@@ -1,14 +1,15 @@
|
||||
import { debounce } from '@material-ui/core'
|
||||
import axios from 'axios'
|
||||
import { Contract, providers } from 'ethers'
|
||||
import { Contract, providers, Wallet } from 'ethers'
|
||||
import { bzzContractInterface } from './bzz-contract-interface'
|
||||
|
||||
const PROVIDER = 'https://gno.getblock.io/mainnet/?api_key=d7b92d96-9784-49a8-a800-b3edd1647fc7'
|
||||
export const JSON_RPC_PROVIDER = 'https://gno.getblock.io/mainnet/?api_key=d7b92d96-9784-49a8-a800-b3edd1647fc7'
|
||||
|
||||
async function eth_getBalance(address: string): Promise<string> {
|
||||
if (!address.startsWith('0x')) {
|
||||
address = `0x${address}`
|
||||
}
|
||||
const response = await axios(PROVIDER, {
|
||||
const response = await axios(JSON_RPC_PROVIDER, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
@@ -24,6 +25,23 @@ async function eth_getBalance(address: string): Promise<string> {
|
||||
return response.data.result
|
||||
}
|
||||
|
||||
async function eth_getBlockByNumber(provider = JSON_RPC_PROVIDER): Promise<string> {
|
||||
const response = await axios(provider, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
data: {
|
||||
jsonrpc: '2.0',
|
||||
method: 'eth_getBlockByNumber',
|
||||
params: ['latest', false],
|
||||
id: 1,
|
||||
},
|
||||
})
|
||||
|
||||
return response.data.result
|
||||
}
|
||||
|
||||
const partialERC20tokenABI = [
|
||||
{
|
||||
constant: true,
|
||||
@@ -45,7 +63,7 @@ const partialERC20tokenABI = [
|
||||
},
|
||||
]
|
||||
|
||||
const provider = new providers.JsonRpcProvider(PROVIDER)
|
||||
const provider = new providers.JsonRpcProvider(JSON_RPC_PROVIDER)
|
||||
|
||||
async function eth_getBalanceERC20(
|
||||
address: string,
|
||||
@@ -60,7 +78,54 @@ async function eth_getBalanceERC20(
|
||||
return balance.toString()
|
||||
}
|
||||
|
||||
interface TransferResponse {
|
||||
transaction: providers.TransactionResponse
|
||||
receipt: providers.TransactionReceipt
|
||||
}
|
||||
|
||||
export async function sendNativeTransaction(
|
||||
privateKey: string,
|
||||
to: string,
|
||||
value: string,
|
||||
jsonRpcProvider: string,
|
||||
): Promise<TransferResponse> {
|
||||
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
|
||||
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,
|
||||
): Promise<TransferResponse> {
|
||||
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
|
||||
const gasPrice = await signer.getGasPrice()
|
||||
const bzz = new Contract('0xdBF3Ea6F5beE45c02255B2c26a16F300502F68da', bzzContractInterface, signer)
|
||||
const transaction = await bzz.transfer(to, value, { gasPrice })
|
||||
const receipt = await transaction.wait(1)
|
||||
|
||||
return { transaction, receipt }
|
||||
}
|
||||
|
||||
async function makeReadySigner(privateKey: string, jsonRpcProvider: string) {
|
||||
const provider = new providers.JsonRpcProvider(jsonRpcProvider, 100)
|
||||
await provider.ready
|
||||
const signer = new Wallet(privateKey, provider)
|
||||
|
||||
return signer
|
||||
}
|
||||
|
||||
export const Rpc = {
|
||||
sendNativeTransaction,
|
||||
sendBzzTransaction,
|
||||
_eth_getBalance: eth_getBalance,
|
||||
_eth_getBalanceERC20: eth_getBalanceERC20,
|
||||
eth_getBalance: debounce(eth_getBalance, 1_000),
|
||||
eth_getBalanceERC20: debounce(eth_getBalanceERC20, 1_000),
|
||||
eth_getBlockByNumber,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user