Skip to content

Commit

Permalink
feat: transfer everything out of the gift wallet (#456)
Browse files Browse the repository at this point in the history
* feat: transfer everything out of the gift wallet

* refactor: extract bzz token address into variable
  • Loading branch information
vojtechsimetka authored Jun 29, 2022
1 parent fb8775d commit 7225c5c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 57 deletions.
45 changes: 45 additions & 0 deletions src/utils/bzz-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export const BZZ_TOKEN_ADDRESS = '0xdBF3Ea6F5beE45c02255B2c26a16F300502F68da'
export const bzzABI = [
{
type: 'function',
stateMutability: 'view',
payable: false,
outputs: [
{
type: 'uint256',
name: '',
},
],
name: 'balanceOf',
inputs: [
{
type: 'address',
name: '_owner',
},
],
constant: true,
},
{
type: 'function',
stateMutability: 'nonpayable',
payable: false,
outputs: [
{
type: 'bool',
name: '',
},
],
name: 'transfer',
inputs: [
{
type: 'address',
name: '_to',
},
{
type: 'uint256',
name: '_value',
},
],
constant: false,
},
]
25 changes: 0 additions & 25 deletions src/utils/bzz-contract-interface.ts

This file was deleted.

45 changes: 18 additions & 27 deletions src/utils/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debounce } from '@material-ui/core'
import { Contract, providers, Wallet } from 'ethers'
import { bzzContractInterface } from './bzz-contract-interface'
import { Contract, providers, Wallet, BigNumber as BN } from 'ethers'
import { bzzABI, BZZ_TOKEN_ADDRESS } from './bzz-abi'

async function eth_getBalance(address: string, provider: providers.JsonRpcProvider): Promise<string> {
if (!address.startsWith('0x')) {
Expand All @@ -17,36 +17,15 @@ async function eth_getBlockByNumber(provider: providers.JsonRpcProvider): Promis
return blockNumber.toString()
}

const partialERC20tokenABI = [
{
constant: true,
inputs: [
{
name: '_owner',
type: 'address',
},
],
name: 'balanceOf',
outputs: [
{
name: 'balance',
type: 'uint256',
},
],
payable: false,
type: 'function',
},
]

async function eth_getBalanceERC20(
address: string,
provider: providers.JsonRpcProvider,
tokenAddress = '0xdbf3ea6f5bee45c02255b2c26a16f300502f68da',
tokenAddress = BZZ_TOKEN_ADDRESS,
): Promise<string> {
if (!address.startsWith('0x')) {
address = `0x${address}`
}
const contract = new Contract(tokenAddress, partialERC20tokenABI, provider)
const contract = new Contract(tokenAddress, bzzABI, provider)
const balance = await contract.balanceOf(address)

return balance.toString()
Expand All @@ -57,14 +36,26 @@ interface TransferResponse {
receipt: providers.TransactionReceipt
}

export async function estimateNativeTransferTransactionCost(
privateKey: string,
jsonRpcProvider: string,
): Promise<{ gasPrice: BN; totalCost: BN }> {
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
const gasLimit = '21000'
const gasPrice = await signer.getGasPrice()

return { gasPrice, totalCost: gasPrice.mul(gasLimit) }
}

export async function sendNativeTransaction(
privateKey: string,
to: string,
value: string,
jsonRpcProvider: string,
externalGasPrice?: BN,
): Promise<TransferResponse> {
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
const gasPrice = await signer.getGasPrice()
const gasPrice = externalGasPrice ?? (await signer.getGasPrice())
const transaction = await signer.sendTransaction({ to, value, gasPrice })
const receipt = await transaction.wait(1)

Expand All @@ -79,7 +70,7 @@ export async function sendBzzTransaction(
): Promise<TransferResponse> {
const signer = await makeReadySigner(privateKey, jsonRpcProvider)
const gasPrice = await signer.getGasPrice()
const bzz = new Contract('0xdBF3Ea6F5beE45c02255B2c26a16F300502F68da', bzzContractInterface, signer)
const bzz = new Contract(BZZ_TOKEN_ADDRESS, bzzABI, signer)
const transaction = await bzz.transfer(to, value, { gasPrice })
const receipt = await transaction.wait(1)

Expand Down
11 changes: 6 additions & 5 deletions src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { providers, Wallet } from 'ethers'
import { sleepMs } from '.'
import { BzzToken } from '../models/BzzToken'
import { DaiToken } from '../models/DaiToken'
import { Rpc } from './rpc'
import { estimateNativeTransferTransactionCost, Rpc } from './rpc'

export class WalletAddress {
private constructor(
Expand Down Expand Up @@ -59,19 +59,20 @@ export class ResolvedWallet {
}

public async transfer(destination: string, jsonRpcProvider: string): Promise<void> {
const DUMMY_GAS_PRICE = '50000000000000'

if (this.bzz.toDecimal.gt(0.05)) {
await Rpc.sendBzzTransaction(this.privateKey, destination, this.bzz.toString, jsonRpcProvider)
await sleepMs(5_000)
}

if (this.dai.toBigNumber.gt(DUMMY_GAS_PRICE)) {
const { gasPrice, totalCost } = await estimateNativeTransferTransactionCost(this.privateKey, jsonRpcProvider)

if (this.dai.toBigNumber.gt(totalCost.toString())) {
await Rpc.sendNativeTransaction(
this.privateKey,
destination,
this.dai.toBigNumber.minus(DUMMY_GAS_PRICE).toString(),
this.dai.toBigNumber.minus(totalCost.toString()).toString(),
jsonRpcProvider,
gasPrice,
)
}
}
Expand Down

0 comments on commit 7225c5c

Please sign in to comment.