@gala-chain/gala-core-sdk 1.1.0-beta.4 → 1.1.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -99,13 +99,23 @@ bridge-out request was accepted the tokens may be locked: a later failure is
99
99
  `BRIDGE_OUT_OUTCOME_UNKNOWN` (with the step, the bridge request id and the `uniqueKey`), and a
100
100
  deposit that may have been sent is `BRIDGE_DEPOSIT_OUTCOME_UNKNOWN`; neither is ever retried by the
101
101
  SDK. `WrapService` wraps a token from its own channel to the asset channel (MUSIC to GMUSIC) and
102
- back, signed natively by a `GalaChainSigner`. A `RequestTokenBridgeOut` (the bridge-out and the wrap /
102
+ back, signed natively by a `GalaChainSigner`; `createWrapService({ environment, walletAddress, signer })`
103
+ builds one for `prod` or `stage` (the environment supplies the GalaConnect, swap backend and gateway URLs;
104
+ an option overrides any), so a wrap needs no product SDK. A `RequestTokenBridgeOut` (the bridge-out and the wrap /
103
105
  unwrap) signs the TOKEN contract's `dtoOperation`
104
106
  (`asset-channel_basic-asset_GalaChainToken:RequestTokenBridgeOut`, not the bridge contract's, which the
105
107
  chain refuses; verified on stage, 2026-11-02) as a field of its EIP-712 `GalaTransaction` type, so the
106
108
  signature covers it; `buildBridgeOutPayload` does this. The wrap / unwrap request carries no `wrap` field:
107
- the contract's `RequestTokenBridgeOut` DTO has none (strict validation refuses it) and the direction is the
108
- destination channel id. The other bridge strings, all seen live on stage and signed by GalaConnect (this SDK
109
+ the chain's strict validation refuses it and the direction is the destination channel id (verified on stage in
110
+ both directions). It is signed under the SOURCE channel's token contract
111
+ (`asset-channel_basic-asset_GalaChainToken:RequestTokenBridgeOut` to unwrap, `music_music_GalaChainToken:RequestTokenBridgeOut`
112
+ to wrap; the gateway answers a wrong operation with a bare 401 `UNAUTHORIZED`) and goes through the swap backend's
113
+ chain route (`GalaConnectClient.requestWrapOut`, which needs the client's `dexBackendBaseUrl`: the backend root
114
+ without `/api/v1`; it checks the signer, the amount and the operation, then relays to the chain), not through
115
+ GalaConnect's `/RequestTokenBridgeOut`, which only treats a request as a wrap when it carries the `wrap: true` the
116
+ chain refuses. Only the completion
117
+ (`BridgeTokenOut`) is GalaConnect's: the chain wants the bridge authority's signature for it (a user-signed one is
118
+ refused with `NOT_A_BRIDGE_AUTHORITY`). The other bridge strings, all seen live on stage and signed by GalaConnect (this SDK
109
119
  signs none of them): `BridgeTokenOut` on `/asset/bridge-contract` is
110
120
  `asset-channel_bridge-chaincode_BridgeContract:BridgeTokenOut` (the operation table's bridge-contract entry
111
121
  is right for it), and the grants embedded in a `BridgeTokenOut` sign
@@ -292,7 +302,7 @@ gzipped, before `ethers` and `@solana/*`) are loaded only by an app that imports
292
302
 
293
303
  ## Status
294
304
 
