Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Sep 27, 2019
1 parent 3777572 commit 54a24c1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/celotool/src/lib/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export async function sendTransaction(tx: TransactionObject<any>) {
stableToken = await StableToken(web3)
}

return sendTransactionAsync(web3, tx, account, stableToken, emptyTxLogger).then(awaitConfirmation)
return sendTransactionAsync(tx, account, stableToken, emptyTxLogger).then(awaitConfirmation)
}
2 changes: 1 addition & 1 deletion packages/mobile/src/escrow/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { TransactionStatus, TransactionTypes } from 'src/transactions/reducer'
import { sendAndMonitorTransaction } from 'src/transactions/saga'
import { sendTransaction } from 'src/transactions/send'
import Logger from 'src/utils/Logger'
import { getWeb3, isZeroSyncMode, addLocalAccount } from 'src/web3/contracts'
import { addLocalAccount, getWeb3, isZeroSyncMode } from 'src/web3/contracts'
import { getConnectedAccount, getConnectedUnlockedAccount } from 'src/web3/saga'

const TAG = 'escrow/saga'
Expand Down
10 changes: 8 additions & 2 deletions packages/mobile/src/transactions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {
awaitConfirmation,
getStableTokenContract,
sendTransactionAsync,
sendTransactionAsyncWithLocalSigning,
SendTransactionLogEvent,
SendTransactionLogEventType,
} from '@celo/walletkit'
import CeloAnalytics from 'src/analytics/CeloAnalytics'
import { CustomEventNames } from 'src/analytics/constants'
import Logger from 'src/utils/Logger'
import { getWeb3 } from 'src/web3/contracts'
import { getWeb3, isZeroSyncMode } from 'src/web3/contracts'
import { TransactionObject } from 'web3/eth/types'

// As per https://www.typescriptlang.org/docs/handbook/advanced-types.html#exhaustiveness-checking
Expand Down Expand Up @@ -63,7 +64,12 @@ export const sendTransactionPromises = async (
) => {
const web3 = await getWeb3()
const stableToken = await getStableTokenContract(web3)
return sendTransactionAsync(web3, tx, account, stableToken, getLogger(tag, txId), staticGas)
// This if-else case is temprary and will disappear once we move from `walletkit` to `contractkit`.
if (isZeroSyncMode()) {
return sendTransactionAsyncWithLocalSigning(web3, tx, account, stableToken, getLogger(tag, txId), staticGas)
} else {
return sendTransactionAsync(tx, account, stableToken, getLogger(tag, txId), staticGas)
}
}

