Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sending token via Ethereum smart contract interaction with redux-saga
// 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