@weblock-wallet/sdk 0.1.38 → 0.1.40

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/dist/index.js CHANGED
@@ -104106,6 +104106,18 @@ var ERC20_ABI = [
104106
104106
  "function decimals() view returns (uint8)",
104107
104107
  "function symbol() view returns (string)"
104108
104108
  ];
104109
+ var hexToBigInt = (hex) => BigInt(hex);
104110
+ var pow10 = (d4) => [...Array(d4)].reduce((n5) => n5 * 10n, 1n);
104111
+ var ERC20_ABI_MIN = [
104112
+ "function decimals() view returns (uint8)",
104113
+ "function symbol() view returns (string)",
104114
+ "function approve(address spender, uint256 amount) returns (bool)",
104115
+ "function transfer(address to, uint256 amount) returns (bool)",
104116
+ "function allowance(address owner, address spender) view returns (uint256)"
104117
+ ];
104118
+ var RBT_ABI_MIN = ["function mint(address to, uint256 amount)"];
104119
+ var erc20Iface = new Interface(ERC20_ABI_MIN);
104120
+ var rbtIface = new Interface(RBT_ABI_MIN);
104109
104121
  var WalletService = class {
104110
104122
  constructor(walletClient, rpcClient, orgHost, networkService) {
104111
104123
  this.walletClient = walletClient;
@@ -104360,6 +104372,24 @@ var WalletService = class {
104360
104372
  symbol
104361
104373
  };
104362
104374
  }
104375
+ /** "123.45" → wei(BigInt) */
104376
+ humanToRaw(amount, decimals) {
104377
+ const [intStr, fracStrRaw = ""] = amount.split(".");
104378
+ const fracStr = fracStrRaw.padEnd(decimals, "0").slice(0, decimals);
104379
+ return BigInt(intStr || "0") * pow10(decimals) + BigInt(fracStr || "0");
104380
+ }
104381
+ /** on-chain ERC20 decimals 조회 */
104382
+ async fetchErc20Decimals(call, token, chainId) {
104383
+ const data = erc20Iface.encodeFunctionData("decimals", []);
104384
+ const res = await call({ to: token, data }, "latest", chainId);
104385
+ return parseInt(res, 16);
104386
+ }
104387
+ /** allowance 조회 */
104388
+ async fetchAllowance(call, token, owner, spender, chainId) {
104389
+ const data = erc20Iface.encodeFunctionData("allowance", [owner, spender]);
104390
+ const res = await call({ to: token, data }, "latest", chainId);
104391
+ return hexToBigInt(res);
104392
+ }
104363
104393
  async getTransactionCount(address, chainId) {
104364
104394
  const response = await this.rpcClient.sendRpc({
104365
104395
  chainId,
@@ -105951,6 +105981,10 @@ var InternalCoreImpl = class {
105951
105981
  retrieveWallet: (password) => this.walletService.retrieveWallet(password),
105952
105982
  getBalance: (address, chainId) => this.walletService.getBalance(address, chainId),
105953
105983
  getTokenBalance: (tokenAddress, walletAddress, chainId) => this.walletService.getTokenBalance(tokenAddress, walletAddress, chainId),
105984
+ humanToRaw: (amount, decimals) => this.walletService.humanToRaw(amount, decimals),
105985
+ fetchErc20Decimals: (call, token, chainId) => this.walletService.fetchErc20Decimals(call, token, chainId),
105986
+ fetchAllowance: (call, token, owner, spender, chainId) => this.walletService.fetchAllowance(call, token, owner, spender, chainId),
105987
+ sendTransaction: (params) => this.walletService.sendTransaction(params),
105954
105988
  getTransactionCount: (address, chainId) => this.walletService.getTransactionCount(address, chainId),
105955
105989
  getBlockNumber: (chainId) => this.walletService.getBlockNumber(chainId),
105956
105990
  sendRawTransaction: (signedTx, chainId) => this.walletService.sendRawTransaction(signedTx, chainId),
@@ -106078,6 +106112,19 @@ var UserModule = class {
106078
106112
  };
106079
106113
 
106080
106114
  // src/modules/wallet.ts
106115
+ import { Interface as Interface3 } from "ethers";
106116
+ var bigIntToHex = (v5) => "0x" + v5.toString(16);
106117
+ var pow102 = (d4) => [...Array(d4)].reduce((n5) => n5 * 10n, 1n);
106118
+ var ERC20_ABI_MIN2 = [
106119
+ "function decimals() view returns (uint8)",
106120
+ "function symbol() view returns (string)",
106121
+ "function approve(address spender, uint256 amount) returns (bool)",
106122
+ "function transfer(address to, uint256 amount) returns (bool)",
106123
+ "function allowance(address owner, address spender) view returns (uint256)"
106124
+ ];
106125
+ var RBT_ABI_MIN2 = ["function mint(address to, uint256 amount)"];
106126
+ var erc20Iface2 = new Interface3(ERC20_ABI_MIN2);
106127
+ var rbtIface2 = new Interface3(RBT_ABI_MIN2);
106081
106128
  var WalletModule = class {
106082
106129
  constructor(options, core) {
106083
106130
  this.options = options;
@@ -106167,7 +106214,40 @@ var WalletModule = class {
106167
106214
  balance: nativeBalance,
106168
106215
  decimals: 18
106169
106216
  },
106170
- tokens: [],
106217
+ tokens: [
106218
+ {
106219
+ symbol: rbtBalance.symbol,
106220
+ name: "Real-estate Backed Token",
106221
+ address: "0xB10536cC40Cb6E6415f70d3a4C1AF7Fa638AE829",
106222
+ balance: rbtBalance,
106223
+ decimals: rbtBalance.decimals,
106224
+ totalSupply: rbtBalance
106225
+ },
106226
+ {
106227
+ symbol: usdrBalance.symbol,
106228
+ name: "USD Real-estate",
106229
+ address: "0x8d335fe5B30e27F2B21F057a4766cf4BB8c30785",
106230
+ balance: usdrBalance,
106231
+ decimals: usdrBalance.decimals,
106232
+ totalSupply: usdrBalance
106233
+ },
106234
+ {
106235
+ symbol: wftBalance.symbol,
106236
+ name: "WeBlock Foundation Token",
106237
+ address: "0x6fa62Eda03956ef4E54f3C8597E8c3f3bE40A45B",
106238
+ balance: wftBalance,
106239
+ decimals: wftBalance.decimals,
106240
+ totalSupply: wftBalance
106241
+ },
106242
+ {
106243
+ symbol: usdtBalance.symbol,
106244
+ name: "USD Tether",
106245
+ address: "0xfF54B9ebe777f528E64C74bc95c68433B7546038",
106246
+ balance: usdtBalance,
106247
+ decimals: usdtBalance.decimals,
106248
+ totalSupply: usdtBalance
106249
+ }
106250
+ ],
106171
106251
  nfts: [],
106172
106252
  securities: []
106173
106253
  },
@@ -106184,6 +106264,142 @@ var WalletModule = class {
106184
106264
  );
106185
106265
  }
106186
106266
  }
106267
+ /**
106268
+ * USDT 10개당 1 RBT 민트 규칙으로 투자 실행
106269
+ * mode: 'direct' → caller가 MINTER_ROLE을 가진 어드민 지갑일 때 바로 RBT.mint 실행
106270
+ * mode: 'backend' → USDT를 트레저리로 보낸 뒤, 백엔드가 민팅하도록 payload 반환
106271
+ */
106272
+ async investUsdtForRbt10to1(opts) {
106273
+ const { usdtAddress, rbtAddress, treasuryAddress, amountUsdt, chainId } = opts;
106274
+ const mode = opts.mode ?? "backend";
106275
+ const isLoggedIn = await this.core.auth.isLoggedIn();
106276
+ if (!isLoggedIn)
106277
+ throw new SDKError("\uB85C\uADF8\uC778\uC774 \uD544\uC694\uD569\uB2C8\uB2E4", "NOT_LOGGED_IN" /* NOT_LOGGED_IN */);
106278
+ const from = await this.core.wallet.getAddress();
106279
+ if (!from)
106280
+ throw new SDKError("\uC9C0\uAC11 \uC8FC\uC18C \uC5C6\uC74C", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
106281
+ const network = await this.core.network.getCurrentNetwork();
106282
+ if (!network || network.chainId !== chainId) {
106283
+ await this.core.network.switchNetwork(chainId.toString());
106284
+ }
106285
+ const usdtDecimals = await this.core.wallet.fetchErc20Decimals(
106286
+ this.call.bind(this),
106287
+ usdtAddress,
106288
+ chainId
106289
+ );
106290
+ const rbtDecimals = await this.core.wallet.fetchErc20Decimals(
106291
+ this.call.bind(this),
106292
+ rbtAddress,
106293
+ chainId
106294
+ );
106295
+ const usdtRaw = this.core.wallet.humanToRaw(amountUsdt, usdtDecimals);
106296
+ if (rbtDecimals < usdtDecimals) {
106297
+ throw new SDKError(
106298
+ "RBT decimals < USDT decimals\uB294 \uC9C0\uC6D0\uD558\uC9C0 \uC54A\uC74C",
106299
+ "INVALID_PARAMS" /* INVALID_PARAMS */
106300
+ );
106301
+ }
106302
+ const scale = pow102(rbtDecimals - usdtDecimals);
106303
+ const rbtRaw = usdtRaw * scale / 10n;
106304
+ const rbtAmountRawHex = bigIntToHex(rbtRaw);
106305
+ let usdtApproveTxHash;
106306
+ const allowance = await this.core.wallet.fetchAllowance(
106307
+ this.call.bind(this),
106308
+ usdtAddress,
106309
+ from,
106310
+ treasuryAddress,
106311
+ chainId
106312
+ );
106313
+ if (allowance < usdtRaw) {
106314
+ const approveData = erc20Iface2.encodeFunctionData("approve", [
106315
+ treasuryAddress,
106316
+ bigIntToHex(usdtRaw)
106317
+ ]);
106318
+ const gasLimit = await this.estimateGas(
106319
+ { from, to: usdtAddress, data: approveData, value: "0x0" },
106320
+ chainId
106321
+ ).catch(() => void 0);
106322
+ usdtApproveTxHash = await this.core.wallet.sendTransaction({
106323
+ to: usdtAddress,
106324
+ data: approveData,
106325
+ value: "0x0",
106326
+ chainId
106327
+ });
106328
+ }
106329
+ const transferData = erc20Iface2.encodeFunctionData("transfer", [
106330
+ treasuryAddress,
106331
+ bigIntToHex(usdtRaw)
106332
+ ]);
106333
+ const transferGas = await this.estimateGas(
106334
+ { from, to: usdtAddress, data: transferData, value: "0x0" },
106335
+ chainId
106336
+ ).catch(() => void 0);
106337
+ const usdtTransferTxHash = await this.core.wallet.sendTransaction({
106338
+ to: usdtAddress,
106339
+ data: transferData,
106340
+ value: "0x0",
106341
+ chainId
106342
+ });
106343
+ const to = opts.recipient || from;
106344
+ if (mode === "direct") {
106345
+ const mintData = rbtIface2.encodeFunctionData("mint", [
106346
+ to,
106347
+ rbtAmountRawHex
106348
+ ]);
106349
+ const mintGas = await this.estimateGas(
106350
+ { from, to: rbtAddress, data: mintData, value: "0x0" },
106351
+ chainId
106352
+ ).catch(() => void 0);
106353
+ const rbtMintTxHash = await this.core.wallet.sendTransaction({
106354
+ to: rbtAddress,
106355
+ data: mintData,
106356
+ value: "0x0",
106357
+ chainId
106358
+ });
106359
+ return {
106360
+ usdtApproveTxHash,
106361
+ usdtTransferTxHash,
106362
+ rbtMintTxHash,
106363
+ rbtAmountRaw: rbtAmountRawHex,
106364
+ rbtAmountHuman: `${(Number(rbtRaw) / Number(pow102(rbtDecimals))).toString()}`,
106365
+ note: "direct: \uD638\uCD9C \uC9C0\uAC11\uC774 MINTER_ROLE\uC744 \uBCF4\uC720\uD558\uBBC0\uB85C \uC628\uCCB4\uC778\uC5D0\uC11C \uC989\uC2DC RBT mint \uC644\uB8CC"
106366
+ };
106367
+ }
106368
+ return {
106369
+ usdtApproveTxHash,
106370
+ usdtTransferTxHash,
106371
+ rbtAmountRaw: rbtAmountRawHex,
106372
+ rbtAmountHuman: `${(Number(rbtRaw) / Number(pow102(rbtDecimals))).toString()}`,
106373
+ note: "backend: USDT \uC785\uAE08 \uC644\uB8CC. \uBC31\uC5D4\uB4DC(\uBBFC\uD130 \uD0A4 \uBCF4\uC720)\uAC00 RBT.mint(to, amount) \uD638\uCD9C \uD544\uC694"
106374
+ };
106375
+ }
106376
+ /**
106377
+ * (옵션) RBT 민트를 단독으로 호출하는 유틸
106378
+ * - 호출 지갑이 MINTER_ROLE을 보유해야 성공
106379
+ */
106380
+ async mintRbtDirect(opts) {
106381
+ const { rbtAddress, to, amountRbtHuman, chainId } = opts;
106382
+ const from = await this.core.wallet.getAddress();
106383
+ if (!from)
106384
+ throw new SDKError("\uC9C0\uAC11 \uC8FC\uC18C \uC5C6\uC74C", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
106385
+ const rbtDecimals = await this.core.wallet.fetchErc20Decimals(
106386
+ this.call.bind(this),
106387
+ rbtAddress,
106388
+ chainId
106389
+ );
106390
+ const rbtRaw = this.core.wallet.humanToRaw(amountRbtHuman, rbtDecimals);
106391
+ const data = rbtIface2.encodeFunctionData("mint", [to, bigIntToHex(rbtRaw)]);
106392
+ const gasLimit = await this.estimateGas(
106393
+ { from, to: rbtAddress, data, value: "0x0" },
106394
+ chainId
106395
+ ).catch(() => void 0);
106396
+ return this.core.wallet.sendTransaction({
106397
+ to: rbtAddress,
106398
+ data,
106399
+ value: "0x0",
106400
+ chainId
106401
+ });
106402
+ }
106187
106403
  async getBalance(address, chainId) {
106188
106404
  return this.core.wallet.getBalance(address, chainId);
106189
106405
  }