@stratosphere-network/token
Version:
Token module for StratoSphere SDK - ERC20 token minting and factory functionality
120 lines • 5.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Chain = void 0;
const types_1 = require("./types");
var types_2 = require("./types");
Object.defineProperty(exports, "Chain", { enumerable: true, get: function () { return types_2.Chain; } });
const tokenFactory_1 = require("./utils/tokenFactory");
const token_1 = require("./utils/token");
const types_3 = require("./types");
const ethers_1 = require("ethers");
class Token {
getWallet(chain, privateKey, walletInstance) {
if (walletInstance) {
return walletInstance;
}
if (privateKey) {
const rpcUrl = types_1.RPC_URLS[chain];
const provider = new ethers_1.ethers.JsonRpcProvider(rpcUrl);
return new ethers_1.ethers.Wallet(privateKey, provider);
}
throw new Error("Private key or wallet instance is required");
}
getProvider(chain) {
const rpcUrl = types_1.RPC_URLS[chain];
return new ethers_1.ethers.JsonRpcProvider(rpcUrl);
}
getTokenContract(tokenAddress, walletOrProvider) {
return new ethers_1.ethers.Contract(tokenAddress, token_1.TokenAbi, walletOrProvider);
}
//support provider from metamask too
async deployToken(config, deployerPrivateKey, walletInstance) {
if (!config.chain) {
throw new Error("Chain is required");
}
const wallet = this.getWallet(config.chain, deployerPrivateKey, walletInstance);
const tokenFactoryAddress = types_3.factoryAddress[config.chain];
if (!tokenFactoryAddress) {
throw new Error("Unsupported chain.");
}
const tokenFactory = new ethers_1.ethers.Contract(tokenFactoryAddress, tokenFactory_1.TokenFactoryAbi, wallet);
try {
const tx = await tokenFactory.createToken(config.name, config.symbol, config.totalSupply, config.decimals);
const receipt = await tx.wait();
if (!receipt) {
throw new Error("Transaction failed: no receipt");
}
const eventTopic = tokenFactory.interface.getEvent("TokenCreated")?.topicHash;
if (!eventTopic) {
throw new Error("TokenCreated event topic not found in ABI");
}
const log = receipt.logs.find((x) => x.topics[0] === eventTopic);
if (!log) {
throw new Error("TokenCreated event not found in transaction receipt");
}
const decodedLog = tokenFactory.interface.parseLog({
topics: [...log.topics],
data: log.data,
});
if (!decodedLog) {
throw new Error("Failed to decode TokenCreated event");
}
const tokenAddress = decodedLog.args.tokenAddress;
console.log("Token deployed successfully.");
return {
transactionHash: receipt.hash,
tokenAddress: tokenAddress,
};
}
catch (error) {
console.error("Error deploying token:", error);
throw error;
}
}
async grantRole(chain, tokenAddress, role, toAddress, deployerPrivateKey, walletInstance) {
const wallet = this.getWallet(chain, deployerPrivateKey, walletInstance);
const tokenContract = this.getTokenContract(tokenAddress, wallet);
const roleHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(role));
const tx = await tokenContract.grantRole(roleHash, toAddress);
return await tx.wait();
}
async revokeRole(chain, tokenAddress, role, fromAddress, deployerPrivateKey, walletInstance) {
const wallet = this.getWallet(chain, deployerPrivateKey, walletInstance);
const tokenContract = this.getTokenContract(tokenAddress, wallet);
const roleHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(role));
const tx = await tokenContract.revokeRole(roleHash, fromAddress);
return await tx.wait();
}
async hasRole(chain, tokenAddress, role, address) {
const provider = this.getProvider(chain);
const tokenContract = this.getTokenContract(tokenAddress, provider);
const roleHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(role));
return await tokenContract.hasRole(roleHash, address);
}
async setTransferable(chain, tokenAddress, isTransferable, deployerPrivateKey, walletInstance) {
const wallet = this.getWallet(chain, deployerPrivateKey, walletInstance);
const tokenContract = this.getTokenContract(tokenAddress, wallet);
const tx = await tokenContract.setTransferable(isTransferable);
return await tx.wait();
}
async pause(chain, tokenAddress, deployerPrivateKey, walletInstance) {
const wallet = this.getWallet(chain, deployerPrivateKey, walletInstance);
const tokenContract = this.getTokenContract(tokenAddress, wallet);
const tx = await tokenContract.pause();
return await tx.wait();
}
async unpause(chain, tokenAddress, deployerPrivateKey, walletInstance) {
const wallet = this.getWallet(chain, deployerPrivateKey, walletInstance);
const tokenContract = this.getTokenContract(tokenAddress, wallet);
const tx = await tokenContract.unpause();
return await tx.wait();
}
async upgradeTo(chain, tokenAddress, newImplementationAddress, deployerPrivateKey, walletInstance) {
const wallet = this.getWallet(chain, deployerPrivateKey, walletInstance);
const tokenContract = this.getTokenContract(tokenAddress, wallet);
const tx = await tokenContract.upgradeToAndCall(newImplementationAddress, "0x");
return await tx.wait();
}
}
exports.default = Token;
//# sourceMappingURL=index.js.map