I want to use taquito to successfully transfer the amount between two tz1 accounts without using the transfer function of contract API or wallet API.
Instead, I want to use taquito RPC functions such as forgeOperations and injectOperation.
Tezos Stack Exchange is a question and answer site for users, experts, and developers of the Tezos blockchain project. It only takes a minute to sign up.
Sign up to join this communityUse the below function to achieve the desired.
Note to give input like below:
const input = {
"receiverAddress" : "address of receiver",
"value" : "amount in Mutez",
"sender" : "address of sender",
"senderPublicKey" : "public key of sender",
"senderPrivateKey" : "private key of sender"
}
Transfer Function:
const transfer = async function(input){
try{
const publicKey = await getManagerKey(input.sender);
if(publicKey != null && typeof(publicKey) != 'string') throw publicKey;
var counter = await getCounter(input.sender);
if(counter.Error != undefined) throw counter;
var contents = [];
if(publicKey == null){
const estimatedReveal = await estimateReveal(input);
if(estimatedReveal.Error != undefined) throw estimatedReveal;
if(estimatedReveal != undefined){
contents.push({
"kind" : "reveal",
"fee" : estimatedReveal.suggestedFee,
"public_key" : input.senderPublicKey,
"source" : input.sender,
"gas_limit" : estimatedReveal.gasLimit,
"storage_limit" : estimatedReveal.storageLimit,
"counter" : counter
});
counter = (Number(counter) + 1 ).toString();
}else{
throw({
"Error" : "CREATE_TRANSACTION :: REVEAL :: Repeat Error",
"message" : "Trying to reveal already revealed account"
});
}
}
const estimatedTransaction = await estimateTransaction(input);
if(estimatedTransaction.Error != undefined) throw estimatedTransaction;
contents.push({
"kind": "transaction",
"fee": estimatedTransaction.suggestedFee,
"gas_limit": estimatedTransaction.gasLimit,
"storage_limit": estimatedTransaction.storageLimit,
"amount": input.value,
"destination": input.receiverAddress,
"parameters" : undefined,
"source": input.sender,
"counter": counter
});
console.log(contents);
const blockHeader = await getBlockHeader();
const opObj = {
"branch" : blockHeader.hash,
"contents" : contents
}
const forgedBytes = await forgeOperations(opObj);
console.log(forgedBytes);
const signedObject = await signOp(forgedBytes, input.senderPrivateKey);
console.log(signedObject);
const opHash = await injectOp(signedObject.opbytes);
console.log(opHash);
}catch(error){
var errObj = {};
errObj["Error"] = "CREATE_TRANSACTION :: Error";
errObj["message"] = "Unable to create transaction";
if(error.Error != undefined){
errObj["memo"] = error;
}else{
errObj["memo"] = error.toString();
}
console.log(errObj);
return errObj;
}
}
Note that I have used some RPC functions like getManagerKey, getBlockHeader, getBlock and getContract.
I also have used some estimate functions of the taquito. Find below:
const estimateReveal = async function(input){
try{
tezos.setProvider({ signer: new InMemorySigner(input.senderPrivateKey) });
const estimated = await tezos.estimate
.reveal()
.then((est) => {
return({
"gasLimit" : est.gasLimit.toString(),
"storageLimit" : est.storageLimit.toString(),
"suggestedFee" : est.suggestedFeeMutez.toString()
});
})
.catch((error) => {
throw({
"Error" : "TAQUITO :: Network Error",
"message" : "Taquito estimate method returned error",
"memo" : JSON.stringify(error, null, 2)
})
});
return estimated;
}catch(error){
var errObj = {};
errObj["Error"] = "CREATE_TRANSACTION :: ESTIMATE_REVEAL :: Network Error";
errObj["message"] = "Unable to estimate reveal operation";
if(error.Error != undefined){
errObj["memo"] = error;
}else{
errObj["memo"] = error.toString();
}
return errObj;
}
}
and
const estimateTransaction = async function(input){
try{
tezos.setProvider({ signer: new InMemorySigner(input.senderPrivateKey) });
const estimated = await tezos.estimate
.transfer({ to: input.receiverAddress, amount: Number(input.value)/1000000 })
.then((est) => {
return({
"gasLimit" : est.gasLimit.toString(),
"storageLimit" : est.storageLimit.toString(),
"suggestedFee" : est.suggestedFeeMutez.toString()
});
})
.catch((error) => {
throw({
"Error" : "TAQUITO :: Network Error",
"message" : "Taquito estimate method returned error",
"memo" : JSON.stringify(error, null, 2)
})
});
return estimated;
}catch(error){
var errObj = {};
errObj["Error"] = "CREATE_TRANSACTION :: ESTIMATE_TRANSACTION:: Network Error";
errObj["message"] = "Unable to estimate transaction operation";
if(error.Error != undefined){
errObj["memo"] = error;
}else{
errObj["memo"] = error.toString();
}
return errObj;
}
}
and
const getCounter = async function(address){
try{
const contractData = await getContract(address);
if(contractData.Error != undefined) throw contractData;
return (Number(contractData.counter) + 1).toString();
}catch(error){
return error;
}
}
Please feel free to comment down below if you need any clarifications. Thanks
NOTE: Import necessary packaged from '@taquito/rpc', '@taquito/taquito' and '@taquito/signer'.