// Send a transaction and await for its confirmation
Expand Down
1 change: 1 addition & 0 deletions packages/walletkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export {
SendTransaction,
sendTransaction,
sendTransactionAsync,
sendTransactionAsyncWithLocalSigning,
SendTransactionLogEvent,
SendTransactionLogEventType,
TxLogger,
Expand Down
146 changes: 113 additions & 33 deletions packages/walletkit/src/contract-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ async function getGasPrice(
// nonce calculation will be correct over the order of seconds (restart time).
const currentNonce = new Map<string, number>()

//
/**
* sendTransactionAsync mainly abstracts the sending of a transaction in a promise like
* interface. Use the higher-order sendTransactionFactory as a consumer to configure
Expand All @@ -249,6 +248,119 @@ const currentNonce = new Map<string, number>()
* a transaction ID
*/
export async function sendTransactionAsync<T>(
tx: TransactionObject<T>,
account: string,
gasCurrencyContract: StableToken | GoldToken,
logger: TxLogger = emptyTxLogger,
estimatedGas?: number | undefined
): Promise<TxPromises> {
// @ts-ignore
const resolvers: TxPromiseResolvers = {}
// @ts-ignore
const rejectors: TxPromiseReject = {}

const receipt: Promise<TransactionReceipt> = new Promise((resolve, reject) => {
resolvers.receipt = resolve
rejectors.receipt = reject
})

const transactionHash: Promise<string> = new Promise((resolve, reject) => {
resolvers.transactionHash = resolve
rejectors.transactionHash = reject
})

const confirmation: Promise<boolean> = new Promise((resolve, reject) => {
resolvers.confirmation = resolve
rejectors.confirmation = reject
})

const rejectAll = (error: Error) => {
values(rejectors).map((reject) => {
// @ts-ignore
reject(error)
})
}

try {
logger(Started)
const txParams: any = {
from: account,
gasCurrency: gasCurrencyContract._address,
gasPrice: '0',
}

if (estimatedGas === undefined) {
estimatedGas = Math.round((await tx.estimateGas(txParams)) * gasInflateFactor)
logger(EstimatedGas(estimatedGas))
}

tx.send({
from: account,
// @ts-ignore
gasCurrency: gasCurrencyContract._address,
gas: estimatedGas,
// Hack to prevent web3 from adding the suggested gold gas price, allowing geth to add
// the suggested price in the selected gasCurrency.
gasPrice: '0',
})
.on('receipt', (r: TransactionReceipt) => {
logger(ReceiptReceived(r))
if (resolvers.receipt) {
resolvers.receipt(r)
}
})
.on('transactionHash', (txHash: string) => {
logger(TransactionHashReceived(txHash))

if (resolvers.transactionHash) {
resolvers.transactionHash(txHash)
}
})
.on('confirmation', (confirmationNumber: number) => {
if (confirmationNumber > 1) {
// "confirmation" event is called for 24 blocks.
// if check to avoid polluting the logs and trying to remove the standby notification more than once
return
}
logger(Confirmed)

if (resolvers.confirmation) {
resolvers.confirmation(true)
}
})
.on('error', (error: Error) => {
logger(Failed(error))
rejectAll(error)
})
} catch (error) {
logger(Exception(error))
rejectAll(error)
}

return {
receipt,
transactionHash,
confirmation,
}
}

/**
* sendTransactionAsyncWithLocalSigning is same as sendTransactionAsync except it fills
* in the missing fields and locally signs the transaction. It will fail if the `from`
* is not one of the account whose local signing keys are available. This method
* should only be used in Zero sync (infura-like) mode where Geth is running
* remotely.
*
* This separate function is temporary and contractkit uses a unified function
* for both web3 (local) and remote (geth) siging.
*
* @param tx The transaction object itself
* @param account The address from which the transaction should be sent
* @param gasCurrencyContract The contract instance of the Token in which to pay gas for
* @param logger An object whose log level functions can be passed a function to pass
* a transaction ID
*/
export async function sendTransactionAsyncWithLocalSigning<T>(
web3: Web3,
tx: TransactionObject<T>,
account: string,
Expand Down Expand Up @@ -367,38 +479,6 @@ export async function sendTransactionAsync<T>(
currentNonce.set(account, nonce + 1)
try {
await tx.send(celoTx)
// TODO: disable this only in infura mode.
// .on('receipt', (r: TransactionReceipt) => {
// logger(ReceiptReceived(r))
// if (resolvers.receipt) {
// resolvers.receipt(r)
// }
// })
// .on('transactionHash', (txHash: string) => {
// recievedTxHash = txHash
// logger(TransactionHashReceived(txHash))

// if (resolvers.transactionHash) {
// resolvers.transactionHash(txHash)
// }
// })
// .on('confirmation', (confirmationNumber: number) => {
// if (confirmationNumber > 1) {
// console.debug(`Confirmation number is ${confirmationNumber} > 1, ignored...`)
// // "confirmation" event is called for 24 blocks.
// // if check to avoid polluting the logs and trying to remove the standby notification more than once
// return
// }
// informAboutConfirmation()
// })
// .on('error', (error: Error) => {
// Logger.info(
// 'contract-utils@sendTransactionAsync',
// `Txn failed: txn ${util.inspect(error)} `
// )
// logger(Failed(error))
// rejectAll(error)
// })
} catch (e) {
Logger.debug('contract-utils@sendTransactionAsync', `Ignoring error: ${util.inspect(e)}`)
Logger.debug('contract-utils@sendTransactionAsync', `error message: ${e.message}`)
Expand Down

0 comments on commit 54a24c1

Please sign in to comment.