Forked from cfluke-cb/gist:964b916912328a938c400b6f05ff8e11
Created
May 8, 2023 07:15
-
-
Save SyedImam1998/ec71ebadd0ce9c4d46b94eb3d214a960 to your computer and use it in GitHub Desktop.
Waas Example transaction
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
const erc20Abi= [ | |
'function transfer(address to, uint256 amount)', | |
'function balanceOf(address owner) view returns (uint256)', | |
'function decimals() view returns (uint8)', | |
'function symbol() view returns (string)', | |
'function name() view returns (string)', | |
] | |
const provider = new ethers.providers.AlchemyProvider( | |
'goerli', | |
alchemyKey, | |
); | |
const txCount = await provider.getTransactionCount(fromAddress); | |
const gasInfo = await provider.getFeeData(); | |
const tx = { | |
ChainID: '0x5', | |
Nonce: txCount, | |
MaxPriorityFeePerGas: gasInfo.maxPriorityFeePerGas.toHexString(), | |
MaxFeePerGas: gasInfo.maxFeePerGas.toHexString(), | |
Gas: 21000, | |
}; | |
tx.To = tokenAddress; | |
tx.Value = '0x0'; | |
const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, provider); | |
const txAmount = ethers.utils.parseUnits(amount, tokenDecimals); | |
/* Calculate the transaction fee */ | |
const gasEstimate = await tokenContract.estimateGas.transfer(to, txAmount, { | |
from: address, | |
}); | |
/* Encode the parameters */ | |
const data = new ethers.utils.Interface(erc20Abi).encodeFunctionData( | |
'transfer', | |
[to, txAmount] | |
); | |
tx.Gas = gasEstimate.toNumber(); | |
tx.Data = data.substring(2); | |
try { | |
console.log('started'); | |
await initMPCSdk(true); | |
await initMPCKeyService(apiKeyName, privateKey); | |
await initMPCWalletService(apiKeyName, privateKey); | |
const retrievedAddress = await getAddress(addressName); | |
const keyName = retrievedAddress.MPCKeys[0]; | |
await createSignatureFromTx(keyName, tx); | |
const pendingSignatures = await pollForPendingSignatures(deviceGroupName); | |
await computeMPCOperation(pendingSignatures[0].MPCData); | |
let signatureResult = await waitPendingSignature( | |
pendingSignatures[0].Operation | |
); | |
const signedTxResult = await getSignedTransaction( | |
tx, | |
signatureResult | |
); | |
setSignedTx(signedTxResult); | |
const txReceipt = await provider.sendTransaction( | |
'0x'+signedTxResult.RawTransaction | |
); | |
console.log('Transaction hash:', txReceipt.hash); | |
} catch (error) { | |
console.log('error', error); | |
throw new Error('Invalid initialTx value'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment