@b3dotfun/sdk 0.0.40-alpha.4 → 0.0.40-alpha.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.
Files changed (38) hide show
  1. package/dist/cjs/bondkit/bondkitToken.d.ts +36 -1
  2. package/dist/cjs/bondkit/bondkitToken.js +266 -0
  3. package/dist/cjs/bondkit/constants.d.ts +4 -0
  4. package/dist/cjs/bondkit/constants.js +6 -1
  5. package/dist/cjs/bondkit/index.d.ts +1 -0
  6. package/dist/cjs/bondkit/index.js +4 -1
  7. package/dist/cjs/bondkit/swapService.d.ts +43 -0
  8. package/dist/cjs/bondkit/swapService.js +373 -0
  9. package/dist/cjs/bondkit/types.d.ts +10 -4
  10. package/dist/cjs/bondkit/types.js +4 -5
  11. package/dist/cjs/global-account/react/components/LinkAccount/LinkAccount.js +63 -3
  12. package/dist/cjs/global-account/react/components/ManageAccount/ManageAccount.js +35 -2
  13. package/dist/esm/bondkit/bondkitToken.d.ts +36 -1
  14. package/dist/esm/bondkit/bondkitToken.js +266 -0
  15. package/dist/esm/bondkit/constants.d.ts +4 -0
  16. package/dist/esm/bondkit/constants.js +5 -0
  17. package/dist/esm/bondkit/index.d.ts +1 -0
  18. package/dist/esm/bondkit/index.js +2 -0
  19. package/dist/esm/bondkit/swapService.d.ts +43 -0
  20. package/dist/esm/bondkit/swapService.js +369 -0
  21. package/dist/esm/bondkit/types.d.ts +10 -4
  22. package/dist/esm/bondkit/types.js +4 -5
  23. package/dist/esm/global-account/react/components/LinkAccount/LinkAccount.js +65 -5
  24. package/dist/esm/global-account/react/components/ManageAccount/ManageAccount.js +35 -2
  25. package/dist/styles/index.css +1 -1
  26. package/dist/types/bondkit/bondkitToken.d.ts +36 -1
  27. package/dist/types/bondkit/constants.d.ts +4 -0
  28. package/dist/types/bondkit/index.d.ts +1 -0
  29. package/dist/types/bondkit/swapService.d.ts +43 -0
  30. package/dist/types/bondkit/types.d.ts +10 -4
  31. package/package.json +1 -1
  32. package/src/bondkit/bondkitToken.ts +321 -1
  33. package/src/bondkit/constants.ts +7 -0
  34. package/src/bondkit/index.ts +3 -0
  35. package/src/bondkit/swapService.ts +461 -0
  36. package/src/bondkit/types.ts +12 -5
  37. package/src/global-account/react/components/LinkAccount/LinkAccount.tsx +106 -32
  38. package/src/global-account/react/components/ManageAccount/ManageAccount.tsx +60 -5
@@ -13,13 +13,15 @@ import { privateKeyToAccount } from "viem/accounts";
13
13
  import { base } from "viem/chains";
14
14
  import { BondkitTokenABI } from "./abis";
15
15
  import { getConfig } from "./config";
16
+ import { BondkitSwapService } from "./swapService";
16
17
  import type {
17
18
  BondkitTokenInitializationConfig,
18
19
  GetTransactionHistoryOptions,
20
+ SwapQuote,
19
21
  TokenDetails,
20
- TokenStatus,
21
22
  TransactionResponse,
22
23
  } from "./types";
24
+ import { TokenStatus } from "./types";
23
25
 
24
26
  // Event ABI snippets for decoding
25
27
  const boughtEventAbi = BondkitTokenABI.find(item => item.type === "event" && item.name === "BondingCurveBuy");
