transactions.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.createApplySystemInstruction =
  143. exports.InitializeComponent =
  144. exports.AddEntity =
  145. exports.InitializeNewWorld =
  146. void 0;
  147. var index_1 = require("../index");
  148. var bn_js_1 = __importDefault(require("bn.js"));
  149. var web3_js_1 = require("@solana/web3.js");
  150. var MAX_COMPONENTS = 5;
  151. function InitializeNewWorld(_a) {
  152. var payer = _a.payer,
  153. connection = _a.connection;
  154. return __awaiter(this, void 0, void 0, function () {
  155. var registryPda, registry, worldId, worldPda, initializeWorldIx;
  156. return __generator(this, function (_b) {
  157. switch (_b.label) {
  158. case 0:
  159. registryPda = (0, index_1.FindWorldRegistryPda)();
  160. return [
  161. 4,
  162. index_1.Registry.fromAccountAddress(connection, registryPda),
  163. ];
  164. case 1:
  165. registry = _b.sent();
  166. worldId = new bn_js_1.default(registry.worlds);
  167. worldPda = (0, index_1.FindWorldPda)(new bn_js_1.default(worldId));
  168. initializeWorldIx = (0, index_1.createInitializeNewWorldInstruction)({
  169. world: worldPda,
  170. registry: registryPda,
  171. payer: payer,
  172. });
  173. return [
  174. 2,
  175. {
  176. transaction: new web3_js_1.Transaction().add(initializeWorldIx),
  177. worldPda: worldPda,
  178. worldId: worldId,
  179. },
  180. ];
  181. }
  182. });
  183. });
  184. }
  185. exports.InitializeNewWorld = InitializeNewWorld;
  186. function AddEntity(_a) {
  187. var payer = _a.payer,
  188. world = _a.world,
  189. connection = _a.connection;
  190. return __awaiter(this, void 0, void 0, function () {
  191. var worldInstance, entityId, entityPda, createEntityIx;
  192. return __generator(this, function (_b) {
  193. switch (_b.label) {
  194. case 0:
  195. return [4, index_1.World.fromAccountAddress(connection, world)];
  196. case 1:
  197. worldInstance = _b.sent();
  198. entityId = new bn_js_1.default(worldInstance.entities);
  199. entityPda = (0, index_1.FindEntityPda)(
  200. new bn_js_1.default(worldInstance.id),
  201. entityId
  202. );
  203. createEntityIx = (0, index_1.createAddEntityInstruction)({
  204. world: world,
  205. payer: payer,
  206. entity: entityPda,
  207. });
  208. return [
  209. 2,
  210. {
  211. transaction: new web3_js_1.Transaction().add(createEntityIx),
  212. entityPda: entityPda,
  213. entityId: entityId,
  214. },
  215. ];
  216. }
  217. });
  218. });
  219. }
  220. exports.AddEntity = AddEntity;
  221. function InitializeComponent(_a) {
  222. var payer = _a.payer,
  223. entity = _a.entity,
  224. componentId = _a.componentId,
  225. _b = _a.seed,
  226. seed = _b === void 0 ? "" : _b,
  227. authority = _a.authority,
  228. anchorRemainingAccounts = _a.anchorRemainingAccounts;
  229. return __awaiter(this, void 0, void 0, function () {
  230. var componentPda, initComponentIx;
  231. return __generator(this, function (_c) {
  232. componentPda = (0, index_1.FindComponentPda)(componentId, entity, seed);
  233. initComponentIx = (0, index_1.createInitializeComponentInstruction)({
  234. payer: payer,
  235. entity: entity,
  236. data: componentPda,
  237. componentProgram: componentId,
  238. authority: authority,
  239. anchorRemainingAccounts: anchorRemainingAccounts,
  240. });
  241. return [
  242. 2,
  243. {
  244. transaction: new web3_js_1.Transaction().add(initComponentIx),
  245. componentPda: componentPda,
  246. },
  247. ];
  248. });
  249. });
  250. }
  251. exports.InitializeComponent = InitializeComponent;
  252. function createApplySystemInstruction(_a) {
  253. var entity = _a.entity,
  254. components = _a.components,
  255. system = _a.system,
  256. seeds = _a.seeds,
  257. authority = _a.authority,
  258. extraAccounts = _a.extraAccounts,
  259. args = _a.args;
  260. var instructionFunctions = {
  261. createApplyInstruction: index_1.createApplyInstruction,
  262. createApply2Instruction: index_1.createApply2Instruction,
  263. createApply3Instruction: index_1.createApply3Instruction,
  264. createApply4Instruction: index_1.createApply4Instruction,
  265. createApply5Instruction: index_1.createApply5Instruction,
  266. };
  267. if (components.length === 0) throw new Error("No components provided");
  268. if (seeds == null) seeds = new Array(components.length).fill("");
  269. if (seeds.length !== components.length)
  270. throw new Error("Seed length does not match components length");
  271. var componentPdas = [];
  272. for (var i = 0; i < components.length; i++) {
  273. var componentPda = (0, index_1.FindComponentPda)(
  274. components[i],
  275. entity,
  276. seeds[i]
  277. );
  278. componentPdas.push(componentPda);
  279. }
  280. if (components.length < 1 || components.length > MAX_COMPONENTS) {
  281. throw new Error(
  282. "Not implemented for component counts outside 1-".concat(MAX_COMPONENTS)
  283. );
  284. }
  285. var instructionArgs = {
  286. authority: authority,
  287. boltSystem: system,
  288. anchorRemainingAccounts: extraAccounts,
  289. };
  290. components.forEach(function (component, index) {
  291. instructionArgs[getBoltComponentProgramName(index, components.length)] =
  292. component;
  293. instructionArgs[getBoltComponentName(index, components.length)] =
  294. componentPdas[index];
  295. });
  296. var functionName = getApplyInstructionFunctionName(components.length);
  297. return instructionFunctions[functionName](instructionArgs, {
  298. args: (0, index_1.SerializeArgs)(args),
  299. });
  300. }
  301. exports.createApplySystemInstruction = createApplySystemInstruction;
  302. function ApplySystem(_a) {
  303. var authority = _a.authority,
  304. system = _a.system,
  305. entity = _a.entity,
  306. components = _a.components,
  307. _b = _a.args,
  308. args = _b === void 0 ? {} : _b,
  309. extraAccounts = _a.extraAccounts,
  310. seeds = _a.seeds;
  311. return __awaiter(this, void 0, void 0, function () {
  312. var applySystemIx;
  313. return __generator(this, function (_c) {
  314. applySystemIx = createApplySystemInstruction({
  315. entity: entity,
  316. components: components,
  317. system: system,
  318. authority: authority,
  319. seeds: seeds,
  320. extraAccounts: extraAccounts,
  321. args: args,
  322. });
  323. return [
  324. 2,
  325. {
  326. transaction: new web3_js_1.Transaction().add(applySystemIx),
  327. },
  328. ];
  329. });
  330. });
  331. }
  332. exports.ApplySystem = ApplySystem;
  333. function getApplyInstructionFunctionName(componentsLength) {
  334. if (componentsLength === 1) return "createApplyInstruction";
  335. return "createApply".concat(componentsLength, "Instruction");
  336. }
  337. function getBoltComponentName(index, componentsLength) {
  338. if (componentsLength === 1) return "boltComponent";
  339. return "boltComponent".concat(index + 1);
  340. }
  341. function getBoltComponentProgramName(index, componentsLength) {
  342. if (componentsLength === 1) return "componentProgram";
  343. return "componentProgram".concat(index + 1);
  344. }
  345. //# sourceMappingURL=transactions.js.map