@orderly.network/core 2.9.1 → 2.10.0-alpha.0
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.d.mts +121 -83
- package/dist/index.d.ts +121 -83
- package/dist/index.js +237 -418
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +237 -417
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -5435,154 +5435,13 @@ var Assets = class {
|
|
|
5435
5435
|
this.contractManger = contractManger;
|
|
5436
5436
|
this.account = account;
|
|
5437
5437
|
}
|
|
5438
|
-
/**
|
|
5439
|
-
* Convert non-USDC asset to USDC manually
|
|
5440
|
-
*/
|
|
5441
|
-
async convert(inputs) {
|
|
5442
|
-
if (!this.account.walletAdapter) {
|
|
5443
|
-
throw new Error("walletAdapter is undefined");
|
|
5444
|
-
}
|
|
5445
|
-
if (!this.account.stateValue.address) {
|
|
5446
|
-
throw new Error("account address is required");
|
|
5447
|
-
}
|
|
5448
|
-
const { slippage, amount, converted_asset } = inputs;
|
|
5449
|
-
if (!converted_asset || converted_asset.toUpperCase() === "USDC") {
|
|
5450
|
-
throw new Error("converted_asset cannot be USDC");
|
|
5451
|
-
}
|
|
5452
|
-
const payload = {
|
|
5453
|
-
slippage,
|
|
5454
|
-
amount,
|
|
5455
|
-
converted_asset
|
|
5456
|
-
};
|
|
5457
|
-
const url = "/v1/asset/manual_convert";
|
|
5458
|
-
const timestamp = getTimestamp().toString();
|
|
5459
|
-
const message = [timestamp, "POST", url, JSON.stringify(payload)].join("");
|
|
5460
|
-
const signer = this.account.signer;
|
|
5461
|
-
const { publicKey, signature } = await signer.signText(message);
|
|
5462
|
-
const res = await this._simpleFetch(url, {
|
|
5463
|
-
method: "POST",
|
|
5464
|
-
headers: {
|
|
5465
|
-
"Content-Type": "application/json",
|
|
5466
|
-
"orderly-account-id": this.account.stateValue.accountId,
|
|
5467
|
-
"orderly-key": publicKey,
|
|
5468
|
-
"orderly-timestamp": timestamp,
|
|
5469
|
-
"orderly-signature": signature
|
|
5470
|
-
},
|
|
5471
|
-
body: JSON.stringify(payload)
|
|
5472
|
-
});
|
|
5473
|
-
if (!res.success) {
|
|
5474
|
-
throw new ApiError(res.message, res.code);
|
|
5475
|
-
}
|
|
5476
|
-
return res;
|
|
5477
|
-
}
|
|
5478
|
-
async withdraw(inputs) {
|
|
5479
|
-
if (!this.account.walletAdapter) {
|
|
5480
|
-
throw new Error("walletAdapter is undefined");
|
|
5481
|
-
}
|
|
5482
|
-
if (!this.account.stateValue.address) {
|
|
5483
|
-
throw new Error("account address is required");
|
|
5484
|
-
}
|
|
5485
|
-
const { chainId, token, allowCrossChainWithdraw, decimals, receiver } = inputs;
|
|
5486
|
-
let { amount } = inputs;
|
|
5487
|
-
if (typeof amount === "number") {
|
|
5488
|
-
amount = amount.toString();
|
|
5489
|
-
}
|
|
5490
|
-
if (decimals === void 0 || decimals === null) {
|
|
5491
|
-
throw new Error("decimals is required");
|
|
5492
|
-
}
|
|
5493
|
-
const url = "/v1/withdraw_request";
|
|
5494
|
-
const nonce = await this.getWithdrawalNonce();
|
|
5495
|
-
const timestamp = getTimestamp();
|
|
5496
|
-
const messageData = {
|
|
5497
|
-
receiver: receiver || this.account.stateValue.address,
|
|
5498
|
-
token,
|
|
5499
|
-
brokerId: this.configStore.get("brokerId"),
|
|
5500
|
-
amount: this.account.walletAdapter.parseUnits(amount, decimals),
|
|
5501
|
-
nonce,
|
|
5502
|
-
timestamp,
|
|
5503
|
-
// domain,
|
|
5504
|
-
verifyContract: this.contractManger.getContractInfoByEnv().verifyContractAddress
|
|
5505
|
-
};
|
|
5506
|
-
const agwGobalAddress = this.account.getAdditionalInfo()?.AGWAddress ?? "";
|
|
5507
|
-
if (ABSTRACT_CHAIN_ID_MAP2.has(chainId) && agwGobalAddress) {
|
|
5508
|
-
messageData.receiver = agwGobalAddress;
|
|
5509
|
-
}
|
|
5510
|
-
const { message, signatured, domain } = await this.account.walletAdapter.generateWithdrawMessage(messageData);
|
|
5511
|
-
const data = {
|
|
5512
|
-
signature: signatured,
|
|
5513
|
-
message,
|
|
5514
|
-
userAddress: this.account.stateValue.address,
|
|
5515
|
-
verifyingContract: domain.verifyingContract
|
|
5516
|
-
};
|
|
5517
|
-
const payload = {
|
|
5518
|
-
method: "POST",
|
|
5519
|
-
url,
|
|
5520
|
-
data
|
|
5521
|
-
};
|
|
5522
|
-
if (allowCrossChainWithdraw) {
|
|
5523
|
-
data.message = {
|
|
5524
|
-
...data.message,
|
|
5525
|
-
// @ts-ignore
|
|
5526
|
-
allowCrossChainWithdraw
|
|
5527
|
-
};
|
|
5528
|
-
}
|
|
5529
|
-
const signature = await this.account.signer.sign(payload, getTimestamp());
|
|
5530
|
-
const res = await this._simpleFetch(url, {
|
|
5531
|
-
method: "POST",
|
|
5532
|
-
body: JSON.stringify(data),
|
|
5533
|
-
headers: {
|
|
5534
|
-
"Content-Type": "application/json",
|
|
5535
|
-
"orderly-account-id": this.account.stateValue.accountId,
|
|
5536
|
-
...signature
|
|
5537
|
-
}
|
|
5538
|
-
});
|
|
5539
|
-
if (!res.success) {
|
|
5540
|
-
throw new ApiError(res.message, res.code);
|
|
5541
|
-
}
|
|
5542
|
-
return res;
|
|
5543
|
-
}
|
|
5544
|
-
async getWithdrawalNonce() {
|
|
5545
|
-
const timestamp = getTimestamp().toString();
|
|
5546
|
-
const url = "/v1/withdraw_nonce";
|
|
5547
|
-
const message = [timestamp, "GET", url].join("");
|
|
5548
|
-
const signer = this.account.signer;
|
|
5549
|
-
const { publicKey, signature } = await signer.signText(message);
|
|
5550
|
-
const res = await this._simpleFetch(url, {
|
|
5551
|
-
headers: {
|
|
5552
|
-
"orderly-account-id": this.account.stateValue.accountId,
|
|
5553
|
-
"orderly-key": publicKey,
|
|
5554
|
-
"orderly-timestamp": timestamp,
|
|
5555
|
-
"orderly-signature": signature
|
|
5556
|
-
}
|
|
5557
|
-
});
|
|
5558
|
-
if (res.success) {
|
|
5559
|
-
return res.data?.withdraw_nonce;
|
|
5560
|
-
} else {
|
|
5561
|
-
throw new Error(res.message);
|
|
5562
|
-
}
|
|
5563
|
-
}
|
|
5564
|
-
async getNativeBalance(options) {
|
|
5565
|
-
if (!this.account.walletAdapter) {
|
|
5566
|
-
return "0";
|
|
5567
|
-
}
|
|
5568
|
-
const result = await this.account.walletAdapter.getBalance();
|
|
5569
|
-
return this.account.walletAdapter?.formatUnits(result, options?.decimals);
|
|
5570
|
-
}
|
|
5571
5438
|
async getBalance(address, options) {
|
|
5572
5439
|
if (!this.account.walletAdapter) {
|
|
5573
5440
|
return "0";
|
|
5574
5441
|
}
|
|
5575
5442
|
const contractInfo = this.contractManger.getContractInfoByEnv();
|
|
5576
|
-
const
|
|
5577
|
-
|
|
5578
|
-
contractInfo
|
|
5579
|
-
);
|
|
5580
|
-
const tokenAddress = address || contractAddress.tokenAddress;
|
|
5581
|
-
let userAddress = this.account.stateValue.address;
|
|
5582
|
-
const agwGobalAddress = this.account.getAdditionalInfo()?.AGWAddress ?? "";
|
|
5583
|
-
if (ABSTRACT_CHAIN_ID_MAP2.has(this.account.walletAdapter.chainId) && agwGobalAddress) {
|
|
5584
|
-
userAddress = agwGobalAddress;
|
|
5585
|
-
}
|
|
5443
|
+
const tokenAddress = address;
|
|
5444
|
+
const userAddress = this.isAbstract() ? this.getAgwGlobalAddress() : this.account.stateValue.address;
|
|
5586
5445
|
const result = await this.account.walletAdapter?.call(
|
|
5587
5446
|
tokenAddress,
|
|
5588
5447
|
"balanceOf",
|
|
@@ -5593,44 +5452,40 @@ var Assets = class {
|
|
|
5593
5452
|
);
|
|
5594
5453
|
return this.account.walletAdapter?.formatUnits(result, options?.decimals);
|
|
5595
5454
|
}
|
|
5455
|
+
async getNativeBalance(options) {
|
|
5456
|
+
if (!this.account.walletAdapter) {
|
|
5457
|
+
return "0";
|
|
5458
|
+
}
|
|
5459
|
+
const result = await this.account.walletAdapter.getBalance();
|
|
5460
|
+
return this.account.walletAdapter?.formatUnits(result, options?.decimals);
|
|
5461
|
+
}
|
|
5596
5462
|
/**
|
|
5597
5463
|
* @deprecated use getBalance instead, will be removed in the future
|
|
5598
5464
|
*/
|
|
5599
5465
|
async getBalanceByAddress(address, options) {
|
|
5466
|
+
return this.getBalance(address, options);
|
|
5467
|
+
}
|
|
5468
|
+
async getBalances(tokens) {
|
|
5600
5469
|
if (!this.account.walletAdapter) {
|
|
5601
|
-
return
|
|
5602
|
-
}
|
|
5603
|
-
const
|
|
5604
|
-
const result = await this.account.walletAdapter
|
|
5605
|
-
|
|
5606
|
-
|
|
5607
|
-
[
|
|
5608
|
-
|
|
5609
|
-
|
|
5610
|
-
|
|
5611
|
-
|
|
5612
|
-
return this.account.walletAdapter?.formatUnits?.(
|
|
5613
|
-
result,
|
|
5614
|
-
options?.decimals
|
|
5615
|
-
);
|
|
5470
|
+
return {};
|
|
5471
|
+
}
|
|
5472
|
+
const addresses = tokens.map((token) => token.address);
|
|
5473
|
+
const result = await this.account.walletAdapter.getBalances(addresses);
|
|
5474
|
+
const balances = {};
|
|
5475
|
+
result.forEach((item, index) => {
|
|
5476
|
+
const { symbol, decimals } = tokens[index];
|
|
5477
|
+
const balance = this.account.walletAdapter?.formatUnits(item, decimals);
|
|
5478
|
+
balances[symbol] = balance;
|
|
5479
|
+
});
|
|
5480
|
+
return balances;
|
|
5616
5481
|
}
|
|
5617
5482
|
async getAllowance(inputs) {
|
|
5618
|
-
const { address, vaultAddress
|
|
5483
|
+
const { address: tokenAddress, vaultAddress, decimals } = inputs;
|
|
5619
5484
|
if (!this.account.walletAdapter) {
|
|
5620
5485
|
return "0";
|
|
5621
5486
|
}
|
|
5622
|
-
|
|
5487
|
+
const userAddress = this.isAbstract() ? this.getAgwGlobalAddress() : this.account.stateValue.address;
|
|
5623
5488
|
const contractInfo = this.contractManger.getContractInfoByEnv();
|
|
5624
|
-
const contractAddress = getContractInfoByChainId(
|
|
5625
|
-
this.account.walletAdapter.chainId,
|
|
5626
|
-
contractInfo
|
|
5627
|
-
);
|
|
5628
|
-
const vaultAddress = inputVaultAddress || contractAddress.vaultAddress;
|
|
5629
|
-
const tokenAddress = address || contractAddress.tokenAddress;
|
|
5630
|
-
const agwGobalAddress = this.account.getAdditionalInfo()?.AGWAddress ?? "";
|
|
5631
|
-
if (ABSTRACT_CHAIN_ID_MAP2.has(this.account.walletAdapter.chainId) && agwGobalAddress) {
|
|
5632
|
-
userAddress = agwGobalAddress;
|
|
5633
|
-
}
|
|
5634
5489
|
console.info("get allowance", {
|
|
5635
5490
|
tokenAddress,
|
|
5636
5491
|
vaultAddress,
|
|
@@ -5648,13 +5503,13 @@ var Assets = class {
|
|
|
5648
5503
|
}
|
|
5649
5504
|
async approve(inputs) {
|
|
5650
5505
|
const {
|
|
5651
|
-
address,
|
|
5506
|
+
address: tokenAddress,
|
|
5652
5507
|
amount,
|
|
5653
|
-
vaultAddress
|
|
5508
|
+
vaultAddress,
|
|
5654
5509
|
decimals,
|
|
5655
5510
|
isSetMaxValue
|
|
5656
5511
|
} = inputs;
|
|
5657
|
-
if (!
|
|
5512
|
+
if (!tokenAddress) {
|
|
5658
5513
|
throw new Error("address is required");
|
|
5659
5514
|
}
|
|
5660
5515
|
if (!this.account.walletAdapter) {
|
|
@@ -5668,12 +5523,6 @@ var Assets = class {
|
|
|
5668
5523
|
parsedAmount = typeof amount !== "undefined" && amount !== "" ? this.account.walletAdapter.parseUnits(amount, decimals) : MaxUint256.toString();
|
|
5669
5524
|
}
|
|
5670
5525
|
const contractInfo = this.contractManger.getContractInfoByEnv();
|
|
5671
|
-
const contractAddress = getContractInfoByChainId(
|
|
5672
|
-
this.account.walletAdapter.chainId,
|
|
5673
|
-
contractInfo
|
|
5674
|
-
);
|
|
5675
|
-
const vaultAddress = inputVaultAddress || contractAddress.vaultAddress;
|
|
5676
|
-
const tokenAddress = address || contractAddress.tokenAddress;
|
|
5677
5526
|
console.info("approve", {
|
|
5678
5527
|
vaultAddress,
|
|
5679
5528
|
tokenAddress,
|
|
@@ -5694,106 +5543,65 @@ var Assets = class {
|
|
|
5694
5543
|
*/
|
|
5695
5544
|
async approveByAddress(inputs) {
|
|
5696
5545
|
const { address, amount, decimals } = inputs;
|
|
5697
|
-
if (!this.account.walletAdapter) {
|
|
5698
|
-
throw new Error("walletAdapter is undefined");
|
|
5699
|
-
}
|
|
5700
|
-
if (decimals === void 0 || decimals === null) {
|
|
5701
|
-
throw new Error("decimals is required");
|
|
5702
|
-
}
|
|
5703
5546
|
const contractInfo = this.contractManger.getContractInfoByEnv();
|
|
5704
5547
|
const contractAddress = getContractInfoByChainId(
|
|
5705
|
-
this.account.walletAdapter
|
|
5548
|
+
this.account.walletAdapter?.chainId,
|
|
5706
5549
|
contractInfo
|
|
5707
5550
|
);
|
|
5708
|
-
|
|
5709
|
-
const parsedAmount = typeof amount !== "undefined" && amount !== "" ? this.account.walletAdapter.parseUnits(amount, decimals) : MaxUint256.toString();
|
|
5710
|
-
const result = await this.account.walletAdapter?.call(
|
|
5551
|
+
return this.approve({
|
|
5711
5552
|
address,
|
|
5712
|
-
"approve",
|
|
5713
|
-
[vaultAddress, parsedAmount],
|
|
5714
|
-
{
|
|
5715
|
-
abi: contractInfo.erc20Abi
|
|
5716
|
-
}
|
|
5717
|
-
);
|
|
5718
|
-
return result;
|
|
5719
|
-
}
|
|
5720
|
-
async getDepositFee(inputs) {
|
|
5721
|
-
const {
|
|
5722
5553
|
amount,
|
|
5723
|
-
|
|
5724
|
-
decimals
|
|
5725
|
-
token = "USDC",
|
|
5726
|
-
address: tokenAddress
|
|
5727
|
-
} = inputs;
|
|
5728
|
-
if (!this.account.walletAdapter) {
|
|
5729
|
-
throw new Error("walletAdapter is undefined");
|
|
5730
|
-
}
|
|
5731
|
-
if (decimals === void 0 || decimals === null) {
|
|
5732
|
-
throw new Error("decimals is required");
|
|
5733
|
-
}
|
|
5734
|
-
const brokerId = this.configStore.get("brokerId");
|
|
5735
|
-
if (!brokerId) throw new Error("[Assets]:brokerId is required");
|
|
5736
|
-
const depositData = {
|
|
5737
|
-
accountId: this.account.accountIdHashStr,
|
|
5738
|
-
brokerHash: parseBrokerHash(brokerId),
|
|
5739
|
-
tokenHash: parseTokenHash(token),
|
|
5740
|
-
tokenAmount: this.account.walletAdapter?.parseUnits(amount, decimals)
|
|
5741
|
-
};
|
|
5742
|
-
const userAddress = this.account.stateValue.address;
|
|
5743
|
-
const contractInfo = this.contractManger.getContractInfoByEnv();
|
|
5744
|
-
const contractAddress = getContractInfoByChainId(
|
|
5745
|
-
this.account.walletAdapter.chainId,
|
|
5746
|
-
contractInfo
|
|
5747
|
-
);
|
|
5748
|
-
let vaultAddress = chain.vault_address;
|
|
5749
|
-
if (this.account.walletAdapter.chainNamespace === ChainNamespace.solana) {
|
|
5750
|
-
if (!vaultAddress) {
|
|
5751
|
-
vaultAddress = contractInfo.solanaVaultAddress;
|
|
5752
|
-
}
|
|
5753
|
-
depositData["tokenAddress"] = tokenAddress || contractInfo.solanaUSDCAddress;
|
|
5754
|
-
}
|
|
5755
|
-
if (!vaultAddress) {
|
|
5756
|
-
vaultAddress = contractAddress.vaultAddress;
|
|
5757
|
-
}
|
|
5758
|
-
console.info("get deposit fee", {
|
|
5759
|
-
userAddress,
|
|
5760
|
-
vaultAddress,
|
|
5761
|
-
depositData
|
|
5554
|
+
vaultAddress: contractAddress.vaultAddress,
|
|
5555
|
+
decimals
|
|
5762
5556
|
});
|
|
5557
|
+
}
|
|
5558
|
+
async getDepositFee(inputs) {
|
|
5559
|
+
const { chain } = inputs;
|
|
5560
|
+
const { userAddress, depositData, abi } = await this.getDepositData(inputs);
|
|
5561
|
+
const vaultAddress = chain.vault_address;
|
|
5763
5562
|
return await this.account.walletAdapter.callOnChain(
|
|
5764
5563
|
chain,
|
|
5765
5564
|
vaultAddress,
|
|
5766
5565
|
"getDepositFee",
|
|
5767
5566
|
[userAddress, depositData],
|
|
5567
|
+
{ abi }
|
|
5568
|
+
);
|
|
5569
|
+
}
|
|
5570
|
+
async estimateDepositGasFee(inputs) {
|
|
5571
|
+
const { vaultAddress, fee } = inputs;
|
|
5572
|
+
const { contractMethod, fromAddress, contractData, abi } = await this.getDepositData(inputs);
|
|
5573
|
+
const gasFee = await this.account.walletAdapter.estimateGasFee(
|
|
5574
|
+
vaultAddress,
|
|
5575
|
+
contractMethod,
|
|
5768
5576
|
{
|
|
5769
|
-
|
|
5770
|
-
|
|
5577
|
+
from: fromAddress,
|
|
5578
|
+
to: vaultAddress,
|
|
5579
|
+
data: contractData,
|
|
5580
|
+
value: fee
|
|
5581
|
+
},
|
|
5582
|
+
{ abi }
|
|
5771
5583
|
);
|
|
5584
|
+
console.info("gasFee", gasFee);
|
|
5585
|
+
return gasFee;
|
|
5772
5586
|
}
|
|
5773
|
-
|
|
5774
|
-
|
|
5775
|
-
const { amount, fee, decimals, token } = inputs;
|
|
5587
|
+
async estimateNativeDepositGasFee(inputs) {
|
|
5588
|
+
const { vaultAddress, fee, amount, decimals, token, address } = inputs;
|
|
5776
5589
|
const tokenAmount = this.account.walletAdapter?.parseUnits(
|
|
5777
5590
|
amount,
|
|
5778
5591
|
decimals
|
|
5779
5592
|
);
|
|
5780
5593
|
const value = BigInt(tokenAmount) + fee;
|
|
5781
|
-
return this.
|
|
5594
|
+
return this.estimateDepositGasFee({
|
|
5782
5595
|
amount,
|
|
5783
5596
|
fee: value,
|
|
5784
5597
|
decimals,
|
|
5785
|
-
token
|
|
5598
|
+
token,
|
|
5599
|
+
address,
|
|
5600
|
+
vaultAddress
|
|
5786
5601
|
});
|
|
5787
5602
|
}
|
|
5788
|
-
async
|
|
5789
|
-
const {
|
|
5790
|
-
amount,
|
|
5791
|
-
fee,
|
|
5792
|
-
decimals,
|
|
5793
|
-
token = "USDC",
|
|
5794
|
-
address: tokenAddress,
|
|
5795
|
-
vaultAddress: inputVaultAddress
|
|
5796
|
-
} = inputs;
|
|
5603
|
+
async getDepositData(inputs) {
|
|
5604
|
+
const { amount, decimals, token = "USDC", address: tokenAddress } = inputs;
|
|
5797
5605
|
if (!this.account.walletAdapter) {
|
|
5798
5606
|
throw new Error("walletAdapter is undefined");
|
|
5799
5607
|
}
|
|
@@ -5801,50 +5609,45 @@ var Assets = class {
|
|
|
5801
5609
|
throw new Error("decimals is required");
|
|
5802
5610
|
}
|
|
5803
5611
|
const brokerId = this.configStore.get("brokerId");
|
|
5804
|
-
if (!brokerId)
|
|
5612
|
+
if (!brokerId) {
|
|
5613
|
+
throw new Error("[Assets]:brokerId is required");
|
|
5614
|
+
}
|
|
5805
5615
|
const contractInfo = this.contractManger.getContractInfoByEnv();
|
|
5806
|
-
const contractAddress = getContractInfoByChainId(
|
|
5807
|
-
this.account.walletAdapter.chainId,
|
|
5808
|
-
contractInfo
|
|
5809
|
-
);
|
|
5810
5616
|
const depositData = {
|
|
5811
5617
|
accountId: this.account.accountIdHashStr,
|
|
5812
5618
|
brokerHash: parseBrokerHash(brokerId),
|
|
5813
5619
|
tokenHash: parseTokenHash(token),
|
|
5814
5620
|
tokenAmount: this.account.walletAdapter?.parseUnits(amount, decimals)
|
|
5815
5621
|
};
|
|
5816
|
-
|
|
5817
|
-
|
|
5818
|
-
if (this.account.walletAdapter.chainNamespace === ChainNamespace.solana) {
|
|
5819
|
-
if (!vaultAddress) {
|
|
5820
|
-
vaultAddress = contractInfo.solanaVaultAddress;
|
|
5821
|
-
}
|
|
5822
|
-
depositData["tokenAddress"] = tokenAddress || contractInfo.solanaUSDCAddress;
|
|
5823
|
-
}
|
|
5824
|
-
if (!vaultAddress) {
|
|
5825
|
-
vaultAddress = contractAddress.vaultAddress;
|
|
5622
|
+
if (this.isSolana()) {
|
|
5623
|
+
depositData["tokenAddress"] = tokenAddress;
|
|
5826
5624
|
}
|
|
5827
|
-
const
|
|
5625
|
+
const userAddress = this.account.stateValue.address;
|
|
5828
5626
|
let contractMethod = "deposit";
|
|
5829
5627
|
let fromAddress = userAddress;
|
|
5830
5628
|
let contractData = [depositData];
|
|
5831
|
-
if (
|
|
5832
|
-
console.info(
|
|
5833
|
-
"agw address",
|
|
5834
|
-
agwGobalAddress,
|
|
5835
|
-
this.account.walletAdapter.chainId
|
|
5836
|
-
);
|
|
5629
|
+
if (this.isAbstract()) {
|
|
5837
5630
|
contractMethod = "depositTo";
|
|
5838
|
-
fromAddress =
|
|
5631
|
+
fromAddress = this.getAgwGlobalAddress();
|
|
5839
5632
|
contractData = [userAddress, depositData];
|
|
5840
5633
|
}
|
|
5841
|
-
console.info("
|
|
5842
|
-
vaultAddress,
|
|
5634
|
+
console.info("deposit data", {
|
|
5843
5635
|
fromAddress,
|
|
5844
5636
|
contractMethod,
|
|
5845
|
-
contractData
|
|
5846
|
-
fee
|
|
5637
|
+
contractData
|
|
5847
5638
|
});
|
|
5639
|
+
return {
|
|
5640
|
+
contractMethod,
|
|
5641
|
+
userAddress,
|
|
5642
|
+
fromAddress,
|
|
5643
|
+
depositData,
|
|
5644
|
+
contractData,
|
|
5645
|
+
abi: contractInfo.vaultAbi
|
|
5646
|
+
};
|
|
5647
|
+
}
|
|
5648
|
+
async deposit(inputs) {
|
|
5649
|
+
const { fee, vaultAddress } = inputs;
|
|
5650
|
+
const { contractMethod, fromAddress, contractData, abi } = await this.getDepositData(inputs);
|
|
5848
5651
|
const result = await this.account.walletAdapter?.sendTransaction(
|
|
5849
5652
|
vaultAddress,
|
|
5850
5653
|
contractMethod,
|
|
@@ -5854,12 +5657,112 @@ var Assets = class {
|
|
|
5854
5657
|
data: contractData,
|
|
5855
5658
|
value: fee
|
|
5856
5659
|
},
|
|
5857
|
-
{
|
|
5858
|
-
abi: contractInfo.vaultAbi
|
|
5859
|
-
}
|
|
5660
|
+
{ abi }
|
|
5860
5661
|
);
|
|
5861
5662
|
return result;
|
|
5862
5663
|
}
|
|
5664
|
+
/** deposit native token */
|
|
5665
|
+
async depositNativeToken(inputs) {
|
|
5666
|
+
const { amount, fee, decimals, token, vaultAddress } = inputs;
|
|
5667
|
+
const tokenAmount = this.account.walletAdapter?.parseUnits(
|
|
5668
|
+
amount,
|
|
5669
|
+
decimals
|
|
5670
|
+
);
|
|
5671
|
+
const value = BigInt(tokenAmount) + fee;
|
|
5672
|
+
return this.deposit({
|
|
5673
|
+
amount,
|
|
5674
|
+
fee: value,
|
|
5675
|
+
decimals,
|
|
5676
|
+
token,
|
|
5677
|
+
vaultAddress
|
|
5678
|
+
});
|
|
5679
|
+
}
|
|
5680
|
+
async withdraw(inputs) {
|
|
5681
|
+
if (!this.account.walletAdapter) {
|
|
5682
|
+
throw new Error("walletAdapter is undefined");
|
|
5683
|
+
}
|
|
5684
|
+
if (!this.account.stateValue.address) {
|
|
5685
|
+
throw new Error("account address is required");
|
|
5686
|
+
}
|
|
5687
|
+
const { chainId, token, allowCrossChainWithdraw, decimals, receiver } = inputs;
|
|
5688
|
+
let { amount } = inputs;
|
|
5689
|
+
if (typeof amount === "number") {
|
|
5690
|
+
amount = amount.toString();
|
|
5691
|
+
}
|
|
5692
|
+
if (decimals === void 0 || decimals === null) {
|
|
5693
|
+
throw new Error("decimals is required");
|
|
5694
|
+
}
|
|
5695
|
+
const url = "/v1/withdraw_request";
|
|
5696
|
+
const nonce = await this.getWithdrawalNonce();
|
|
5697
|
+
const timestamp = getTimestamp();
|
|
5698
|
+
const messageData = {
|
|
5699
|
+
receiver: receiver || this.account.stateValue.address,
|
|
5700
|
+
token,
|
|
5701
|
+
brokerId: this.configStore.get("brokerId"),
|
|
5702
|
+
amount: this.account.walletAdapter.parseUnits(amount, decimals),
|
|
5703
|
+
nonce,
|
|
5704
|
+
timestamp,
|
|
5705
|
+
// domain,
|
|
5706
|
+
verifyContract: this.contractManger.getContractInfoByEnv().verifyContractAddress
|
|
5707
|
+
};
|
|
5708
|
+
const agwGobalAddress = this.account.getAdditionalInfo()?.AGWAddress ?? "";
|
|
5709
|
+
if (ABSTRACT_CHAIN_ID_MAP2.has(chainId) && agwGobalAddress) {
|
|
5710
|
+
messageData.receiver = agwGobalAddress;
|
|
5711
|
+
}
|
|
5712
|
+
const { message, signatured, domain } = await this.account.walletAdapter.generateWithdrawMessage(messageData);
|
|
5713
|
+
const data = {
|
|
5714
|
+
signature: signatured,
|
|
5715
|
+
message,
|
|
5716
|
+
userAddress: this.account.stateValue.address,
|
|
5717
|
+
verifyingContract: domain.verifyingContract
|
|
5718
|
+
};
|
|
5719
|
+
const payload = {
|
|
5720
|
+
method: "POST",
|
|
5721
|
+
url,
|
|
5722
|
+
data
|
|
5723
|
+
};
|
|
5724
|
+
if (allowCrossChainWithdraw) {
|
|
5725
|
+
data.message = {
|
|
5726
|
+
...data.message,
|
|
5727
|
+
// @ts-ignore
|
|
5728
|
+
allowCrossChainWithdraw
|
|
5729
|
+
};
|
|
5730
|
+
}
|
|
5731
|
+
const signature = await this.account.signer.sign(payload, getTimestamp());
|
|
5732
|
+
const res = await this._simpleFetch(url, {
|
|
5733
|
+
method: "POST",
|
|
5734
|
+
body: JSON.stringify(data),
|
|
5735
|
+
headers: {
|
|
5736
|
+
"Content-Type": "application/json",
|
|
5737
|
+
"orderly-account-id": this.account.stateValue.accountId,
|
|
5738
|
+
...signature
|
|
5739
|
+
}
|
|
5740
|
+
});
|
|
5741
|
+
if (!res.success) {
|
|
5742
|
+
throw new ApiError(res.message, res.code);
|
|
5743
|
+
}
|
|
5744
|
+
return res;
|
|
5745
|
+
}
|
|
5746
|
+
async getWithdrawalNonce() {
|
|
5747
|
+
const timestamp = getTimestamp().toString();
|
|
5748
|
+
const url = "/v1/withdraw_nonce";
|
|
5749
|
+
const message = [timestamp, "GET", url].join("");
|
|
5750
|
+
const signer = this.account.signer;
|
|
5751
|
+
const { publicKey, signature } = await signer.signText(message);
|
|
5752
|
+
const res = await this._simpleFetch(url, {
|
|
5753
|
+
headers: {
|
|
5754
|
+
"orderly-account-id": this.account.stateValue.accountId,
|
|
5755
|
+
"orderly-key": publicKey,
|
|
5756
|
+
"orderly-timestamp": timestamp,
|
|
5757
|
+
"orderly-signature": signature
|
|
5758
|
+
}
|
|
5759
|
+
});
|
|
5760
|
+
if (res.success) {
|
|
5761
|
+
return res.data?.withdraw_nonce;
|
|
5762
|
+
} else {
|
|
5763
|
+
throw new Error(res.message);
|
|
5764
|
+
}
|
|
5765
|
+
}
|
|
5863
5766
|
async internalTransfer(inputs) {
|
|
5864
5767
|
if (!this.account.walletAdapter) {
|
|
5865
5768
|
throw new Error("walletAdapter is undefined");
|
|
@@ -5906,6 +5809,46 @@ var Assets = class {
|
|
|
5906
5809
|
}
|
|
5907
5810
|
return res;
|
|
5908
5811
|
}
|
|
5812
|
+
/**
|
|
5813
|
+
* Convert non-USDC asset to USDC manually
|
|
5814
|
+
*/
|
|
5815
|
+
async convert(inputs) {
|
|
5816
|
+
if (!this.account.walletAdapter) {
|
|
5817
|
+
throw new Error("walletAdapter is undefined");
|
|
5818
|
+
}
|
|
5819
|
+
if (!this.account.stateValue.address) {
|
|
5820
|
+
throw new Error("account address is required");
|
|
5821
|
+
}
|
|
5822
|
+
const { slippage, amount, converted_asset } = inputs;
|
|
5823
|
+
if (!converted_asset || converted_asset.toUpperCase() === "USDC") {
|
|
5824
|
+
throw new Error("converted_asset cannot be USDC");
|
|
5825
|
+
}
|
|
5826
|
+
const payload = {
|
|
5827
|
+
slippage,
|
|
5828
|
+
amount,
|
|
5829
|
+
converted_asset
|
|
5830
|
+
};
|
|
5831
|
+
const url = "/v1/asset/manual_convert";
|
|
5832
|
+
const timestamp = getTimestamp().toString();
|
|
5833
|
+
const message = [timestamp, "POST", url, JSON.stringify(payload)].join("");
|
|
5834
|
+
const signer = this.account.signer;
|
|
5835
|
+
const { publicKey, signature } = await signer.signText(message);
|
|
5836
|
+
const res = await this._simpleFetch(url, {
|
|
5837
|
+
method: "POST",
|
|
5838
|
+
headers: {
|
|
5839
|
+
"Content-Type": "application/json",
|
|
5840
|
+
"orderly-account-id": this.account.stateValue.accountId,
|
|
5841
|
+
"orderly-key": publicKey,
|
|
5842
|
+
"orderly-timestamp": timestamp,
|
|
5843
|
+
"orderly-signature": signature
|
|
5844
|
+
},
|
|
5845
|
+
body: JSON.stringify(payload)
|
|
5846
|
+
});
|
|
5847
|
+
if (!res.success) {
|
|
5848
|
+
throw new ApiError(res.message, res.code);
|
|
5849
|
+
}
|
|
5850
|
+
return res;
|
|
5851
|
+
}
|
|
5909
5852
|
async getTransferNonce() {
|
|
5910
5853
|
const timestamp = getTimestamp().toString();
|
|
5911
5854
|
const url = "/v1/transfer_nonce";
|
|
@@ -5933,6 +5876,16 @@ var Assets = class {
|
|
|
5933
5876
|
get usdcAddress() {
|
|
5934
5877
|
return this.contractManger.getContractInfoByEnv().usdcAddress;
|
|
5935
5878
|
}
|
|
5879
|
+
isSolana() {
|
|
5880
|
+
return this.account.walletAdapter?.chainNamespace === ChainNamespace.solana;
|
|
5881
|
+
}
|
|
5882
|
+
isAbstract() {
|
|
5883
|
+
const agwGobalAddress = this.getAgwGlobalAddress();
|
|
5884
|
+
return agwGobalAddress && ABSTRACT_CHAIN_ID_MAP2.has(this.account.walletAdapter?.chainId);
|
|
5885
|
+
}
|
|
5886
|
+
getAgwGlobalAddress() {
|
|
5887
|
+
return this.account.getAdditionalInfo()?.AGWAddress ?? "";
|
|
5888
|
+
}
|
|
5936
5889
|
};
|
|
5937
5890
|
|
|
5938
5891
|
// src/repository.ts
|
|
@@ -7080,138 +7033,6 @@ var BaseWalletAdapter = class {
|
|
|
7080
7033
|
|
|
7081
7034
|
// src/wallet/index.ts
|
|
7082
7035
|
init_shim();
|
|
7083
|
-
|
|
7084
|
-
// src/wallet/etherAdapter.ts
|
|
7085
|
-
init_shim();
|
|
7086
|
-
import { BrowserProvider, ethers as ethers2, toNumber } from "ethers";
|
|
7087
|
-
import { ErrorDecoder } from "ethers-decode-error";
|
|
7088
|
-
var errorDecoder = ErrorDecoder.create();
|
|
7089
|
-
var EtherAdapter = class {
|
|
7090
|
-
constructor(options) {
|
|
7091
|
-
this._chainId = options.chain.id;
|
|
7092
|
-
this.provider = new BrowserProvider(options.provider, "any");
|
|
7093
|
-
this._address = options.address;
|
|
7094
|
-
}
|
|
7095
|
-
parseUnits(amount, decimals) {
|
|
7096
|
-
return ethers2.parseUnits(amount, decimals).toString();
|
|
7097
|
-
}
|
|
7098
|
-
formatUnits(amount, decimals) {
|
|
7099
|
-
return ethers2.formatUnits(amount, decimals);
|
|
7100
|
-
}
|
|
7101
|
-
getBalance(userAddress) {
|
|
7102
|
-
return this.provider.getBalance(userAddress).then(
|
|
7103
|
-
(res) => {
|
|
7104
|
-
return res;
|
|
7105
|
-
},
|
|
7106
|
-
(error) => {
|
|
7107
|
-
}
|
|
7108
|
-
);
|
|
7109
|
-
}
|
|
7110
|
-
deposit(from, to, amount) {
|
|
7111
|
-
throw new Error("Method not implemented.");
|
|
7112
|
-
}
|
|
7113
|
-
async call(address, method, params, options) {
|
|
7114
|
-
const singer = await this.provider?.getSigner();
|
|
7115
|
-
const contract = new ethers2.Contract(address, options.abi, singer);
|
|
7116
|
-
return contract[method].apply(null, params).catch(async (error) => {
|
|
7117
|
-
const parsedEthersError = await parseError(error);
|
|
7118
|
-
throw parsedEthersError;
|
|
7119
|
-
});
|
|
7120
|
-
}
|
|
7121
|
-
async callOnChain(chain, address, method, params, options) {
|
|
7122
|
-
const provider = new ethers2.JsonRpcProvider(chain.public_rpc_url);
|
|
7123
|
-
const contract = new ethers2.Contract(address, options.abi, provider);
|
|
7124
|
-
return contract[method].apply(null, params).catch(async (error) => {
|
|
7125
|
-
const parsedEthersError = await parseError(error);
|
|
7126
|
-
throw parsedEthersError;
|
|
7127
|
-
});
|
|
7128
|
-
}
|
|
7129
|
-
get chainId() {
|
|
7130
|
-
return this._chainId;
|
|
7131
|
-
}
|
|
7132
|
-
set chainId(chainId) {
|
|
7133
|
-
this._chainId = chainId;
|
|
7134
|
-
}
|
|
7135
|
-
get addresses() {
|
|
7136
|
-
return this._address;
|
|
7137
|
-
}
|
|
7138
|
-
async send(method, params) {
|
|
7139
|
-
return await this.provider?.send(method, params);
|
|
7140
|
-
}
|
|
7141
|
-
async sendTransaction(contractAddress, method, payload, options) {
|
|
7142
|
-
const singer = await this.provider?.getSigner();
|
|
7143
|
-
if (!singer) {
|
|
7144
|
-
throw new Error("singer is not exist");
|
|
7145
|
-
}
|
|
7146
|
-
const contract = new ethers2.Contract(
|
|
7147
|
-
contractAddress,
|
|
7148
|
-
options.abi,
|
|
7149
|
-
this.provider
|
|
7150
|
-
);
|
|
7151
|
-
const encodeFunctionData = contract.interface.encodeFunctionData(
|
|
7152
|
-
method,
|
|
7153
|
-
payload.data
|
|
7154
|
-
);
|
|
7155
|
-
const tx = {
|
|
7156
|
-
from: payload.from,
|
|
7157
|
-
to: payload.to,
|
|
7158
|
-
data: encodeFunctionData,
|
|
7159
|
-
value: payload.value
|
|
7160
|
-
};
|
|
7161
|
-
try {
|
|
7162
|
-
const result = await singer.sendTransaction(tx);
|
|
7163
|
-
return result;
|
|
7164
|
-
} catch (error) {
|
|
7165
|
-
const parsedEthersError = await parseError(error);
|
|
7166
|
-
throw parsedEthersError;
|
|
7167
|
-
}
|
|
7168
|
-
}
|
|
7169
|
-
async getTransactionRecipect(txHash) {
|
|
7170
|
-
await this.provider.getTransactionReceipt(txHash);
|
|
7171
|
-
}
|
|
7172
|
-
async pollTransactionReceiptWithBackoff(txHash, baseInterval = 1e3, maxInterval = 6e3, maxRetries = 30) {
|
|
7173
|
-
let interval = baseInterval;
|
|
7174
|
-
let retries = 0;
|
|
7175
|
-
while (retries < maxRetries) {
|
|
7176
|
-
try {
|
|
7177
|
-
const receipt = await this.provider.getTransactionReceipt(txHash);
|
|
7178
|
-
if (receipt) {
|
|
7179
|
-
return receipt;
|
|
7180
|
-
}
|
|
7181
|
-
} catch (error) {
|
|
7182
|
-
}
|
|
7183
|
-
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
7184
|
-
interval = Math.min(interval * 2, maxInterval);
|
|
7185
|
-
retries++;
|
|
7186
|
-
}
|
|
7187
|
-
throw new Error("Transaction did not complete after maximum retries.");
|
|
7188
|
-
}
|
|
7189
|
-
async estimateGas(tx) {
|
|
7190
|
-
const gas = await this.provider.estimateGas(tx);
|
|
7191
|
-
return toNumber(gas);
|
|
7192
|
-
}
|
|
7193
|
-
async signTypedData(address, data) {
|
|
7194
|
-
return await this.provider?.send("eth_signTypedData_v4", [address, data]);
|
|
7195
|
-
}
|
|
7196
|
-
async verify(data, signature) {
|
|
7197
|
-
const { domain, types, message } = data;
|
|
7198
|
-
const recovered = ethers2.verifyTypedData(domain, types, message, signature);
|
|
7199
|
-
}
|
|
7200
|
-
on(eventName, listener) {
|
|
7201
|
-
this.provider?.on(eventName, listener);
|
|
7202
|
-
}
|
|
7203
|
-
off(eventName, listener) {
|
|
7204
|
-
this.provider?.off(eventName, listener);
|
|
7205
|
-
}
|
|
7206
|
-
getContract(address, abi) {
|
|
7207
|
-
const contract = new ethers2.Contract(address, abi, this.provider);
|
|
7208
|
-
return contract;
|
|
7209
|
-
}
|
|
7210
|
-
};
|
|
7211
|
-
async function parseError(rawError) {
|
|
7212
|
-
const error = await errorDecoder.decode(rawError);
|
|
7213
|
-
return error;
|
|
7214
|
-
}
|
|
7215
7036
|
export {
|
|
7216
7037
|
API_URLS,
|
|
7217
7038
|
Account,
|
|
@@ -7222,7 +7043,6 @@ export {
|
|
|
7222
7043
|
BaseWalletAdapter,
|
|
7223
7044
|
DefaultConfigStore,
|
|
7224
7045
|
EVENT_NAMES,
|
|
7225
|
-
EtherAdapter,
|
|
7226
7046
|
default2 as EventEmitter,
|
|
7227
7047
|
LocalStorageStore,
|
|
7228
7048
|
MockKeyStore,
|