Skip to content

Commit

Permalink
[sdk] improve typing of splitCoins and make splitCoins have a concret…
Browse files Browse the repository at this point in the history
…e size (#20665)

## Description 

fixes #20642

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
hayes-mysten authored Dec 19, 2024
1 parent 85bd9e4 commit 4f012b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-gorillas-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/sui': minor
---

Improve typing for the return type of splitCoins and update the splitCoins result type to have a concrete size
32 changes: 18 additions & 14 deletions sdk/typescript/src/transactions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type TransactionObjectArgument =
export type TransactionResult = Extract<Argument, { Result: unknown }> &
Extract<Argument, { NestedResult: unknown }>[];

function createTransactionResult(index: number) {
function createTransactionResult(index: number, length = Infinity): TransactionResult {
const baseResult = { $kind: 'Result' as const, Result: index };

const nestedResults: {
Expand Down Expand Up @@ -71,7 +71,7 @@ function createTransactionResult(index: number) {
if (property === Symbol.iterator) {
return function* () {
let i = 0;
while (true) {
while (i < length) {
yield nestedResultFor(i);
i++;
}
Expand Down Expand Up @@ -410,20 +410,24 @@ export class Transaction {

// Method shorthands:

splitCoins(
coin: TransactionObjectArgument | string,
amounts: (TransactionArgument | SerializedBcs<any> | number | string | bigint)[],
) {
return this.add(
Commands.SplitCoins(
typeof coin === 'string' ? this.object(coin) : this.#resolveArgument(coin),
amounts.map((amount) =>
typeof amount === 'number' || typeof amount === 'bigint' || typeof amount === 'string'
? this.pure.u64(amount)
: this.#normalizeTransactionArgument(amount),
),
splitCoins<
const Amounts extends (TransactionArgument | SerializedBcs<any> | number | string | bigint)[],
>(coin: TransactionObjectArgument | string, amounts: Amounts) {
const command = Commands.SplitCoins(
typeof coin === 'string' ? this.object(coin) : this.#resolveArgument(coin),
amounts.map((amount) =>
typeof amount === 'number' || typeof amount === 'bigint' || typeof amount === 'string'
? this.pure.u64(amount)
: this.#normalizeTransactionArgument(amount),
),
);
const index = this.#data.commands.push(command);
return createTransactionResult(index - 1, amounts.length) as Extract<
Argument,
{ Result: unknown }
> & {
[K in keyof Amounts]: Extract<Argument, { NestedResult: unknown }>;
};
}
mergeCoins(
destination: TransactionObjectArgument | string,
Expand Down

0 comments on commit 4f012b9

Please sign in to comment.