Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cfluke-cb/964b916912328a938c400b6f05ff8e11 to your computer and use it in GitHub Desktop.
Save cfluke-cb/964b916912328a938c400b6f05ff8e11 to your computer and use it in GitHub Desktop.
Waas Example transaction
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