Last active
August 30, 2018 08:13
-
-
Save PaulLaux/741a0787aa5cd2b81ecb02ad79ca3772 to your computer and use it in GitHub Desktop.
Sending token via Ethereum smart contract interaction with redux-saga
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// full source: https://github.com/PaulLaux/eth-hot-wallet/blob/master/app/containers/Header/saga.js#L203 | |
export function* SendTransaction() { | |
try { | |
// ... | |
const contractAddress = tokenInfo.contractAddress; | |
const sendParams = { from: fromAddress, value: '0x0', gasPrice, gas: maxGasForTokenSend }; | |
const tokenAmount = amount * (10 ** tokenInfo.decimals); | |
function sendTokenPromise(tokenContractAddress, sendToAddress, sendAmount, params) { | |
return new Promise((resolve, reject) => { | |
const tokenContract = erc20Contract.at(tokenContractAddress); | |
tokenContract.transfer.sendTransaction(sendToAddress, sendAmount, params, (err, sendTx) => { | |
if (err) return reject(err); | |
return resolve(sendTx); | |
}); | |
}); | |
} | |
const tx = yield call(sendTokenPromise, contractAddress, toAddress, tokenAmount, sendParams); | |
// This saga will resume running after the transaction is sent and tx is returned by Ethereum node | |
// ... | |
yield put(sendTransactionSuccess(tx)); | |
} catch (err) { | |
// ... | |
yield put(sendTransactionError(errMsg)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment