Skip to content

Commit 32bdba6

Browse files
committed
chore(ts-sdk-sui): added comments
Signed-off-by: kaancaglan <[email protected]>
1 parent 321dd24 commit 32bdba6

File tree

1 file changed

+120
-183
lines changed

1 file changed

+120
-183
lines changed

ts-sdk-sui/src/Sui.ts

Lines changed: 120 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,16 @@ export class CreateWalletClientError
299299
{}
300300

301301
/**
302-
* Read Coin token metadata (name, symbol, decimals)
303-
* @param tokenAddress The address of the Coin token
304-
* @returns An Effect that resolves to the coin metadata
302+
* Read Coin metadata (name, symbol, decimals, …) for a given `coinType`.
303+
*
304+
* Example:
305+
* ```ts
306+
* const meta = yield* readCoinMetadata("0x2::sui::SUI")
307+
* ```
308+
*
309+
* @param tokenAddress Canonical coin type string (e.g., `"0x2::sui::SUI"`)
310+
* @returns Effect resolving to `getCoinMetadata` result
311+
* @throws ReadCoinError on RPC failure
305312
*
306313
* @category utils
307314
* @since 0.0.0
@@ -323,6 +330,20 @@ export const readCoinMetadata = (tokenAddress: Address) =>
323330
return metadata
324331
})
325332

333+
/**
334+
* Read all coin objects for a given `coinType` and owner address.
335+
*
336+
* Note:
337+
* - Sui splits balances across multiple coin objects; each carries `balance` and `coinObjectId`.
338+
* - Use {@link readTotalCoinBalance} if you want a single summed value.
339+
*
340+
* @param contractAddress Canonical coin type (e.g., `"0x2::sui::SUI"`)
341+
* @param address Owner Sui address
342+
* @returns Effect resolving to the paged coin objects’ `data` array
343+
*
344+
* @category utils
345+
* @since 0.0.0
346+
*/
326347
export const readCoinBalances = (contractAddress: string, address: string) =>
327348
Effect.gen(function*() {
328349
const client = (yield* PublicClient).client
@@ -344,6 +365,16 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
344365
return coins
345366
})
346367

368+
/**
369+
* Read and sum all coin object balances for a given `coinType` and owner.
370+
*
371+
* @param contractAddress Canonical coin type
372+
* @param address Owner Sui address
373+
* @returns Effect resolving to a `bigint` total (in the coin’s base units)
374+
*
375+
* @category utils
376+
* @since 0.0.0
377+
*/
347378
export const readTotalCoinBalance = (contractAddress: string, address: string) =>
348379
Effect.gen(function*() {
349380
const client = (yield* PublicClient).client
@@ -368,6 +399,16 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
368399
return totalBalance
369400
})
370401

402+
403+
/**
404+
* Fetch *all* coin objects (any coin type) for an owner.
405+
*
406+
* @param address Owner Sui address
407+
* @returns Effect resolving to `getAllCoins().data`
408+
*
409+
* @category utils
410+
* @since 0.0.0
411+
*/
371412
export const getAllCoins = (address: string) =>
372413
Effect.gen(function*() {
373414
const client = (yield* PublicClient).client
@@ -388,6 +429,24 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
388429
return coins
389430
})
390431

432+
/**
433+
* Fetch all coins for an owner and return a unique list grouped by `coinType`,
434+
* with balances summed across coin objects.
435+
*
436+
* Example output:
437+
* ```ts
438+
* [
439+
* { coinType: "0x2::sui::SUI", balance: "123456789" },
440+
* { coinType: "0x...::USDC::USDC", balance: "4200000" }
441+
* ]
442+
* ```
443+
*
444+
* @param address Owner Sui address
445+
* @returns Effect resolving to `{ coinType, balance }[]` (balance as string)
446+
*
447+
* @category utils
448+
* @since 0.0.0
449+
*/
391450
export const getAllCoinsUnique = (address: string) =>
392451
Effect.gen(function*() {
393452
const client = (yield* PublicClient).client
@@ -430,6 +489,16 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
430489
return result
431490
})
432491

492+
493+
/**
494+
* Convenience: read coin **name** for a given `coinType`.
495+
*
496+
* @param address Canonical coin type
497+
* @returns Effect resolving to `string | undefined`
498+
*
499+
* @category utils
500+
* @since 0.0.0
501+
*/
433502
export const getCoinName = (address: string) =>
434503
Effect.gen(function*() {
435504
const client = (yield* PublicClient).client
@@ -447,6 +516,15 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
447516
return name
448517
})
449518

