transactions.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. "use strict";
  2. var __awaiter =
  3. (this && this.__awaiter) ||
  4. function (thisArg, _arguments, P, generator) {
  5. function adopt(value) {
  6. return value instanceof P
  7. ? value
  8. : new P(function (resolve) {
  9. resolve(value);
  10. });
  11. }
  12. return new (P || (P = Promise))(function (resolve, reject) {
  13. function fulfilled(value) {
  14. try {
  15. step(generator.next(value));
  16. } catch (e) {
  17. reject(e);
  18. }
  19. }
  20. function rejected(value) {
  21. try {
  22. step(generator["throw"](value));
  23. } catch (e) {
  24. reject(e);
  25. }
  26. }
  27. function step(result) {
  28. result.done
  29. ? resolve(result.value)
  30. : adopt(result.value).then(fulfilled, rejected);
  31. }
  32. step((generator = generator.apply(thisArg, _arguments || [])).next());
  33. });
  34. };
  35. var __generator =
  36. (this && this.__generator) ||
  37. function (thisArg, body) {
  38. var _ = {
  39. label: 0,
  40. sent: function () {
  41. if (t[0] & 1) throw t[1];
  42. return t[1];
  43. },
  44. trys: [],
  45. ops: [],
  46. },
  47. f,
  48. y,
  49. t,
  50. g;
  51. return (
  52. (g = { next: verb(0), throw: verb(1), return: verb(2) }),
  53. typeof Symbol === "function" &&
  54. (g[Symbol.iterator] = function () {
  55. return this;
  56. }),
  57. g
  58. );
  59. function verb(n) {
  60. return function (v) {
  61. return step([n, v]);
  62. };
  63. }
  64. function step(op) {
  65. if (f) throw new TypeError("Generator is already executing.");
  66. while ((g && ((g = 0), op[0] && (_ = 0)), _))
  67. try {
  68. if (
  69. ((f = 1),
  70. y &&
  71. (t =
  72. op[0] & 2
  73. ? y["return"]
  74. : op[0]
  75. ? y["throw"] || ((t = y["return"]) && t.call(y), 0)
  76. : y.next) &&
  77. !(t = t.call(y, op[1])).done)
  78. )
  79. return t;
  80. if (((y = 0), t)) op = [op[0] & 2, t.value];
  81. switch (op[0]) {
  82. case 0:
  83. case 1:
  84. t = op;
  85. break;
  86. case 4:
  87. _.label++;
  88. return { value: op[1], done: false };
  89. case 5:
  90. _.label++;
  91. y = op[1];
  92. op = [0];
  93. continue;
  94. case 7:
  95. op = _.ops.pop();
  96. _.trys.pop();
  97. continue;
  98. default:
  99. if (
  100. !((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
  101. (op[0] === 6 || op[0] === 2)
  102. ) {
  103. _ = 0;
  104. continue;
  105. }
  106. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
  107. _.label = op[1];
  108. break;
  109. }
  110. if (op[0] === 6 && _.label < t[1]) {
  111. _.label = t[1];
  112. t = op;
  113. break;
  114. }
  115. if (t && _.label < t[2]) {
  116. _.label = t[2];
  117. _.ops.push(op);
  118. break;
  119. }
  120. if (t[2]) _.ops.pop();
  121. _.trys.pop();
  122. continue;
  123. }
  124. op = body.call(thisArg, _);
  125. } catch (e) {
  126. op = [6, e];
  127. y = 0;
  128. } finally {
  129. f = t = 0;
  130. }
  131. if (op[0] & 5) throw op[1];
  132. return { value: op[0] ? op[1] : void 0, done: true };
  133. }
  134. };
  135. var __importDefault =
  136. (this && this.__importDefault) ||
  137. function (mod) {
  138. return mod && mod.__esModule ? mod : { default: mod };
  139. };
  140. Object.defineProperty(exports, "__esModule", { value: true });
  141. exports.ApplySystem =
  142. exports.InitializeComponent =
  143. exports.AddEntity =
  144. exports.InitializeNewWorld =
  145. void 0;
  146. var index_1 = require("../index");
  147. var bn_js_1 = __importDefault(require("bn.js"));
  148. var web3_js_1 = require("@solana/web3.js");
  149. var MAX_COMPONENTS = 5;
  150. function InitializeNewWorld(_a) {
  151. var payer = _a.payer,
  152. connection = _a.connection;
  153. return __awaiter(this, void 0, void 0, function () {
  154. var registryPda, registry, worldId, worldPda, initializeWorldIx;
  155. return __generator(this, function (_b) {
  156. switch (_b.label) {
  157. case 0:
  158. registryPda = (0, index_1.FindWorldRegistryPda)();
  159. return [
  160. 4,
  161. index_1.Registry.fromAccountAddress(connection, registryPda),
  162. ];
  163. case 1:
  164. registry = _b.sent();
  165. worldId = new bn_js_1.default(registry.worlds);
  166. worldPda = (0, index_1.FindWorldPda)(new bn_js_1.default(worldId));
  167. initializeWorldIx = (0, index_1.createInitializeNewWorldInstruction)({
  168. world: worldPda,
  169. registry: registryPda,
  170. payer: payer,
  171. });
  172. return [
  173. 2,
  174. {
  175. transaction: new web3_js_1.Transaction().add(initializeWorldIx),
  176. worldPda: worldPda,
  177. worldId: worldId,
  178. },
  179. ];
  180. }
  181. });
  182. });
  183. }
  184. exports.InitializeNewWorld = InitializeNewWorld;
  185. function AddEntity(_a) {
  186. var payer = _a.payer,
  187. worldPda = _a.worldPda,
  188. connection = _a.connection;
  189. return __awaiter(this, void 0, void 0, function () {
  190. var world, entityId, entityPda, createEntityIx;
  191. return __generator(this, function (_b) {
  192. switch (_b.label) {
  193. case 0:
  194. return [4, index_1.World.fromAccountAddress(connection, worldPda)];
  195. case 1:
  196. world = _b.sent();
  197. entityId = new bn_js_1.default(world.entities);
  198. entityPda = (0, index_1.FindEntityPda)(
  199. new bn_js_1.default(world.id),
  200. entityId
  201. );
  202. createEntityIx = (0, index_1.createAddEntityInstruction)({
  203. world: worldPda,
  204. payer: payer,
  205. entity: entityPda,
  206. });
  207. return [
  208. 2,
  209. {
  210. transaction: new web3_js_1.Transaction().add(createEntityIx),
  211. entityPda: entityPda,
  212. entityId: entityId,
  213. },
  214. ];
  215. }
  216. });
  217. });
  218. }
  219. exports.AddEntity = AddEntity;
  220. function InitializeComponent(_a) {
  221. var payer = _a.payer,
  222. entityPda = _a.entityPda,
  223. componentId = _a.componentId,
  224. _b = _a.seed,
  225. seed = _b === void 0 ? "" : _b,
  226. authority = _a.authority,
  227. anchorRemainingAccounts = _a.anchorRemainingAccounts;
  228. return __awaiter(this, void 0, void 0, function () {
  229. var componentPda, initComponentIx;
  230. return __generator(this, function (_c) {
  231. componentPda = (0, index_1.FindComponentPda)(
  232. componentId,
  233. entityPda,
  234. seed
  235. );
  236. initComponentIx = (0, index_1.createInitializeComponentInstruction)({
  237. payer: payer,
  238. entity: entityPda,
  239. data: componentPda,
  240. componentProgram: componentId,
  241. authority: authority,
  242. anchorRemainingAccounts: anchorRemainingAccounts,
  243. });
  244. return [
  245. 2,
  246. {
  247. transaction: new web3_js_1.Transaction().add(initComponentIx),
  248. componentPda: componentPda,
  249. },
  250. ];
  251. });
  252. });
  253. }
  254. exports.InitializeComponent = InitializeComponent;
  255. function ApplySystem(_a) {
  256. var authority = _a.authority,
  257. boltSystem = _a.boltSystem,
  258. entityPda = _a.entityPda,
  259. components = _a.components,
  260. _b = _a.args,
  261. args = _b === void 0 ? {} : _b,
  262. extraAccounts = _a.extraAccounts,
  263. seed = _a.seed;
  264. return __awaiter(this, void 0, void 0, function () {
  265. var instructionFunctions,
  266. componentPdas,
  267. instructionArgs,
  268. functionName,
  269. applySystemIx;
  270. return __generator(this, function (_c) {
  271. instructionFunctions = {
  272. createApplyInstruction: index_1.createApplyInstruction,
  273. createApply2Instruction: index_1.createApply2Instruction,
  274. createApply3Instruction: index_1.createApply3Instruction,
  275. createApply4Instruction: index_1.createApply4Instruction,
  276. createApply5Instruction: index_1.createApply5Instruction,
  277. };
  278. if (components.length === 0) throw new Error("No components provided");
  279. if (seed == null) seed = new Array(components.length).fill("");
  280. if (seed.length !== components.length)
  281. throw new Error("Seed length does not match components length");
  282. componentPdas = [];
  283. components.forEach(function (component) {
  284. var componentPda = (0, index_1.FindComponentPda)(
  285. component,
  286. entityPda,
  287. ""
  288. );
  289. componentPdas.push(componentPda);
  290. });
  291. if (components.length < 1 || components.length > MAX_COMPONENTS) {
  292. throw new Error(
  293. "Not implemented for component counts outside 1-".concat(
  294. MAX_COMPONENTS
  295. )
  296. );
  297. }
  298. instructionArgs = {
  299. authority: authority,
  300. boltSystem: boltSystem,
  301. anchorRemainingAccounts: extraAccounts,
  302. };
  303. components.forEach(function (component, index) {
  304. instructionArgs[getBoltComponentProgramName(index, components.length)] =
  305. component;
  306. instructionArgs[getBoltComponentName(index, components.length)] =
  307. componentPdas[index];
  308. });
  309. functionName = getApplyInstructionFunctionName(components.length);
  310. applySystemIx = instructionFunctions[functionName](instructionArgs, {
  311. args: (0, index_1.SerializeArgs)(args),
  312. });
  313. return [
  314. 2,
  315. {
  316. transaction: new web3_js_1.Transaction().add(applySystemIx),
  317. },
  318. ];
  319. });
  320. });
  321. }
  322. exports.ApplySystem = ApplySystem;
  323. function getApplyInstructionFunctionName(componentsLength) {
  324. if (componentsLength === 1) return "createApplyInstruction";
  325. return "createApply".concat(componentsLength, "Instruction");
  326. }
  327. function getBoltComponentName(index, componentsLength) {
  328. if (componentsLength === 1) return "boltComponent";
  329. return "boltComponent".concat(index + 1);
  330. }
  331. function getBoltComponentProgramName(index, componentsLength) {
  332. if (componentsLength === 1) return "componentProgram";
  333. return "componentProgram".concat(index + 1);
  334. }
  335. //# sourceMappingURL=transactions.js.map