@sodax/sdk 0.0.1-rc.30 → 0.0.1-rc.31
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 +747 -695
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +134 -48
- package/dist/index.d.ts +134 -48
- package/dist/index.mjs +747 -696
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6728,8 +6728,8 @@ var spokeChainConfig = {
|
|
|
6728
6728
|
[types.SUI_MAINNET_CHAIN_ID]: {
|
|
6729
6729
|
addresses: {
|
|
6730
6730
|
connection: "0xf3b1e696a66d02cb776dc15aae73c68bc8f03adcb6ba0ec7f6332d9d90a6a3d2::connectionv3::0x3ee76d13909ac58ae13baab4c9be5a5142818d9a387aed641825e5d4356969bf",
|
|
6731
|
-
|
|
6732
|
-
|
|
6731
|
+
assetManagerConfigId: "0xcb7346339340b7f8dea40fcafb70721dc2fcfa7e8626a89fd954d46c1f928b61",
|
|
6732
|
+
originalAssetManager: "0xa17a409164d1676db71b411ab50813ba2c7dd547d2df538c699049566f1ff922::asset_manager::0xcb7346339340b7f8dea40fcafb70721dc2fcfa7e8626a89fd954d46c1f928b61",
|
|
6733
6733
|
xTokenManager: "",
|
|
6734
6734
|
rateLimit: "",
|
|
6735
6735
|
testToken: ""
|
|
@@ -8402,684 +8402,850 @@ var StellarSpokeProvider = class {
|
|
|
8402
8402
|
return viem.toHex(Buffer.from(stellaraddress, "hex"));
|
|
8403
8403
|
}
|
|
8404
8404
|
};
|
|
8405
|
-
var
|
|
8406
|
-
|
|
8407
|
-
chainConfig;
|
|
8408
|
-
publicClient;
|
|
8409
|
-
constructor(config, wallet_provider) {
|
|
8410
|
-
this.chainConfig = config;
|
|
8411
|
-
this.walletProvider = wallet_provider;
|
|
8412
|
-
this.publicClient = new client.SuiClient({ url: client.getFullnodeUrl("mainnet") });
|
|
8413
|
-
}
|
|
8414
|
-
async getBalance(token) {
|
|
8415
|
-
const assetmanager = this.splitAddress(this.chainConfig.addresses.assetManager);
|
|
8416
|
-
const tx = new transactions.Transaction();
|
|
8417
|
-
const result = await this.walletProvider.viewContract(
|
|
8418
|
-
tx,
|
|
8419
|
-
assetmanager.packageId,
|
|
8420
|
-
assetmanager.moduleId,
|
|
8421
|
-
"get_token_balance",
|
|
8422
|
-
[tx.object(assetmanager.stateId)],
|
|
8423
|
-
[token]
|
|
8424
|
-
);
|
|
8425
|
-
if (!Array.isArray(result?.returnValues) || !Array.isArray(result.returnValues[0]) || result.returnValues[0][0] === void 0) {
|
|
8426
|
-
throw new Error("Failed to get Balance");
|
|
8427
|
-
}
|
|
8428
|
-
const val = result.returnValues[0][0];
|
|
8429
|
-
const str_u64 = bcs.bcs.U64.parse(Uint8Array.from(val));
|
|
8430
|
-
return BigInt(str_u64);
|
|
8405
|
+
var Erc20Service = class {
|
|
8406
|
+
constructor() {
|
|
8431
8407
|
}
|
|
8432
|
-
|
|
8433
|
-
|
|
8434
|
-
|
|
8435
|
-
|
|
8436
|
-
|
|
8437
|
-
|
|
8438
|
-
|
|
8439
|
-
|
|
8440
|
-
|
|
8441
|
-
|
|
8442
|
-
|
|
8443
|
-
|
|
8444
|
-
|
|
8445
|
-
|
|
8446
|
-
|
|
8447
|
-
|
|
8448
|
-
|
|
8449
|
-
|
|
8450
|
-
|
|
8451
|
-
|
|
8452
|
-
|
|
8453
|
-
|
|
8454
|
-
client: this.publicClient,
|
|
8455
|
-
onlyTransactionKind: true
|
|
8408
|
+
/**
|
|
8409
|
+
* Check if spender has enough ERC20 allowance for given amount
|
|
8410
|
+
* @param token - ERC20 token address
|
|
8411
|
+
* @param amount - Amount to check allowance for
|
|
8412
|
+
* @param owner - User wallet address
|
|
8413
|
+
* @param spender - Spender address
|
|
8414
|
+
* @param spokeProvider - EVM Spoke provider
|
|
8415
|
+
* @return - True if spender is allowed to spend amount on behalf of owner
|
|
8416
|
+
*/
|
|
8417
|
+
static async isAllowanceValid(token, amount, owner, spender, spokeProvider) {
|
|
8418
|
+
try {
|
|
8419
|
+
if (token.toLowerCase() === spokeProvider.chainConfig.nativeToken.toLowerCase()) {
|
|
8420
|
+
return {
|
|
8421
|
+
ok: true,
|
|
8422
|
+
value: true
|
|
8423
|
+
};
|
|
8424
|
+
}
|
|
8425
|
+
const allowedAmount = await spokeProvider.publicClient.readContract({
|
|
8426
|
+
address: token,
|
|
8427
|
+
abi: viem.erc20Abi,
|
|
8428
|
+
functionName: "allowance",
|
|
8429
|
+
args: [owner, spender]
|
|
8456
8430
|
});
|
|
8457
|
-
const transactionRawBase64String = Buffer.from(transactionRaw).toString("base64");
|
|
8458
8431
|
return {
|
|
8459
|
-
|
|
8460
|
-
|
|
8461
|
-
value: amount,
|
|
8462
|
-
data: transactionRawBase64String
|
|
8432
|
+
ok: true,
|
|
8433
|
+
value: allowedAmount >= amount
|
|
8463
8434
|
};
|
|
8464
|
-
}
|
|
8465
|
-
return this.walletProvider.signAndExecuteTxn(tx);
|
|
8466
|
-
}
|
|
8467
|
-
async getNativeCoin(tx, amount) {
|
|
8468
|
-
const coin = tx.splitCoins(tx.gas, [tx.pure.u64(amount)])[0];
|
|
8469
|
-
if (coin === void 0) {
|
|
8470
|
-
return Promise.reject(Error("[SuiIntentService.getNativeCoin] coin undefined"));
|
|
8471
|
-
}
|
|
8472
|
-
return coin;
|
|
8473
|
-
}
|
|
8474
|
-
async getCoin(tx, coin, amount, address) {
|
|
8475
|
-
const coins = await this.walletProvider.getCoins(address, coin);
|
|
8476
|
-
const objects = [];
|
|
8477
|
-
let totalAmount = BigInt(0);
|
|
8478
|
-
for (const coin2 of coins.data) {
|
|
8479
|
-
totalAmount += BigInt(coin2.balance);
|
|
8480
|
-
objects.push(coin2.coinObjectId);
|
|
8481
|
-
if (totalAmount >= amount) {
|
|
8482
|
-
break;
|
|
8483
|
-
}
|
|
8484
|
-
}
|
|
8485
|
-
const firstObject = objects[0];
|
|
8486
|
-
if (!firstObject) {
|
|
8487
|
-
throw new Error(`[SuiIntentService.getCoin] Coin=${coin} not found for address=${address} and amount=${amount}`);
|
|
8488
|
-
}
|
|
8489
|
-
if (objects.length > 1) {
|
|
8490
|
-
tx.mergeCoins(firstObject, objects.slice(1));
|
|
8491
|
-
}
|
|
8492
|
-
if (totalAmount === amount) {
|
|
8493
|
-
return tx.object(firstObject);
|
|
8494
|
-
}
|
|
8495
|
-
return tx.splitCoins(firstObject, [amount]);
|
|
8496
|
-
}
|
|
8497
|
-
splitAddress(address) {
|
|
8498
|
-
const parts = address.split("::");
|
|
8499
|
-
if (parts.length === 3) {
|
|
8500
|
-
if (parts[0] && parts[1] && parts[2]) {
|
|
8501
|
-
return { packageId: parts[0], moduleId: parts[1], stateId: parts[2] };
|
|
8502
|
-
}
|
|
8503
|
-
throw new Error("Invalid package address");
|
|
8504
|
-
}
|
|
8505
|
-
throw new Error("Invalid package address");
|
|
8506
|
-
}
|
|
8507
|
-
async sendMessage(dst_chain_id, dst_address, data, raw) {
|
|
8508
|
-
const txb = new transactions.Transaction();
|
|
8509
|
-
const connection = this.splitAddress(this.chainConfig.addresses.connection);
|
|
8510
|
-
txb.moveCall({
|
|
8511
|
-
target: `${connection.packageId}::${connection.moduleId}::send_message_ua`,
|
|
8512
|
-
arguments: [
|
|
8513
|
-
txb.object(connection.stateId),
|
|
8514
|
-
txb.pure.u256(dst_chain_id),
|
|
8515
|
-
txb.pure(bcs.bcs.vector(bcs.bcs.u8()).serialize(dst_address)),
|
|
8516
|
-
txb.pure(bcs.bcs.vector(bcs.bcs.u8()).serialize(data))
|
|
8517
|
-
]
|
|
8518
|
-
});
|
|
8519
|
-
const walletAddress = await this.walletProvider.getWalletAddress();
|
|
8520
|
-
if (raw) {
|
|
8521
|
-
txb.setSender(walletAddress);
|
|
8522
|
-
const transactionRaw = await txb.build({
|
|
8523
|
-
client: this.publicClient,
|
|
8524
|
-
onlyTransactionKind: true
|
|
8525
|
-
});
|
|
8526
|
-
const transactionRawBase64String = Buffer.from(transactionRaw).toString("base64");
|
|
8435
|
+
} catch (e) {
|
|
8527
8436
|
return {
|
|
8528
|
-
|
|
8529
|
-
|
|
8530
|
-
value: 0n,
|
|
8531
|
-
data: transactionRawBase64String
|
|
8437
|
+
ok: false,
|
|
8438
|
+
error: e
|
|
8532
8439
|
};
|
|
8533
8440
|
}
|
|
8534
|
-
return this.walletProvider.signAndExecuteTxn(txb);
|
|
8535
8441
|
}
|
|
8536
|
-
|
|
8537
|
-
|
|
8538
|
-
|
|
8539
|
-
|
|
8540
|
-
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
|
|
8544
|
-
|
|
8442
|
+
/**
|
|
8443
|
+
* Approve ERC20 amount spending
|
|
8444
|
+
* @param token - ERC20 token address
|
|
8445
|
+
* @param amount - Amount to approve
|
|
8446
|
+
* @param spender - Spender address
|
|
8447
|
+
* @param provider - EVM Provider
|
|
8448
|
+
*/
|
|
8449
|
+
static async approve(token, amount, spender, spokeProvider, raw) {
|
|
8450
|
+
const walletAddress = await spokeProvider.walletProvider.getWalletAddress();
|
|
8451
|
+
const rawTx = {
|
|
8452
|
+
from: walletAddress,
|
|
8453
|
+
to: token,
|
|
8454
|
+
value: 0n,
|
|
8455
|
+
data: viem.encodeFunctionData({
|
|
8456
|
+
abi: viem.erc20Abi,
|
|
8457
|
+
functionName: "approve",
|
|
8458
|
+
args: [spender, amount]
|
|
8459
|
+
})
|
|
8460
|
+
};
|
|
8461
|
+
if (raw) {
|
|
8462
|
+
return rawTx;
|
|
8463
|
+
}
|
|
8464
|
+
return spokeProvider.walletProvider.sendTransaction(rawTx);
|
|
8545
8465
|
}
|
|
8546
|
-
|
|
8547
|
-
|
|
8466
|
+
/**
|
|
8467
|
+
* Encodes a transfer transaction for a token.
|
|
8468
|
+
* @param token - The address of the token.
|
|
8469
|
+
* @param to - The address to transfer the token to.
|
|
8470
|
+
* @param amount - The amount of the token to transfer.
|
|
8471
|
+
* @returns The encoded contract call.
|
|
8472
|
+
*/
|
|
8473
|
+
static encodeTransfer(token, to, amount) {
|
|
8474
|
+
return {
|
|
8475
|
+
address: token,
|
|
8476
|
+
value: 0n,
|
|
8477
|
+
data: viem.encodeFunctionData({
|
|
8478
|
+
abi: viem.erc20Abi,
|
|
8479
|
+
functionName: "transfer",
|
|
8480
|
+
args: [to, amount]
|
|
8481
|
+
})
|
|
8482
|
+
};
|
|
8548
8483
|
}
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8484
|
+
/**
|
|
8485
|
+
* Encodes a transferFrom transaction for a token.
|
|
8486
|
+
* @param token - The address of the token.
|
|
8487
|
+
* @param from - The address to transfer the token from.
|
|
8488
|
+
* @param to - The address to transfer the token to.
|
|
8489
|
+
* @param amount - The amount of the token to transfer.
|
|
8490
|
+
* @returns The encoded contract call.
|
|
8491
|
+
*/
|
|
8492
|
+
static encodeTransferFrom(token, from, to, amount) {
|
|
8493
|
+
return {
|
|
8494
|
+
address: token,
|
|
8495
|
+
value: 0n,
|
|
8496
|
+
data: viem.encodeFunctionData({
|
|
8497
|
+
abi: viem.erc20Abi,
|
|
8498
|
+
functionName: "transferFrom",
|
|
8499
|
+
args: [from, to, amount]
|
|
8500
|
+
})
|
|
8501
|
+
};
|
|
8552
8502
|
}
|
|
8553
|
-
|
|
8554
|
-
|
|
8503
|
+
/**
|
|
8504
|
+
* Encodes an approval transaction for a token.
|
|
8505
|
+
* @param token - The address of the token.
|
|
8506
|
+
* @param to - The address to approve the token to.
|
|
8507
|
+
* @param amount - The amount of the token to approve.
|
|
8508
|
+
* @returns The encoded contract call.
|
|
8509
|
+
*/
|
|
8510
|
+
static encodeApprove(token, to, amount) {
|
|
8511
|
+
return {
|
|
8512
|
+
address: token,
|
|
8513
|
+
value: 0n,
|
|
8514
|
+
data: viem.encodeFunctionData({
|
|
8515
|
+
abi: viem.erc20Abi,
|
|
8516
|
+
functionName: "approve",
|
|
8517
|
+
args: [to, amount]
|
|
8518
|
+
})
|
|
8519
|
+
};
|
|
8555
8520
|
}
|
|
8556
8521
|
};
|
|
8557
|
-
|
|
8558
|
-
|
|
8559
|
-
var SolanaSpokeProvider = class {
|
|
8560
|
-
walletProvider;
|
|
8561
|
-
chainConfig;
|
|
8562
|
-
constructor(walletProvider, chainConfig) {
|
|
8563
|
-
this.walletProvider = walletProvider;
|
|
8564
|
-
this.chainConfig = chainConfig;
|
|
8522
|
+
var EvmVaultTokenService = class {
|
|
8523
|
+
constructor() {
|
|
8565
8524
|
}
|
|
8566
|
-
|
|
8567
|
-
|
|
8568
|
-
|
|
8569
|
-
|
|
8570
|
-
|
|
8571
|
-
|
|
8572
|
-
|
|
8573
|
-
|
|
8525
|
+
/**
|
|
8526
|
+
* Fetches token information for a specific token in the vault.
|
|
8527
|
+
* @param vault - The address of the vault.
|
|
8528
|
+
* @param token - The address of the token.
|
|
8529
|
+
* @param publicClient - PublicClient<HttpTransport>
|
|
8530
|
+
* @returns Token information as a TokenInfo object.
|
|
8531
|
+
*/
|
|
8532
|
+
static async getTokenInfo(vault, token, publicClient) {
|
|
8533
|
+
const [decimals, depositFee, withdrawalFee, maxDeposit, isSupported] = await publicClient.readContract({
|
|
8534
|
+
address: vault,
|
|
8535
|
+
abi: vaultTokenAbi,
|
|
8536
|
+
functionName: "tokenInfo",
|
|
8537
|
+
args: [token]
|
|
8538
|
+
});
|
|
8539
|
+
return { decimals, depositFee, withdrawalFee, maxDeposit, isSupported };
|
|
8574
8540
|
}
|
|
8575
|
-
|
|
8576
|
-
|
|
8577
|
-
|
|
8578
|
-
|
|
8579
|
-
|
|
8580
|
-
|
|
8581
|
-
|
|
8582
|
-
|
|
8583
|
-
|
|
8584
|
-
|
|
8585
|
-
|
|
8586
|
-
|
|
8587
|
-
}
|
|
8588
|
-
|
|
8589
|
-
// src/entities/icon/HanaWalletConnector.ts
|
|
8590
|
-
function requestAddress() {
|
|
8591
|
-
return new Promise((resolve) => {
|
|
8592
|
-
const eventHandler = (event) => {
|
|
8593
|
-
const customEvent = event;
|
|
8594
|
-
const response = customEvent.detail;
|
|
8595
|
-
if (isResponseAddressType(response)) {
|
|
8596
|
-
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8597
|
-
resolve({
|
|
8598
|
-
ok: true,
|
|
8599
|
-
value: response.payload
|
|
8600
|
-
});
|
|
8601
|
-
}
|
|
8602
|
-
};
|
|
8603
|
-
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8604
|
-
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8605
|
-
window.dispatchEvent(
|
|
8606
|
-
new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
8607
|
-
detail: {
|
|
8608
|
-
type: "REQUEST_ADDRESS"
|
|
8609
|
-
}
|
|
8610
|
-
})
|
|
8611
|
-
);
|
|
8612
|
-
});
|
|
8613
|
-
}
|
|
8614
|
-
function requestSigning(from, hash) {
|
|
8615
|
-
return new Promise((resolve, reject) => {
|
|
8616
|
-
const signRequest = new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
8617
|
-
detail: {
|
|
8618
|
-
type: "REQUEST_SIGNING",
|
|
8619
|
-
payload: {
|
|
8620
|
-
from,
|
|
8621
|
-
hash
|
|
8622
|
-
}
|
|
8623
|
-
}
|
|
8541
|
+
/**
|
|
8542
|
+
* Retrieves the reserves of the vault.
|
|
8543
|
+
* @param vault - The address of the vault.
|
|
8544
|
+
* @param publicClient - PublicClient<HttpTransport>
|
|
8545
|
+
* @returns An object containing tokens and their balances.
|
|
8546
|
+
*/
|
|
8547
|
+
static async getVaultReserves(vault, publicClient) {
|
|
8548
|
+
const [tokens, balances] = await publicClient.readContract({
|
|
8549
|
+
address: vault,
|
|
8550
|
+
abi: vaultTokenAbi,
|
|
8551
|
+
functionName: "getVaultReserves",
|
|
8552
|
+
args: []
|
|
8624
8553
|
});
|
|
8625
|
-
|
|
8626
|
-
|
|
8627
|
-
|
|
8628
|
-
if (isResponseSigningType(response)) {
|
|
8629
|
-
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8630
|
-
resolve({
|
|
8631
|
-
ok: true,
|
|
8632
|
-
value: response.payload
|
|
8633
|
-
});
|
|
8634
|
-
} else if (response.type === "CANCEL_SIGNING") {
|
|
8635
|
-
reject(new Error("CANCEL_SIGNING"));
|
|
8636
|
-
}
|
|
8637
|
-
};
|
|
8638
|
-
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8639
|
-
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8640
|
-
window.dispatchEvent(signRequest);
|
|
8641
|
-
});
|
|
8642
|
-
}
|
|
8643
|
-
function requestJsonRpc(rawTransaction, id = 99999) {
|
|
8644
|
-
return new Promise((resolve, reject) => {
|
|
8645
|
-
const eventHandler = (event) => {
|
|
8646
|
-
const customEvent = event;
|
|
8647
|
-
const { type, payload } = customEvent.detail;
|
|
8648
|
-
if (type === "RESPONSE_JSON-RPC") {
|
|
8649
|
-
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8650
|
-
if (isJsonRpcPayloadResponse(payload)) {
|
|
8651
|
-
resolve({
|
|
8652
|
-
ok: true,
|
|
8653
|
-
value: payload
|
|
8654
|
-
});
|
|
8655
|
-
} else {
|
|
8656
|
-
reject(new Error("Invalid payload response type (expected JsonRpcPayloadResponse)"));
|
|
8657
|
-
}
|
|
8658
|
-
} else if (type === "CANCEL_JSON-RPC") {
|
|
8659
|
-
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
8660
|
-
reject(new Error("CANCEL_JSON-RPC"));
|
|
8661
|
-
}
|
|
8554
|
+
return {
|
|
8555
|
+
tokens,
|
|
8556
|
+
balances
|
|
8662
8557
|
};
|
|
8663
|
-
|
|
8664
|
-
|
|
8665
|
-
|
|
8666
|
-
|
|
8667
|
-
|
|
8668
|
-
|
|
8669
|
-
|
|
8670
|
-
|
|
8671
|
-
|
|
8672
|
-
|
|
8673
|
-
|
|
8674
|
-
|
|
8675
|
-
|
|
8558
|
+
}
|
|
8559
|
+
/**
|
|
8560
|
+
* Retrieves all token information for the vault.
|
|
8561
|
+
* @param vault - The address of the vault.
|
|
8562
|
+
* @param publicClient - PublicClient<HttpTransport>
|
|
8563
|
+
* @returns A promise that resolves to an object containing tokens, their infos, and reserves.
|
|
8564
|
+
*/
|
|
8565
|
+
async getAllTokenInfo(vault, publicClient) {
|
|
8566
|
+
const [tokens, infos, reserves] = await publicClient.readContract({
|
|
8567
|
+
address: vault,
|
|
8568
|
+
abi: vaultTokenAbi,
|
|
8569
|
+
functionName: "getAllTokenInfo",
|
|
8570
|
+
args: []
|
|
8571
|
+
});
|
|
8572
|
+
return {
|
|
8573
|
+
tokens,
|
|
8574
|
+
infos,
|
|
8575
|
+
reserves
|
|
8576
|
+
};
|
|
8577
|
+
}
|
|
8578
|
+
/**
|
|
8579
|
+
* Deposits a specified amount of a token into the vault.
|
|
8580
|
+
* @param vault - The address of the vault.
|
|
8581
|
+
* @param token - The address of the token to deposit.
|
|
8582
|
+
* @param amount - The amount of the token to deposit.
|
|
8583
|
+
* @param walletProvider - IEvmWalletProvider
|
|
8584
|
+
* @returns Transaction hash
|
|
8585
|
+
*/
|
|
8586
|
+
static async deposit(vault, token, amount, walletProvider) {
|
|
8587
|
+
const from = await walletProvider.getWalletAddress();
|
|
8588
|
+
return walletProvider.sendTransaction({
|
|
8589
|
+
from,
|
|
8590
|
+
to: vault,
|
|
8591
|
+
value: 0n,
|
|
8592
|
+
data: viem.encodeFunctionData({
|
|
8593
|
+
abi: vaultTokenAbi,
|
|
8594
|
+
functionName: "deposit",
|
|
8595
|
+
args: [token, amount]
|
|
8676
8596
|
})
|
|
8677
|
-
);
|
|
8678
|
-
});
|
|
8679
|
-
}
|
|
8680
|
-
var Erc20Service = class {
|
|
8681
|
-
constructor() {
|
|
8597
|
+
});
|
|
8682
8598
|
}
|
|
8683
8599
|
/**
|
|
8684
|
-
*
|
|
8685
|
-
* @param
|
|
8686
|
-
* @param
|
|
8687
|
-
* @param
|
|
8688
|
-
* @param
|
|
8689
|
-
* @
|
|
8690
|
-
* @return - True if spender is allowed to spend amount on behalf of owner
|
|
8600
|
+
* Withdraws a specified amount of a token from the vault.
|
|
8601
|
+
* @param vault - The address of the vault.
|
|
8602
|
+
* @param token - The address of the token to withdraw.
|
|
8603
|
+
* @param amount - The amount of the token to withdraw.
|
|
8604
|
+
* @param provider - EvmWalletProvider
|
|
8605
|
+
* @returns Transaction hash
|
|
8691
8606
|
*/
|
|
8692
|
-
static async
|
|
8693
|
-
|
|
8694
|
-
|
|
8695
|
-
|
|
8696
|
-
|
|
8697
|
-
|
|
8698
|
-
|
|
8699
|
-
|
|
8700
|
-
|
|
8701
|
-
|
|
8702
|
-
|
|
8703
|
-
|
|
8704
|
-
args: [owner, spender]
|
|
8705
|
-
});
|
|
8706
|
-
return {
|
|
8707
|
-
ok: true,
|
|
8708
|
-
value: allowedAmount >= amount
|
|
8709
|
-
};
|
|
8710
|
-
} catch (e) {
|
|
8711
|
-
return {
|
|
8712
|
-
ok: false,
|
|
8713
|
-
error: e
|
|
8714
|
-
};
|
|
8715
|
-
}
|
|
8607
|
+
static async withdraw(vault, token, amount, provider) {
|
|
8608
|
+
const from = await provider.getWalletAddress();
|
|
8609
|
+
return provider.sendTransaction({
|
|
8610
|
+
from,
|
|
8611
|
+
to: vault,
|
|
8612
|
+
value: 0n,
|
|
8613
|
+
data: viem.encodeFunctionData({
|
|
8614
|
+
abi: vaultTokenAbi,
|
|
8615
|
+
functionName: "withdraw",
|
|
8616
|
+
args: [token, amount]
|
|
8617
|
+
})
|
|
8618
|
+
});
|
|
8716
8619
|
}
|
|
8717
8620
|
/**
|
|
8718
|
-
*
|
|
8719
|
-
* @param
|
|
8720
|
-
* @param
|
|
8721
|
-
* @param
|
|
8722
|
-
* @
|
|
8621
|
+
* Encodes the deposit transaction data for the vault.
|
|
8622
|
+
* @param vault - The address of the vault.
|
|
8623
|
+
* @param token - The address of the token to deposit.
|
|
8624
|
+
* @param amount - The amount of the token to deposit.
|
|
8625
|
+
* @returns The encoded contract call data.
|
|
8723
8626
|
*/
|
|
8724
|
-
static
|
|
8725
|
-
|
|
8726
|
-
|
|
8727
|
-
from: walletAddress,
|
|
8728
|
-
to: token,
|
|
8627
|
+
static encodeDeposit(vault, token, amount) {
|
|
8628
|
+
return {
|
|
8629
|
+
address: vault,
|
|
8729
8630
|
value: 0n,
|
|
8730
8631
|
data: viem.encodeFunctionData({
|
|
8731
|
-
abi:
|
|
8732
|
-
functionName: "
|
|
8733
|
-
args: [
|
|
8632
|
+
abi: vaultTokenAbi,
|
|
8633
|
+
functionName: "deposit",
|
|
8634
|
+
args: [token, amount]
|
|
8734
8635
|
})
|
|
8735
8636
|
};
|
|
8736
|
-
if (raw) {
|
|
8737
|
-
return rawTx;
|
|
8738
|
-
}
|
|
8739
|
-
return spokeProvider.walletProvider.sendTransaction(rawTx);
|
|
8740
8637
|
}
|
|
8741
8638
|
/**
|
|
8742
|
-
* Encodes
|
|
8743
|
-
* @param
|
|
8744
|
-
* @param
|
|
8745
|
-
* @param amount - The amount of the token to
|
|
8746
|
-
* @returns The encoded contract call.
|
|
8639
|
+
* Encodes the withdraw transaction data for the vault.
|
|
8640
|
+
* @param vault - The address of the vault.
|
|
8641
|
+
* @param token - The address of the token to withdraw.
|
|
8642
|
+
* @param amount - The amount of the token to withdraw.
|
|
8643
|
+
* @returns The encoded contract call data.
|
|
8747
8644
|
*/
|
|
8748
|
-
static
|
|
8645
|
+
static encodeWithdraw(vault, token, amount) {
|
|
8749
8646
|
return {
|
|
8750
|
-
address:
|
|
8647
|
+
address: vault,
|
|
8751
8648
|
value: 0n,
|
|
8752
8649
|
data: viem.encodeFunctionData({
|
|
8753
|
-
abi:
|
|
8754
|
-
functionName: "
|
|
8755
|
-
args: [
|
|
8650
|
+
abi: vaultTokenAbi,
|
|
8651
|
+
functionName: "withdraw",
|
|
8652
|
+
args: [token, amount]
|
|
8756
8653
|
})
|
|
8757
8654
|
};
|
|
8758
8655
|
}
|
|
8759
8656
|
/**
|
|
8760
|
-
*
|
|
8657
|
+
* Translates token amounts from their native decimals to 18 decimals
|
|
8658
|
+
* @param decimals - The number of decimals of the token
|
|
8659
|
+
* @param amount - The amount to translate
|
|
8660
|
+
* @returns The translated amount
|
|
8661
|
+
*/
|
|
8662
|
+
static translateIncomingDecimals(decimals, amount) {
|
|
8663
|
+
if (decimals <= 18) {
|
|
8664
|
+
return amount * BigInt(10 ** (18 - decimals));
|
|
8665
|
+
}
|
|
8666
|
+
return amount / BigInt(10 ** (decimals - 18));
|
|
8667
|
+
}
|
|
8668
|
+
/**
|
|
8669
|
+
* Translates token amounts from 18 decimals back to their native decimals
|
|
8670
|
+
* @param decimals - The number of decimals of the token
|
|
8671
|
+
* @param amount - The amount to translate
|
|
8672
|
+
* @returns The translated amount
|
|
8673
|
+
*/
|
|
8674
|
+
static translateOutgoingDecimals(decimals, amount) {
|
|
8675
|
+
if (decimals <= 18) {
|
|
8676
|
+
return amount / BigInt(10 ** (18 - decimals));
|
|
8677
|
+
}
|
|
8678
|
+
return amount * BigInt(10 ** (decimals - 18));
|
|
8679
|
+
}
|
|
8680
|
+
};
|
|
8681
|
+
|
|
8682
|
+
// src/services/hub/EvmAssetManagerService.ts
|
|
8683
|
+
var EvmAssetManagerService = class _EvmAssetManagerService {
|
|
8684
|
+
constructor() {
|
|
8685
|
+
}
|
|
8686
|
+
/**
|
|
8687
|
+
* Get asset information for a given asset address
|
|
8688
|
+
* @param asset - The address of the asset contract
|
|
8689
|
+
* @param assetManager - The address of the asset manager contract
|
|
8690
|
+
* @param client - The Viem public client
|
|
8691
|
+
* @returns Object containing chainID and spoke address for the asset
|
|
8692
|
+
*/
|
|
8693
|
+
static async getAssetInfo(asset, assetManager, client) {
|
|
8694
|
+
const [chainId, spokeAddress] = await client.readContract({
|
|
8695
|
+
address: assetManager,
|
|
8696
|
+
abi: assetManagerAbi,
|
|
8697
|
+
functionName: "assetInfo",
|
|
8698
|
+
args: [asset]
|
|
8699
|
+
});
|
|
8700
|
+
return {
|
|
8701
|
+
chainId,
|
|
8702
|
+
spokeAddress
|
|
8703
|
+
};
|
|
8704
|
+
}
|
|
8705
|
+
/**
|
|
8706
|
+
* Encodes a transfer transaction for an asset.
|
|
8761
8707
|
* @param token - The address of the token.
|
|
8762
|
-
* @param from - The address to transfer the token from.
|
|
8763
8708
|
* @param to - The address to transfer the token to.
|
|
8764
8709
|
* @param amount - The amount of the token to transfer.
|
|
8710
|
+
* @param assetManager
|
|
8765
8711
|
* @returns The encoded contract call.
|
|
8766
8712
|
*/
|
|
8767
|
-
static
|
|
8713
|
+
static encodeTransfer(token, to, amount, assetManager) {
|
|
8768
8714
|
return {
|
|
8769
|
-
address:
|
|
8715
|
+
address: assetManager,
|
|
8770
8716
|
value: 0n,
|
|
8771
8717
|
data: viem.encodeFunctionData({
|
|
8772
|
-
abi:
|
|
8773
|
-
functionName: "
|
|
8774
|
-
args: [
|
|
8718
|
+
abi: assetManagerAbi,
|
|
8719
|
+
functionName: "transfer",
|
|
8720
|
+
args: [token, to, amount, "0x"]
|
|
8775
8721
|
})
|
|
8776
8722
|
};
|
|
8777
8723
|
}
|
|
8778
8724
|
/**
|
|
8779
|
-
*
|
|
8780
|
-
* @param
|
|
8781
|
-
* @param
|
|
8782
|
-
* @
|
|
8783
|
-
* @
|
|
8725
|
+
* Constructs the data for depositing tokens to the spoke chain.
|
|
8726
|
+
* @param {EvmDepositToDataParams} params - The address of the token to deposit.
|
|
8727
|
+
* @param {EvmSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8728
|
+
* @returns {Hex} Encoded contract calls for the deposit transaction.
|
|
8729
|
+
* @throws Will throw an error if the asset or vault address is not found.
|
|
8730
|
+
*/
|
|
8731
|
+
static depositToData(params, spokeChainId) {
|
|
8732
|
+
const calls = [];
|
|
8733
|
+
const assetConfig = getHubAssetInfo(spokeChainId, params.token);
|
|
8734
|
+
if (!assetConfig) {
|
|
8735
|
+
throw new Error("[depositToData] Hub asset not found");
|
|
8736
|
+
}
|
|
8737
|
+
const assetAddress = assetConfig.asset;
|
|
8738
|
+
const vaultAddress = assetConfig.vault;
|
|
8739
|
+
calls.push(Erc20Service.encodeApprove(assetAddress, vaultAddress, params.amount));
|
|
8740
|
+
calls.push(EvmVaultTokenService.encodeDeposit(vaultAddress, assetAddress, params.amount));
|
|
8741
|
+
const translatedAmount = EvmVaultTokenService.translateIncomingDecimals(assetConfig.decimal, params.amount);
|
|
8742
|
+
calls.push(Erc20Service.encodeTransfer(vaultAddress, params.to, translatedAmount));
|
|
8743
|
+
return encodeContractCalls(calls);
|
|
8744
|
+
}
|
|
8745
|
+
/**
|
|
8746
|
+
* Withdraw tokens from the spoke chain.
|
|
8747
|
+
* @param {EvmWithdrawAssetDataParams} params - Parameters for the withdrawal.
|
|
8748
|
+
* @param {EvmSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8749
|
+
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
8750
|
+
* @returns {Hex} Encoded contract calls for the withdrawal transaction.
|
|
8751
|
+
* @throws Will throw an error if the asset address is not found.
|
|
8752
|
+
*/
|
|
8753
|
+
static withdrawAssetData(params, hubProvider, spokeChainId) {
|
|
8754
|
+
const calls = [];
|
|
8755
|
+
const assetConfig = getHubAssetInfo(spokeChainId, params.token);
|
|
8756
|
+
if (!assetConfig) {
|
|
8757
|
+
throw new Error("[withdrawAssetData] Hub asset not found");
|
|
8758
|
+
}
|
|
8759
|
+
const assetAddress = assetConfig.asset;
|
|
8760
|
+
calls.push(
|
|
8761
|
+
_EvmAssetManagerService.encodeTransfer(
|
|
8762
|
+
assetAddress,
|
|
8763
|
+
params.to,
|
|
8764
|
+
params.amount,
|
|
8765
|
+
hubProvider.chainConfig.addresses.assetManager
|
|
8766
|
+
)
|
|
8767
|
+
);
|
|
8768
|
+
return encodeContractCalls(calls);
|
|
8769
|
+
}
|
|
8770
|
+
/**
|
|
8771
|
+
* Get asset address for a given chain ID and spoke address
|
|
8772
|
+
* @param chainId Chain ID where the asset exists
|
|
8773
|
+
* @param spokeAddress Address of the asset on the spoke chain
|
|
8774
|
+
* @param assetManager Address of the asset manager contract
|
|
8775
|
+
* @param client The Viem public client
|
|
8776
|
+
* @returns The asset's address on the hub chain
|
|
8784
8777
|
*/
|
|
8785
|
-
|
|
8786
|
-
return {
|
|
8787
|
-
address:
|
|
8788
|
-
|
|
8789
|
-
|
|
8790
|
-
|
|
8791
|
-
|
|
8792
|
-
args: [to, amount]
|
|
8793
|
-
})
|
|
8794
|
-
};
|
|
8778
|
+
async getAssetAddress(chainId, spokeAddress, assetManager, client) {
|
|
8779
|
+
return client.readContract({
|
|
8780
|
+
address: assetManager,
|
|
8781
|
+
abi: assetManagerAbi,
|
|
8782
|
+
functionName: "assets",
|
|
8783
|
+
args: [chainId, spokeAddress]
|
|
8784
|
+
});
|
|
8795
8785
|
}
|
|
8796
8786
|
};
|
|
8797
|
-
|
|
8787
|
+
|
|
8788
|
+
// src/services/hub/EvmWalletAbstraction.ts
|
|
8789
|
+
var EvmWalletAbstraction = class {
|
|
8798
8790
|
constructor() {
|
|
8799
8791
|
}
|
|
8800
8792
|
/**
|
|
8801
|
-
*
|
|
8802
|
-
* @param
|
|
8803
|
-
* @param
|
|
8804
|
-
* @param
|
|
8805
|
-
* @returns
|
|
8793
|
+
* Get the hub wallet address for a given spoke chain and address.
|
|
8794
|
+
* @param chainId - The spoke chain ID.
|
|
8795
|
+
* @param address - The address on the spoke chain.
|
|
8796
|
+
* @param hubProvider - The hub provider.
|
|
8797
|
+
* @returns The hub wallet address.
|
|
8806
8798
|
*/
|
|
8807
|
-
static async
|
|
8808
|
-
|
|
8809
|
-
address:
|
|
8810
|
-
abi:
|
|
8811
|
-
functionName: "
|
|
8812
|
-
args: [
|
|
8799
|
+
static async getUserHubWalletAddress(chainId, address, hubProvider) {
|
|
8800
|
+
return hubProvider.publicClient.readContract({
|
|
8801
|
+
address: hubProvider.chainConfig.addresses.hubWallet,
|
|
8802
|
+
abi: walletFactoryAbi,
|
|
8803
|
+
functionName: "getDeployedAddress",
|
|
8804
|
+
args: [BigInt(getIntentRelayChainId(chainId)), address]
|
|
8813
8805
|
});
|
|
8814
|
-
return { decimals, depositFee, withdrawalFee, maxDeposit, isSupported };
|
|
8815
8806
|
}
|
|
8816
|
-
|
|
8817
|
-
|
|
8818
|
-
|
|
8819
|
-
* @param publicClient - PublicClient<HttpTransport>
|
|
8820
|
-
* @returns An object containing tokens and their balances.
|
|
8821
|
-
*/
|
|
8822
|
-
static async getVaultReserves(vault, publicClient) {
|
|
8823
|
-
const [tokens, balances] = await publicClient.readContract({
|
|
8824
|
-
address: vault,
|
|
8825
|
-
abi: vaultTokenAbi,
|
|
8826
|
-
functionName: "getVaultReserves",
|
|
8827
|
-
args: []
|
|
8828
|
-
});
|
|
8829
|
-
return {
|
|
8830
|
-
tokens,
|
|
8831
|
-
balances
|
|
8832
|
-
};
|
|
8807
|
+
};
|
|
8808
|
+
var SuiSpokeService = class _SuiSpokeService {
|
|
8809
|
+
constructor() {
|
|
8833
8810
|
}
|
|
8834
8811
|
/**
|
|
8835
|
-
*
|
|
8836
|
-
* @param
|
|
8837
|
-
* @param
|
|
8838
|
-
* @returns
|
|
8812
|
+
* Estimate the gas for a transaction.
|
|
8813
|
+
* @param {SuiRawTransaction} rawTx - The raw transaction to estimate the gas for.
|
|
8814
|
+
* @param {SuiSpokeProvider} spokeProvider - The spoke provider.
|
|
8815
|
+
* @returns {Promise<bigint>} The estimated computation cost.
|
|
8839
8816
|
*/
|
|
8840
|
-
async
|
|
8841
|
-
const
|
|
8842
|
-
|
|
8843
|
-
|
|
8844
|
-
|
|
8845
|
-
args: []
|
|
8817
|
+
static async estimateGas(rawTx, spokeProvider) {
|
|
8818
|
+
const txb = transactions.Transaction.fromKind(rawTx.data);
|
|
8819
|
+
const result = await spokeProvider.publicClient.devInspectTransactionBlock({
|
|
8820
|
+
sender: rawTx.from,
|
|
8821
|
+
transactionBlock: txb
|
|
8846
8822
|
});
|
|
8847
|
-
return
|
|
8848
|
-
tokens,
|
|
8849
|
-
infos,
|
|
8850
|
-
reserves
|
|
8851
|
-
};
|
|
8823
|
+
return result.effects.gasUsed;
|
|
8852
8824
|
}
|
|
8853
8825
|
/**
|
|
8854
|
-
*
|
|
8855
|
-
* @param
|
|
8856
|
-
* @param
|
|
8857
|
-
* @param
|
|
8858
|
-
* @param
|
|
8859
|
-
* @returns
|
|
8826
|
+
* Deposit tokens to the spoke chain.
|
|
8827
|
+
* @param {InjectiveSpokeDepositParams} params - The parameters for the deposit, including the user's address, token address, amount, and additional data.
|
|
8828
|
+
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8829
|
+
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
8830
|
+
* @param {boolean} raw - The return type raw or just transaction hash
|
|
8831
|
+
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
8860
8832
|
*/
|
|
8861
|
-
static async deposit(
|
|
8862
|
-
const
|
|
8863
|
-
|
|
8864
|
-
from,
|
|
8865
|
-
|
|
8866
|
-
|
|
8867
|
-
|
|
8868
|
-
|
|
8869
|
-
|
|
8870
|
-
|
|
8871
|
-
|
|
8872
|
-
|
|
8833
|
+
static async deposit(params, spokeProvider, hubProvider, raw) {
|
|
8834
|
+
const userWallet = params.to ?? await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
8835
|
+
spokeProvider.chainConfig.chain.id,
|
|
8836
|
+
params.from,
|
|
8837
|
+
hubProvider
|
|
8838
|
+
);
|
|
8839
|
+
return _SuiSpokeService.transfer(
|
|
8840
|
+
{
|
|
8841
|
+
token: params.token,
|
|
8842
|
+
recipient: userWallet,
|
|
8843
|
+
amount: params.amount,
|
|
8844
|
+
data: params.data
|
|
8845
|
+
},
|
|
8846
|
+
spokeProvider,
|
|
8847
|
+
raw
|
|
8848
|
+
);
|
|
8873
8849
|
}
|
|
8874
8850
|
/**
|
|
8875
|
-
*
|
|
8876
|
-
* @param
|
|
8877
|
-
* @param
|
|
8878
|
-
* @
|
|
8879
|
-
* @param provider - EvmWalletProvider
|
|
8880
|
-
* @returns Transaction hash
|
|
8851
|
+
* Get the balance of the token in the spoke chain.
|
|
8852
|
+
* @param {Address} token - The address of the token to get the balance of.
|
|
8853
|
+
* @param {SuiSpokeProvider} spokeProvider - The spoke provider.
|
|
8854
|
+
* @returns {Promise<bigint>} The balance of the token.
|
|
8881
8855
|
*/
|
|
8882
|
-
static async
|
|
8883
|
-
|
|
8884
|
-
return provider.sendTransaction({
|
|
8885
|
-
from,
|
|
8886
|
-
to: vault,
|
|
8887
|
-
value: 0n,
|
|
8888
|
-
data: viem.encodeFunctionData({
|
|
8889
|
-
abi: vaultTokenAbi,
|
|
8890
|
-
functionName: "withdraw",
|
|
8891
|
-
args: [token, amount]
|
|
8892
|
-
})
|
|
8893
|
-
});
|
|
8856
|
+
static async getDeposit(token, spokeProvider) {
|
|
8857
|
+
return spokeProvider.getBalance(token);
|
|
8894
8858
|
}
|
|
8895
8859
|
/**
|
|
8896
|
-
*
|
|
8897
|
-
* @param
|
|
8898
|
-
* @param
|
|
8899
|
-
* @param
|
|
8900
|
-
* @returns The
|
|
8860
|
+
* Generate simulation parameters for deposit from SuiSpokeDepositParams.
|
|
8861
|
+
* @param {SuiSpokeDepositParams} params - The deposit parameters.
|
|
8862
|
+
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8863
|
+
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
8864
|
+
* @returns {Promise<DepositSimulationParams>} The simulation parameters.
|
|
8901
8865
|
*/
|
|
8902
|
-
static
|
|
8866
|
+
static async getSimulateDepositParams(params, spokeProvider, hubProvider) {
|
|
8867
|
+
const to = params.to ?? await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
8868
|
+
spokeProvider.chainConfig.chain.id,
|
|
8869
|
+
params.from,
|
|
8870
|
+
hubProvider
|
|
8871
|
+
);
|
|
8872
|
+
const encoder = new TextEncoder();
|
|
8903
8873
|
return {
|
|
8904
|
-
|
|
8905
|
-
|
|
8906
|
-
|
|
8907
|
-
|
|
8908
|
-
|
|
8909
|
-
|
|
8910
|
-
|
|
8874
|
+
spokeChainID: spokeProvider.chainConfig.chain.id,
|
|
8875
|
+
token: viem.toHex(encoder.encode(params.token)),
|
|
8876
|
+
from: encodeAddress(spokeProvider.chainConfig.chain.id, params.from),
|
|
8877
|
+
to,
|
|
8878
|
+
amount: params.amount,
|
|
8879
|
+
data: params.data,
|
|
8880
|
+
srcAddress: viem.toHex(encoder.encode(spokeProvider.chainConfig.addresses.originalAssetManager))
|
|
8911
8881
|
};
|
|
8912
8882
|
}
|
|
8913
8883
|
/**
|
|
8914
|
-
*
|
|
8915
|
-
* @param
|
|
8916
|
-
* @param
|
|
8917
|
-
* @param
|
|
8918
|
-
* @
|
|
8884
|
+
* Calls a contract on the spoke chain using the user's wallet.
|
|
8885
|
+
* @param {HubAddress} from - The address of the user on the spoke chain.
|
|
8886
|
+
* @param {Hex} payload - The payload to send to the contract.
|
|
8887
|
+
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8888
|
+
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
8889
|
+
* @param {boolean} raw - The return type raw or just transaction hash
|
|
8890
|
+
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
8919
8891
|
*/
|
|
8920
|
-
static
|
|
8921
|
-
|
|
8922
|
-
|
|
8923
|
-
value: 0n,
|
|
8924
|
-
data: viem.encodeFunctionData({
|
|
8925
|
-
abi: vaultTokenAbi,
|
|
8926
|
-
functionName: "withdraw",
|
|
8927
|
-
args: [token, amount]
|
|
8928
|
-
})
|
|
8929
|
-
};
|
|
8892
|
+
static async callWallet(from, payload, spokeProvider, hubProvider, raw) {
|
|
8893
|
+
const relayId = getIntentRelayChainId(hubProvider.chainConfig.chain.id);
|
|
8894
|
+
return _SuiSpokeService.call(BigInt(relayId), from, payload, spokeProvider, raw);
|
|
8930
8895
|
}
|
|
8931
8896
|
/**
|
|
8932
|
-
*
|
|
8933
|
-
* @param
|
|
8934
|
-
* @
|
|
8935
|
-
|
|
8897
|
+
* Fetch the asset manager config from the spoke chain.
|
|
8898
|
+
* @param {SuiSpokeProvider} suiSpokeProvider - The spoke provider.
|
|
8899
|
+
* @returns {Promise<string>} The asset manager config.
|
|
8900
|
+
*/
|
|
8901
|
+
static async fetchAssetManagerAddress(suiSpokeProvider) {
|
|
8902
|
+
const latestPackageId = await _SuiSpokeService.fetchLatestAssetManagerPackageId(suiSpokeProvider);
|
|
8903
|
+
return `${latestPackageId}::asset_manager::${suiSpokeProvider.chainConfig.addresses.assetManagerConfigId}`;
|
|
8904
|
+
}
|
|
8905
|
+
/**
|
|
8906
|
+
* Fetch the latest asset manager package id from the spoke chain.
|
|
8907
|
+
* @param {SuiSpokeProvider} suiSpokeProvider - The spoke provider.
|
|
8908
|
+
* @returns {Promise<string>} The latest asset manager package id.
|
|
8909
|
+
*/
|
|
8910
|
+
static async fetchLatestAssetManagerPackageId(suiSpokeProvider) {
|
|
8911
|
+
const configData = await suiSpokeProvider.publicClient.getObject({
|
|
8912
|
+
id: suiSpokeProvider.chainConfig.addresses.assetManagerConfigId,
|
|
8913
|
+
options: {
|
|
8914
|
+
showContent: true
|
|
8915
|
+
}
|
|
8916
|
+
});
|
|
8917
|
+
if (configData.error) {
|
|
8918
|
+
throw new Error(`Failed to fetch asset manager id. Details: ${JSON.stringify(configData.error)}`);
|
|
8919
|
+
}
|
|
8920
|
+
if (!configData.data) {
|
|
8921
|
+
throw new Error("Asset manager id not found (no data)");
|
|
8922
|
+
}
|
|
8923
|
+
if (configData.data.content?.dataType !== "moveObject") {
|
|
8924
|
+
throw new Error("Asset manager id not found (not a move object)");
|
|
8925
|
+
}
|
|
8926
|
+
if (!("latest_package_id" in configData.data.content.fields)) {
|
|
8927
|
+
throw new Error("Asset manager id not found (no latest package id)");
|
|
8928
|
+
}
|
|
8929
|
+
const latestPackageId = configData.data.content.fields["latest_package_id"];
|
|
8930
|
+
if (typeof latestPackageId !== "string") {
|
|
8931
|
+
throw new Error("Asset manager id invalid (latest package id is not a string)");
|
|
8932
|
+
}
|
|
8933
|
+
if (!latestPackageId) {
|
|
8934
|
+
throw new Error("Asset manager id not found (no latest package id)");
|
|
8935
|
+
}
|
|
8936
|
+
return latestPackageId.toString();
|
|
8937
|
+
}
|
|
8938
|
+
/**
|
|
8939
|
+
* Transfers tokens to the hub chain.
|
|
8940
|
+
* @param {SuiTransferToHubParams} params - The parameters for the transfer, including:
|
|
8941
|
+
* - {string} token: The address of the token to transfer (use address(0) for native token).
|
|
8942
|
+
* - {Uint8Array} recipient: The recipient address on the hub chain.
|
|
8943
|
+
* - {string} amount: The amount to transfer.
|
|
8944
|
+
* - {Uint8Array} [data=new Uint8Array([])]: Additional data for the transfer.
|
|
8945
|
+
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8946
|
+
* @param {boolean} raw - The return type raw or just transaction hash
|
|
8947
|
+
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
8936
8948
|
*/
|
|
8937
|
-
static
|
|
8938
|
-
|
|
8939
|
-
return amount * BigInt(10 ** (18 - decimals));
|
|
8940
|
-
}
|
|
8941
|
-
return amount / BigInt(10 ** (decimals - 18));
|
|
8949
|
+
static async transfer({ token, recipient, amount, data = "0x" }, spokeProvider, raw) {
|
|
8950
|
+
return spokeProvider.transfer(token, amount, viem.fromHex(recipient, "bytes"), viem.fromHex(data, "bytes"), raw);
|
|
8942
8951
|
}
|
|
8943
8952
|
/**
|
|
8944
|
-
*
|
|
8945
|
-
* @param
|
|
8946
|
-
* @param
|
|
8947
|
-
* @
|
|
8953
|
+
* Sends a message to the hub chain.
|
|
8954
|
+
* @param {bigint} dstChainId - The chain ID of the hub chain.
|
|
8955
|
+
* @param {HubAddress} dstAddress - The address on the hub chain.
|
|
8956
|
+
* @param {Hex} payload - The payload to send.
|
|
8957
|
+
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
8958
|
+
* @param {boolean} raw - The return type raw or just transaction hash
|
|
8959
|
+
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
8948
8960
|
*/
|
|
8949
|
-
static
|
|
8950
|
-
|
|
8951
|
-
return amount / BigInt(10 ** (18 - decimals));
|
|
8952
|
-
}
|
|
8953
|
-
return amount * BigInt(10 ** (decimals - 18));
|
|
8961
|
+
static async call(dstChainId, dstAddress, payload, spokeProvider, raw) {
|
|
8962
|
+
return spokeProvider.sendMessage(dstChainId, viem.fromHex(dstAddress, "bytes"), viem.fromHex(payload, "bytes"), raw);
|
|
8954
8963
|
}
|
|
8955
8964
|
};
|
|
8956
8965
|
|
|
8957
|
-
// src/
|
|
8958
|
-
var
|
|
8959
|
-
|
|
8966
|
+
// src/entities/sui/SuiSpokeProvider.ts
|
|
8967
|
+
var SuiSpokeProvider = class _SuiSpokeProvider {
|
|
8968
|
+
walletProvider;
|
|
8969
|
+
chainConfig;
|
|
8970
|
+
publicClient;
|
|
8971
|
+
assetManagerAddress;
|
|
8972
|
+
constructor(config, wallet_provider) {
|
|
8973
|
+
this.chainConfig = config;
|
|
8974
|
+
this.walletProvider = wallet_provider;
|
|
8975
|
+
this.publicClient = new client.SuiClient({ url: client.getFullnodeUrl("mainnet") });
|
|
8960
8976
|
}
|
|
8961
|
-
|
|
8962
|
-
|
|
8963
|
-
|
|
8964
|
-
|
|
8965
|
-
|
|
8966
|
-
|
|
8967
|
-
|
|
8968
|
-
|
|
8969
|
-
|
|
8970
|
-
|
|
8971
|
-
|
|
8972
|
-
|
|
8973
|
-
|
|
8977
|
+
async getBalance(token) {
|
|
8978
|
+
const assetmanager = this.splitAddress(await this.getAssetManagerAddress());
|
|
8979
|
+
const tx = new transactions.Transaction();
|
|
8980
|
+
const result = await this.walletProvider.viewContract(
|
|
8981
|
+
tx,
|
|
8982
|
+
assetmanager.packageId,
|
|
8983
|
+
assetmanager.moduleId,
|
|
8984
|
+
"get_token_balance",
|
|
8985
|
+
[tx.object(assetmanager.stateId)],
|
|
8986
|
+
[token]
|
|
8987
|
+
);
|
|
8988
|
+
if (!Array.isArray(result?.returnValues) || !Array.isArray(result.returnValues[0]) || result.returnValues[0][0] === void 0) {
|
|
8989
|
+
throw new Error("Failed to get Balance");
|
|
8990
|
+
}
|
|
8991
|
+
const val = result.returnValues[0][0];
|
|
8992
|
+
const str_u64 = bcs.bcs.U64.parse(Uint8Array.from(val));
|
|
8993
|
+
return BigInt(str_u64);
|
|
8994
|
+
}
|
|
8995
|
+
async transfer(token, amount, to, data, raw) {
|
|
8996
|
+
const isNative2 = token.toLowerCase() === this.chainConfig.nativeToken.toLowerCase();
|
|
8997
|
+
const tx = new transactions.Transaction();
|
|
8998
|
+
const walletAddress = await this.walletProvider.getWalletAddressBytes();
|
|
8999
|
+
const coin = isNative2 ? await this.getNativeCoin(tx, amount) : await this.getCoin(tx, token, amount, walletAddress);
|
|
9000
|
+
const connection = this.splitAddress(this.chainConfig.addresses.connection);
|
|
9001
|
+
const assetManager = this.splitAddress(await this.getAssetManagerAddress());
|
|
9002
|
+
tx.moveCall({
|
|
9003
|
+
target: `${assetManager.packageId}::${assetManager.moduleId}::transfer`,
|
|
9004
|
+
typeArguments: [token],
|
|
9005
|
+
arguments: [
|
|
9006
|
+
tx.object(assetManager.stateId),
|
|
9007
|
+
tx.object(connection.stateId),
|
|
9008
|
+
// Connection state object
|
|
9009
|
+
coin,
|
|
9010
|
+
tx.pure(bcs.bcs.vector(bcs.bcs.u8()).serialize(to)),
|
|
9011
|
+
tx.pure(bcs.bcs.vector(bcs.bcs.u8()).serialize(data))
|
|
9012
|
+
]
|
|
8974
9013
|
});
|
|
8975
|
-
|
|
8976
|
-
|
|
8977
|
-
|
|
8978
|
-
|
|
9014
|
+
if (raw) {
|
|
9015
|
+
tx.setSender(walletAddress);
|
|
9016
|
+
const transactionRaw = await tx.build({
|
|
9017
|
+
client: this.publicClient,
|
|
9018
|
+
onlyTransactionKind: true
|
|
9019
|
+
});
|
|
9020
|
+
const transactionRawBase64String = Buffer.from(transactionRaw).toString("base64");
|
|
9021
|
+
return {
|
|
9022
|
+
from: walletAddress,
|
|
9023
|
+
to: `${assetManager.packageId}::${assetManager.moduleId}::transfer`,
|
|
9024
|
+
value: amount,
|
|
9025
|
+
data: transactionRawBase64String
|
|
9026
|
+
};
|
|
9027
|
+
}
|
|
9028
|
+
return this.walletProvider.signAndExecuteTxn(tx);
|
|
8979
9029
|
}
|
|
8980
|
-
|
|
8981
|
-
|
|
8982
|
-
|
|
8983
|
-
|
|
8984
|
-
|
|
8985
|
-
|
|
8986
|
-
* @returns The encoded contract call.
|
|
8987
|
-
*/
|
|
8988
|
-
static encodeTransfer(token, to, amount, assetManager) {
|
|
8989
|
-
return {
|
|
8990
|
-
address: assetManager,
|
|
8991
|
-
value: 0n,
|
|
8992
|
-
data: viem.encodeFunctionData({
|
|
8993
|
-
abi: assetManagerAbi,
|
|
8994
|
-
functionName: "transfer",
|
|
8995
|
-
args: [token, to, amount, "0x"]
|
|
8996
|
-
})
|
|
8997
|
-
};
|
|
9030
|
+
async getNativeCoin(tx, amount) {
|
|
9031
|
+
const coin = tx.splitCoins(tx.gas, [tx.pure.u64(amount)])[0];
|
|
9032
|
+
if (coin === void 0) {
|
|
9033
|
+
return Promise.reject(Error("[SuiIntentService.getNativeCoin] coin undefined"));
|
|
9034
|
+
}
|
|
9035
|
+
return coin;
|
|
8998
9036
|
}
|
|
8999
|
-
|
|
9000
|
-
|
|
9001
|
-
|
|
9002
|
-
|
|
9003
|
-
|
|
9004
|
-
|
|
9005
|
-
|
|
9006
|
-
|
|
9007
|
-
|
|
9008
|
-
|
|
9009
|
-
if (!assetConfig) {
|
|
9010
|
-
throw new Error("[depositToData] Hub asset not found");
|
|
9037
|
+
async getCoin(tx, coin, amount, address) {
|
|
9038
|
+
const coins = await this.walletProvider.getCoins(address, coin);
|
|
9039
|
+
const objects = [];
|
|
9040
|
+
let totalAmount = BigInt(0);
|
|
9041
|
+
for (const coin2 of coins.data) {
|
|
9042
|
+
totalAmount += BigInt(coin2.balance);
|
|
9043
|
+
objects.push(coin2.coinObjectId);
|
|
9044
|
+
if (totalAmount >= amount) {
|
|
9045
|
+
break;
|
|
9046
|
+
}
|
|
9011
9047
|
}
|
|
9012
|
-
const
|
|
9013
|
-
|
|
9014
|
-
|
|
9015
|
-
|
|
9016
|
-
|
|
9017
|
-
|
|
9018
|
-
|
|
9048
|
+
const firstObject = objects[0];
|
|
9049
|
+
if (!firstObject) {
|
|
9050
|
+
throw new Error(`[SuiIntentService.getCoin] Coin=${coin} not found for address=${address} and amount=${amount}`);
|
|
9051
|
+
}
|
|
9052
|
+
if (objects.length > 1) {
|
|
9053
|
+
tx.mergeCoins(firstObject, objects.slice(1));
|
|
9054
|
+
}
|
|
9055
|
+
if (totalAmount === amount) {
|
|
9056
|
+
return tx.object(firstObject);
|
|
9057
|
+
}
|
|
9058
|
+
return tx.splitCoins(firstObject, [amount]);
|
|
9019
9059
|
}
|
|
9020
|
-
|
|
9021
|
-
|
|
9022
|
-
|
|
9023
|
-
|
|
9024
|
-
|
|
9025
|
-
|
|
9026
|
-
|
|
9027
|
-
*/
|
|
9028
|
-
static withdrawAssetData(params, hubProvider, spokeChainId) {
|
|
9029
|
-
const calls = [];
|
|
9030
|
-
const assetConfig = getHubAssetInfo(spokeChainId, params.token);
|
|
9031
|
-
if (!assetConfig) {
|
|
9032
|
-
throw new Error("[withdrawAssetData] Hub asset not found");
|
|
9060
|
+
splitAddress(address) {
|
|
9061
|
+
const parts = address.split("::");
|
|
9062
|
+
if (parts.length === 3) {
|
|
9063
|
+
if (parts[0] && parts[1] && parts[2]) {
|
|
9064
|
+
return { packageId: parts[0], moduleId: parts[1], stateId: parts[2] };
|
|
9065
|
+
}
|
|
9066
|
+
throw new Error("Invalid package address");
|
|
9033
9067
|
}
|
|
9034
|
-
|
|
9035
|
-
calls.push(
|
|
9036
|
-
_EvmAssetManagerService.encodeTransfer(
|
|
9037
|
-
assetAddress,
|
|
9038
|
-
params.to,
|
|
9039
|
-
params.amount,
|
|
9040
|
-
hubProvider.chainConfig.addresses.assetManager
|
|
9041
|
-
)
|
|
9042
|
-
);
|
|
9043
|
-
return encodeContractCalls(calls);
|
|
9068
|
+
throw new Error("Invalid package address");
|
|
9044
9069
|
}
|
|
9045
|
-
|
|
9046
|
-
|
|
9047
|
-
|
|
9048
|
-
|
|
9049
|
-
|
|
9050
|
-
|
|
9051
|
-
|
|
9052
|
-
|
|
9053
|
-
|
|
9054
|
-
|
|
9055
|
-
|
|
9056
|
-
|
|
9057
|
-
|
|
9058
|
-
|
|
9070
|
+
async sendMessage(dst_chain_id, dst_address, data, raw) {
|
|
9071
|
+
const txb = new transactions.Transaction();
|
|
9072
|
+
const connection = this.splitAddress(this.chainConfig.addresses.connection);
|
|
9073
|
+
txb.moveCall({
|
|
9074
|
+
target: `${connection.packageId}::${connection.moduleId}::send_message_ua`,
|
|
9075
|
+
arguments: [
|
|
9076
|
+
txb.object(connection.stateId),
|
|
9077
|
+
txb.pure.u256(dst_chain_id),
|
|
9078
|
+
txb.pure(bcs.bcs.vector(bcs.bcs.u8()).serialize(dst_address)),
|
|
9079
|
+
txb.pure(bcs.bcs.vector(bcs.bcs.u8()).serialize(data))
|
|
9080
|
+
]
|
|
9081
|
+
});
|
|
9082
|
+
const walletAddress = await this.walletProvider.getWalletAddress();
|
|
9083
|
+
if (raw) {
|
|
9084
|
+
txb.setSender(walletAddress);
|
|
9085
|
+
const transactionRaw = await txb.build({
|
|
9086
|
+
client: this.publicClient,
|
|
9087
|
+
onlyTransactionKind: true
|
|
9088
|
+
});
|
|
9089
|
+
const transactionRawBase64String = Buffer.from(transactionRaw).toString("base64");
|
|
9090
|
+
return {
|
|
9091
|
+
from: walletAddress,
|
|
9092
|
+
to: `${connection.packageId}::${connection.moduleId}::send_message_ua`,
|
|
9093
|
+
value: 0n,
|
|
9094
|
+
data: transactionRawBase64String
|
|
9095
|
+
};
|
|
9096
|
+
}
|
|
9097
|
+
return this.walletProvider.signAndExecuteTxn(txb);
|
|
9098
|
+
}
|
|
9099
|
+
async configureAssetManagerHub(hubNetworkId, hubAssetManager) {
|
|
9100
|
+
const tx = new transactions.Transaction();
|
|
9101
|
+
const assetmanager = this.splitAddress(await this.getAssetManagerAddress());
|
|
9102
|
+
tx.moveCall({
|
|
9103
|
+
target: `${assetmanager.packageId}::${assetmanager.moduleId}::set_hub_details`,
|
|
9104
|
+
arguments: [tx.object(assetmanager.stateId), tx.pure.u64(hubNetworkId), tx.pure.vector("u8", hubAssetManager)]
|
|
9059
9105
|
});
|
|
9106
|
+
const result = await this.walletProvider.signAndExecuteTxn(tx);
|
|
9107
|
+
return result;
|
|
9108
|
+
}
|
|
9109
|
+
async getWalletAddress() {
|
|
9110
|
+
return this.walletProvider.getWalletAddress();
|
|
9111
|
+
}
|
|
9112
|
+
async getWalletAddressBytes() {
|
|
9113
|
+
const address = await this.getWalletAddress();
|
|
9114
|
+
return _SuiSpokeProvider.getAddressBCSBytes(address);
|
|
9115
|
+
}
|
|
9116
|
+
static getAddressBCSBytes(suiaddress) {
|
|
9117
|
+
return viem.toHex(bcs.bcs.Address.serialize(suiaddress).toBytes());
|
|
9118
|
+
}
|
|
9119
|
+
async getAssetManagerAddress() {
|
|
9120
|
+
if (!this.assetManagerAddress) {
|
|
9121
|
+
this.assetManagerAddress = await SuiSpokeService.fetchAssetManagerAddress(this);
|
|
9122
|
+
}
|
|
9123
|
+
return this.assetManagerAddress.toString();
|
|
9060
9124
|
}
|
|
9061
9125
|
};
|
|
9062
9126
|
|
|
9063
|
-
// src/
|
|
9064
|
-
var
|
|
9065
|
-
|
|
9066
|
-
|
|
9067
|
-
|
|
9068
|
-
|
|
9069
|
-
|
|
9070
|
-
* @param address - The address on the spoke chain.
|
|
9071
|
-
* @param hubProvider - The hub provider.
|
|
9072
|
-
* @returns The hub wallet address.
|
|
9073
|
-
*/
|
|
9074
|
-
static async getUserHubWalletAddress(chainId, address, hubProvider) {
|
|
9075
|
-
return hubProvider.publicClient.readContract({
|
|
9076
|
-
address: hubProvider.chainConfig.addresses.hubWallet,
|
|
9077
|
-
abi: walletFactoryAbi,
|
|
9078
|
-
functionName: "getDeployedAddress",
|
|
9079
|
-
args: [BigInt(getIntentRelayChainId(chainId)), address]
|
|
9080
|
-
});
|
|
9127
|
+
// src/entities/solana/SolanaSpokeProvider.ts
|
|
9128
|
+
var SolanaSpokeProvider = class {
|
|
9129
|
+
walletProvider;
|
|
9130
|
+
chainConfig;
|
|
9131
|
+
constructor(walletProvider, chainConfig) {
|
|
9132
|
+
this.walletProvider = walletProvider;
|
|
9133
|
+
this.chainConfig = chainConfig;
|
|
9081
9134
|
}
|
|
9082
9135
|
};
|
|
9136
|
+
var solanaSpokeChainConfig = spokeChainConfig[types.SOLANA_MAINNET_CHAIN_ID];
|
|
9137
|
+
function getSolanaAddressBytes(address) {
|
|
9138
|
+
return `0x${Buffer.from(address.toBytes()).toString("hex")}`;
|
|
9139
|
+
}
|
|
9140
|
+
function isNative(address) {
|
|
9141
|
+
if (address.equals(new web3_js.PublicKey(solanaSpokeChainConfig.nativeToken))) {
|
|
9142
|
+
return true;
|
|
9143
|
+
}
|
|
9144
|
+
return false;
|
|
9145
|
+
}
|
|
9146
|
+
function convertTransactionInstructionToRaw(instruction) {
|
|
9147
|
+
return {
|
|
9148
|
+
keys: instruction.keys.map((key) => ({
|
|
9149
|
+
pubkey: key.pubkey.toBase58(),
|
|
9150
|
+
isSigner: key.isSigner,
|
|
9151
|
+
isWritable: key.isWritable
|
|
9152
|
+
})),
|
|
9153
|
+
programId: instruction.programId.toBase58(),
|
|
9154
|
+
data: instruction.data
|
|
9155
|
+
};
|
|
9156
|
+
}
|
|
9157
|
+
|
|
9158
|
+
// src/entities/icon/HanaWalletConnector.ts
|
|
9159
|
+
function requestAddress() {
|
|
9160
|
+
return new Promise((resolve) => {
|
|
9161
|
+
const eventHandler = (event) => {
|
|
9162
|
+
const customEvent = event;
|
|
9163
|
+
const response = customEvent.detail;
|
|
9164
|
+
if (isResponseAddressType(response)) {
|
|
9165
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9166
|
+
resolve({
|
|
9167
|
+
ok: true,
|
|
9168
|
+
value: response.payload
|
|
9169
|
+
});
|
|
9170
|
+
}
|
|
9171
|
+
};
|
|
9172
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9173
|
+
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9174
|
+
window.dispatchEvent(
|
|
9175
|
+
new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
9176
|
+
detail: {
|
|
9177
|
+
type: "REQUEST_ADDRESS"
|
|
9178
|
+
}
|
|
9179
|
+
})
|
|
9180
|
+
);
|
|
9181
|
+
});
|
|
9182
|
+
}
|
|
9183
|
+
function requestSigning(from, hash) {
|
|
9184
|
+
return new Promise((resolve, reject) => {
|
|
9185
|
+
const signRequest = new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
9186
|
+
detail: {
|
|
9187
|
+
type: "REQUEST_SIGNING",
|
|
9188
|
+
payload: {
|
|
9189
|
+
from,
|
|
9190
|
+
hash
|
|
9191
|
+
}
|
|
9192
|
+
}
|
|
9193
|
+
});
|
|
9194
|
+
const eventHandler = (event) => {
|
|
9195
|
+
const customEvent = event;
|
|
9196
|
+
const response = customEvent.detail;
|
|
9197
|
+
if (isResponseSigningType(response)) {
|
|
9198
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9199
|
+
resolve({
|
|
9200
|
+
ok: true,
|
|
9201
|
+
value: response.payload
|
|
9202
|
+
});
|
|
9203
|
+
} else if (response.type === "CANCEL_SIGNING") {
|
|
9204
|
+
reject(new Error("CANCEL_SIGNING"));
|
|
9205
|
+
}
|
|
9206
|
+
};
|
|
9207
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9208
|
+
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9209
|
+
window.dispatchEvent(signRequest);
|
|
9210
|
+
});
|
|
9211
|
+
}
|
|
9212
|
+
function requestJsonRpc(rawTransaction, id = 99999) {
|
|
9213
|
+
return new Promise((resolve, reject) => {
|
|
9214
|
+
const eventHandler = (event) => {
|
|
9215
|
+
const customEvent = event;
|
|
9216
|
+
const { type, payload } = customEvent.detail;
|
|
9217
|
+
if (type === "RESPONSE_JSON-RPC") {
|
|
9218
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9219
|
+
if (isJsonRpcPayloadResponse(payload)) {
|
|
9220
|
+
resolve({
|
|
9221
|
+
ok: true,
|
|
9222
|
+
value: payload
|
|
9223
|
+
});
|
|
9224
|
+
} else {
|
|
9225
|
+
reject(new Error("Invalid payload response type (expected JsonRpcPayloadResponse)"));
|
|
9226
|
+
}
|
|
9227
|
+
} else if (type === "CANCEL_JSON-RPC") {
|
|
9228
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9229
|
+
reject(new Error("CANCEL_JSON-RPC"));
|
|
9230
|
+
}
|
|
9231
|
+
};
|
|
9232
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9233
|
+
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
9234
|
+
window.dispatchEvent(
|
|
9235
|
+
new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
9236
|
+
detail: {
|
|
9237
|
+
type: "REQUEST_JSON-RPC",
|
|
9238
|
+
payload: {
|
|
9239
|
+
jsonrpc: "2.0",
|
|
9240
|
+
method: "icx_sendTransaction",
|
|
9241
|
+
params: rawTransaction,
|
|
9242
|
+
id
|
|
9243
|
+
}
|
|
9244
|
+
}
|
|
9245
|
+
})
|
|
9246
|
+
);
|
|
9247
|
+
});
|
|
9248
|
+
}
|
|
9083
9249
|
var EvmSpokeService = class _EvmSpokeService {
|
|
9084
9250
|
constructor() {
|
|
9085
9251
|
}
|
|
@@ -9941,121 +10107,6 @@ var StellarSpokeService = class _StellarSpokeService {
|
|
|
9941
10107
|
);
|
|
9942
10108
|
}
|
|
9943
10109
|
};
|
|
9944
|
-
var SuiSpokeService = class _SuiSpokeService {
|
|
9945
|
-
constructor() {
|
|
9946
|
-
}
|
|
9947
|
-
/**
|
|
9948
|
-
* Estimate the gas for a transaction.
|
|
9949
|
-
* @param {SuiRawTransaction} rawTx - The raw transaction to estimate the gas for.
|
|
9950
|
-
* @param {SuiSpokeProvider} spokeProvider - The spoke provider.
|
|
9951
|
-
* @returns {Promise<bigint>} The estimated computation cost.
|
|
9952
|
-
*/
|
|
9953
|
-
static async estimateGas(rawTx, spokeProvider) {
|
|
9954
|
-
const txb = transactions.Transaction.fromKind(rawTx.data);
|
|
9955
|
-
const result = await spokeProvider.publicClient.devInspectTransactionBlock({
|
|
9956
|
-
sender: rawTx.from,
|
|
9957
|
-
transactionBlock: txb
|
|
9958
|
-
});
|
|
9959
|
-
return result.effects.gasUsed;
|
|
9960
|
-
}
|
|
9961
|
-
/**
|
|
9962
|
-
* Deposit tokens to the spoke chain.
|
|
9963
|
-
* @param {InjectiveSpokeDepositParams} params - The parameters for the deposit, including the user's address, token address, amount, and additional data.
|
|
9964
|
-
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
9965
|
-
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
9966
|
-
* @param {boolean} raw - The return type raw or just transaction hash
|
|
9967
|
-
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
9968
|
-
*/
|
|
9969
|
-
static async deposit(params, spokeProvider, hubProvider, raw) {
|
|
9970
|
-
const userWallet = params.to ?? await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
9971
|
-
spokeProvider.chainConfig.chain.id,
|
|
9972
|
-
params.from,
|
|
9973
|
-
hubProvider
|
|
9974
|
-
);
|
|
9975
|
-
return _SuiSpokeService.transfer(
|
|
9976
|
-
{
|
|
9977
|
-
token: params.token,
|
|
9978
|
-
recipient: userWallet,
|
|
9979
|
-
amount: params.amount,
|
|
9980
|
-
data: params.data
|
|
9981
|
-
},
|
|
9982
|
-
spokeProvider,
|
|
9983
|
-
raw
|
|
9984
|
-
);
|
|
9985
|
-
}
|
|
9986
|
-
/**
|
|
9987
|
-
* Get the balance of the token in the spoke chain.
|
|
9988
|
-
* @param {Address} token - The address of the token to get the balance of.
|
|
9989
|
-
* @param {SuiSpokeProvider} spokeProvider - The spoke provider.
|
|
9990
|
-
* @returns {Promise<bigint>} The balance of the token.
|
|
9991
|
-
*/
|
|
9992
|
-
static async getDeposit(token, spokeProvider) {
|
|
9993
|
-
return spokeProvider.getBalance(token);
|
|
9994
|
-
}
|
|
9995
|
-
/**
|
|
9996
|
-
* Generate simulation parameters for deposit from SuiSpokeDepositParams.
|
|
9997
|
-
* @param {SuiSpokeDepositParams} params - The deposit parameters.
|
|
9998
|
-
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
9999
|
-
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
10000
|
-
* @returns {Promise<DepositSimulationParams>} The simulation parameters.
|
|
10001
|
-
*/
|
|
10002
|
-
static async getSimulateDepositParams(params, spokeProvider, hubProvider) {
|
|
10003
|
-
const to = params.to ?? await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
10004
|
-
spokeProvider.chainConfig.chain.id,
|
|
10005
|
-
params.from,
|
|
10006
|
-
hubProvider
|
|
10007
|
-
);
|
|
10008
|
-
const encoder = new TextEncoder();
|
|
10009
|
-
return {
|
|
10010
|
-
spokeChainID: spokeProvider.chainConfig.chain.id,
|
|
10011
|
-
token: viem.toHex(encoder.encode(params.token)),
|
|
10012
|
-
from: encodeAddress(spokeProvider.chainConfig.chain.id, params.from),
|
|
10013
|
-
to,
|
|
10014
|
-
amount: params.amount,
|
|
10015
|
-
data: params.data,
|
|
10016
|
-
srcAddress: viem.toHex(encoder.encode(spokeProvider.chainConfig.addresses.assetManagerId))
|
|
10017
|
-
};
|
|
10018
|
-
}
|
|
10019
|
-
/**
|
|
10020
|
-
* Calls a contract on the spoke chain using the user's wallet.
|
|
10021
|
-
* @param {HubAddress} from - The address of the user on the spoke chain.
|
|
10022
|
-
* @param {Hex} payload - The payload to send to the contract.
|
|
10023
|
-
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
10024
|
-
* @param {EvmHubProvider} hubProvider - The provider for the hub chain.
|
|
10025
|
-
* @param {boolean} raw - The return type raw or just transaction hash
|
|
10026
|
-
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
10027
|
-
*/
|
|
10028
|
-
static async callWallet(from, payload, spokeProvider, hubProvider, raw) {
|
|
10029
|
-
const relayId = getIntentRelayChainId(hubProvider.chainConfig.chain.id);
|
|
10030
|
-
return _SuiSpokeService.call(BigInt(relayId), from, payload, spokeProvider, raw);
|
|
10031
|
-
}
|
|
10032
|
-
/**
|
|
10033
|
-
* Transfers tokens to the hub chain.
|
|
10034
|
-
* @param {SuiTransferToHubParams} params - The parameters for the transfer, including:
|
|
10035
|
-
* - {string} token: The address of the token to transfer (use address(0) for native token).
|
|
10036
|
-
* - {Uint8Array} recipient: The recipient address on the hub chain.
|
|
10037
|
-
* - {string} amount: The amount to transfer.
|
|
10038
|
-
* - {Uint8Array} [data=new Uint8Array([])]: Additional data for the transfer.
|
|
10039
|
-
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
10040
|
-
* @param {boolean} raw - The return type raw or just transaction hash
|
|
10041
|
-
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
10042
|
-
*/
|
|
10043
|
-
static async transfer({ token, recipient, amount, data = "0x" }, spokeProvider, raw) {
|
|
10044
|
-
return spokeProvider.transfer(token, amount, viem.fromHex(recipient, "bytes"), viem.fromHex(data, "bytes"), raw);
|
|
10045
|
-
}
|
|
10046
|
-
/**
|
|
10047
|
-
* Sends a message to the hub chain.
|
|
10048
|
-
* @param {bigint} dstChainId - The chain ID of the hub chain.
|
|
10049
|
-
* @param {HubAddress} dstAddress - The address on the hub chain.
|
|
10050
|
-
* @param {Hex} payload - The payload to send.
|
|
10051
|
-
* @param {SuiSpokeProvider} spokeProvider - The provider for the spoke chain.
|
|
10052
|
-
* @param {boolean} raw - The return type raw or just transaction hash
|
|
10053
|
-
* @returns {PromiseSuiTxReturnType<R>} A promise that resolves to the transaction hash or raw transaction base64 string.
|
|
10054
|
-
*/
|
|
10055
|
-
static async call(dstChainId, dstAddress, payload, spokeProvider, raw) {
|
|
10056
|
-
return spokeProvider.sendMessage(dstChainId, viem.fromHex(dstAddress, "bytes"), viem.fromHex(payload, "bytes"), raw);
|
|
10057
|
-
}
|
|
10058
|
-
};
|
|
10059
10110
|
var SonicSpokeService = class _SonicSpokeService {
|
|
10060
10111
|
constructor() {
|
|
10061
10112
|
}
|
|
@@ -16711,6 +16762,7 @@ exports.SonicSpokeService = SonicSpokeService;
|
|
|
16711
16762
|
exports.SpokeService = SpokeService;
|
|
16712
16763
|
exports.StellarSpokeProvider = StellarSpokeProvider;
|
|
16713
16764
|
exports.SuiSpokeProvider = SuiSpokeProvider;
|
|
16765
|
+
exports.SuiSpokeService = SuiSpokeService;
|
|
16714
16766
|
exports.SupportedMigrationTokens = SupportedMigrationTokens;
|
|
16715
16767
|
exports.USD_DECIMALS = USD_DECIMALS;
|
|
16716
16768
|
exports.UiPoolDataProviderService = UiPoolDataProviderService;
|