519+
/**
520+
* Convenience: read coin **decimals** for a given `coinType`.
521+
*
522+
* @param address Canonical coin type
523+
* @returns Effect resolving to `number | undefined`
524+
*
525+
* @category utils
526+
* @since 0.0.0
527+
*/
450528
export const getCoinDecimals = (address: string) =>
451529
Effect.gen(function*() {
452530
const client = (yield* PublicClient).client
@@ -463,7 +541,16 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
463541
})
464542
return decimals
465543
})
466-
544+
545+
/**
546+
* Convenience: read coin **symbol** for a given `coinType`.
547+
*
548+
* @param address Canonical coin type
549+
* @returns Effect resolving to `string | undefined`
550+
*
551+
* @category utils
552+
* @since 0.0.0
553+
*/
467554
export const readCoinSymbol = (address: string) =>
468555
Effect.gen(function*() {
469556
const client = (yield* PublicClient).client
@@ -482,26 +569,41 @@ export const readCoinSymbol = (address: string) =>
482569
})
483570

484571

572+
// // TODO: Decide the parameters here.
485573
// /**
486-
// * Read the balance of an ERC20 token for a specific address
487-
// *
488-
// * @param tokenAddress The address of the ERC20 token
489-
// * @param ownerAddress The address to check the balance for
490-
// *
491-
// * @returns An Effect that resolves to the token balance
492-
// *
493574
// * @category utils
494575
// * @since 0.0.0
495576
// */
496-
// export const readErc20Balance = (tokenAddress: Address, ownerAddress: Address) =>
577+
// export const sendInstruction = (instruction: Ucs03.Instruction) =>
497578
// Effect.gen(function*() {
498-
// const client = (yield* PublicClient).client
579+
// const walletClient = yield* WalletClient
580+
// const sourceConfig = yield* ChannelSource
499581

500-
// return yield* readContract(client, {
501-
// address: tokenAddress,
502-
// abi: erc20Abi,
503-
// functionName: "balanceOf",
504-
// args: [ownerAddress],
582+
// const timeoutTimestamp = Utils.getTimeoutInNanoseconds24HoursFromNow()
583+
// const salt = yield* Utils.generateSalt("evm")
584+
585+
// const operand = yield* S.encode(Ucs03.InstructionFromHex)(instruction)
586+
587+
// return yield* writeContract({
588+
// client: walletClient.client,
589+
// signer: walletClient.signer,
590+
// account: walletClient.account,
591+
// abi: Ucs03.Abi,
592+
// // chain: walletClient.chain, TODO: Do we need this?
593+
// fn: "send",
594+
// address: sourceConfig.ucs03address,
595+
// args: [
596+
// sourceConfig.channelId,
597+
// 0n,
598+
// timeoutTimestamp,
599+
// salt,
600+
// {
601+
// opcode: instruction.opcode,
602+
// version: instruction.version,
603+
// operand,
604+
// },
605+
// ],
606+
// value: 10n,
505607
// })
506608
// })
507609

@@ -532,63 +634,6 @@ export const readCoinSymbol = (address: string) =>
532634
// })
533635
// })
534636