295
- <!-- docs:var version -->1.1.0-beta.4<!-- /docs:var --> (rc.7: `eth|0x<hex>` is refused as `INVALID_ETH_USER_REF_PREFIX`, not an address form; rc.6: `fetchPublicKey`, MetaMask and Trust Wallet detection, silent `restore()`;
305
+ <!-- docs:var version -->1.1.0-beta.6<!-- /docs:var --> (rc.7: `eth|0x<hex>` is refused as `INVALID_ETH_USER_REF_PREFIX`, not an address form; rc.6: `fetchPublicKey`, MetaMask and Trust Wallet detection, silent `restore()`;
296
306
  rc.5: every Ethereum account a read, write or GalaConnect client sends is
297
307
  `eth|<EIP-55>`, the only spelling the gateway accepts, and `galaChainUserRef` is exported; rc.4:
298
308
  `bridgeableTokenService` is required). In: signing, signers, transport and the error model (rc.0, from gswap-sdk); the
package/dist/bridge.cjs CHANGED
@@ -1345,6 +1345,18 @@ function uniqueKeyOf(payload) {
1345
1345
  const key = payload["uniqueKey"];
1346
1346
  return typeof key === "string" && key !== "" ? key : void 0;
1347
1347
  }
1348
+ /**
1349
+ * The chain's own answer inside the swap backend's chain-route envelope
1350
+ * (`{ status, data: { transactionId, blockNumber, mode, result: { Data, Status } } }`); an answer
1351
+ * without that shape is returned as it is, for the caller's checks to refuse.
1352
+ */
1353
+ function chainResultOf(answer) {
1354
+ if (typeof answer !== "object" || answer === null) return answer;
1355
+ const data = answer["data"];
1356
+ if (typeof data !== "object" || data === null) return answer;
1357
+ const result = data["result"];
1358
+ return typeof result === "object" && result !== null ? result : answer;
1359
+ }
1348
1360
  /** `GALACONNECT_HTTP_ERROR` for an answer of status `status` and body text `text`. */
1349
1361
  function httpFailure(op, url, status, text) {
1350
1362
  const body = safeParseJson(text, { rawBody: text.slice(0, MAX_ERROR_BODY_LENGTH) });
@@ -1435,6 +1447,7 @@ function shouldRetryAttempt(error, op) {
1435
1447
  var GalaConnectClient = class {
1436
1448
  baseUrl;
1437
1449
  galachainBaseUrl;
1450
+ dexBackendBaseUrl;
1438
1451
  walletAddress;
1439
1452
  rateLimiter;
1440
1453
  defaultHeaders;
@@ -1457,6 +1470,7 @@ var GalaConnectClient = class {
1457
1470
  this.requestTimeoutMs = (0, _gala_chain_gala_core_sdk.validateTimeoutMs)(config.requestTimeoutMs ?? DEFAULT_CONFIG.requestTimeoutMs, "requestTimeoutMs");
1458
1471
  this.baseUrl = assertHttpUrlOption(config.baseUrl ?? DEFAULT_CONFIG.baseUrl, "baseUrl");
1459
1472
  this.galachainBaseUrl = assertHttpUrlOption(config.galachainBaseUrl ?? DEFAULT_CONFIG.galachainBaseUrl, "galachainBaseUrl");
1473
+ this.dexBackendBaseUrl = config.dexBackendBaseUrl === void 0 ? void 0 : assertHttpUrlOption(config.dexBackendBaseUrl, "dexBackendBaseUrl").replace(/\/+$/u, "");
1460
1474
  this.walletAddress = config.walletAddress === "" ? "" : (0, _gala_chain_gala_core_sdk.galaChainUserRef)(config.walletAddress, "walletAddress");
1461
1475
  this.rateLimiter = new RateLimiter(config.requestsPerSecond ?? DEFAULT_CONFIG.requestsPerSecond);
1462
1476
  this.requestor = config.httpRequestor ?? ((url, init) => globalThis.fetch(url, init));
@@ -1554,6 +1568,34 @@ var GalaConnectClient = class {
1554
1568
  }, payload);
1555
1569
  }
1556
1570
  /**
1571
+ * Request a wrap or unwrap: the natively signed `RequestTokenBridgeOut` on the SOURCE channel's
1572
+ * token contract, sent through the swap backend's chain route
1573
+ * (`<dexBackendBaseUrl>/v1/chain/<channel>/token-contract/RequestTokenBridgeOut`), which checks
1574
+ * the signer, the amount and the `dtoOperation` before it relays the request to the chain.
1575
+ *
1576
+ * A wrap is a channel move, so it is not a GalaConnect request: the message names the
1577
+ * destination channel (`destinationChainId`) and carries no wrap flag. GalaConnect's own
1578
+ * `/RequestTokenBridgeOut` route decides "wrap" from a `wrap: true` field that the chain refuses,
1579
+ * which is why this request does not go through it. Only the completion (`bridgeTokenOut`) is
1580
+ * GalaConnect's, because the chain wants the bridge authority's signature for it.
1581
+ *
1582
+ * @param channel - The source channel (`asset`, `music`): one lowercase path segment
1583
+ * @param payload - The signed `RequestTokenBridgeOut` body
1584
+ * @returns The chain's answer, `{ Data: <bridge request id>, Status: 1 }` (the backend's envelope
1585
+ * `{ status, data: { transactionId, result } }` is read down to `result`)
1586
+ * @throws GalaSDKError `INVALID_BRIDGE_OPTION` (`channel`) for a channel that is no path segment,
1587
+ * (`dexBackendBaseUrl`) for a client built without the swap backend's URL
1588
+ */
1589
+ async requestWrapOut(channel, payload) {
1590
+ if (!/^[a-z0-9-]+$/u.test(channel)) throw new _gala_chain_gala_core_sdk.GalaSDKError("INVALID_BRIDGE_OPTION", { option: "channel" });
1591
+ if (this.dexBackendBaseUrl === void 0) throw new _gala_chain_gala_core_sdk.GalaSDKError("INVALID_BRIDGE_OPTION", { option: "dexBackendBaseUrl" });
1592
+ return chainResultOf(await this.postJson({
1593
+ path: `/v1/chain/${channel}/token-contract/RequestTokenBridgeOut`,
1594
+ write: true,
1595
+ uniqueKey: uniqueKeyOf(payload)
1596
+ }, payload, { baseUrl: this.dexBackendBaseUrl }));
1597
+ }
1598
+ /**
1557
1599
  * Execute bridge token out (generate wormhole message).
1558
1600
  *
1559
1601
  * This is the second step in the bridge out process.
@@ -4641,8 +4683,11 @@ var WrappableTokenService = class {
4641
4683
  * Wrap Service
4642
4684
  *
4643
4685
  * Cross-channel wrap (a token from its own channel to the asset channel, e.g. MUSIC → GMUSIC) and
4644
- * unwrap (back), through GalaConnect: the natively signed `RequestTokenBridgeOut`, then
4645
- * `BridgeTokenOut` from the source channel.
4686
+ * unwrap (back): the natively signed `RequestTokenBridgeOut` goes through the swap backend's chain
4687
+ * route (`dexBackendBaseUrl`, which checks it and relays it) to the source channel's token contract
4688
+ * (the destination channel id in the message is the whole instruction; there is no wrap flag), then
4689
+ * GalaConnect completes it with `BridgeTokenOut` (the chain wants the bridge authority's signature
4690
+ * for that call).
4646
4691
  *
4647
4692
  * A wrap or unwrap that does not complete throws (nothing is returned as a "failed" result):
4648
4693
  * before anything is sent, the refusal of the input (`TOKEN_NOT_WRAPPABLE`,
@@ -4661,6 +4706,11 @@ const CHANNEL_IDS = {
4661
4706
  [ASSET_CHANNEL]: GALACHAIN_CHANNELS.ASSET,
4662
4707
  music: GALACHAIN_CHANNELS.MUSIC
4663
4708
  };
4709
+ /** The token contract a wrap or unwrap is requested on, by its source channel. */
4710
+ const WRAP_TOKEN_CONTRACT_PATHS = {
4711
+ [ASSET_CHANNEL]: "asset/token-contract",
4712
+ music: "music/token-contract"
4713
+ };
4664
4714
  /** The prefix of a wrap or unwrap uniqueKey. */
4665
4715
  const WRAP_UNIQUE_KEY_PREFIX = "galaswap-operation-";
4666
4716
  /**
@@ -4791,8 +4841,8 @@ var WrapService = class {
4791
4841
  recipient,
4792
4842
  uniqueKey
4793
4843
  };
4794
- const requestBody = await (0, _gala_chain_gala_core_sdk.signPreSend)(this.signer, BRIDGE_OUT_CONTRACT_PATH, "RequestTokenBridgeOut", message);
4795
- const response = await this.galaConnectClient.requestBridgeOut(requestBody);
4844
+ const requestBody = await (0, _gala_chain_gala_core_sdk.signPreSend)(this.signer, WRAP_TOKEN_CONTRACT_PATHS[routing.sourceChannel], "RequestTokenBridgeOut", message);
4845
+ const response = await this.galaConnectClient.requestWrapOut(routing.sourceChannel, requestBody);
4796
4846
  assertRequestAccepted("RequestTokenBridgeOut", response, uniqueKey);
4797
4847
  return {
4798
4848
  success: true,
@@ -4822,6 +4872,77 @@ function readWrapResult(bridgeOutResponse, bridgeRequestId) {
4822
4872
  return typeof hash === "string" && hash !== "" ? hash : bridgeRequestId;
4823
4873
  }
4824
4874
 
4875
+ //#endregion
4876
+ //#region src/config/environments.ts
4877
+ /**
4878
+ * The built-in endpoints of each deployment. A product SDK extends these with its own backends
4879
+ * (launchpad-sdk's launchpad and bundler URLs, gswap-sdk's swap backend); it never redefines them.
4880
+ */
4881
+ const GALA_CHAIN_ENVIRONMENTS = Object.freeze({
4882
+ prod: Object.freeze({
4883
+ gatewayBaseUrl: "https://gateway-mainnet.galachain.com",
4884
+ dexApiBaseUrl: "https://dex-api-platform-dex-prod-gala.gala.com",
4885
+ dexBackendBaseUrl: "https://dex-backend-prod1.defi.gala.com"
4886
+ }),
4887
+ stage: Object.freeze({
4888
+ gatewayBaseUrl: "https://gateway-testnet.galachain.com",
4889
+ dexApiBaseUrl: "https://dex-api-platform-dex-stage-gala.gala.com",
4890
+ dexBackendBaseUrl: "https://swap-backend.stage.defi.ovh.gala.com",
4891
+ eip712DomainName: "GalaChainStage"
4892
+ })
4893
+ });
4894
+
4895
+ //#endregion
4896
+ //#region src/bridge/tokens/createWrapService.ts
4897
+ /**
4898
+ * createWrapService
4899
+ *
4900
+ * Builds a {@link WrapService} for a deployment the way `BridgeService` builds its client: the
4901
+ * environment (`prod` by default, or `stage`) supplies the URLs, an option overrides any of them.
4902
+ *
4903
+ * @module bridge/tokens/createWrapService
4904
+ */
4905
+ /**
4906
+ * A {@link WrapService} for `options.environment`: wrap a token from its own channel to the asset
4907
+ * channel (MUSIC to GMUSIC) and unwrap it back.
4908
+ *
4909
+ * @param options - The environment, the wallet and its signer, optional URL overrides
4910
+ * @returns The wrap service, with its GalaConnect client and wrappable token list
4911
+ * @throws GalaSDKError `INVALID_BRIDGE_OPTION` (`environment`) for an environment that is not
4912
+ * `prod` or `stage`; the client's own (`walletAddress`, a URL option)
4913
+ *
4914
+ * @example
4915
+ * ```typescript
4916
+ * import { PrivateKeySigner } from '@gala-chain/gala-core-sdk';
4917
+ * import { createWrapService } from '@gala-chain/gala-core-sdk/bridge';
4918
+ *
4919
+ * const wrap = createWrapService({
4920
+ * environment: 'stage',
4921
+ * walletAddress: 'eth|70997970C51812dc3A010C7d01b50e0d17dc79C8',
4922
+ * signer: new PrivateKeySigner(privateKey),
4923
+ * });
4924
+ * const done = await wrap.unwrapToken({ tokenId: 'GMUSIC', amount: '1' });
4925
+ * ```
4926
+ */
4927
+ function createWrapService(options) {
4928
+ const environment = options.environment ?? "prod";
4929
+ const urls = Object.prototype.hasOwnProperty.call(GALA_CHAIN_ENVIRONMENTS, environment) ? GALA_CHAIN_ENVIRONMENTS[environment] : void 0;
4930
+ if (urls === void 0) throw new _gala_chain_gala_core_sdk.GalaSDKError("INVALID_BRIDGE_OPTION", { option: "environment" });
4931
+ const galaConnectClient = new GalaConnectClient({
4932
+ baseUrl: options.galaConnectBaseUrl ?? urls.dexApiBaseUrl,
4933
+ galachainBaseUrl: options.galaChainApiBaseUrl ?? urls.gatewayBaseUrl,
4934
+ dexBackendBaseUrl: options.dexBackendBaseUrl ?? urls.dexBackendBaseUrl,
4935
+ walletAddress: options.walletAddress,
4936
+ ...options.httpRequestor !== void 0 && { httpRequestor: options.httpRequestor }
4937
+ });
4938
+ return new WrapService({
4939
+ galaConnectClient,
4940
+ wrappableTokenService: new WrappableTokenService(galaConnectClient),
4941
+ walletAddress: options.walletAddress,
4942
+ ...options.signer !== void 0 && { signer: options.signer }
4943
+ });
4944
+ }
4945
+
4825
4946
  //#endregion
4826
4947
  //#region src/bridge/solanaWallet.ts
4827
4948
  /**
@@ -4922,6 +5043,7 @@ exports.WrappableTokenService = WrappableTokenService;
4922
5043
  exports.buildBridgeOutPayload = buildBridgeOutPayload;
4923
5044
  exports.compareAmounts = compareAmounts;
4924
5045
  exports.createSolanaWallet = createSolanaWallet;
5046
+ exports.createWrapService = createWrapService;
4925
5047
  exports.formatTokenAmount = formatTokenAmount;
4926
5048
  exports.formatTokenDescriptor = formatTokenDescriptor;
4927
5049
  exports.fromBaseUnits = fromBaseUnits;