@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.cjs +217 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +217 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -104151,6 +104151,18 @@ var ERC20_ABI = [
|
|
|
104151
104151
|
"function decimals() view returns (uint8)",
|
|
104152
104152
|
"function symbol() view returns (string)"
|
|
104153
104153
|
];
|
|
104154
|
+
var hexToBigInt = (hex) => BigInt(hex);
|
|
104155
|
+
var pow10 = (d4) => [...Array(d4)].reduce((n5) => n5 * 10n, 1n);
|
|
104156
|
+
var ERC20_ABI_MIN = [
|
|
104157
|
+
"function decimals() view returns (uint8)",
|
|
104158
|
+
"function symbol() view returns (string)",
|
|
104159
|
+
"function approve(address spender, uint256 amount) returns (bool)",
|
|
104160
|
+
"function transfer(address to, uint256 amount) returns (bool)",
|
|
104161
|
+
"function allowance(address owner, address spender) view returns (uint256)"
|
|
104162
|
+
];
|
|
104163
|
+
var RBT_ABI_MIN = ["function mint(address to, uint256 amount)"];
|
|
104164
|
+
var erc20Iface = new import_ethers2.Interface(ERC20_ABI_MIN);
|
|
104165
|
+
var rbtIface = new import_ethers2.Interface(RBT_ABI_MIN);
|
|
104154
104166
|
var WalletService = class {
|
|
104155
104167
|
constructor(walletClient, rpcClient, orgHost, networkService) {
|
|
104156
104168
|
this.walletClient = walletClient;
|
|
@@ -104405,6 +104417,24 @@ var WalletService = class {
|
|
|
104405
104417
|
symbol
|
|
104406
104418
|
};
|
|
104407
104419
|
}
|
|
104420
|
+
/** "123.45" → wei(BigInt) */
|
|
104421
|
+
humanToRaw(amount, decimals) {
|
|
104422
|
+
const [intStr, fracStrRaw = ""] = amount.split(".");
|
|
104423
|
+
const fracStr = fracStrRaw.padEnd(decimals, "0").slice(0, decimals);
|
|
104424
|
+
return BigInt(intStr || "0") * pow10(decimals) + BigInt(fracStr || "0");
|
|
104425
|
+
}
|
|
104426
|
+
/** on-chain ERC20 decimals 조회 */
|
|
104427
|
+
async fetchErc20Decimals(call, token, chainId) {
|
|
104428
|
+
const data = erc20Iface.encodeFunctionData("decimals", []);
|
|
104429
|
+
const res = await call({ to: token, data }, "latest", chainId);
|
|
104430
|
+
return parseInt(res, 16);
|
|
104431
|
+
}
|
|
104432
|
+
/** allowance 조회 */
|
|
104433
|
+
async fetchAllowance(call, token, owner, spender, chainId) {
|
|
104434
|
+
const data = erc20Iface.encodeFunctionData("allowance", [owner, spender]);
|
|
104435
|
+
const res = await call({ to: token, data }, "latest", chainId);
|
|
104436
|
+
return hexToBigInt(res);
|
|
104437
|
+
}
|
|
104408
104438
|
async getTransactionCount(address, chainId) {
|
|
104409
104439
|
const response = await this.rpcClient.sendRpc({
|
|
104410
104440
|
chainId,
|
|
@@ -105992,6 +106022,10 @@ var InternalCoreImpl = class {
|
|
|
105992
106022
|
retrieveWallet: (password) => this.walletService.retrieveWallet(password),
|
|
105993
106023
|
getBalance: (address, chainId) => this.walletService.getBalance(address, chainId),
|
|
105994
106024
|
getTokenBalance: (tokenAddress, walletAddress, chainId) => this.walletService.getTokenBalance(tokenAddress, walletAddress, chainId),
|
|
106025
|
+
humanToRaw: (amount, decimals) => this.walletService.humanToRaw(amount, decimals),
|
|
106026
|
+
fetchErc20Decimals: (call, token, chainId) => this.walletService.fetchErc20Decimals(call, token, chainId),
|
|
106027
|
+
fetchAllowance: (call, token, owner, spender, chainId) => this.walletService.fetchAllowance(call, token, owner, spender, chainId),
|
|
106028
|
+
sendTransaction: (params) => this.walletService.sendTransaction(params),
|
|
105995
106029
|
getTransactionCount: (address, chainId) => this.walletService.getTransactionCount(address, chainId),
|
|
105996
106030
|
getBlockNumber: (chainId) => this.walletService.getBlockNumber(chainId),
|
|
105997
106031
|
sendRawTransaction: (signedTx, chainId) => this.walletService.sendRawTransaction(signedTx, chainId),
|
|
@@ -106119,6 +106153,19 @@ var UserModule = class {
|
|
|
106119
106153
|
};
|
|
106120
106154
|
|
|
106121
106155
|
// src/modules/wallet.ts
|
|
106156
|
+
var import_ethers4 = require("ethers");
|
|
106157
|
+
var bigIntToHex = (v5) => "0x" + v5.toString(16);
|
|
106158
|
+
var pow102 = (d4) => [...Array(d4)].reduce((n5) => n5 * 10n, 1n);
|
|
106159
|
+
var ERC20_ABI_MIN2 = [
|
|
106160
|
+
"function decimals() view returns (uint8)",
|
|
106161
|
+
"function symbol() view returns (string)",
|
|
106162
|
+
"function approve(address spender, uint256 amount) returns (bool)",
|
|
106163
|
+
"function transfer(address to, uint256 amount) returns (bool)",
|
|
106164
|
+
"function allowance(address owner, address spender) view returns (uint256)"
|
|
106165
|
+
];
|
|
106166
|
+
var RBT_ABI_MIN2 = ["function mint(address to, uint256 amount)"];
|
|
106167
|
+
var erc20Iface2 = new import_ethers4.Interface(ERC20_ABI_MIN2);
|
|
106168
|
+
var rbtIface2 = new import_ethers4.Interface(RBT_ABI_MIN2);
|
|
106122
106169
|
var WalletModule = class {
|
|
106123
106170
|
constructor(options, core) {
|
|
106124
106171
|
this.options = options;
|
|
@@ -106208,7 +106255,40 @@ var WalletModule = class {
|
|
|
106208
106255
|
balance: nativeBalance,
|
|
106209
106256
|
decimals: 18
|
|
106210
106257
|
},
|
|
106211
|
-
tokens: [
|
|
106258
|
+
tokens: [
|
|
106259
|
+
{
|
|
106260
|
+
symbol: rbtBalance.symbol,
|
|
106261
|
+
name: "Real-estate Backed Token",
|
|
106262
|
+
address: "0xB10536cC40Cb6E6415f70d3a4C1AF7Fa638AE829",
|
|
106263
|
+
balance: rbtBalance,
|
|
106264
|
+
decimals: rbtBalance.decimals,
|
|
106265
|
+
totalSupply: rbtBalance
|
|
106266
|
+
},
|
|
106267
|
+
{
|
|
106268
|
+
symbol: usdrBalance.symbol,
|
|
106269
|
+
name: "USD Real-estate",
|
|
106270
|
+
address: "0x8d335fe5B30e27F2B21F057a4766cf4BB8c30785",
|
|
106271
|
+
balance: usdrBalance,
|
|
106272
|
+
decimals: usdrBalance.decimals,
|
|
106273
|
+
totalSupply: usdrBalance
|
|
106274
|
+
},
|
|
106275
|
+
{
|
|
106276
|
+
symbol: wftBalance.symbol,
|
|
106277
|
+
name: "WeBlock Foundation Token",
|
|
106278
|
+
address: "0x6fa62Eda03956ef4E54f3C8597E8c3f3bE40A45B",
|
|
106279
|
+
balance: wftBalance,
|
|
106280
|
+
decimals: wftBalance.decimals,
|
|
106281
|
+
totalSupply: wftBalance
|
|
106282
|
+
},
|
|
106283
|
+
{
|
|
106284
|
+
symbol: usdtBalance.symbol,
|
|
106285
|
+
name: "USD Tether",
|
|
106286
|
+
address: "0xfF54B9ebe777f528E64C74bc95c68433B7546038",
|
|
106287
|
+
balance: usdtBalance,
|
|
106288
|
+
decimals: usdtBalance.decimals,
|
|
106289
|
+
totalSupply: usdtBalance
|
|
106290
|
+
}
|
|
106291
|
+
],
|
|
106212
106292
|
nfts: [],
|
|
106213
106293
|
securities: []
|
|
106214
106294
|
},
|
|
@@ -106225,6 +106305,142 @@ var WalletModule = class {
|
|
|
106225
106305
|
);
|
|
106226
106306
|
}
|
|
106227
106307
|
}
|
|
106308
|
+
/**
|
|
106309
|
+
* USDT 10개당 1 RBT 민트 규칙으로 투자 실행
|
|
106310
|
+
* mode: 'direct' → caller가 MINTER_ROLE을 가진 어드민 지갑일 때 바로 RBT.mint 실행
|
|
106311
|
+
* mode: 'backend' → USDT를 트레저리로 보낸 뒤, 백엔드가 민팅하도록 payload 반환
|
|
106312
|
+
*/
|
|
106313
|
+
async investUsdtForRbt10to1(opts) {
|
|
106314
|
+
const { usdtAddress, rbtAddress, treasuryAddress, amountUsdt, chainId } = opts;
|
|
106315
|
+
const mode = opts.mode ?? "backend";
|
|
106316
|
+
const isLoggedIn = await this.core.auth.isLoggedIn();
|
|
106317
|
+
if (!isLoggedIn)
|
|
106318
|
+
throw new SDKError("\uB85C\uADF8\uC778\uC774 \uD544\uC694\uD569\uB2C8\uB2E4", "NOT_LOGGED_IN" /* NOT_LOGGED_IN */);
|
|
106319
|
+
const from = await this.core.wallet.getAddress();
|
|
106320
|
+
if (!from)
|
|
106321
|
+
throw new SDKError("\uC9C0\uAC11 \uC8FC\uC18C \uC5C6\uC74C", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
|
|
106322
|
+
const network = await this.core.network.getCurrentNetwork();
|
|
106323
|
+
if (!network || network.chainId !== chainId) {
|
|
106324
|
+
await this.core.network.switchNetwork(chainId.toString());
|
|
106325
|
+
}
|
|
106326
|
+
const usdtDecimals = await this.core.wallet.fetchErc20Decimals(
|
|
106327
|
+
this.call.bind(this),
|
|
106328
|
+
usdtAddress,
|
|
106329
|
+
chainId
|
|
106330
|
+
);
|
|
106331
|
+
const rbtDecimals = await this.core.wallet.fetchErc20Decimals(
|
|
106332
|
+
this.call.bind(this),
|
|
106333
|
+
rbtAddress,
|
|
106334
|
+
chainId
|
|
106335
|
+
);
|
|
106336
|
+
const usdtRaw = this.core.wallet.humanToRaw(amountUsdt, usdtDecimals);
|
|
106337
|
+
if (rbtDecimals < usdtDecimals) {
|
|
106338
|
+
throw new SDKError(
|
|
106339
|
+
"RBT decimals < USDT decimals\uB294 \uC9C0\uC6D0\uD558\uC9C0 \uC54A\uC74C",
|
|
106340
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */
|
|
106341
|
+
);
|
|
106342
|
+
}
|
|
106343
|
+
const scale = pow102(rbtDecimals - usdtDecimals);
|
|
106344
|
+
const rbtRaw = usdtRaw * scale / 10n;
|
|
106345
|
+
const rbtAmountRawHex = bigIntToHex(rbtRaw);
|
|
106346
|
+
let usdtApproveTxHash;
|
|
106347
|
+
const allowance = await this.core.wallet.fetchAllowance(
|
|
106348
|
+
this.call.bind(this),
|
|
106349
|
+
usdtAddress,
|
|
106350
|
+
from,
|
|
106351
|
+
treasuryAddress,
|
|
106352
|
+
chainId
|
|
106353
|
+
);
|
|
106354
|
+
if (allowance < usdtRaw) {
|
|
106355
|
+
const approveData = erc20Iface2.encodeFunctionData("approve", [
|
|
106356
|
+
treasuryAddress,
|
|
106357
|
+
bigIntToHex(usdtRaw)
|
|
106358
|
+
]);
|
|
106359
|
+
const gasLimit = await this.estimateGas(
|
|
106360
|
+
{ from, to: usdtAddress, data: approveData, value: "0x0" },
|
|
106361
|
+
chainId
|
|
106362
|
+
).catch(() => void 0);
|
|
106363
|
+
usdtApproveTxHash = await this.core.wallet.sendTransaction({
|
|
106364
|
+
to: usdtAddress,
|
|
106365
|
+
data: approveData,
|
|
106366
|
+
value: "0x0",
|
|
106367
|
+
chainId
|
|
106368
|
+
});
|
|
106369
|
+
}
|
|
106370
|
+
const transferData = erc20Iface2.encodeFunctionData("transfer", [
|
|
106371
|
+
treasuryAddress,
|
|
106372
|
+
bigIntToHex(usdtRaw)
|
|
106373
|
+
]);
|
|
106374
|
+
const transferGas = await this.estimateGas(
|
|
106375
|
+
{ from, to: usdtAddress, data: transferData, value: "0x0" },
|
|
106376
|
+
chainId
|
|
106377
|
+
).catch(() => void 0);
|
|
106378
|
+
const usdtTransferTxHash = await this.core.wallet.sendTransaction({
|
|
106379
|
+
to: usdtAddress,
|
|
106380
|
+
data: transferData,
|
|
106381
|
+
value: "0x0",
|
|
106382
|
+
chainId
|
|
106383
|
+
});
|
|
106384
|
+
const to = opts.recipient || from;
|
|
106385
|
+
if (mode === "direct") {
|
|
106386
|
+
const mintData = rbtIface2.encodeFunctionData("mint", [
|
|
106387
|
+
to,
|
|
106388
|
+
rbtAmountRawHex
|
|
106389
|
+
]);
|
|
106390
|
+
const mintGas = await this.estimateGas(
|
|
106391
|
+
{ from, to: rbtAddress, data: mintData, value: "0x0" },
|
|
106392
|
+
chainId
|
|
106393
|
+
).catch(() => void 0);
|
|
106394
|
+
const rbtMintTxHash = await this.core.wallet.sendTransaction({
|
|
106395
|
+
to: rbtAddress,
|
|
106396
|
+
data: mintData,
|
|
106397
|
+
value: "0x0",
|
|
106398
|
+
chainId
|
|
106399
|
+
});
|
|
106400
|
+
return {
|
|
106401
|
+
usdtApproveTxHash,
|
|
106402
|
+
usdtTransferTxHash,
|
|
106403
|
+
rbtMintTxHash,
|
|
106404
|
+
rbtAmountRaw: rbtAmountRawHex,
|
|
106405
|
+
rbtAmountHuman: `${(Number(rbtRaw) / Number(pow102(rbtDecimals))).toString()}`,
|
|
106406
|
+
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"
|
|
106407
|
+
};
|
|
106408
|
+
}
|
|
106409
|
+
return {
|
|
106410
|
+
usdtApproveTxHash,
|
|
106411
|
+
usdtTransferTxHash,
|
|
106412
|
+
rbtAmountRaw: rbtAmountRawHex,
|
|
106413
|
+
rbtAmountHuman: `${(Number(rbtRaw) / Number(pow102(rbtDecimals))).toString()}`,
|
|
106414
|
+
note: "backend: USDT \uC785\uAE08 \uC644\uB8CC. \uBC31\uC5D4\uB4DC(\uBBFC\uD130 \uD0A4 \uBCF4\uC720)\uAC00 RBT.mint(to, amount) \uD638\uCD9C \uD544\uC694"
|
|
106415
|
+
};
|
|
106416
|
+
}
|
|
106417
|
+
/**
|
|
106418
|
+
* (옵션) RBT 민트를 단독으로 호출하는 유틸
|
|
106419
|
+
* - 호출 지갑이 MINTER_ROLE을 보유해야 성공
|
|
106420
|
+
*/
|
|
106421
|
+
async mintRbtDirect(opts) {
|
|
106422
|
+
const { rbtAddress, to, amountRbtHuman, chainId } = opts;
|
|
106423
|
+
const from = await this.core.wallet.getAddress();
|
|
106424
|
+
if (!from)
|
|
106425
|
+
throw new SDKError("\uC9C0\uAC11 \uC8FC\uC18C \uC5C6\uC74C", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
|
|
106426
|
+
const rbtDecimals = await this.core.wallet.fetchErc20Decimals(
|
|
106427
|
+
this.call.bind(this),
|
|
106428
|
+
rbtAddress,
|
|
106429
|
+
chainId
|
|
106430
|
+
);
|
|
106431
|
+
const rbtRaw = this.core.wallet.humanToRaw(amountRbtHuman, rbtDecimals);
|
|
106432
|
+
const data = rbtIface2.encodeFunctionData("mint", [to, bigIntToHex(rbtRaw)]);
|
|
106433
|
+
const gasLimit = await this.estimateGas(
|
|
106434
|
+
{ from, to: rbtAddress, data, value: "0x0" },
|
|
106435
|
+
chainId
|
|
106436
|
+
).catch(() => void 0);
|
|
106437
|
+
return this.core.wallet.sendTransaction({
|
|
106438
|
+
to: rbtAddress,
|
|
106439
|
+
data,
|
|
106440
|
+
value: "0x0",
|
|
106441
|
+
chainId
|
|
106442
|
+
});
|
|
106443
|
+
}
|
|
106228
106444
|
async getBalance(address, chainId) {
|
|
106229
106445
|
return this.core.wallet.getBalance(address, chainId);
|
|
106230
106446
|
}
|