535-
// /**
536-
// * Read the name of an ERC20 token
537-
// * @param tokenAddress The address of the ERC20 token
538-
// * @returns An Effect that resolves to the token name
539-
// *
540-
// * @category utils
541-
// * @since 0.0.0
542-
// */
543-
// export const readErc20Name = (tokenAddress: Address) =>
544-
// Effect.gen(function*() {
545-
// const client = (yield* PublicClient).client
546-
547-
// return yield* readContract(client, {
548-
// address: tokenAddress,
549-
// abi: erc20Abi,
550-
// functionName: "name",
551-
// })
552-
// })
553-
554-
// /**
555-
// * Read the symbol of an ERC20 token
556-
// * @param tokenAddress The address of the ERC20 token
557-
// * @returns An Effect that resolves to the token symbol
558-
// *
559-
// * @category utils
560-
// * @since 0.0.0
561-
// */
562-
// export const readErc20Symbol = (tokenAddress: Address) =>
563-
// Effect.gen(function*() {
564-
// const client = (yield* PublicClient).client
565-
566-
// return yield* readContract(client, {
567-
// address: tokenAddress,
568-
// abi: erc20Abi,
569-
// functionName: "symbol",
570-
// })
571-
// })
572-
573-
// /**
574-
// * Read the decimals of an ERC20 token
575-
// * @param tokenAddress The address of the ERC20 token
576-
// * @returns An Effect that resolves to the token decimals
577-
// *
578-
// * @category utils
579-
// * @since 0.0.0
580-
// */
581-
// export const readErc20Decimals = (tokenAddress: Address) =>
582-
// Effect.gen(function*() {
583-
// const client = (yield* PublicClient).client
584-
585-
// return yield* readContract(client, {
586-
// address: tokenAddress,
587-
// abi: erc20Abi,
588-
// functionName: "decimals",
589-
// })
590-
// })
591-
592637
// /**
593638
// * Read the TotalSupply of an ERC20 token
594639
// * @param tokenAddress The address of the ERC20 token
@@ -609,111 +654,3 @@ export const readCoinSymbol = (address: string) =>
609654
// blockNumber: blockNumber,
610655
// })
611656
// })
612-
613-
// /**
614-
// * Read the TotalSupply of an ERC20 token
615-
// * @param tokenAddress The address of the ERC20 token
616-
// * @returns An Effect that resolves to the totalSupply
617-
// *
618-
// * @category utils
619-
// * @since 0.0.0
620-
// */
621-
// export const readErc20TotalSupply = (tokenAddress: Address) =>
622-
// Effect.gen(function*() {
623-
// const client = (yield* PublicClient).client
624-
625-
// return yield* readContract(client, {
626-
// address: tokenAddress,
627-
// abi: erc20Abi,
628-
// functionName: "totalSupply",
629-
// })
630-
// })
631-
632-
// /**
633-
// * Read the allowance of an ERC20 token for a specific owner and spender
634-
// * @param tokenAddress The address of the ERC20 token
635-
// * @param ownerAddress The address of the token owner
636-
// * @param spenderAddress The address of the spender
637-
// * @returns An Effect that resolves to the token allowance
638-
// *
639-
// * @category utils
640-
// * @since 0.0.0
641-
// */
642-
// export const readErc20Allowance = (
643-
// tokenAddress: Address,
644-
// ownerAddress: Address,
645-
// spenderAddress: Address,
646-
// ) =>
647-
// Effect.gen(function*() {
648-
// const client = (yield* PublicClient).client
649-
650-
// return yield* readContract(client, {
651-
// address: tokenAddress,
652-
// abi: erc20Abi,
653-
// functionName: "allowance",
654-
// args: [ownerAddress, spenderAddress],
655-
// })
656-
// })
657-
658-
// /**
659-
// * Increase the allowance of an ERC20 token for a specific spender
660-
// * @param tokenAddress The address of the ERC20 token
661-
// * @param spenderAddress The address of the spender
662-
// * @param amount The amount to increase the allowance by
663-
// * @returns An Effect that resolves to the transaction hash
664-
// *
665-
// * @category utils
666-
// * @since 0.0.0
667-
// */
668-
// export const increaseErc20Allowance = (
669-
// tokenAddress: Address,
670-
// spenderAddress: Address,
671-
// amount: bigint,
672-
// ) =>
673-
// Effect.gen(function*() {
674-
// const walletClient = yield* WalletClient
675-
676-
// return yield* writeContract({
677-
// account: walletClient.account,
678-
// chain: walletClient.chain,
679-
// address: tokenAddress,
680-
// abi: erc20Abi,
681-
// functionName: "approve",
682-
// args: [spenderAddress, amount],
683-
// })
684-
// })
685-
686-
// /**
687-
// * @category utils
688-
// * @since 0.0.0
689-
// */
690-
// export const sendInstruction = (instruction: Ucs03.Instruction) =>
691-
// Effect.gen(function*() {
692-
// const walletClient = yield* WalletClient
693-
// const sourceConfig = yield* ChannelSource
694-
695-
// const timeoutTimestamp = Utils.getTimeoutInNanoseconds24HoursFromNow()
696-
// const salt = yield* Utils.generateSalt("evm")
697-
698-
// const operand = yield* S.encode(Ucs03.InstructionFromHex)(instruction)
699-
700-
// return yield* writeContract({
701-
// account: walletClient.account,
702-
// abi: Ucs03.Abi,
703-
// chain: walletClient.chain,
704-
// functionName: "send",
705-
// address: sourceConfig.ucs03address,
706-
// args: [
707-
// sourceConfig.channelId,
708-
// 0n,
709-
// timeoutTimestamp,
710-
// salt,
711-
// {
712-
// opcode: instruction.opcode,
713-
// version: instruction.version,
714-
// operand,
715-
// },
716-
// ],
717-
// value: 10n,
718-
// })
719-
// })

0 commit comments

Comments
 (0)