UNPKG

@alephium/web3-test

Version:
124 lines (120 loc) 5.28 kB
"use strict"; /* Copyright 2018 - 2022 The Alephium Authors This file is part of the alephium project. The library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the library. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.testNodeWallet = testNodeWallet; exports.getSigner = getSigner; exports.getSigners = getSigners; exports.transfer = transfer; exports.expectAssertionError = expectAssertionError; exports.randomContractId = randomContractId; exports.randomContractAddress = randomContractAddress; const web3_1 = require("@alephium/web3"); const web3_wallet_1 = require("@alephium/web3-wallet"); const crypto_1 = require("crypto"); const const_1 = require("./const"); async function prepareWallet(testNodeProvider) { const wallets = await testNodeProvider.wallets.getWallets(); if (wallets.find((wallet) => wallet.walletName === const_1.testWalletName)) { await unlockWallet(testNodeProvider); } else { await createWallet(testNodeProvider); } } async function changeActiveAddress(testNodeProvider) { await testNodeProvider.wallets.postWalletsWalletNameChangeActiveAddress(const_1.testWalletName, { address: const_1.testAddress }); } async function createWallet(testNodeProvider) { await testNodeProvider.wallets.putWallets({ password: const_1.testPassword, mnemonic: const_1.testMnemonic, walletName: const_1.testWalletName, isMiner: true }); await changeActiveAddress(testNodeProvider); } async function unlockWallet(testNodeProvider) { await testNodeProvider.wallets.postWalletsWalletNameUnlock(const_1.testWalletName, { password: const_1.testPassword }); await changeActiveAddress(testNodeProvider); } async function testNodeWallet(baseUrl = 'http://127.0.0.1:22973') { const nodeProvider = new web3_1.NodeProvider(baseUrl, undefined, (...params) => fetch(...params)); await prepareWallet(nodeProvider); const wallet = new web3_wallet_1.NodeWallet(const_1.testWalletName, nodeProvider); await wallet.unlock(const_1.testPassword); return wallet; } function checkGroup(group) { if (group < 0 || group >= web3_1.TOTAL_NUMBER_OF_GROUPS) { throw new Error('Invalid group index'); } } async function getSigner(alphAmount = web3_1.ONE_ALPH * 100n, group = 0, keyType) { checkGroup(group); try { const nodeProvider = await (0, const_1.tryGetDevnetNodeProvider)(); const balances = await nodeProvider.addresses.getAddressesAddressBalance(const_1.testAddress); const availableBalance = BigInt(balances.balance) - BigInt(balances.lockedBalance); if (availableBalance < alphAmount) { throw new Error('Not enough balance, please restart the devnet'); } const wallet = web3_wallet_1.PrivateKeyWallet.Random(group, nodeProvider, keyType); if (alphAmount > 0n) { const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }]; await const_1.testPrivateKeyWallet.signAndSubmitTransferTx({ signerAddress: const_1.testAddress, destinations }); } return wallet; } catch (error) { throw new web3_1.TraceableError(`Failed to get signer, please restart the devnet`, error); } } async function getSigners(num, alphAmountPerSigner = web3_1.ONE_ALPH * 100n, group = 0) { checkGroup(group); try { const wallets = []; for (let index = 0; index < num; index++) { const wallet = await getSigner(alphAmountPerSigner, group); wallets.push(wallet); } return wallets; } catch (error) { throw new web3_1.TraceableError('Failed to get signers, please restart the devnet', error); } } async function transfer(from, to, tokenId, amount) { const destination = { address: to, attoAlphAmount: tokenId === web3_1.ALPH_TOKEN_ID ? amount : web3_1.DUST_AMOUNT, tokens: tokenId === web3_1.ALPH_TOKEN_ID ? [] : [{ id: tokenId, amount }] }; return await from.signAndSubmitTransferTx({ signerAddress: from.address, destinations: [destination] }); } async function expectAssertionError(p, address, errorCode) { expect((0, web3_1.isBase58)(address)).toEqual(true); await expect(p).rejects.toThrowError(new RegExp(`Assertion Failed in Contract @ ${address}, Error Code: ${errorCode}`, 'mg')); } function randomContractId(groupIndex = 0) { checkGroup(groupIndex); const bytes = new Uint8Array([...(0, crypto_1.randomBytes)(31), groupIndex]); return (0, web3_1.binToHex)(bytes); } function randomContractAddress(groupIndex = 0) { return (0, web3_1.addressFromContractId)(randomContractId(groupIndex)); }