@b3dotfun/sdk 0.0.23 → 0.0.24
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/cjs/anyspend/react/components/AnySpend.js +5 -8
- package/dist/cjs/anyspend/react/components/AnySpendBondKit.js +2 -2
- package/dist/cjs/anyspend/react/components/AnySpendBuySpin.js +35 -28
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +4 -4
- package/dist/cjs/anyspend/react/components/AnySpendStakeB3.js +35 -28
- package/dist/cjs/anyspend/react/components/common/Accordion.d.ts +7 -0
- package/dist/cjs/anyspend/react/components/common/Accordion.js +53 -0
- package/dist/cjs/anyspend/react/components/common/ConnectWalletPayment.js +3 -22
- package/dist/cjs/anyspend/react/components/common/FiatPaymentMethod.js +2 -2
- package/dist/cjs/anyspend/react/components/common/OrderDetails.js +76 -83
- package/dist/cjs/anyspend/react/components/common/OrderStatus.js +1 -1
- package/dist/cjs/anyspend/react/components/common/PanelOnramp.js +4 -3
- package/dist/cjs/anyspend/react/components/common/PanelOnrampPayment.js +1 -5
- package/dist/cjs/anyspend/react/components/common/StepProgress.js +2 -2
- package/dist/cjs/anyspend/react/components/common/TokenBalance.js +1 -1
- package/dist/cjs/anyspend/react/hooks/useGeoOnrampOptions.d.ts +0 -1
- package/dist/cjs/anyspend/react/hooks/useGeoOnrampOptions.js +2 -4
- package/dist/cjs/anyspend/react/hooks/useSigMint.d.ts +2 -2
- package/dist/cjs/anyspend/react/hooks/useStripeSupport.d.ts +0 -1
- package/dist/cjs/anyspend/react/hooks/useStripeSupport.js +0 -1
- package/dist/cjs/anyspend/utils/chain.js +1 -1
- package/dist/cjs/bondkit/abis/BondkitTokenABI.d.ts +1020 -0
- package/dist/cjs/bondkit/abis/BondkitTokenABI.js +1332 -0
- package/dist/cjs/bondkit/abis/BondkitTokenFactoryABI.d.ts +391 -0
- package/dist/cjs/bondkit/abis/BondkitTokenFactoryABI.js +514 -0
- package/dist/cjs/bondkit/abis/index.d.ts +2 -0
- package/dist/cjs/bondkit/abis/index.js +18 -0
- package/dist/cjs/bondkit/bondkitToken.d.ts +68 -0
- package/dist/cjs/bondkit/bondkitToken.js +456 -0
- package/dist/cjs/bondkit/bondkitTokenFactory.d.ts +60 -0
- package/dist/cjs/bondkit/bondkitTokenFactory.js +274 -0
- package/dist/cjs/bondkit/components/TradingView.d.ts +3 -0
- package/dist/cjs/bondkit/components/TradingView.js +296 -0
- package/dist/cjs/bondkit/components/config/cdn.d.ts +42 -0
- package/dist/cjs/bondkit/components/config/cdn.js +63 -0
- package/dist/cjs/bondkit/components/index.d.ts +5 -0
- package/dist/cjs/bondkit/components/index.js +25 -0
- package/dist/cjs/bondkit/components/types.d.ts +8 -0
- package/dist/cjs/bondkit/components/types.js +5 -0
- package/dist/cjs/bondkit/components/utils/cdn-loader.d.ts +24 -0
- package/dist/cjs/bondkit/components/utils/cdn-loader.js +73 -0
- package/dist/cjs/bondkit/components/utils/format.d.ts +4 -0
- package/dist/cjs/bondkit/components/utils/format.js +31 -0
- package/dist/cjs/bondkit/config.d.ts +10 -0
- package/dist/cjs/bondkit/config.js +18 -0
- package/dist/cjs/bondkit/constants.d.ts +3 -0
- package/dist/cjs/bondkit/constants.js +5 -0
- package/dist/cjs/bondkit/index.d.ts +7 -0
- package/dist/cjs/bondkit/index.js +33 -0
- package/dist/cjs/bondkit/json_abis/BondkitABI.json +1329 -0
- package/dist/cjs/bondkit/json_abis/BondkitFactoryABI.json +511 -0
- package/dist/cjs/bondkit/json_abis/index.d.ts +3 -0
- package/dist/cjs/bondkit/json_abis/index.js +10 -0
- package/dist/cjs/bondkit/json_abis/index.ts +4 -0
- package/dist/cjs/bondkit/types.d.ts +77 -0
- package/dist/cjs/bondkit/types.js +11 -0
- package/dist/cjs/global-account/react/components/B3DynamicModal.js +1 -1
- package/dist/cjs/global-account/react/components/MintButton/MintButton.js +0 -1
- package/dist/cjs/global-account/react/components/SendERC20Button/SendERC20Button.d.ts +13 -0
- package/dist/cjs/global-account/react/components/SendERC20Button/SendERC20Button.js +33 -0
- package/dist/cjs/global-account/react/components/SendETHButton/SendETHButton.d.ts +12 -0
- package/dist/cjs/global-account/react/components/SendETHButton/SendETHButton.js +23 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +4 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +2 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +1 -1
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +2 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +2 -2
- package/dist/cjs/global-account/react/components/index.d.ts +2 -0
- package/dist/cjs/global-account/react/components/index.js +8 -2
- package/dist/cjs/global-account/react/components/ui/scroll-area.js +1 -1
- package/dist/cjs/global-account/react/components/ui/tooltip.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/index.d.ts +2 -1
- package/dist/cjs/global-account/react/hooks/index.js +18 -3
- package/dist/cjs/global-account/react/hooks/useAccountWallet.js +8 -0
- package/dist/cjs/global-account/react/hooks/useAuthentication.js +4 -4
- package/dist/cjs/global-account/react/hooks/useChainSwitchWithAction.d.ts +1 -2
- package/dist/cjs/global-account/react/hooks/useChainSwitchWithAction.js +2 -2
- package/dist/cjs/global-account/react/hooks/useFirstEOA.js +9 -7
- package/dist/cjs/global-account/react/hooks/useUnifiedChainSwitchAndExecute.d.ts +11 -0
- package/dist/cjs/global-account/react/hooks/useUnifiedChainSwitchAndExecute.js +160 -0
- package/dist/cjs/shared/constants/chains/supported.d.ts +4 -0
- package/dist/cjs/shared/constants/chains/supported.js +11 -0
- package/dist/cjs/shared/generated/chain-networks.json +1 -1
- package/dist/esm/anyspend/react/components/AnySpend.js +5 -8
- package/dist/esm/anyspend/react/components/AnySpendBondKit.js +2 -2
- package/dist/esm/anyspend/react/components/AnySpendBuySpin.js +37 -30
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +4 -4
- package/dist/esm/anyspend/react/components/AnySpendStakeB3.js +37 -30
- package/dist/esm/anyspend/react/components/common/Accordion.d.ts +7 -0
- package/dist/esm/anyspend/react/components/common/Accordion.js +14 -0
- package/dist/esm/anyspend/react/components/common/ConnectWalletPayment.js +3 -22
- package/dist/esm/anyspend/react/components/common/FiatPaymentMethod.js +2 -2
- package/dist/esm/anyspend/react/components/common/OrderDetails.js +79 -86
- package/dist/esm/anyspend/react/components/common/OrderStatus.js +1 -1
- package/dist/esm/anyspend/react/components/common/PanelOnramp.js +5 -4
- package/dist/esm/anyspend/react/components/common/PanelOnrampPayment.js +1 -5
- package/dist/esm/anyspend/react/components/common/StepProgress.js +2 -2
- package/dist/esm/anyspend/react/components/common/TokenBalance.js +1 -1
- package/dist/esm/anyspend/react/hooks/useGeoOnrampOptions.d.ts +0 -1
- package/dist/esm/anyspend/react/hooks/useGeoOnrampOptions.js +2 -4
- package/dist/esm/anyspend/react/hooks/useSigMint.d.ts +2 -2
- package/dist/esm/anyspend/react/hooks/useStripeSupport.d.ts +0 -1
- package/dist/esm/anyspend/react/hooks/useStripeSupport.js +0 -1
- package/dist/esm/anyspend/utils/chain.js +1 -1
- package/dist/esm/bondkit/abis/BondkitTokenABI.d.ts +1020 -0
- package/dist/esm/bondkit/abis/BondkitTokenABI.js +1329 -0
- package/dist/esm/bondkit/abis/BondkitTokenFactoryABI.d.ts +391 -0
- package/dist/esm/bondkit/abis/BondkitTokenFactoryABI.js +511 -0
- package/dist/esm/bondkit/abis/index.d.ts +2 -0
- package/dist/esm/bondkit/abis/index.js +2 -0
- package/dist/esm/bondkit/bondkitToken.d.ts +68 -0
- package/dist/esm/bondkit/bondkitToken.js +452 -0
- package/dist/esm/bondkit/bondkitTokenFactory.d.ts +60 -0
- package/dist/esm/bondkit/bondkitTokenFactory.js +270 -0
- package/dist/esm/bondkit/components/TradingView.d.ts +3 -0
- package/dist/esm/bondkit/components/TradingView.js +294 -0
- package/dist/esm/bondkit/components/config/cdn.d.ts +42 -0
- package/dist/esm/bondkit/components/config/cdn.js +55 -0
- package/dist/esm/bondkit/components/index.d.ts +5 -0
- package/dist/esm/bondkit/components/index.js +4 -0
- package/dist/esm/bondkit/components/types.d.ts +8 -0
- package/dist/esm/bondkit/components/types.js +4 -0
- package/dist/esm/bondkit/components/utils/cdn-loader.d.ts +24 -0
- package/dist/esm/bondkit/components/utils/cdn-loader.js +66 -0
- package/dist/esm/bondkit/components/utils/format.d.ts +4 -0
- package/dist/esm/bondkit/components/utils/format.js +28 -0
- package/dist/esm/bondkit/config.d.ts +10 -0
- package/dist/esm/bondkit/config.js +14 -0
- package/dist/esm/bondkit/constants.d.ts +3 -0
- package/dist/esm/bondkit/constants.js +2 -0
- package/dist/esm/bondkit/index.d.ts +7 -0
- package/dist/esm/bondkit/index.js +12 -0
- package/dist/esm/bondkit/json_abis/BondkitABI.json +1329 -0
- package/dist/esm/bondkit/json_abis/BondkitFactoryABI.json +511 -0
- package/dist/esm/bondkit/json_abis/index.d.ts +3 -0
- package/dist/esm/bondkit/json_abis/index.js +3 -0
- package/dist/esm/bondkit/json_abis/index.ts +4 -0
- package/dist/esm/bondkit/types.d.ts +77 -0
- package/dist/esm/bondkit/types.js +8 -0
- package/dist/esm/global-account/react/components/B3DynamicModal.js +1 -1
- package/dist/esm/global-account/react/components/MintButton/MintButton.js +0 -1
- package/dist/esm/global-account/react/components/SendERC20Button/SendERC20Button.d.ts +13 -0
- package/dist/esm/global-account/react/components/SendERC20Button/SendERC20Button.js +30 -0
- package/dist/esm/global-account/react/components/SendETHButton/SendETHButton.d.ts +12 -0
- package/dist/esm/global-account/react/components/SendETHButton/SendETHButton.js +20 -0
- package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +4 -2
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +2 -2
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +1 -1
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +2 -2
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +2 -2
- package/dist/esm/global-account/react/components/index.d.ts +2 -0
- package/dist/esm/global-account/react/components/index.js +4 -0
- package/dist/esm/global-account/react/components/ui/scroll-area.js +1 -1
- package/dist/esm/global-account/react/components/ui/tooltip.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/index.d.ts +2 -1
- package/dist/esm/global-account/react/hooks/index.js +2 -1
- package/dist/esm/global-account/react/hooks/useAccountWallet.js +8 -0
- package/dist/esm/global-account/react/hooks/useAuthentication.js +4 -4
- package/dist/esm/global-account/react/hooks/useChainSwitchWithAction.d.ts +1 -2
- package/dist/esm/global-account/react/hooks/useChainSwitchWithAction.js +2 -2
- package/dist/esm/global-account/react/hooks/useFirstEOA.js +9 -7
- package/dist/esm/global-account/react/hooks/useUnifiedChainSwitchAndExecute.d.ts +11 -0
- package/dist/esm/global-account/react/hooks/useUnifiedChainSwitchAndExecute.js +154 -0
- package/dist/esm/shared/constants/chains/supported.d.ts +4 -0
- package/dist/esm/shared/constants/chains/supported.js +10 -0
- package/dist/esm/shared/generated/chain-networks.json +1 -1
- package/dist/styles/index.css +1 -1
- package/dist/types/anyspend/react/components/common/Accordion.d.ts +7 -0
- package/dist/types/anyspend/react/hooks/useGeoOnrampOptions.d.ts +0 -1
- package/dist/types/anyspend/react/hooks/useSigMint.d.ts +2 -2
- package/dist/types/anyspend/react/hooks/useStripeSupport.d.ts +0 -1
- package/dist/types/bondkit/abis/BondkitTokenABI.d.ts +1020 -0
- package/dist/types/bondkit/abis/BondkitTokenFactoryABI.d.ts +391 -0
- package/dist/types/bondkit/abis/index.d.ts +2 -0
- package/dist/types/bondkit/bondkitToken.d.ts +68 -0
- package/dist/types/bondkit/bondkitTokenFactory.d.ts +60 -0
- package/dist/types/bondkit/components/TradingView.d.ts +3 -0
- package/dist/types/bondkit/components/config/cdn.d.ts +42 -0
- package/dist/types/bondkit/components/index.d.ts +5 -0
- package/dist/types/bondkit/components/types.d.ts +8 -0
- package/dist/types/bondkit/components/utils/cdn-loader.d.ts +24 -0
- package/dist/types/bondkit/components/utils/format.d.ts +4 -0
- package/dist/types/bondkit/config.d.ts +10 -0
- package/dist/types/bondkit/constants.d.ts +3 -0
- package/dist/types/bondkit/index.d.ts +7 -0
- package/dist/types/bondkit/json_abis/index.d.ts +3 -0
- package/dist/types/bondkit/types.d.ts +77 -0
- package/dist/types/global-account/react/components/SendERC20Button/SendERC20Button.d.ts +13 -0
- package/dist/types/global-account/react/components/SendETHButton/SendETHButton.d.ts +12 -0
- package/dist/types/global-account/react/components/index.d.ts +2 -0
- package/dist/types/global-account/react/components/ui/tooltip.d.ts +1 -1
- package/dist/types/global-account/react/hooks/index.d.ts +2 -1
- package/dist/types/global-account/react/hooks/useChainSwitchWithAction.d.ts +1 -2
- package/dist/types/global-account/react/hooks/useUnifiedChainSwitchAndExecute.d.ts +11 -0
- package/dist/types/shared/constants/chains/supported.d.ts +4 -0
- package/package.json +42 -7
- package/src/anyspend/react/components/AnySpend.tsx +5 -23
- package/src/anyspend/react/components/AnySpendBondKit.tsx +2 -2
- package/src/anyspend/react/components/AnySpendBuySpin.tsx +42 -32
- package/src/anyspend/react/components/AnySpendCustom.tsx +7 -13
- package/src/anyspend/react/components/AnySpendStakeB3.tsx +44 -34
- package/src/anyspend/react/components/common/Accordion.tsx +56 -0
- package/src/anyspend/react/components/common/ConnectWalletPayment.tsx +0 -25
- package/src/anyspend/react/components/common/FiatPaymentMethod.tsx +1 -2
- package/src/anyspend/react/components/common/OrderDetails.tsx +292 -260
- package/src/anyspend/react/components/common/OrderStatus.tsx +1 -1
- package/src/anyspend/react/components/common/PanelOnramp.tsx +7 -7
- package/src/anyspend/react/components/common/PanelOnrampPayment.tsx +0 -6
- package/src/anyspend/react/components/common/StepProgress.tsx +2 -2
- package/src/anyspend/react/components/common/TokenBalance.tsx +3 -3
- package/src/anyspend/react/hooks/useGeoOnrampOptions.ts +2 -4
- package/src/anyspend/react/hooks/useStripeSupport.ts +0 -1
- package/src/anyspend/utils/chain.ts +1 -1
- package/src/bondkit/abis/BondkitTokenABI.ts +1329 -0
- package/src/bondkit/abis/BondkitTokenFactoryABI.ts +511 -0
- package/src/bondkit/abis/index.ts +2 -0
- package/src/bondkit/bondkitToken.ts +539 -0
- package/src/bondkit/bondkitTokenFactory.ts +336 -0
- package/src/bondkit/components/TradingView.tsx +341 -0
- package/src/bondkit/components/config/cdn.ts +63 -0
- package/src/bondkit/components/index.ts +5 -0
- package/src/bondkit/components/types.ts +9 -0
- package/src/bondkit/components/utils/cdn-loader.ts +77 -0
- package/src/bondkit/components/utils/format.ts +36 -0
- package/src/bondkit/config.ts +26 -0
- package/src/bondkit/constants.ts +5 -0
- package/src/bondkit/index.ts +16 -0
- package/src/bondkit/json_abis/BondkitABI.json +1329 -0
- package/src/bondkit/json_abis/BondkitFactoryABI.json +511 -0
- package/src/bondkit/json_abis/index.ts +4 -0
- package/src/bondkit/types.ts +98 -0
- package/src/global-account/react/components/B3DynamicModal.tsx +2 -1
- package/src/global-account/react/components/MintButton/MintButton.tsx +0 -1
- package/src/global-account/react/components/SendERC20Button/SendERC20Button.tsx +57 -0
- package/src/global-account/react/components/SendETHButton/SendETHButton.tsx +37 -0
- package/src/global-account/react/components/SignInWithB3/SignIn.tsx +21 -22
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +2 -2
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Privy.tsx +1 -1
- package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +2 -2
- package/src/global-account/react/components/SignInWithB3/steps/LoginStepCustom.tsx +2 -2
- package/src/global-account/react/components/index.ts +6 -0
- package/src/global-account/react/components/ui/scroll-area.tsx +2 -2
- package/src/global-account/react/components/ui/tooltip.tsx +1 -1
- package/src/global-account/react/hooks/index.ts +2 -1
- package/src/global-account/react/hooks/useAccountWallet.tsx +10 -1
- package/src/global-account/react/hooks/useAuthentication.ts +4 -4
- package/src/global-account/react/hooks/useChainSwitchWithAction.ts +3 -4
- package/src/global-account/react/hooks/useFirstEOA.tsx +10 -7
- package/src/global-account/react/hooks/useUnifiedChainSwitchAndExecute.ts +186 -0
- package/src/shared/constants/chains/supported.ts +11 -0
- package/src/shared/generated/chain-networks.json +1 -1
- package/src/styles/index.css +27 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Address,
|
|
3
|
+
Chain,
|
|
4
|
+
EIP1193Provider,
|
|
5
|
+
GetContractReturnType,
|
|
6
|
+
Hex,
|
|
7
|
+
PublicClient,
|
|
8
|
+
TransactionReceipt,
|
|
9
|
+
Transport,
|
|
10
|
+
WalletClient,
|
|
11
|
+
} from "viem";
|
|
12
|
+
import { createPublicClient, createWalletClient, custom, decodeEventLog, getContract, http } from "viem";
|
|
13
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
14
|
+
import { BondkitTokenFactoryABI } from "./abis";
|
|
15
|
+
import { getConfig, SupportedChainId } from "./config";
|
|
16
|
+
import type { BondkitTokenConfig, BondkitTokenCreatedEventArgs } from "./types";
|
|
17
|
+
|
|
18
|
+
// Define the event ABI snippet for BondkitTokenCreated specifically for decoding
|
|
19
|
+
const bondkitTokenCreatedEventAbi = BondkitTokenFactoryABI.find(
|
|
20
|
+
item => item.type === "event" && item.name === "BondkitTokenCreated",
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
export class BondkitTokenFactory {
|
|
24
|
+
private contract: GetContractReturnType<typeof BondkitTokenFactoryABI, WalletClient>;
|
|
25
|
+
private publicClient: PublicClient;
|
|
26
|
+
private contractAddress: Address;
|
|
27
|
+
private chain: Chain;
|
|
28
|
+
private walletKey?: Hex;
|
|
29
|
+
private rpcUrl: string;
|
|
30
|
+
private walletClientInstance: WalletClient; // Made non-optional, initialized in constructor
|
|
31
|
+
private connectedProvider?: EIP1193Provider;
|
|
32
|
+
|
|
33
|
+
constructor(chainId: SupportedChainId, walletKey?: string) {
|
|
34
|
+
if (walletKey && !walletKey.startsWith("0x")) {
|
|
35
|
+
this.walletKey = `0x${walletKey}` as Hex;
|
|
36
|
+
} else if (walletKey) {
|
|
37
|
+
this.walletKey = walletKey as Hex;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const config = getConfig(chainId);
|
|
41
|
+
this.chain = config.chain;
|
|
42
|
+
this.contractAddress = config.factoryAddress;
|
|
43
|
+
this.rpcUrl = config.rpcUrl;
|
|
44
|
+
|
|
45
|
+
this.publicClient = createPublicClient({
|
|
46
|
+
chain: this.chain,
|
|
47
|
+
transport: http(this.rpcUrl),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
this.walletClientInstance = createWalletClient({
|
|
51
|
+
chain: this.chain,
|
|
52
|
+
transport: http(this.rpcUrl),
|
|
53
|
+
account: this.walletKey ? privateKeyToAccount(this.walletKey) : undefined,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
this.contract = getContract({
|
|
57
|
+
address: this.contractAddress,
|
|
58
|
+
abi: BondkitTokenFactoryABI,
|
|
59
|
+
client: this.walletClientInstance,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public connect(provider?: EIP1193Provider): boolean {
|
|
64
|
+
try {
|
|
65
|
+
const transport: Transport = provider ? custom(provider) : http(this.rpcUrl);
|
|
66
|
+
|
|
67
|
+
this.connectedProvider = provider;
|
|
68
|
+
|
|
69
|
+
this.walletClientInstance = createWalletClient({
|
|
70
|
+
chain: this.chain,
|
|
71
|
+
transport,
|
|
72
|
+
account: this.walletKey ? privateKeyToAccount(this.walletKey) : undefined,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.contract = getContract({
|
|
76
|
+
address: this.contractAddress,
|
|
77
|
+
abi: BondkitTokenFactoryABI,
|
|
78
|
+
client: this.walletClientInstance,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
this.publicClient = createPublicClient({
|
|
82
|
+
chain: this.chain,
|
|
83
|
+
transport,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return true;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error("Connection failed:", error);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Connects using an EIP-1193 provider and requests accounts, selecting the first one.
|
|
95
|
+
* Enables frontend wallet flows without requiring a private key.
|
|
96
|
+
*/
|
|
97
|
+
public async connectWithProvider(provider: EIP1193Provider): Promise<boolean> {
|
|
98
|
+
try {
|
|
99
|
+
const transport: Transport = custom(provider);
|
|
100
|
+
this.connectedProvider = provider;
|
|
101
|
+
|
|
102
|
+
// Request accounts; fall back to eth_accounts if user already connected
|
|
103
|
+
let addresses: string[] = [];
|
|
104
|
+
try {
|
|
105
|
+
const result = await provider.request({ method: "eth_requestAccounts" });
|
|
106
|
+
if (Array.isArray(result)) addresses = result as string[];
|
|
107
|
+
} catch (requestErr) {
|
|
108
|
+
try {
|
|
109
|
+
const result = await provider.request({ method: "eth_accounts" });
|
|
110
|
+
if (Array.isArray(result)) addresses = result as string[];
|
|
111
|
+
} catch (_) {}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const selectedAccount = (addresses?.[0] ?? undefined) as Address | undefined;
|
|
115
|
+
|
|
116
|
+
this.walletClientInstance = createWalletClient({
|
|
117
|
+
chain: this.chain,
|
|
118
|
+
transport,
|
|
119
|
+
account: selectedAccount,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
this.publicClient = createPublicClient({
|
|
123
|
+
chain: this.chain,
|
|
124
|
+
transport,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
this.contract = getContract({
|
|
128
|
+
address: this.contractAddress,
|
|
129
|
+
abi: BondkitTokenFactoryABI,
|
|
130
|
+
client: this.walletClientInstance,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return true;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error("Connection failed:", error);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Ensure we have an account set for write operations.
|
|
142
|
+
* If not, try to resolve from a connected provider and reinitialize the client.
|
|
143
|
+
*/
|
|
144
|
+
private async ensureWriteAccount(): Promise<void> {
|
|
145
|
+
if (this.walletClientInstance.account || this.walletKey) return;
|
|
146
|
+
if (!this.connectedProvider) return;
|
|
147
|
+
try {
|
|
148
|
+
const addresses = (await this.connectedProvider.request({ method: "eth_accounts" })) as string[];
|
|
149
|
+
const selectedAccount = (addresses?.[0] ?? undefined) as Address | undefined;
|
|
150
|
+
if (selectedAccount) {
|
|
151
|
+
const transport: Transport = custom(this.connectedProvider);
|
|
152
|
+
this.walletClientInstance = createWalletClient({
|
|
153
|
+
chain: this.chain,
|
|
154
|
+
transport,
|
|
155
|
+
account: selectedAccount,
|
|
156
|
+
});
|
|
157
|
+
this.contract = getContract({
|
|
158
|
+
address: this.contractAddress,
|
|
159
|
+
abi: BondkitTokenFactoryABI,
|
|
160
|
+
client: this.walletClientInstance,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
} catch (_) {}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// TODO: Implement a more generic handleError based on leaderboards-sdk style if common errors are identified
|
|
167
|
+
private async handleError(error: any, context?: string): Promise<never> {
|
|
168
|
+
const defaultMessage = context ? `Error in ${context}:` : "An error occurred:";
|
|
169
|
+
console.error(defaultMessage, error);
|
|
170
|
+
// You could check for common error signatures here if needed
|
|
171
|
+
// e.g. if (error.data === "0xsomeErrorCode") throw new Error("Specific known error");
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Deploys a new Bondkit token using the factory.
|
|
177
|
+
* @param config The configuration for the token to be deployed.
|
|
178
|
+
* @returns The address of the newly created BondkitToken.
|
|
179
|
+
*/
|
|
180
|
+
public async deployBondkitToken(
|
|
181
|
+
configArg: BondkitTokenConfig, // Renamed to avoid conflict with getConfig
|
|
182
|
+
): Promise<Address> {
|
|
183
|
+
if (!this.walletClientInstance.account && !this.walletKey) {
|
|
184
|
+
await this.ensureWriteAccount();
|
|
185
|
+
if (!this.walletClientInstance.account && !this.walletKey) {
|
|
186
|
+
throw new Error("Wallet key not set or client not connected with an account.");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!bondkitTokenCreatedEventAbi) {
|
|
190
|
+
throw new Error("BondkitTokenCreated event ABI not found.");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
const accountToUse = this.walletKey ? privateKeyToAccount(this.walletKey) : this.walletClientInstance.account;
|
|
195
|
+
if (!accountToUse) {
|
|
196
|
+
throw new Error("Account for transaction could not be determined.");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const hash = await this.contract.write.deployBondkitToken([configArg], {
|
|
200
|
+
account: accountToUse,
|
|
201
|
+
chain: this.chain,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
console.log("hash", hash);
|
|
205
|
+
const receipt: TransactionReceipt = await this.publicClient.waitForTransactionReceipt({ hash });
|
|
206
|
+
|
|
207
|
+
for (const log of receipt.logs) {
|
|
208
|
+
try {
|
|
209
|
+
if (log.topics[0] !== "0x75b2d0aabe689b83d7eb7920447b5ae7bef7f28da01d0beb3e197899392eb0d6") {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const decodedEvent = decodeEventLog({
|
|
214
|
+
abi: BondkitTokenFactoryABI,
|
|
215
|
+
data: log.data,
|
|
216
|
+
topics: log.topics,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
return (decodedEvent.args as BondkitTokenCreatedEventArgs).tokenAddress;
|
|
220
|
+
} catch (e) {}
|
|
221
|
+
}
|
|
222
|
+
throw new Error("BondkitTokenCreated event not found in transaction logs.");
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return this.handleError(error, "deployBondkitToken");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Gets the configuration of a deployed Bondkit token.
|
|
230
|
+
* @param tokenAddress The address of the Bondkit token.
|
|
231
|
+
* @returns The configuration object for the token.
|
|
232
|
+
*/
|
|
233
|
+
public async getBondkitTokenConfig(tokenAddress: Address): Promise<BondkitTokenConfig | undefined> {
|
|
234
|
+
try {
|
|
235
|
+
const result = await this.contract.read.getBondkitTokenConfig([tokenAddress]);
|
|
236
|
+
return result as BondkitTokenConfig;
|
|
237
|
+
} catch (error) {
|
|
238
|
+
console.error(`Error getting config for token ${tokenAddress}:`, error);
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Gets a list of all Bondkit tokens deployed by this factory.
|
|
245
|
+
* @returns An array of token addresses.
|
|
246
|
+
*/
|
|
247
|
+
public async getDeployedBondkitTokens(): Promise<readonly Address[]> {
|
|
248
|
+
// ABI returns address[] which viem treats as readonly Address[]
|
|
249
|
+
try {
|
|
250
|
+
return await this.contract.read.getDeployedBondkitTokens(); // No arguments if function takes none
|
|
251
|
+
} catch (error) {
|
|
252
|
+
console.error("Error getting all deployed tokens:", error);
|
|
253
|
+
return [];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Gets the owner of the factory contract.
|
|
259
|
+
* @returns The address of the owner.
|
|
260
|
+
*/
|
|
261
|
+
public async getOwner(): Promise<Address | undefined> {
|
|
262
|
+
try {
|
|
263
|
+
return await this.contract.read.owner(); // No arguments if function takes none
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.error("Error getting factory owner:", error);
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Gets the implementation address used for new Bondkit token clones.
|
|
272
|
+
* @returns The address of the BondkitToken implementation contract.
|
|
273
|
+
*/
|
|
274
|
+
public async getBondkitTokenImplementation(): Promise<Address | undefined> {
|
|
275
|
+
try {
|
|
276
|
+
return await this.contract.read.bondkitTokenImplementation(); // No arguments if function takes none
|
|
277
|
+
} catch (error) {
|
|
278
|
+
console.error("Error getting Bondkit token implementation address:", error);
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// --- Write Methods (Ownership, etc.) --- //
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Transfers ownership of the factory contract to a new owner.
|
|
287
|
+
* Requires the current owner to call this method.
|
|
288
|
+
* @param newOwner The address of the new owner.
|
|
289
|
+
* @returns A promise that resolves with the transaction hash.
|
|
290
|
+
*/
|
|
291
|
+
public async transferOwnership(newOwner: Address): Promise<Hex | undefined> {
|
|
292
|
+
if (!this.walletClientInstance.account && !this.walletKey) {
|
|
293
|
+
await this.ensureWriteAccount();
|
|
294
|
+
if (!this.walletClientInstance.account && !this.walletKey) {
|
|
295
|
+
throw new Error("Wallet key not set or client not connected with an account for write operation.");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
try {
|
|
299
|
+
const accountToUse = this.walletKey ? privateKeyToAccount(this.walletKey) : this.walletClientInstance.account;
|
|
300
|
+
if (!accountToUse) {
|
|
301
|
+
throw new Error("Account for transaction could not be determined.");
|
|
302
|
+
}
|
|
303
|
+
const hash = await this.contract.write.transferOwnership([newOwner], {
|
|
304
|
+
account: accountToUse,
|
|
305
|
+
chain: this.chain,
|
|
306
|
+
});
|
|
307
|
+
return hash;
|
|
308
|
+
} catch (error) {
|
|
309
|
+
return this.handleError(error, "transferOwnership");
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// /**
|
|
314
|
+
// * Allows the current owner to renounce ownership of the factory contract.
|
|
315
|
+
// * This is a dangerous operation and irreversible.
|
|
316
|
+
// * @returns A promise that resolves with the transaction hash.
|
|
317
|
+
// */
|
|
318
|
+
// public async renounceOwnership(): Promise<Hex | undefined> {
|
|
319
|
+
// if (!this.walletClientInstance.account && !this.walletKey) {
|
|
320
|
+
// throw new Error("Wallet key not set or client not connected with an account for write operation.");
|
|
321
|
+
// }
|
|
322
|
+
// try {
|
|
323
|
+
// const accountToUse = this.walletKey ? privateKeyToAccount(this.walletKey) : this.walletClientInstance.account;
|
|
324
|
+
// if (!accountToUse) {
|
|
325
|
+
// throw new Error("Account for transaction could not be determined.");
|
|
326
|
+
// }
|
|
327
|
+
// const hash = await this.contract.write.renounceOwnership([], {
|
|
328
|
+
// account: accountToUse,
|
|
329
|
+
// chain: this.chain,
|
|
330
|
+
// });
|
|
331
|
+
// return hash;
|
|
332
|
+
// } catch (error) {
|
|
333
|
+
// return this.handleError(error, "renounceOwnership");
|
|
334
|
+
// }
|
|
335
|
+
// }
|
|
336
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { loadScriptFromCDN } from "./utils/cdn-loader";
|
|
4
|
+
import { formatNumberSmall } from "./utils/format";
|
|
5
|
+
import { Loader2 } from "lucide-react";
|
|
6
|
+
import { useEffect, useRef, useState } from "react";
|
|
7
|
+
import { TradingViewProps } from "./types";
|
|
8
|
+
|
|
9
|
+
// TypeScript types - these will be loaded from CDN at runtime
|
|
10
|
+
type ChartingLibraryWidgetOptions = any;
|
|
11
|
+
type IChartingLibraryWidget = any;
|
|
12
|
+
type ResolutionString = string;
|
|
13
|
+
type Timezone = string;
|
|
14
|
+
|
|
15
|
+
// TradingView will be available on window after loading from CDN
|
|
16
|
+
|
|
17
|
+
// Datafeed will be implemented inline
|
|
18
|
+
|
|
19
|
+
// Mock loading overlay - replace with your actual loading component
|
|
20
|
+
const GifLoadingOverlay = ({ className }: { className?: string }) => (
|
|
21
|
+
<div className={`absolute inset-0 flex items-center justify-center bg-white/50 backdrop-blur-sm ${className || ""}`}>
|
|
22
|
+
<Loader2 className="text-secondary-grey h-8 w-8 animate-spin" />
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const TradingView = ({ className, tokenAddress, tokenSymbol }: TradingViewProps) => {
|
|
27
|
+
const theme = "light";
|
|
28
|
+
|
|
29
|
+
// Use token info for the current trade
|
|
30
|
+
const currentTrade = {
|
|
31
|
+
product_id: tokenAddress && tokenSymbol ? `${tokenSymbol}-${tokenAddress}` : "BONDKIT",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const [tradingViewDefaultInterval, setTradingViewDefaultInterval] = useState<ResolutionString>("60");
|
|
35
|
+
const [tradingViewTimezone, setTradingViewTimezone] = useState<string>("");
|
|
36
|
+
|
|
37
|
+
const chartContainerRef = useRef<HTMLDivElement>(null);
|
|
38
|
+
const tvWidgetRef = useRef<IChartingLibraryWidget | null>(null);
|
|
39
|
+
const [chartLoaded, setChartLoaded] = useState(false);
|
|
40
|
+
const [showLoading, setShowLoading] = useState(true);
|
|
41
|
+
const [librariesLoaded, setLibrariesLoaded] = useState(false);
|
|
42
|
+
const isChangingInterval = useRef(false);
|
|
43
|
+
|
|
44
|
+
// Load TradingView libraries from CDN
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
const loadLibraries = async () => {
|
|
47
|
+
try {
|
|
48
|
+
// Load TradingView charting library
|
|
49
|
+
await loadScriptFromCDN("/static/charting_library/charting_library.js");
|
|
50
|
+
setLibrariesLoaded(true);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error("Failed to load TradingView libraries:", error);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
loadLibraries();
|
|
57
|
+
}, []);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!chartContainerRef.current || !librariesLoaded) return;
|
|
61
|
+
|
|
62
|
+
// Create UDF-compatible datafeed that mimics the original UDFCompatibleDatafeed
|
|
63
|
+
const createUDFDatafeed = (baseUrl: string) => {
|
|
64
|
+
return {
|
|
65
|
+
onReady: (callback: any) => {
|
|
66
|
+
// Fetch configuration from UDF config endpoint
|
|
67
|
+
fetch(`${baseUrl}/config`)
|
|
68
|
+
.then(response => response.json())
|
|
69
|
+
.then(config => callback(config))
|
|
70
|
+
.catch(() => {
|
|
71
|
+
// Fallback configuration if config endpoint fails
|
|
72
|
+
callback({
|
|
73
|
+
supported_resolutions: ["1", "5", "15", "30", "60", "1D", "1W", "1M"],
|
|
74
|
+
supports_group_request: false,
|
|
75
|
+
supports_marks: false,
|
|
76
|
+
supports_search: false,
|
|
77
|
+
supports_timescale_marks: false,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
searchSymbols: () => {},
|
|
83
|
+
|
|
84
|
+
resolveSymbol: (symbolName: string, onSymbolResolvedCallback: any) => {
|
|
85
|
+
// Try to fetch symbol info from UDF endpoint
|
|
86
|
+
fetch(`${baseUrl}/symbols?symbol=${symbolName}`)
|
|
87
|
+
.then(response => response.json())
|
|
88
|
+
.then(symbolInfo => onSymbolResolvedCallback(symbolInfo))
|
|
89
|
+
.catch(() => {
|
|
90
|
+
// Fallback symbol info
|
|
91
|
+
onSymbolResolvedCallback({
|
|
92
|
+
name: symbolName,
|
|
93
|
+
ticker: symbolName,
|
|
94
|
+
description: symbolName,
|
|
95
|
+
type: "crypto",
|
|
96
|
+
session: "24x7",
|
|
97
|
+
timezone: "Etc/UTC",
|
|
98
|
+
exchange: "BONDKIT",
|
|
99
|
+
minmov: 1,
|
|
100
|
+
pricescale: 10000,
|
|
101
|
+
has_intraday: true,
|
|
102
|
+
supported_resolutions: ["1", "5", "15", "30", "60", "1D", "1W", "1M"],
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
getBars: async (symbolInfo: any, resolution: string, periodParams: any, onHistoryCallback: any) => {
|
|
108
|
+
try {
|
|
109
|
+
const { from, to, countback } = periodParams;
|
|
110
|
+
|
|
111
|
+
// Build URL with parameters matching production
|
|
112
|
+
const params = new URLSearchParams({
|
|
113
|
+
symbol: symbolInfo.ticker,
|
|
114
|
+
resolution: resolution,
|
|
115
|
+
from: from.toString(),
|
|
116
|
+
to: to.toString(),
|
|
117
|
+
currencyCode: "ETH",
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Add countback if provided
|
|
121
|
+
if (countback) {
|
|
122
|
+
params.append("countback", countback.toString());
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const response = await fetch(`${baseUrl}/history?${params}`);
|
|
126
|
+
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const data = await response.json();
|
|
132
|
+
|
|
133
|
+
if (data.s === "ok" && data.t && data.t.length > 0) {
|
|
134
|
+
const bars = data.t.map((time: any, index: number) => ({
|
|
135
|
+
time: parseInt(time) * 1000, // Convert string to number, then to milliseconds
|
|
136
|
+
open: parseFloat(data.o[index]), // Parse string to number
|
|
137
|
+
high: parseFloat(data.h[index]),
|
|
138
|
+
low: parseFloat(data.l[index]),
|
|
139
|
+
close: parseFloat(data.c[index]),
|
|
140
|
+
volume: data.v ? parseFloat(data.v[index]) : 0, // Parse volume string to number
|
|
141
|
+
}));
|
|
142
|
+
|
|
143
|
+
onHistoryCallback(bars, { noData: false });
|
|
144
|
+
} else {
|
|
145
|
+
onHistoryCallback([], { noData: true });
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.error("Error fetching bars:", error);
|
|
149
|
+
onHistoryCallback([], { noData: true });
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
subscribeBars: () => {},
|
|
154
|
+
unsubscribeBars: () => {},
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const datafeed = createUDFDatafeed("https://b3-udf-worker.sean-430.workers.dev/bondkit/udf");
|
|
159
|
+
// Calculate timeframe for last 2 days
|
|
160
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
161
|
+
const twoDaysAgo = currentTime - 2 * 24 * 60 * 60; // 2 days in seconds
|
|
162
|
+
|
|
163
|
+
const widgetOptions: ChartingLibraryWidgetOptions = {
|
|
164
|
+
timeframe: { from: twoDaysAgo, to: currentTime }, // Show last 2 days
|
|
165
|
+
symbol: currentTrade?.product_id || "BONDKIT",
|
|
166
|
+
datafeed: datafeed,
|
|
167
|
+
interval: tradingViewDefaultInterval,
|
|
168
|
+
container: chartContainerRef.current as HTMLElement,
|
|
169
|
+
library_path: "https://cdn.b3.fun/static/charting_library/",
|
|
170
|
+
locale: "en",
|
|
171
|
+
disabled_features: [
|
|
172
|
+
"use_localstorage_for_settings",
|
|
173
|
+
"header_symbol_search",
|
|
174
|
+
"save_chart_properties_to_local_storage",
|
|
175
|
+
"header_compare",
|
|
176
|
+
"vert_touch_drag_scroll",
|
|
177
|
+
],
|
|
178
|
+
enabled_features: ["study_templates", "hide_left_toolbar_by_default"],
|
|
179
|
+
charts_storage_url: "https://saveload.tradingview.com",
|
|
180
|
+
charts_storage_api_version: "1.1",
|
|
181
|
+
client_id: "tradingview.com",
|
|
182
|
+
user_id: "public_user_id",
|
|
183
|
+
fullscreen: false,
|
|
184
|
+
autosize: true,
|
|
185
|
+
timezone: (tradingViewTimezone || getTimezone()) as Timezone,
|
|
186
|
+
debug: true,
|
|
187
|
+
// Configure time frame buttons in the bottom left
|
|
188
|
+
time_frames: [
|
|
189
|
+
{
|
|
190
|
+
text: "5m",
|
|
191
|
+
resolution: "5" as ResolutionString,
|
|
192
|
+
description: "5 days in 5 minute intervals",
|
|
193
|
+
title: "5m",
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
text: "1h",
|
|
197
|
+
resolution: "60" as ResolutionString,
|
|
198
|
+
description: "1 week in 1 hour intervals",
|
|
199
|
+
title: "1h",
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
text: "1d",
|
|
203
|
+
resolution: "1D" as ResolutionString,
|
|
204
|
+
description: "1 month in 1 day intervals",
|
|
205
|
+
title: "1d",
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
text: "1w",
|
|
209
|
+
resolution: "1W" as ResolutionString,
|
|
210
|
+
description: "6 months in 1 week intervals",
|
|
211
|
+
title: "1w",
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
overrides: {
|
|
215
|
+
"paneProperties.background": "#FFFFFF",
|
|
216
|
+
"paneProperties.backgroundType": "solid",
|
|
217
|
+
"mainSeriesProperties.candleStyle.upColor": "#4AD50D",
|
|
218
|
+
"mainSeriesProperties.candleStyle.borderUpColor": "#4AD50D",
|
|
219
|
+
"mainSeriesProperties.candleStyle.downColor": "#F04438",
|
|
220
|
+
"mainSeriesProperties.candleStyle.borderDownColor": "#F04438",
|
|
221
|
+
"scalesProperties.fontSize": 10,
|
|
222
|
+
},
|
|
223
|
+
studies_overrides: {
|
|
224
|
+
"volume.volume.color.0": "#80231C",
|
|
225
|
+
"volume.volume.color.1": "#215611",
|
|
226
|
+
},
|
|
227
|
+
loading_screen: {
|
|
228
|
+
backgroundColor: "transparent",
|
|
229
|
+
},
|
|
230
|
+
custom_css_url: "/custom-css/tradingview.css",
|
|
231
|
+
custom_font_family: "'Roboto', sans-serif",
|
|
232
|
+
toolbar_bg: "#FFFFFF",
|
|
233
|
+
theme: "light",
|
|
234
|
+
|
|
235
|
+
custom_formatters: {
|
|
236
|
+
priceFormatterFactory: () => {
|
|
237
|
+
return {
|
|
238
|
+
format: (price: number) => {
|
|
239
|
+
if (price < 0.0001) {
|
|
240
|
+
return formatNumberSmall(price, true);
|
|
241
|
+
}
|
|
242
|
+
return price.toFixed(4);
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const tvWidget = new (window as any).TradingView.widget(widgetOptions);
|
|
250
|
+
tvWidgetRef.current = tvWidget;
|
|
251
|
+
|
|
252
|
+
tvWidget.onChartReady(() => {
|
|
253
|
+
setShowLoading(false);
|
|
254
|
+
setChartLoaded(true);
|
|
255
|
+
|
|
256
|
+
// Subscribe to interval changes
|
|
257
|
+
tvWidget
|
|
258
|
+
.activeChart()
|
|
259
|
+
.onIntervalChanged()
|
|
260
|
+
.subscribe(null, (interval: ResolutionString) => {
|
|
261
|
+
isChangingInterval.current = true;
|
|
262
|
+
setTradingViewDefaultInterval(interval);
|
|
263
|
+
// Reset the flag after a short delay
|
|
264
|
+
setTimeout(() => {
|
|
265
|
+
isChangingInterval.current = false;
|
|
266
|
+
}, 300);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// Subscribe to timezone changes
|
|
270
|
+
tvWidget
|
|
271
|
+
.activeChart()
|
|
272
|
+
.getTimezoneApi()
|
|
273
|
+
.onTimezoneChanged()
|
|
274
|
+
.subscribe(null, (timezone: string) => {
|
|
275
|
+
setTradingViewTimezone(timezone);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
return () => {
|
|
280
|
+
if (tvWidgetRef.current) {
|
|
281
|
+
tvWidgetRef.current.remove();
|
|
282
|
+
tvWidgetRef.current = null;
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
}, [theme, librariesLoaded]);
|
|
286
|
+
|
|
287
|
+
useEffect(() => {
|
|
288
|
+
if (
|
|
289
|
+
chartLoaded &&
|
|
290
|
+
currentTrade?.product_id &&
|
|
291
|
+
tvWidgetRef.current &&
|
|
292
|
+
!isChangingInterval.current // Only check symbol if we're not changing interval
|
|
293
|
+
) {
|
|
294
|
+
try {
|
|
295
|
+
const currentSymbol = (tvWidgetRef.current as any).symbolInterval()?.symbol;
|
|
296
|
+
const targetSymbol = currentTrade.product_id;
|
|
297
|
+
|
|
298
|
+
// Check if symbols are different (handle both normal and contract address formats)
|
|
299
|
+
const isSameSymbol =
|
|
300
|
+
currentSymbol === targetSymbol ||
|
|
301
|
+
(currentSymbol && targetSymbol && currentSymbol.split("-")[1] === targetSymbol.split("-")[1]); // Compare contract addresses
|
|
302
|
+
|
|
303
|
+
if (!isSameSymbol) {
|
|
304
|
+
setShowLoading(true);
|
|
305
|
+
try {
|
|
306
|
+
(tvWidgetRef.current as any)?.setSymbol?.(targetSymbol, tradingViewDefaultInterval, () => {
|
|
307
|
+
setShowLoading(false);
|
|
308
|
+
});
|
|
309
|
+
} catch (e) {
|
|
310
|
+
console.error("Error setting symbol:", e);
|
|
311
|
+
setShowLoading(false);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
} catch (error) {
|
|
315
|
+
console.error("Error checking symbol:", error);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}, [chartLoaded, currentTrade?.product_id, tradingViewDefaultInterval]);
|
|
319
|
+
|
|
320
|
+
return (
|
|
321
|
+
<div className={`relative h-[600px] w-full ${className || ""}`}>
|
|
322
|
+
<div className="h-full w-full" ref={chartContainerRef} />
|
|
323
|
+
{showLoading && !isChangingInterval.current && <GifLoadingOverlay />}
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export default TradingView;
|
|
329
|
+
|
|
330
|
+
const getTimezone = (): string => {
|
|
331
|
+
// Simple timezone detection
|
|
332
|
+
try {
|
|
333
|
+
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
334
|
+
if (timezone === "Asia/Saigon") {
|
|
335
|
+
return "Asia/Ho_Chi_Minh";
|
|
336
|
+
}
|
|
337
|
+
return timezone;
|
|
338
|
+
} catch (e) {
|
|
339
|
+
return "UTC";
|
|
340
|
+
}
|
|
341
|
+
};
|