@@ -53,6 +55,7 @@ export class BondkitToken {
53
55
  private walletClientInstance: WalletClient;
54
56
  private connectedProvider?: EIP1193Provider;
55
57
  private tradingToken?: Address;
58
+ private swapService?: BondkitSwapService;
56
59
 
57
60
  constructor(contractAddress: string, walletKey?: string, rpcUrl?: string) {
58
61
  const sdkConfig = getConfig(base.id, rpcUrl);
@@ -657,6 +660,323 @@ export class BondkitToken {
657
660
  return this.executeWrite("renounceOwnership", [], options);
658
661
  }
659
662
 
663
+ // --- DEX Swap Methods ---
664
+
665
+ /**
666
+ * Get the swap service instance (lazy initialization)
667
+ */
668
+ private getSwapService(): BondkitSwapService {
669
+ if (!this.swapService) {
670
+ this.swapService = new BondkitSwapService(this.contractAddress);
671
+ }
672
+ return this.swapService;
673
+ }
674
+
675
+ /**
676
+ * Check if DEX swapping is available (token must be in Dex phase)
677
+ */
678
+ public async isSwapAvailable(): Promise<boolean | undefined> {
679
+ try {
680
+ const status = await this.currentStatus();
681
+ return status === TokenStatus.Dex;
682
+ } catch (error) {
683
+ console.warn("Error checking swap availability:", error);
684
+ return undefined;
685
+ }
686
+ }
687
+
688
+ /**
689
+ * Get swap quote for trading token → bondkit token
690
+ */
691
+ public async getSwapQuoteForBondkitToken(
692
+ amountTradingTokenIn: string,
693
+ slippageTolerance = 0.5,
694
+ ): Promise<SwapQuote | undefined> {
695
+ try {
696
+ // Check if swapping is available
697
+ const swapAvailable = await this.isSwapAvailable();
698
+ if (!swapAvailable) {
699
+ console.warn("DEX swapping not available - token must be in Dex phase");
700
+ return undefined;
701
+ }
702
+
703
+ const tradingTokenAddress = await this.getTradingTokenAddress();
704
+ if (!tradingTokenAddress) {
705
+ console.warn("Trading token address not available");
706
+ return undefined;
707
+ }
708
+
709
+ // Get token details for decimals
710
+ const [tradingTokenDecimals, bondkitTokenDecimals] = await Promise.all([
711
+ this.getTradingTokenDecimals(tradingTokenAddress),
712
+ this.decimals(),
713
+ ]);
714
+
715
+ if (tradingTokenDecimals === undefined || bondkitTokenDecimals === undefined) {
716
+ console.warn("Unable to fetch token decimals");
717
+ return undefined;
718
+ }
719
+
720
+ const swapService = this.getSwapService();
721
+ const quote = await swapService.getSwapQuote({
722
+ tokenIn: tradingTokenAddress,
723
+ tokenOut: this.contractAddress,
724
+ amountIn: amountTradingTokenIn,
725
+ tokenInDecimals: tradingTokenDecimals,
726
+ tokenOutDecimals: bondkitTokenDecimals,
727
+ slippageTolerance,
728
+ recipient: this.walletClientInstance.account?.address || "0x0000000000000000000000000000000000000000",
729
+ });
730
+ return quote || undefined;
731
+ } catch (error) {
732
+ console.warn("Error getting swap quote for bondkit token:", error);
733
+ return undefined;
734
+ }
735
+ }
736
+
737
+ /**
738
+ * Get swap quote for bondkit token → trading token
739
+ */
740
+ public async getSwapQuoteForTradingToken(
741
+ amountBondkitTokenIn: string,
742
+ slippageTolerance = 0.5,
743
+ ): Promise<SwapQuote | undefined> {
744
+ try {
745
+ // Check if swapping is available
746
+ const swapAvailable = await this.isSwapAvailable();
747
+ if (!swapAvailable) {
748
+ console.warn("DEX swapping not available - token must be in Dex phase");
749
+ return undefined;
750
+ }
751
+
752
+ const tradingTokenAddress = await this.getTradingTokenAddress();
753
+ if (!tradingTokenAddress) {
754
+ console.warn("Trading token address not available");
755
+ return undefined;
756
+ }
757
+
758
+ // Get token details for decimals
759
+ const [bondkitTokenDecimals, tradingTokenDecimals] = await Promise.all([
760
+ this.decimals(),
761
+ this.getTradingTokenDecimals(tradingTokenAddress),
762
+ ]);
763
+
764
+ if (bondkitTokenDecimals === undefined || tradingTokenDecimals === undefined) {
765
+ console.warn("Unable to fetch token decimals");
766
+ return undefined;
767
+ }
768
+
769
+ const swapService = this.getSwapService();
770
+ const quote = await swapService.getSwapQuote({
771
+ tokenIn: this.contractAddress,
772
+ tokenOut: tradingTokenAddress,
773
+ amountIn: amountBondkitTokenIn,
774
+ tokenInDecimals: bondkitTokenDecimals,
775
+ tokenOutDecimals: tradingTokenDecimals,
776
+ slippageTolerance,
777
+ recipient: this.walletClientInstance.account?.address || "0x0000000000000000000000000000000000000000",
778
+ });
779
+ return quote || undefined;
780
+ } catch (error) {
781
+ console.warn("Error getting swap quote for trading token:", error);
782
+ return undefined;
783
+ }
784
+ }
785
+
786
+ /**
787
+ * Swap trading token for bondkit token
788
+ */
789
+ public async swapTradingTokenForBondkitToken(
790
+ amountTradingTokenIn: string,
791
+ slippageTolerance = 0.5,
792
+ options?: ExecuteWriteOptions,
793
+ ): Promise<Hex | undefined> {
794
+ try {
795
+ // Check if swapping is available
796
+ const swapAvailable = await this.isSwapAvailable();
797
+ if (!swapAvailable) {
798
+ console.warn("DEX swapping not available - token must be in Dex phase");
799
+ return undefined;
800
+ }
801
+
802
+ if (!this.walletClientInstance.account && !this.walletKey) {
803
+ console.warn("Wallet key not set or client not connected for swap operation");
804
+ return undefined;
805
+ }
806
+
807
+ const tradingTokenAddress = await this.getTradingTokenAddress();
808
+ if (!tradingTokenAddress) {
809
+ console.warn("Trading token address not available");
810
+ return undefined;
811
+ }
812
+
813
+ // Get token details for decimals
814
+ const [tradingTokenDecimals, bondkitTokenDecimals] = await Promise.all([
815
+ this.getTradingTokenDecimals(tradingTokenAddress),
816
+ this.decimals(),
817
+ ]);
818
+
819
+ if (tradingTokenDecimals === undefined || bondkitTokenDecimals === undefined) {
820
+ console.warn("Unable to fetch token decimals");
821
+ return undefined;
822
+ }
823
+
824
+ const recipient =
825
+ this.walletClientInstance.account?.address ||
826
+ (this.walletKey ? privateKeyToAccount(this.walletKey).address : undefined);
827
+
828
+ if (!recipient) {
829
+ console.warn("Unable to determine recipient address");
830
+ return undefined;
831
+ }
832
+
833
+ const swapService = this.getSwapService();
834
+ const txHash = await swapService.executeSwap(
835
+ {
836
+ tokenIn: tradingTokenAddress,
837
+ tokenOut: this.contractAddress,
838
+ amountIn: amountTradingTokenIn,
839
+ tokenInDecimals: tradingTokenDecimals,
840
+ tokenOutDecimals: bondkitTokenDecimals,
841
+ slippageTolerance,
842
+ recipient,
843
+ deadline: (options?.value ? Math.floor(Date.now() / 1000) : 0) + 3600,
844
+ },
845
+ this.walletClientInstance,
846
+ );
847
+
848
+ return txHash ? (txHash as Hex) : undefined;
849
+ } catch (error) {
850
+ console.warn("Error swapping trading token for bondkit token:", error);
851
+ return undefined;
852
+ }
853
+ }
854
+
855
+ /**
856
+ * Swap bondkit token for trading token
857
+ */
858
+ public async swapBondkitTokenForTradingToken(
859
+ amountBondkitTokenIn: string,
860
+ slippageTolerance = 0.5,
861
+ options?: ExecuteWriteOptions,
862
+ ): Promise<Hex | undefined> {
863
+ try {
864
+ // Check if swapping is available
865
+ const swapAvailable = await this.isSwapAvailable();
866
+ if (!swapAvailable) {
867
+ console.warn("DEX swapping not available - token must be in Dex phase");
868
+ return undefined;
869
+ }
870
+
871
+ if (!this.walletClientInstance.account && !this.walletKey) {
872
+ console.warn("Wallet key not set or client not connected for swap operation");
873
+ return undefined;
874
+ }
875
+
876
+ const tradingTokenAddress = await this.getTradingTokenAddress();
877
+ if (!tradingTokenAddress) {
878
+ console.warn("Trading token address not available");
879
+ return undefined;
880
+ }
881
+
882
+ // Get token details for decimals
883
+ const [bondkitTokenDecimals, tradingTokenDecimals] = await Promise.all([
884
+ this.decimals(),
885
+ this.getTradingTokenDecimals(tradingTokenAddress),
886
+ ]);
887
+
888
+ if (bondkitTokenDecimals === undefined || tradingTokenDecimals === undefined) {
889
+ console.warn("Unable to fetch token decimals");
890
+ return undefined;
891
+ }
892
+
893
+ const recipient =
894
+ this.walletClientInstance.account?.address ||
895
+ (this.walletKey ? privateKeyToAccount(this.walletKey).address : undefined);
896
+
897
+ if (!recipient) {
898
+ console.warn("Unable to determine recipient address");
899
+ return undefined;
900
+ }
901
+
902
+ const swapService = this.getSwapService();
903
+ const txHash = await swapService.executeSwap(
904
+ {
905
+ tokenIn: this.contractAddress,
906
+ tokenOut: tradingTokenAddress,
907
+ amountIn: amountBondkitTokenIn,
908
+ tokenInDecimals: bondkitTokenDecimals,
909
+ tokenOutDecimals: tradingTokenDecimals,
910
+ slippageTolerance,
911
+ recipient,
912
+ deadline: (options?.value ? Math.floor(Date.now() / 1000) : 0) + 3600,
913
+ },
914
+ this.walletClientInstance,
915
+ );
916
+
917
+ return txHash ? (txHash as Hex) : undefined;
918
+ } catch (error) {
919
+ console.warn("Error swapping bondkit token for trading token:", error);
920
+ return undefined;
921
+ }
922
+ }
923
+
924
+ /**
925
+ * Helper method to get trading token decimals
926
+ */
927
+ private async getTradingTokenDecimals(tradingTokenAddress: Address): Promise<number | undefined> {
928
+ try {
929
+ // ETH has 18 decimals
930
+ if (tradingTokenAddress === "0x0000000000000000000000000000000000000000") {
931
+ return 18;
932
+ }
933
+
934
+ // For ERC20 tokens, read decimals from contract
935
+ const tradingTokenContract = getContract({
936
+ address: tradingTokenAddress,
937
+ abi: erc20Abi,
938
+ client: this.publicClient,
939
+ });
940
+
941
+ const decimals = await tradingTokenContract.read.decimals();
942
+ return Number(decimals);
943
+ } catch (error) {
944
+ console.warn("Error fetching trading token decimals:", error);
945
+ return undefined;
946
+ }
947
+ }
948
+
949
+ /**
950
+ * Get trading token symbol
951
+ * @param tradingTokenAddress Optional trading token address to avoid fetching it again
952
+ */
953
+ public async getTradingTokenSymbol(tradingTokenAddress?: Address): Promise<string | undefined> {
954
+ try {
955
+ const tokenAddress = tradingTokenAddress || (await this.getTradingTokenAddress());
956
+ if (!tokenAddress) {
957
+ return undefined;
958
+ }
959
+
960
+ // ETH symbol
961
+ if (tokenAddress === "0x0000000000000000000000000000000000000000") {
962
+ return "ETH";
963
+ }
964
+
965
+ // For ERC20 tokens, read symbol from contract
966
+ const tradingTokenContract = getContract({
967
+ address: tokenAddress,
968
+ abi: erc20Abi,
969
+ client: this.publicClient,
970
+ });
971
+
972
+ const symbol = await tradingTokenContract.read.symbol();
973
+ return symbol;
974
+ } catch (error) {
975
+ console.warn("Error fetching trading token symbol:", error);
976
+ return undefined;
977
+ }
978
+ }
979
+
660
980
  // TODO: Add other specific write methods from BondkitTokenABI.ts
661
981
  // e.g., setBondingCurve (if it exists and is external), updateArtistAddress, etc.
662
982
  }
@@ -3,3 +3,10 @@ import type { Address } from "viem";
3
3
  export const BaseBondkitTokenFactoryContractAddress: Address = "0x5d641bbB206d4B5585eCCd919F36270200A9A2Ad";
4
4
 
5
5
  export const BaseMainnetRpcUrl = "https://base-rpc.publicnode.com";
6
+
7
+ // Uniswap V4 addresses on Base
8
+ export const UniversalRouterAddress: Address = "0x6ff5693b99212da76ad316178a184ab56d299b43";
9
+ export const QuoterAddress: Address = "0x0d5e0f971ed27fbff6c2837bf31316121532048d";
10
+ export const Permit2Address: Address = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
11
+
12
+ export const B3TokenAddress: Address = "0xB3B32F9f8827D4634fE7d973Fa1034Ec9fdDB3B3";
@@ -12,5 +12,8 @@ export * from "./types";
12
12
  // ABIs
13
13
  export * from "./abis";
14
14
 
15
+ // Swap functionality
16
+ export { BondkitSwapService } from "./swapService";
17
+
15
18
  // Components
16
19
  export { default as TradingView } from "./components/TradingView";