UNPKG

@mai3/phaser-sdk

Version:

A UI component library based on the Phaser game engine

421 lines (420 loc) 20.8 kB
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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { createAppKit } from "@reown/appkit"; import * as networks from "@reown/appkit/networks"; import { WagmiAdapter } from "@reown/appkit-adapter-wagmi"; import { signMessage as wagmiSignMessage, writeContract, readContract, waitForTransactionReceipt, } from "@wagmi/core"; import VaultABI from "./abi/Vault.json"; import TokenABI from "./abi/Token.json"; import { parseUnits } from "viem"; var EVMConnector = /** @class */ (function () { function EVMConnector() { } EVMConnector.init = function (config, walletChanged, onSigned) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: this.config = config; this.walletChanged = walletChanged; this.onSigned = onSigned; return [4 /*yield*/, this.getInstance()]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; EVMConnector.getInstance = function () { return __awaiter(this, void 0, void 0, function () { var _a, err_1; var _b, _c, _d, _e, _f, _g, _h, _j, _k; return __generator(this, function (_l) { switch (_l.label) { case 0: if (!this.config) { throw new Error("Config is not initialized"); } this.wagmiAdapter = new WagmiAdapter({ projectId: (_b = this.config.projectId) !== null && _b !== void 0 ? _b : "", networks: [networks[(_c = this.config.network) !== null && _c !== void 0 ? _c : ""]], }); _a = this; return [4 /*yield*/, createAppKit({ adapters: [this.wagmiAdapter], networks: [networks[(_d = this.config.network) !== null && _d !== void 0 ? _d : ""]], metadata: { name: (_e = this.config.name) !== null && _e !== void 0 ? _e : "Mai3", description: (_f = this.config.description) !== null && _f !== void 0 ? _f : "Mai3 Example", url: (_g = this.config.url) !== null && _g !== void 0 ? _g : "https://example.com", icons: [ (_h = this.config.icon) !== null && _h !== void 0 ? _h : "https://avatars.githubusercontent.com/u/179229932", ], }, projectId: (_j = this.config.projectId) !== null && _j !== void 0 ? _j : "", defaultNetwork: networks[(_k = this.config.network) !== null && _k !== void 0 ? _k : ""], features: { analytics: false, email: false, socials: [], }, })]; case 1: _a.connector = _l.sent(); _l.label = 2; case 2: _l.trys.push([2, 4, , 5]); return [4 /*yield*/, this.connector.disconnect()]; case 3: _l.sent(); return [3 /*break*/, 5]; case 4: err_1 = _l.sent(); console.log(err_1); return [3 /*break*/, 5]; case 5: return [2 /*return*/, this.connector]; } }); }); }; EVMConnector.openModal = function () { return __awaiter(this, void 0, void 0, function () { var sign, err_2, err_3; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: sign = function () { return __awaiter(_this, void 0, void 0, function () { var result; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: return [4 /*yield*/, this.signMessage((_a = this.config.signMessage) !== null && _a !== void 0 ? _a : "Sign in with EVM")]; case 1: result = _e.sent(); if (result) { (_b = this.onSigned) === null || _b === void 0 ? void 0 : _b.call(this, (_c = result.signMessage) !== null && _c !== void 0 ? _c : "Sign in with EVM", (_d = result.signature) !== null && _d !== void 0 ? _d : "", this.getAccountAddress()); } return [2 /*return*/]; } }); }); }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.disconnect()]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: err_2 = _a.sent(); console.log(err_2); return [3 /*break*/, 4]; case 4: _a.trys.push([4, 6, , 7]); return [4 /*yield*/, this.connector.open()]; case 5: _a.sent(); this.connector.subscribeAccount(function (account) { return __awaiter(_this, void 0, void 0, function () { var err_4; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); if (!account.isConnected) return [3 /*break*/, 2]; (_a = this.walletChanged) === null || _a === void 0 ? void 0 : _a.call(this, this.connector.getWalletProvider(), this.getAccountAddress()); return [4 /*yield*/, sign()]; case 1: _b.sent(); _b.label = 2; case 2: return [3 /*break*/, 4]; case 3: err_4 = _b.sent(); this.isSigning = false; console.log(err_4); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }); return [3 /*break*/, 7]; case 6: err_3 = _a.sent(); this.isSigning = false; console.log(err_3); return [3 /*break*/, 7]; case 7: return [2 /*return*/]; } }); }); }; EVMConnector.disconnect = function () { return __awaiter(this, void 0, void 0, function () { var err_5; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); return [4 /*yield*/, this.connector.disconnect()]; case 1: _b.sent(); (_a = this.walletChanged) === null || _a === void 0 ? void 0 : _a.call(this, null, undefined); return [3 /*break*/, 3]; case 2: err_5 = _b.sent(); console.log(err_5); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; EVMConnector.getAccountAddress = function () { if (!this.connector) { return undefined; } return this.connector.getAddress(); }; EVMConnector.signMessage = function (str) { return __awaiter(this, void 0, void 0, function () { var signature, err_6; return __generator(this, function (_a) { switch (_a.label) { case 0: if (this.isSigning) return [2 /*return*/]; this.isSigning = true; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); if (!this.connector) { throw new Error("Provider not initialized"); } return [4 /*yield*/, wagmiSignMessage(this.wagmiAdapter.wagmiConfig, { message: str, })]; case 2: signature = _a.sent(); return [2 /*return*/, { signMessage: str, signature: signature, }]; case 3: err_6 = _a.sent(); this.isSigning = false; throw err_6; case 4: return [2 /*return*/]; } }); }); }; EVMConnector.deposit = function (contractAddress, amount) { return __awaiter(this, void 0, void 0, function () { var config, tokenAddress, decimals, amountWei, depositResult, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.connector) { throw new Error("Provider not initialized"); } _a.label = 1; case 1: _a.trys.push([1, 6, , 7]); config = this.wagmiAdapter.wagmiConfig; return [4 /*yield*/, this.getToken(contractAddress)]; case 2: tokenAddress = _a.sent(); return [4 /*yield*/, this.approveToken(tokenAddress, contractAddress, amount)]; case 3: _a.sent(); return [4 /*yield*/, this.getTokenDecimals(tokenAddress)]; case 4: decimals = _a.sent(); amountWei = parseUnits(amount.toString(), decimals); return [4 /*yield*/, writeContract(config, { address: contractAddress, abi: VaultABI, functionName: "deposit", args: [amountWei], })]; case 5: depositResult = _a.sent(); return [2 /*return*/, depositResult]; case 6: error_1 = _a.sent(); console.error("Deposit failed:", error_1); throw error_1; case 7: return [2 /*return*/]; } }); }); }; EVMConnector.withdraw = function (contractAddress, withdrawNo, amount, signature) { return __awaiter(this, void 0, void 0, function () { var result, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.connector) { throw new Error("Provider not initialized"); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, writeContract(this.wagmiAdapter.wagmiConfig, { address: contractAddress, abi: VaultABI, functionName: "withdraw", args: [withdrawNo, amount, signature], })]; case 2: result = _a.sent(); return [2 /*return*/, result]; case 3: error_2 = _a.sent(); console.error("Withdraw failed:", error_2); throw error_2; case 4: return [2 /*return*/]; } }); }); }; EVMConnector.getToken = function (contractAddress) { return __awaiter(this, void 0, void 0, function () { var tokenAddress, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.connector) { throw new Error("Provider not initialized"); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, readContract(this.wagmiAdapter.wagmiConfig, { address: contractAddress, abi: VaultABI, functionName: "getToken", })]; case 2: tokenAddress = _a.sent(); return [2 /*return*/, tokenAddress]; case 3: error_3 = _a.sent(); console.error("Get token failed:", error_3); throw error_3; case 4: return [2 /*return*/]; } }); }); }; EVMConnector.getTokenDecimals = function (tokenAddress) { return __awaiter(this, void 0, void 0, function () { var config, decimals, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.connector) { throw new Error("Provider not initialized"); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); config = this.wagmiAdapter.wagmiConfig; return [4 /*yield*/, readContract(config, { address: tokenAddress, abi: TokenABI, functionName: "decimals", })]; case 2: decimals = _a.sent(); return [2 /*return*/, decimals]; case 3: error_4 = _a.sent(); console.error("Get token decimals failed:", error_4); throw error_4; case 4: return [2 /*return*/]; } }); }); }; EVMConnector.approveToken = function (tokenAddress, contractAddress, amount) { return __awaiter(this, void 0, void 0, function () { var config, decimals, allowWei, amountWei, approveResult; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: config = this.wagmiAdapter.wagmiConfig; return [4 /*yield*/, this.getTokenDecimals(tokenAddress)]; case 1: decimals = _b.sent(); return [4 /*yield*/, readContract(config, { address: tokenAddress, abi: TokenABI, functionName: "allowance", args: [(_a = this.getAccountAddress()) !== null && _a !== void 0 ? _a : "", contractAddress], })]; case 2: allowWei = (_b.sent()); amountWei = parseUnits(amount.toString(), decimals); if (!(allowWei < amountWei)) return [3 /*break*/, 5]; return [4 /*yield*/, writeContract(config, { address: tokenAddress, abi: TokenABI, functionName: "approve", args: [contractAddress, amountWei], })]; case 3: approveResult = _b.sent(); return [4 /*yield*/, waitForTransactionReceipt(config, { hash: approveResult, confirmations: 1, })]; case 4: _b.sent(); _b.label = 5; case 5: return [2 /*return*/]; } }); }); }; EVMConnector.isSigning = false; return EVMConnector; }()); export { EVMConnector };