Skip to content

Instantly share code, notes, and snippets.

@SirPhemmiey
Created October 31, 2019 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirPhemmiey/f2d0b6c1cbf5588abe38bf6b2beddc92 to your computer and use it in GitHub Desktop.
Save SirPhemmiey/f2d0b6c1cbf5588abe38bf6b2beddc92 to your computer and use it in GitHub Desktop.
/**
* @dev method that handles transfer of ERC20 tokens to other address
* it assumes the calling address has approved this contract
* as spender
* @param symbol_ identifier mapping to a token contract address
* @param to_ beneficiary address
* @param amount_ numbers of token to transfer
*/
function transferTokens(bytes32 symbol_, address to_, uint256 amount_) public whenNotPaused{
require(tokens[symbol_] != address(0), "cannot be zero");
require(amount_ > 0, "must be greater then zero");
address contract_ = tokens[symbol_];
address from_ = msg.sender;
ERC20Interface = ERC20(contract_);
uint256 transactionId = transactions.push(
Transfer({
contract_: contract_,
to_: to_,
amount_: amount_,
failed_: true
})
);
transactionIndexesToSender[from_].push(transactionId - 1);
// emit logCheck(ERC20Interface.allowance(from_, address(this)), address(this));
if(amount_ > ERC20Interface.allowance(from_, address(this))) {
emit TransferFailed(from_, to_, amount_);
revert();
}
ERC20Interface.transferFrom(from_, to_, amount_);
transactions[transactionId - 1].failed_ = false;
emit TransferSuccessful(from_, to_, amount_);
}
async function signAndTransact(masterWallet, details) {
const transaction = new EthereumTx(details);
console.log({transaction})
// The private key is what unlocks your wallet.
transaction.sign(Buffer.from("8eab813abd0ba4d2cf4bae369979701435d63f907518a5fe369698f69018a152", 'hex'))
const serializedTransaction = transaction.serialize()
const addr = transaction.from.toString('hex')
logger.info(`Based on your private key, your wallet address is ${addr}`)
const [transaction_details, err] = await of(web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex')));
//return transaction_details;
console.log({transaction_details});
console.log({err});
}
exports.w = async(senderAddress, receiverAddress, amount, symbol) => {
const getTokenInfo = allTokens[symbol];
const accounts = await web3.eth.getAccounts();
const bytesData = web3.utils.fromAscii(symbol);
const nonce = await web3.eth.getTransactionCount(accounts[9]);
const TZContract = new web3.eth.Contract(tzContract, process.env.TZContract, {
from: accounts[9]
});
const block = await web3.eth.getBlock('latest');
const TokenContract = new web3.eth.Contract(getTokenInfo.abi, getTokenInfo.address);
const estGas = await TZContract.methods.transferTokens(bytesData, receiverAddress, amount).estimateGas({ from: accounts[9] });
console.log({estGas});
let details = {
"to": receiverAddress,
"gasLimit": block.gasLimit,
"gasPrice": "20",
"nonce": nonce,
"data": TZContract.methods.transferTokens(bytesData, receiverAddress, amount).encodeABI(),
"value": '0x0'
};
const res = await signAndTransact("", details);
console.log({res})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment