@alephium/web3-test
Version:
Utility functions for Alephium test
102 lines (91 loc) • 3.84 kB
JavaScript
"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.mintToken = mintToken;
const web3_1 = require("@alephium/web3");
const web3_wallet_1 = require("@alephium/web3-wallet");
const const_1 = require("./const");
function createTokenContract(symbol, name) {
return `
Contract Token(totalSupply: U256) implements IFungibleToken {
pub fn getSymbol() -> ByteVec {
return #${(0, web3_1.stringToHex)(symbol)}
}
pub fn getName() -> ByteVec {
return #${(0, web3_1.stringToHex)(name)}
}
pub fn getDecimals() -> U256 {
return 18
}
pub fn getTotalSupply() -> U256 {
return totalSupply
}
}
@std(id = #0001)
Interface IFungibleToken {
pub fn getSymbol() -> ByteVec
pub fn getName() -> ByteVec
pub fn getDecimals() -> U256
pub fn getTotalSupply() -> U256
}
`;
}
const contractCode = createTokenContract('TestToken', 'TestToken');
let _contract = undefined;
async function getContractArtifact(nodeProvider) {
if (_contract !== undefined)
return _contract;
const compileResult = await nodeProvider.contracts.postContractsCompileContract({ code: contractCode });
_contract = web3_1.Contract.fromCompileResult(compileResult);
return _contract;
}
async function getScriptArtifact(nodeProvider) {
const contract = await getContractArtifact(nodeProvider);
const scriptCode = `
TxScript Main(recipient: Address, totalSupply: U256) {
let (encodedImmFields, encodedMutFields) = Token.encodeFields!(totalSupply)
transferToken!(callerAddress!(), recipient, ALPH, dustAmount!())
createContractWithToken!{callerAddress!() -> ALPH: 1 alph}(
#${contract.bytecode},
encodedImmFields,
encodedMutFields,
totalSupply,
recipient
)
}
${contractCode}
`;
const scriptResult = await nodeProvider.contracts.postContractsCompileScript({ code: scriptCode });
return web3_1.Script.fromCompileResult(scriptResult);
}
async function createAndTransferToken(nodeProvider, deployer, recipient, amount) {
const script = await getScriptArtifact(nodeProvider);
const params = await script.txParamsForExecution({
signer: deployer,
initialFields: { recipient, totalSupply: amount },
attoAlphAmount: web3_1.ONE_ALPH + web3_1.DUST_AMOUNT
});
return (await deployer.signAndSubmitExecuteScriptTx(params));
}
async function mintToken(recipient, amount) {
const group = (0, web3_1.groupOfAddress)(recipient);
const nodeProvider = await (0, const_1.tryGetDevnetNodeProvider)();
const deployer = new web3_wallet_1.PrivateKeyWallet({ privateKey: const_1.testPrivateKeys[`${group}`], nodeProvider });
const result = await createAndTransferToken(nodeProvider, deployer, recipient, amount);
const contractId = await (0, web3_1.getContractIdFromUnsignedTx)(nodeProvider, result.unsignedTx);
const tokenId = contractId;
return { ...result, tokenId, contractId, contractAddress: (0, web3_1.addressFromContractId)(contractId) };
}