@kanalabs/mirai
Version:
Mirai - Account Abstraction SDK (EVM + non-EVM)
278 lines (277 loc) • 13.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SDKGateway = exports.initializeSdkGateway = void 0;
const prime_sdk_1 = require("@etherspot/prime-sdk");
const network_1 = require("./network");
const ethers_1 = require("ethers");
const erc20_helper_1 = require("../utils/erc20-helper");
const erc721_helper_1 = require("../utils/erc721-helper");
const erc1155_helper_1 = require("../utils/erc1155-helper");
const aptos_smart_wallet_1 = require("@kanalabs/aptos-smart-wallet");
const network_project_key_1 = require("../utils/network-project-key");
const initializeSdkGateway = (walletProvider, options) => __awaiter(void 0, void 0, void 0, function* () {
const sdk = new SDKGateway(walletProvider, options);
yield sdk.initialize();
return sdk;
});
exports.initializeSdkGateway = initializeSdkGateway;
class SDKGateway {
constructor(walletProvider, options) {
this.instances = {};
this.contractAddress = {};
this.rpcUrl = {};
if (!(0, prime_sdk_1.isWalletProvider)(walletProvider)) {
throw new Error('Invalid wallet provider (KANA)');
}
this.walletProvider = walletProvider;
this.activeNetworks = network_1.availableNetworks.filter((n) => {
return options.networks.includes(n.name) && n.active === 1;
});
this.sponsor = options.sponsor;
this.accountAddress = options.accountAddress;
this.bundlerApiKey = options.bundlerApiKey;
// this.paymasterApi = {
// url: options.paymasterApiUrl as string,
// context: options.context as any,
// api_key: options.api_key as string,
// }
}
setCurrentInstance(network) {
this.currentInstance = this.instances[network];
if (!this.currentInstance)
throw new Error('Unsupported Network (KANA)');
this.currentNetwork = network;
return this.currentInstance;
}
getAptosInstance(network) {
if (this.isAptosNetwork(network)) {
this.currentInstance = this.instances[network];
if (!this.currentInstance)
throw new Error('Unsupported Network (KANA)');
this.currentNetwork = network;
return this.currentInstance;
}
throw new Error('Not Aptos Network (KANA)');
}
initialize(network) {
return __awaiter(this, void 0, void 0, function* () {
return network
? yield this.initializeOne(this.walletProvider, network)
: yield this.initializeAll(this.walletProvider);
});
}
destroy(network) {
return __awaiter(this, void 0, void 0, function* () {
network ? this.destroyOne(network) : this.destroyAll();
});
}
getNativeBalance(network) {
return __awaiter(this, void 0, void 0, function* () {
return network ? yield this.getAccountNativBalOne(network) : yield this.getAccountNativeBalAll();
});
}
erc20(tokenAddress, network) {
if (network) {
if (!this.instances[network])
throw new Error('Unsupported Network (KANA)');
if (this.isAptosNetwork(network))
throw new Error('Aptos network not supported (KANA)');
return new erc20_helper_1.ERC20Helper(this.instances[network], tokenAddress, this.getProvider(network));
}
else {
if (!this.currentInstance || !this.currentNetwork)
throw new Error('Network instance not set, pass the network (KANA)');
if (this.isAptosNetwork(this.currentNetwork))
throw new Error('Aptos network not supported (KANA)');
return new erc20_helper_1.ERC20Helper(this.currentInstance, tokenAddress, this.getProvider(this.currentNetwork));
}
}
erc721(collectionAddress, network) {
if (network) {
if (!this.instances[network])
throw new Error('Unsupported Network (KANA)');
if (this.isAptosNetwork(network))
throw new Error('Aptos network not supported (KANA)');
return new erc721_helper_1.ERC721Helper(this.instances[network], collectionAddress, this.getProvider(network));
}
else {
if (!this.currentInstance || !this.currentNetwork)
throw new Error('Network instance not set, pass the network (KANA)');
if (this.isAptosNetwork(this.currentNetwork))
throw new Error('Aptos network not supported (KANA)');
return new erc721_helper_1.ERC721Helper(this.currentInstance, collectionAddress, this.getProvider(this.currentNetwork));
}
}
erc1155(collectionAddress, network) {
if (network) {
if (!this.instances[network])
throw new Error('Unsupported Network (KANA)');
if (this.isAptosNetwork(network))
throw new Error('Aptos network not supported (KANA)');
return new erc1155_helper_1.ERC1155Helper(this.instances[network], collectionAddress, this.getProvider(network));
}
else {
if (!this.currentInstance || !this.currentNetwork)
throw new Error('Network instance not set, pass the network (KANA)');
if (this.isAptosNetwork(this.currentNetwork))
throw new Error('Aptos network not supported (KANA)');
return new erc1155_helper_1.ERC1155Helper(this.currentInstance, collectionAddress, this.getProvider(this.currentNetwork));
}
}
getProvider(network) {
if (this.isAptosNetwork(network))
throw new Error('Aptos network not supported (KANA)');
const url = `${this.rpcUrl[network]}?api-key=${this.bundlerApiKey}`;
if (!url)
throw new Error('Unsupported Network (KANA)');
return new ethers_1.providers.JsonRpcProvider(url);
}
initializeOne(walletProvider, networkName) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.isNetworkActive(networkName)) {
throw new Error('Unsupported Network (KANA)');
}
if (this.isAptosNetwork(networkName)) {
const provider = walletProvider;
const aptosWallet = new aptos_smart_wallet_1.AptosWallet(provider.privateKey, {
network: networkName,
sponsor: this.sponsor,
});
this.instances[networkName] = aptosWallet;
this.rpcUrl[networkName] = network_1.BundlerEndpoints[networkName];
this.contractAddress[networkName] = yield aptosWallet.computeAccountAddress();
}
else if (this.isKanaNetwork(networkName)) {
const evmWallet = new prime_sdk_1.PrimeSdk(walletProvider, {
chainId: network_1.kanaNetworkConfig[networkName].chainId,
entryPointAddress: network_1.kanaNetworkConfig[networkName].contracts.entryPoint,
walletFactoryAddress: network_1.kanaNetworkConfig[networkName].contracts.walletFactory.etherspot,
bundlerProvider: { url: `${network_1.kanaNetworkConfig[networkName].bundler}?api-key=${this.bundlerApiKey}` },
factoryWallet: prime_sdk_1.Factory.ETHERSPOT,
accountAddress: this.accountAddress,
});
this.instances[networkName] = evmWallet;
this.rpcUrl[networkName] = network_1.BundlerEndpoints[networkName];
this.contractAddress[networkName] = yield evmWallet.getCounterFactualAddress();
}
else {
const evmWallet = new prime_sdk_1.PrimeSdk(walletProvider, {
chainId: prime_sdk_1.NETWORK_NAME_TO_CHAIN_ID[networkName],
bundlerProvider: { url: `${network_1.BundlerEndpoints[networkName]}?api-key=${this.bundlerApiKey}` },
accountAddress: this.accountAddress,
});
this.instances[networkName] = evmWallet;
this.rpcUrl[networkName] = network_1.BundlerEndpoints[networkName];
this.contractAddress[networkName] = yield evmWallet.getCounterFactualAddress();
}
return this.contractAddress;
});
}
initializeAll(walletProvider) {
var _a, e_1, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
let allInstances = {};
if (this.activeNetworks.length > 0) {
try {
for (var _d = true, _e = __asyncValues(this.activeNetworks), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const network = _c;
allInstances = yield this.initializeOne(walletProvider, network.name);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
return allInstances;
}
else {
throw new Error('No active network found (KANA)');
}
});
}
destroyOne(networkName) {
if (!this.isNetworkActive(networkName)) {
throw new Error('Unsupported Network (KANA)');
}
const instance = this.instances[networkName];
if (instance instanceof prime_sdk_1.PrimeSdk) {
instance.destroy();
}
}
destroyAll() {
this.activeNetworks.map((allowedNetworks) => {
if (this.instances[allowedNetworks.name]) {
const instance = this.instances[allowedNetworks.name];
if (instance instanceof prime_sdk_1.PrimeSdk) {
instance.destroy();
}
}
});
}
getAccountNativBalOne(networkName) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.isNetworkActive(networkName)) {
throw new Error('Unsupported Network (KANA)');
}
const instance = this.instances[networkName];
if (instance instanceof prime_sdk_1.PrimeSdk) {
return yield instance.getNativeBalance();
}
throw new Error('Aptos network not supported (KANA)');
});
}
getAccountNativeBalAll() {
return __awaiter(this, void 0, void 0, function* () {
const activeNetworks = this.activeNetworks.filter((n) => n.active);
const balances = yield Promise.all(activeNetworks.map((n) => __awaiter(this, void 0, void 0, function* () {
const instance = this.instances[n.name];
let balance;
if (instance instanceof prime_sdk_1.PrimeSdk) {
balance = yield instance.getNativeBalance();
}
return {
[n.name]: balance,
};
})));
return Object.assign({}, ...balances);
});
}
isNetworkActive(networkName) {
const network = this.activeNetworks.find((network) => network.name === networkName);
return network ? network.active === 1 : false;
}
isAptosNetwork(networkName) {
return networkName === network_1.NetworkNames.AptosMainnet || networkName === network_1.NetworkNames.AptosTestnet;
}
isKanaNetwork(networkName) {
return network_project_key_1.kanaNetworks.includes(networkName);
}
isMainnetNetwork(networkName) {
return network_project_key_1.mainnetNetworks.includes(networkName);
}
isTestnetNetwork(networkName) {
return network_project_key_1.testnetNetworks.includes(networkName);
}
}
exports.SDKGateway = SDKGateway;