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

Commit

Permalink
refactor sendTransaction$
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Apr 9, 2019
1 parent 2b7501d commit 94d95ea
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions src/rouge.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,9 @@ function RougeProtocol (context) {
}
})

function transact$ (method, to, estimate, encoded) {
function sendTransaction$ (rawTx) {
return new Promise(async (resolve, reject) => {
try {
// workaround incorrect ABI encoding...
if (!estimate) estimate = await method.estimateGas({ from: account.address })
if (!encoded) encoded = await method.encodeABI()
var rawTx = {
gasPrice: web3.utils.toHex(web3.utils.toWei(options.gasPrice, 'gwei')),
gasLimit: web3.utils.toHex(estimate),
to: to,
value: '0x00',
data: encoded
}
// send is returning PromiEvent // use callback -- most stable solution atm
/* Event not yet working beta46 to beta51
.on(
Expand Down Expand Up @@ -101,6 +91,42 @@ function RougeProtocol (context) {
})
}

const sendFinney = ({fuel, recipient}) => new Promise(async (resolve, reject) => {
try {
recipient = universalAccount(recipient)
const rawTx = {
gasPrice: web3.utils.toHex(web3.utils.toWei(options.gasPrice, 'gwei')),
gasLimit: web3.utils.toHex(21000),
to: recipient.address,
value: web3.utils.toHex(web3.utils.toWei(fuel, 'finney'))
}
return resolve(await sendTransaction$(rawTx))
} catch (e) {
reject(e)
// throw new Error('error sending sendFinney. [sendFinney]')
}
})

function transact$ (method, to, estimate, encoded) {
return new Promise(async (resolve, reject) => {
try {
// workaround incorrect ABI encoding...
if (!estimate) estimate = await method.estimateGas({ from: account.address })
if (!encoded) encoded = await method.encodeABI()
var rawTx = {
gasPrice: web3.utils.toHex(web3.utils.toWei(options.gasPrice, 'gwei')),
gasLimit: web3.utils.toHex(estimate),
to: to,
value: '0x00',
data: encoded
}
return resolve(await sendTransaction$(rawTx))
} catch (e) {
reject(e)
}
})
}

const createCampaign = (
{issuance, scheme, tokens, name, expiration, attestor, auths}
) => new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -163,25 +189,6 @@ function RougeProtocol (context) {
}
})

const sendFinney = ({fuel, recipient}) => new Promise(async (resolve, reject) => {
try {
// TODO if (!account.privateKey) or not signing provider throw new Error('Unlockable account. [createCampaign]')
recipient = universalAccount(recipient)
const rawTx = {
gasPrice: web3.utils.toHex(web3.utils.toWei(options.gasPrice, 'gwei')),
gasLimit: web3.utils.toHex(21000),
to: recipient.address,
value: web3.utils.toHex(web3.utils.toWei(fuel, 'finney'))
}
const signed = await web3.eth.accounts.signTransaction(rawTx, account.privateKey)
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction)
if (!successfulTransact(receipt)) throw new Error('error sending sendFinney. [sendFinney]')
resolve(true)
} catch (e) {
reject(e)
}
})

let rgeAddress = null
let rgeInstance = null
let factoryAddress = null
Expand Down

0 comments on commit 94d95ea

Please sign in to comment.