@uswap/toolboxes 4.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cardano/index.cjs +4 -0
- package/dist/src/cardano/index.cjs.map +11 -0
- package/dist/src/cardano/index.js +4 -0
- package/dist/src/cardano/index.js.map +11 -0
- package/dist/src/cosmos/index.cjs +4 -0
- package/dist/src/cosmos/index.cjs.map +20 -0
- package/dist/src/cosmos/index.js +4 -0
- package/dist/src/cosmos/index.js.map +20 -0
- package/dist/src/evm/index.cjs +4 -0
- package/dist/src/evm/index.cjs.map +20 -0
- package/dist/src/evm/index.js +4 -0
- package/dist/src/evm/index.js.map +20 -0
- package/dist/src/index.cjs +5 -0
- package/dist/src/index.cjs.map +67 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +67 -0
- package/dist/src/near/index.cjs +4 -0
- package/dist/src/near/index.cjs.map +16 -0
- package/dist/src/near/index.js +4 -0
- package/dist/src/near/index.js.map +16 -0
- package/dist/src/radix/index.cjs +4 -0
- package/dist/src/radix/index.cjs.map +10 -0
- package/dist/src/radix/index.js +4 -0
- package/dist/src/radix/index.js.map +10 -0
- package/dist/src/ripple/index.cjs +4 -0
- package/dist/src/ripple/index.cjs.map +10 -0
- package/dist/src/ripple/index.js +4 -0
- package/dist/src/ripple/index.js.map +10 -0
- package/dist/src/solana/index.cjs +4 -0
- package/dist/src/solana/index.cjs.map +11 -0
- package/dist/src/solana/index.js +4 -0
- package/dist/src/solana/index.js.map +11 -0
- package/dist/src/substrate/index.cjs +4 -0
- package/dist/src/substrate/index.cjs.map +13 -0
- package/dist/src/substrate/index.js +4 -0
- package/dist/src/substrate/index.js.map +13 -0
- package/dist/src/sui/index.cjs +4 -0
- package/dist/src/sui/index.cjs.map +11 -0
- package/dist/src/sui/index.js +4 -0
- package/dist/src/sui/index.js.map +11 -0
- package/dist/src/ton/index.cjs +4 -0
- package/dist/src/ton/index.cjs.map +11 -0
- package/dist/src/ton/index.js +4 -0
- package/dist/src/ton/index.js.map +11 -0
- package/dist/src/tron/index.cjs +4 -0
- package/dist/src/tron/index.cjs.map +13 -0
- package/dist/src/tron/index.js +4 -0
- package/dist/src/tron/index.js.map +13 -0
- package/dist/src/utxo/index.cjs +5 -0
- package/dist/src/utxo/index.cjs.map +21 -0
- package/dist/src/utxo/index.js +5 -0
- package/dist/src/utxo/index.js.map +21 -0
- package/dist/types/cardano/index.d.ts +3 -0
- package/dist/types/cardano/index.d.ts.map +1 -0
- package/dist/types/cardano/toolbox.d.ts +34 -0
- package/dist/types/cardano/toolbox.d.ts.map +1 -0
- package/dist/types/cardano/types.d.ts +11 -0
- package/dist/types/cardano/types.d.ts.map +1 -0
- package/dist/types/cosmos/index.d.ts +5 -0
- package/dist/types/cosmos/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +63 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts +62 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/index.d.ts +15 -0
- package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
- package/dist/types/cosmos/types.d.ts +49 -0
- package/dist/types/cosmos/types.d.ts.map +1 -0
- package/dist/types/cosmos/util.d.ts +74 -0
- package/dist/types/cosmos/util.d.ts.map +1 -0
- package/dist/types/evm/api.d.ts +8 -0
- package/dist/types/evm/api.d.ts.map +1 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
- package/dist/types/evm/helpers.d.ts +6 -0
- package/dist/types/evm/helpers.d.ts.map +1 -0
- package/dist/types/evm/index.d.ts +5 -0
- package/dist/types/evm/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +83 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
- package/dist/types/evm/toolbox/evm.d.ts +767 -0
- package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
- package/dist/types/evm/toolbox/index.d.ts +7 -0
- package/dist/types/evm/toolbox/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/op.d.ts +76 -0
- package/dist/types/evm/toolbox/op.d.ts.map +1 -0
- package/dist/types/evm/types.d.ts +108 -0
- package/dist/types/evm/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +75 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/near/helpers/core.d.ts +15 -0
- package/dist/types/near/helpers/core.d.ts.map +1 -0
- package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
- package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
- package/dist/types/near/helpers/nep141.d.ts +36 -0
- package/dist/types/near/helpers/nep141.d.ts.map +1 -0
- package/dist/types/near/index.d.ts +10 -0
- package/dist/types/near/index.d.ts.map +1 -0
- package/dist/types/near/toolbox.d.ts +32 -0
- package/dist/types/near/toolbox.d.ts.map +1 -0
- package/dist/types/near/types/contract.d.ts +38 -0
- package/dist/types/near/types/contract.d.ts.map +1 -0
- package/dist/types/near/types/nep141.d.ts +29 -0
- package/dist/types/near/types/nep141.d.ts.map +1 -0
- package/dist/types/near/types/toolbox.d.ts +51 -0
- package/dist/types/near/types/toolbox.d.ts.map +1 -0
- package/dist/types/near/types.d.ts +47 -0
- package/dist/types/near/types.d.ts.map +1 -0
- package/dist/types/radix/index.d.ts +14 -0
- package/dist/types/radix/index.d.ts.map +1 -0
- package/dist/types/ripple/index.d.ts +46 -0
- package/dist/types/ripple/index.d.ts.map +1 -0
- package/dist/types/solana/index.d.ts +23 -0
- package/dist/types/solana/index.d.ts.map +1 -0
- package/dist/types/solana/toolbox.d.ts +51 -0
- package/dist/types/solana/toolbox.d.ts.map +1 -0
- package/dist/types/substrate/balance.d.ts +17 -0
- package/dist/types/substrate/balance.d.ts.map +1 -0
- package/dist/types/substrate/index.d.ts +3 -0
- package/dist/types/substrate/index.d.ts.map +1 -0
- package/dist/types/substrate/substrate.d.ts +148 -0
- package/dist/types/substrate/substrate.d.ts.map +1 -0
- package/dist/types/substrate/types.d.ts +100 -0
- package/dist/types/substrate/types.d.ts.map +1 -0
- package/dist/types/sui/index.d.ts +3 -0
- package/dist/types/sui/index.d.ts.map +1 -0
- package/dist/types/sui/toolbox.d.ts +19 -0
- package/dist/types/sui/toolbox.d.ts.map +1 -0
- package/dist/types/sui/types.d.ts +16 -0
- package/dist/types/sui/types.d.ts.map +1 -0
- package/dist/types/ton/index.d.ts +3 -0
- package/dist/types/ton/index.d.ts.map +1 -0
- package/dist/types/ton/toolbox.d.ts +14 -0
- package/dist/types/ton/toolbox.d.ts.map +1 -0
- package/dist/types/ton/types.d.ts +22 -0
- package/dist/types/ton/types.d.ts.map +1 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
- package/dist/types/tron/helpers/trongrid.d.ts +8 -0
- package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
- package/dist/types/tron/index.d.ts +6 -0
- package/dist/types/tron/index.d.ts.map +1 -0
- package/dist/types/tron/toolbox.d.ts +26 -0
- package/dist/types/tron/toolbox.d.ts.map +1 -0
- package/dist/types/tron/types.d.ts +103 -0
- package/dist/types/tron/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +26 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utxo/helpers/api.d.ts +101 -0
- package/dist/types/utxo/helpers/api.d.ts.map +1 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
- package/dist/types/utxo/helpers/coinselect.d.ts +17 -0
- package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
- package/dist/types/utxo/helpers/index.d.ts +5 -0
- package/dist/types/utxo/helpers/index.d.ts.map +1 -0
- package/dist/types/utxo/helpers/txSize.d.ts +21 -0
- package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
- package/dist/types/utxo/index.d.ts +7 -0
- package/dist/types/utxo/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts +93 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/index.d.ts +28 -0
- package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/params.d.ts +32 -0
- package/dist/types/utxo/toolbox/params.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/utxo.d.ts +103 -0
- package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/validators.d.ts +4 -0
- package/dist/types/utxo/toolbox/validators.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/zcash.d.ts +72 -0
- package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
- package/dist/types/utxo/types.d.ts +46 -0
- package/dist/types/utxo/types.d.ts.map +1 -0
- package/package.json +205 -0
- package/src/__tests__/address-validation-all-chains.test.ts +162 -0
- package/src/__tests__/addressValidator.test.ts +162 -0
- package/src/cardano/__tests__/toolbox.test.ts +48 -0
- package/src/cardano/index.ts +2 -0
- package/src/cardano/toolbox.ts +168 -0
- package/src/cardano/types.ts +10 -0
- package/src/cosmos/__tests__/toolbox.test.ts +91 -0
- package/src/cosmos/index.ts +4 -0
- package/src/cosmos/thorchainUtils/addressFormat.ts +22 -0
- package/src/cosmos/thorchainUtils/index.ts +4 -0
- package/src/cosmos/thorchainUtils/messages.ts +212 -0
- package/src/cosmos/thorchainUtils/registry.ts +43 -0
- package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +2800 -0
- package/src/cosmos/thorchainUtils/types/client-types.ts +54 -0
- package/src/cosmos/thorchainUtils/types/index.ts +1 -0
- package/src/cosmos/toolbox/cosmos.ts +345 -0
- package/src/cosmos/toolbox/index.ts +35 -0
- package/src/cosmos/toolbox/thorchain.ts +249 -0
- package/src/cosmos/types.ts +48 -0
- package/src/cosmos/util.ts +214 -0
- package/src/evm/__tests__/address-validation.test.ts +84 -0
- package/src/evm/__tests__/ethereum.test.ts +137 -0
- package/src/evm/__tests__/signMessage.test.ts +60 -0
- package/src/evm/api.ts +10 -0
- package/src/evm/contracts/eth/multicall.ts +165 -0
- package/src/evm/contracts/op/gasOracle.ts +145 -0
- package/src/evm/helpers.ts +73 -0
- package/src/evm/index.ts +4 -0
- package/src/evm/toolbox/baseEVMToolbox.ts +695 -0
- package/src/evm/toolbox/evm.ts +67 -0
- package/src/evm/toolbox/index.ts +44 -0
- package/src/evm/toolbox/op.ts +156 -0
- package/src/evm/types.ts +146 -0
- package/src/index.ts +260 -0
- package/src/near/__tests__/core.test.ts +70 -0
- package/src/near/helpers/core.ts +85 -0
- package/src/near/helpers/gasEstimation.ts +96 -0
- package/src/near/helpers/nep141.ts +50 -0
- package/src/near/index.ts +21 -0
- package/src/near/toolbox.ts +421 -0
- package/src/near/types/contract.ts +32 -0
- package/src/near/types/nep141.ts +34 -0
- package/src/near/types/toolbox.ts +55 -0
- package/src/near/types.ts +44 -0
- package/src/radix/index.ts +132 -0
- package/src/ripple/index.ts +179 -0
- package/src/solana/index.ts +36 -0
- package/src/solana/toolbox.ts +415 -0
- package/src/substrate/balance.ts +88 -0
- package/src/substrate/index.ts +2 -0
- package/src/substrate/substrate.ts +281 -0
- package/src/substrate/types.ts +115 -0
- package/src/sui/__tests__/toolbox.test.ts +82 -0
- package/src/sui/index.ts +2 -0
- package/src/sui/toolbox.ts +165 -0
- package/src/sui/types.ts +11 -0
- package/src/ton/__tests__/toolbox.test.ts +63 -0
- package/src/ton/index.ts +2 -0
- package/src/ton/toolbox.ts +136 -0
- package/src/ton/types.ts +13 -0
- package/src/tron/__tests__/toolbox.test.ts +221 -0
- package/src/tron/helpers/trc20.abi.ts +107 -0
- package/src/tron/helpers/trongrid.ts +53 -0
- package/src/tron/index.ts +21 -0
- package/src/tron/toolbox.ts +585 -0
- package/src/tron/types.ts +83 -0
- package/src/types.ts +28 -0
- package/src/utils.ts +27 -0
- package/src/utxo/__tests__/zcash-integration.test.ts +97 -0
- package/src/utxo/helpers/api.ts +471 -0
- package/src/utxo/helpers/bchaddrjs.ts +166 -0
- package/src/utxo/helpers/coinselect.ts +92 -0
- package/src/utxo/helpers/index.ts +4 -0
- package/src/utxo/helpers/txSize.ts +137 -0
- package/src/utxo/index.ts +6 -0
- package/src/utxo/toolbox/bitcoinCash.ts +243 -0
- package/src/utxo/toolbox/index.ts +59 -0
- package/src/utxo/toolbox/params.ts +18 -0
- package/src/utxo/toolbox/utxo.ts +439 -0
- package/src/utxo/toolbox/validators.ts +36 -0
- package/src/utxo/toolbox/zcash.ts +245 -0
- package/src/utxo/types.ts +39 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FungibleResourcesCollectionItem,
|
|
3
|
+
GatewayApiClient,
|
|
4
|
+
StateEntityDetailsVaultResponseItem,
|
|
5
|
+
StateEntityFungiblesPageRequest,
|
|
6
|
+
StateEntityFungiblesPageResponse,
|
|
7
|
+
} from "@radixdlt/babylon-gateway-api-sdk";
|
|
8
|
+
import { AssetValue, Chain, SKConfig, type SKConfigIntegrations, SwapKitError } from "@uswap/helpers";
|
|
9
|
+
|
|
10
|
+
export type RadixWallet = Awaited<ReturnType<typeof RadixToolbox>>;
|
|
11
|
+
|
|
12
|
+
type RadixGetBalanceParams = { address: string; networkApi: GatewayApiClient };
|
|
13
|
+
// Could not find anything sync in SDK, ask Radix team
|
|
14
|
+
export function radixValidateAddress(address: string) {
|
|
15
|
+
return address.startsWith("account_rdx1") && address.length === 66;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getBalance({ networkApi }: { networkApi: GatewayApiClient }) {
|
|
19
|
+
return async function getBalance(address: string) {
|
|
20
|
+
const fungibleResources = await fetchFungibleResources({ address, networkApi });
|
|
21
|
+
const fungibleBalances = convertResourcesToBalances({ networkApi, resources: fungibleResources });
|
|
22
|
+
return fungibleBalances;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function fetchFungibleResources({
|
|
27
|
+
address,
|
|
28
|
+
networkApi,
|
|
29
|
+
}: RadixGetBalanceParams): Promise<FungibleResourcesCollectionItem[]> {
|
|
30
|
+
let hasNextPage = true;
|
|
31
|
+
let nextCursor: string | undefined;
|
|
32
|
+
let fungibleResources: FungibleResourcesCollectionItem[] = [];
|
|
33
|
+
const stateVersion = await currentStateVersion(networkApi);
|
|
34
|
+
while (hasNextPage) {
|
|
35
|
+
const stateEntityFungiblesPageRequest: StateEntityFungiblesPageRequest = {
|
|
36
|
+
address: address,
|
|
37
|
+
at_ledger_state: { state_version: stateVersion },
|
|
38
|
+
cursor: nextCursor,
|
|
39
|
+
limit_per_page: 100,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const stateEntityFungiblesPageResponse: StateEntityFungiblesPageResponse =
|
|
43
|
+
await networkApi.state.innerClient.entityFungiblesPage({
|
|
44
|
+
stateEntityFungiblesPageRequest: stateEntityFungiblesPageRequest,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
fungibleResources = fungibleResources.concat(stateEntityFungiblesPageResponse.items);
|
|
48
|
+
if (stateEntityFungiblesPageResponse.next_cursor) {
|
|
49
|
+
nextCursor = stateEntityFungiblesPageResponse.next_cursor;
|
|
50
|
+
} else {
|
|
51
|
+
hasNextPage = false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return fungibleResources;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function convertResourcesToBalances({
|
|
58
|
+
resources,
|
|
59
|
+
networkApi,
|
|
60
|
+
}: {
|
|
61
|
+
resources: FungibleResourcesCollectionItem[]; //| NonFungibleResourcesCollectionItem[];
|
|
62
|
+
networkApi: GatewayApiClient;
|
|
63
|
+
}): Promise<AssetValue[]> {
|
|
64
|
+
const balances: AssetValue[] = [];
|
|
65
|
+
const BATCH_SIZE = 50;
|
|
66
|
+
|
|
67
|
+
// Split resources into batches of up to 50 items
|
|
68
|
+
const resourceBatches: FungibleResourcesCollectionItem[][] = [];
|
|
69
|
+
for (let i = 0; i < resources.length; i += BATCH_SIZE) {
|
|
70
|
+
resourceBatches.push(resources.slice(i, i + BATCH_SIZE));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const batch of resourceBatches) {
|
|
74
|
+
const addresses = batch.map((item) => item.resource_address);
|
|
75
|
+
const response: StateEntityDetailsVaultResponseItem[] =
|
|
76
|
+
await networkApi.state.getEntityDetailsVaultAggregated(addresses);
|
|
77
|
+
|
|
78
|
+
const divisibilities = new Map<string, { decimals: number; symbol: string }>();
|
|
79
|
+
|
|
80
|
+
for (const result of response) {
|
|
81
|
+
if (result.details !== undefined) {
|
|
82
|
+
const metaDataSymbol = result.metadata?.items.find((item) => item.key === "symbol");
|
|
83
|
+
const symbol = metaDataSymbol?.value.typed.type === "String" ? metaDataSymbol.value.typed.value : "?";
|
|
84
|
+
|
|
85
|
+
if (result.details.type === "FungibleResource") {
|
|
86
|
+
divisibilities.set(result.address, { decimals: result.details.divisibility, symbol });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const item of batch) {
|
|
92
|
+
if (item.aggregation_level === "Global") {
|
|
93
|
+
const assetInfo = divisibilities.get(item.resource_address) || { decimals: 0, symbol: "?" };
|
|
94
|
+
|
|
95
|
+
const balance = AssetValue.from({
|
|
96
|
+
asset:
|
|
97
|
+
assetInfo.symbol !== Chain.Radix
|
|
98
|
+
? `${Chain.Radix}.${assetInfo.symbol}-${item.resource_address}`
|
|
99
|
+
: "XRD.XRD",
|
|
100
|
+
value: item.amount,
|
|
101
|
+
});
|
|
102
|
+
balances.push(balance);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return balances;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function currentStateVersion(networkApi: GatewayApiClient) {
|
|
111
|
+
return networkApi.status.getCurrent().then((status) => status.ledger_state.state_version);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export const RadixToolbox = async ({ dappConfig }: { dappConfig?: SKConfigIntegrations["radix"] } = {}) => {
|
|
115
|
+
const { RadixDappToolkit } = await import("@radixdlt/radix-dapp-toolkit");
|
|
116
|
+
const { GatewayApiClient } = await import("@radixdlt/babylon-gateway-api-sdk");
|
|
117
|
+
const config = dappConfig || SKConfig.get("integrations").radix;
|
|
118
|
+
|
|
119
|
+
const radixToolkit = RadixDappToolkit({ ...config, networkId: config.network?.networkId || 1 });
|
|
120
|
+
|
|
121
|
+
const networkApi = GatewayApiClient.initialize(radixToolkit.gatewayApi.clientConfig);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
getAddress: () => "",
|
|
125
|
+
getBalance: getBalance({ networkApi }),
|
|
126
|
+
networkApi,
|
|
127
|
+
signAndBroadcast: (() => {
|
|
128
|
+
throw new SwapKitError("toolbox_radix_method_not_supported", { method: "signAndBroadcast" });
|
|
129
|
+
}) as (params: any) => Promise<string>,
|
|
130
|
+
validateAddress: radixValidateAddress,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AssetValue,
|
|
3
|
+
Chain,
|
|
4
|
+
type ChainSigner,
|
|
5
|
+
type GenericTransferParams,
|
|
6
|
+
getChainConfig,
|
|
7
|
+
getRPCUrl,
|
|
8
|
+
SwapKitError,
|
|
9
|
+
SwapKitNumber,
|
|
10
|
+
} from "@uswap/helpers";
|
|
11
|
+
import type { Transaction } from "xrpl";
|
|
12
|
+
import { Client, isValidAddress, type Payment, Wallet, xrpToDrops } from "xrpl";
|
|
13
|
+
|
|
14
|
+
export type RippleWallet = Awaited<ReturnType<typeof getRippleToolbox>>;
|
|
15
|
+
|
|
16
|
+
export { hashes, type Transaction } from "xrpl";
|
|
17
|
+
|
|
18
|
+
const RIPPLE_ERROR_CODES = { ACCOUNT_NOT_FOUND: 19 } as const;
|
|
19
|
+
|
|
20
|
+
// Note: Ripple seeds generate a single address, no derivation path/index support.
|
|
21
|
+
function createSigner(phrase: string): ChainSigner<Transaction, { tx_blob: string; hash: string }> {
|
|
22
|
+
const wallet = Wallet.fromMnemonic(phrase);
|
|
23
|
+
return {
|
|
24
|
+
// publicKey: wallet.publicKey,
|
|
25
|
+
// Address is sync, but interface requires async
|
|
26
|
+
getAddress: () => Promise.resolve(wallet.address),
|
|
27
|
+
// Signing is sync, but interface requires async
|
|
28
|
+
signTransaction: (tx: Transaction) => Promise.resolve(wallet.sign(tx as Transaction)), // Cast needed as Wallet.sign expects Transaction
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function rippleValidateAddress(address: string) {
|
|
33
|
+
return isValidAddress(address);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type RippleToolboxParams =
|
|
37
|
+
| { phrase?: string }
|
|
38
|
+
| { signer?: ChainSigner<Transaction, { tx_blob: string; hash: string }> };
|
|
39
|
+
|
|
40
|
+
export const getRippleToolbox = async (params: RippleToolboxParams = {}) => {
|
|
41
|
+
const signer =
|
|
42
|
+
"signer" in params && params.signer
|
|
43
|
+
? params.signer
|
|
44
|
+
: "phrase" in params && params.phrase
|
|
45
|
+
? createSigner(params.phrase)
|
|
46
|
+
: undefined;
|
|
47
|
+
|
|
48
|
+
const rpcUrl = await getRPCUrl(Chain.Ripple);
|
|
49
|
+
if (!rpcUrl) {
|
|
50
|
+
throw new SwapKitError({ errorKey: "toolbox_ripple_rpc_not_configured", info: { chain: Chain.Ripple } });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const client = new Client(rpcUrl);
|
|
54
|
+
await client.connect();
|
|
55
|
+
|
|
56
|
+
const getAddress = () => {
|
|
57
|
+
if (!signer) {
|
|
58
|
+
throw new SwapKitError({ errorKey: "toolbox_ripple_signer_not_found" });
|
|
59
|
+
}
|
|
60
|
+
return signer.getAddress();
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const getBalance = async (address?: string) => {
|
|
64
|
+
const addr = address || (await getAddress());
|
|
65
|
+
const { baseDecimal } = getChainConfig(Chain.Ripple);
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
const accountInfo = await client.request({ account: addr, command: "account_info" });
|
|
69
|
+
|
|
70
|
+
const balance = accountInfo.result.account_data.Balance;
|
|
71
|
+
|
|
72
|
+
return [AssetValue.from({ chain: Chain.Ripple, fromBaseDecimal: baseDecimal, value: balance })];
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// empty account
|
|
75
|
+
if ((error as any).data.error_code === RIPPLE_ERROR_CODES.ACCOUNT_NOT_FOUND) {
|
|
76
|
+
return [AssetValue.from({ chain: Chain.Ripple, value: 0 })];
|
|
77
|
+
}
|
|
78
|
+
throw new SwapKitError("toolbox_ripple_get_balance_error", { info: { address: addr, error } });
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const estimateTransactionFee = async () => {
|
|
83
|
+
const feeResponse = await client.request({ command: "fee" });
|
|
84
|
+
const feeDrops = feeResponse.result.drops.open_ledger_fee; // Fee in drops
|
|
85
|
+
|
|
86
|
+
const { baseDecimal } = getChainConfig(Chain.Ripple);
|
|
87
|
+
|
|
88
|
+
return AssetValue.from({
|
|
89
|
+
chain: Chain.Ripple,
|
|
90
|
+
fromBaseDecimal: baseDecimal,
|
|
91
|
+
value: SwapKitNumber.fromBigInt(BigInt(feeDrops), baseDecimal),
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const createTransaction = async ({
|
|
96
|
+
assetValue,
|
|
97
|
+
recipient,
|
|
98
|
+
memo,
|
|
99
|
+
sender,
|
|
100
|
+
}: {
|
|
101
|
+
assetValue: AssetValue;
|
|
102
|
+
recipient: string;
|
|
103
|
+
sender?: string;
|
|
104
|
+
memo?: string;
|
|
105
|
+
}) => {
|
|
106
|
+
if (!rippleValidateAddress(recipient)) {
|
|
107
|
+
throw new SwapKitError({ errorKey: "core_transaction_invalid_recipient_address" });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const senderAddress = sender || (await getAddress());
|
|
111
|
+
|
|
112
|
+
if (!assetValue.isGasAsset || assetValue.chain !== Chain.Ripple) {
|
|
113
|
+
throw new SwapKitError({
|
|
114
|
+
errorKey: "toolbox_ripple_asset_not_supported",
|
|
115
|
+
info: { asset: assetValue.toString() },
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const transaction: Payment = {
|
|
120
|
+
Account: senderAddress,
|
|
121
|
+
Amount: xrpToDrops(assetValue.getValue("string")),
|
|
122
|
+
Destination: recipient,
|
|
123
|
+
TransactionType: "Payment",
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
if (memo) {
|
|
127
|
+
transaction.Memos = [{ Memo: { MemoData: Buffer.from(memo).toString("hex") } }];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const preparedTx = await client.autofill(transaction);
|
|
131
|
+
return preparedTx;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const signTransaction = (tx: Transaction) => {
|
|
135
|
+
if (!signer) {
|
|
136
|
+
throw new SwapKitError({ errorKey: "toolbox_ripple_signer_not_found" });
|
|
137
|
+
}
|
|
138
|
+
return signer.signTransaction(tx);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const broadcastTransaction = async (signedTxHex: string) => {
|
|
142
|
+
const submitResult = await client.submitAndWait(signedTxHex);
|
|
143
|
+
const result = submitResult.result;
|
|
144
|
+
|
|
145
|
+
if (result.validated) {
|
|
146
|
+
return result.hash;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
throw new SwapKitError({ errorKey: "toolbox_ripple_broadcast_error", info: { chain: Chain.Ripple } });
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const transfer = async (params: GenericTransferParams) => {
|
|
153
|
+
if (!signer) {
|
|
154
|
+
throw new SwapKitError({ errorKey: "toolbox_ripple_signer_not_found" });
|
|
155
|
+
}
|
|
156
|
+
const sender = await signer.getAddress();
|
|
157
|
+
const tx = await createTransaction({ ...params, sender });
|
|
158
|
+
const signedTx = await signTransaction(tx);
|
|
159
|
+
return broadcastTransaction(signedTx.tx_blob);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const disconnect = () => client.disconnect();
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
broadcastTransaction,
|
|
166
|
+
createSigner, // Expose the helper
|
|
167
|
+
createTransaction,
|
|
168
|
+
disconnect,
|
|
169
|
+
estimateTransactionFee,
|
|
170
|
+
// Core methods
|
|
171
|
+
getAddress,
|
|
172
|
+
getBalance,
|
|
173
|
+
// Signer related
|
|
174
|
+
signer, // Expose the signer instance if created/provided
|
|
175
|
+
signTransaction,
|
|
176
|
+
transfer,
|
|
177
|
+
validateAddress: rippleValidateAddress,
|
|
178
|
+
};
|
|
179
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
|
|
2
|
+
import type { GenericCreateTransactionParams, GenericTransferParams } from "@uswap/helpers";
|
|
3
|
+
import type { getSolanaToolbox } from "./toolbox";
|
|
4
|
+
|
|
5
|
+
// type DisplayEncoding = "utf8" | "hex";
|
|
6
|
+
|
|
7
|
+
// type PhantomRequestMethod =
|
|
8
|
+
// | "connect"
|
|
9
|
+
// | "disconnect"
|
|
10
|
+
// | "signAndSendTransaction"
|
|
11
|
+
// | "signAndSendTransactionV0"
|
|
12
|
+
// | "signAndSendTransactionV0WithLookupTable"
|
|
13
|
+
// | "signTransaction"
|
|
14
|
+
// | "signAllTransactions"
|
|
15
|
+
// | "signMessage";
|
|
16
|
+
|
|
17
|
+
interface ConnectOpts {
|
|
18
|
+
onlyIfTrusted: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export * from "./toolbox";
|
|
22
|
+
|
|
23
|
+
export type SolanaWallet = Awaited<ReturnType<typeof getSolanaToolbox>>;
|
|
24
|
+
|
|
25
|
+
export interface SolanaProvider {
|
|
26
|
+
connect: (opts?: Partial<ConnectOpts>) => Promise<{ publicKey: PublicKey }>;
|
|
27
|
+
disconnect: () => Promise<void>;
|
|
28
|
+
publicKey: PublicKey | null;
|
|
29
|
+
signTransaction: <T extends Transaction | VersionedTransaction = Transaction>(transaction: T) => Promise<T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type SolanaCreateTransactionParams = Omit<GenericCreateTransactionParams, "feeRate"> & {
|
|
33
|
+
isProgramDerivedAddress?: boolean;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type SolanaTransferParams = Omit<GenericTransferParams, "feeRate"> & { isProgramDerivedAddress?: boolean };
|