UNPKG

pulsar-contracts

Version:

62 lines 2.85 kB
import { AccountUpdate, fetchAccount, Mina, PrivateKey, } from 'o1js'; import { SettlementContract } from '../SettlementContract.js'; export const DeployScripts = { fetchAccounts, waitTransactionAndFetchAccount, deploySettlementContract, deployAndInitializeContract, sendMina, }; async function sendMina(senderKey, receiverKey, amount, fee = 1e9) { const tx = await Mina.transaction({ sender: senderKey.toPublicKey(), fee }, async () => { const senderAccount = AccountUpdate.createSigned(senderKey.toPublicKey()); AccountUpdate.fundNewAccount(senderKey.toPublicKey()); senderAccount.send({ to: receiverKey, amount }); }); await waitTransactionAndFetchAccount(tx, [senderKey], [receiverKey]); } async function fetchAccounts(accounts) { for (let account of accounts) { await fetchAccount({ publicKey: account }); } } async function waitTransactionAndFetchAccount(tx, keys, accountsToFetch) { try { await tx.prove(); const pendingTransaction = await tx.sign(keys).send(); console.log(`Tx hash: ${pendingTransaction.hash}`); const status = await pendingTransaction.safeWait(); if (status.status === 'rejected') { throw new Error('Transaction was rejected: ' + JSON.stringify(status.errors, null, 2)); } if (accountsToFetch) { await fetchAccounts(accountsToFetch); } } catch (error) { console.log('error', error); throw error; } } async function deploySettlementContract(signerPrivateKey, contractPrivateKey = PrivateKey.random(), fee = 1e9) { const contractInstance = new SettlementContract(contractPrivateKey.toPublicKey()); const signerPublicKey = signerPrivateKey.toPublicKey(); const deployTx = await Mina.transaction({ sender: signerPublicKey, fee }, async () => { AccountUpdate.fundNewAccount(signerPublicKey); await contractInstance.deploy(); }); await waitTransactionAndFetchAccount(deployTx, [signerPrivateKey, contractPrivateKey], [contractInstance.address]); return contractPrivateKey; } async function deployAndInitializeContract(signerPrivateKey, contractPrivateKey = PrivateKey.random(), validatorList, fee = 1e9) { const contractInstance = new SettlementContract(contractPrivateKey.toPublicKey()); const signerPublicKey = signerPrivateKey.toPublicKey(); const deployTx = await Mina.transaction({ sender: signerPublicKey, fee }, async () => { AccountUpdate.fundNewAccount(signerPublicKey); await contractInstance.deploy(); await contractInstance.initialize(validatorList.hash); }); await waitTransactionAndFetchAccount(deployTx, [signerPrivateKey, contractPrivateKey], [contractInstance.address]); return contractPrivateKey; } //# sourceMappingURL=deploy.js.map