Skip to content

Commit

Permalink
[ts-sdk] Add tx.object.option for creatnig object options in transact… (
Browse files Browse the repository at this point in the history
MystenLabs#20194)

…ion builder

## Description 

Describe the changes or additions included in this PR.

## 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 Nov 12, 2024
1 parent de2c7de commit d5a23d7
Show file tree
Hide file tree
Showing 21 changed files with 632 additions and 370 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-dancers-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/sui': patch
---

Add tx.object.option for creatnig object options in transaction builder
12 changes: 6 additions & 6 deletions crates/sui-core/src/unit_tests/data/entry_point_types/Move.lock
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 0
version = 3
manifest_digest = "84B1ECECEB235E605B786F20F7F6441F45D0E087391F555801F49504C491BECE"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"

dependencies = [
{ name = "Sui" },
{ id = "Sui", name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
id = "MoveStdlib"
source = { local = "../../../../../sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
id = "Sui"
source = { local = "../../../../../sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
{ id = "MoveStdlib", name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.30.0"
compiler-version = "1.38.0"
edition = "2024.beta"
flavor = "sui"
12 changes: 6 additions & 6 deletions crates/sui-core/src/unit_tests/data/entry_point_vector/Move.lock
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 0
version = 3
manifest_digest = "78784529799C8284788E91AB0A8668CA7759418A3D6133C3ADE575F648524A1E"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"

dependencies = [
{ name = "Sui" },
{ id = "Sui", name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
id = "MoveStdlib"
source = { local = "../../../../../sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
id = "Sui"
source = { local = "../../../../../sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
{ id = "MoveStdlib", name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.30.0"
compiler-version = "1.38.0"
edition = "2024.beta"
flavor = "sui"
20 changes: 19 additions & 1 deletion sdk/docs/pages/typescript/transaction-building/basics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ tx.moveCall({
arguments: [tx.object('0xSomeObject')],
});

// tx.object automaically converts the object ID to receiving transaction arguments if the moveCall expects it
// tx.object automatically converts the object ID to receiving transaction arguments if the moveCall expects it
tx.moveCall({
target: '0xSomeAddress::example::receive_object',
// 0xSomeAddress::example::receive_object expects a receiving argument and has a Move definition that looks like this:
Expand All @@ -217,6 +217,24 @@ tx.object(Inputs.SharedObjectRef({ objectId, initialSharedVersion, mutable }));
tx.object(Inputs.ReceivingRef({ digest, objectId, version }));
```

##### Object helpers

There are a handful of specific object types that can be referenced through helper methods on
tx.object:

```ts
tx.object.system(),
tx.object.clock(),
tx.object.random(),
tx.object.denyList(),

tx.object.option({
type: '0x123::example::Thing',
// value can be an Object ID, or any other object reference, or null for `none`
value: '0x456',
}),
```

#### Transaction results

You can also use the result of a transaction as an argument in a subsequent transactions. Each
Expand Down
10 changes: 9 additions & 1 deletion sdk/typescript/src/transactions/object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import type { TransactionObjectInput } from './Transaction.js';
import type { Transaction, TransactionObjectInput } from './Transaction.js';

export function createObjectMethods<T>(makeObject: (value: TransactionObjectInput) => T) {
function object(value: TransactionObjectInput) {
Expand All @@ -12,6 +12,14 @@ export function createObjectMethods<T>(makeObject: (value: TransactionObjectInpu
object.clock = () => object('0x6');
object.random = () => object('0x8');
object.denyList = () => object('0x403');
object.option =
({ type, value }: { type: string; value: TransactionObjectInput | null }) =>
(tx: Transaction) =>
tx.moveCall({
typeArguments: [type],
target: `0x1::option::${value === null ? 'none' : 'some'}`,
arguments: value === null ? [] : [tx.object(value)],
});

return object;
}
12 changes: 6 additions & 6 deletions sdk/typescript/test/e2e/data/coin_metadata/Move.lock
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 0
version = 3
manifest_digest = "AFCC8C08D078426D5FE98FB22E21578C4A7B9C1259C6EE53BEF2861BB801C976"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"

dependencies = [
{ name = "Sui" },
{ id = "Sui", name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
id = "MoveStdlib"
source = { local = "../../../../../../crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
id = "Sui"
source = { local = "../../../../../../crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
{ id = "MoveStdlib", name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.30.0"
compiler-version = "1.38.0"
edition = "2024.beta"
flavor = "sui"
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module coin_metadata::test {
use std::option;
use sui::coin;
use sui::transfer;
use sui::url;
module coin_metadata::test;

public struct TEST has drop {}
use sui::coin;
use sui::url;

fun init(witness: TEST, ctx: &mut TxContext) {
let (mut treasury_cap, metadata) = coin::create_currency<TEST>(
witness,
2,
b"TEST",
b"Test Coin",
b"Test coin metadata",
option::some(url::new_unsafe_from_bytes(b"http://sui.io")),
ctx
);
public struct TEST has drop {}

coin::mint_and_transfer<TEST>(&mut treasury_cap, 5, tx_context::sender(ctx), ctx);
coin::mint_and_transfer<TEST>(&mut treasury_cap, 6, tx_context::sender(ctx), ctx);
fun init(witness: TEST, ctx: &mut TxContext) {
let (mut treasury_cap, metadata) = coin::create_currency<TEST>(
witness,
2,
b"TEST",
b"Test Coin",
b"Test coin metadata",
option::some(url::new_unsafe_from_bytes(b"http://sui.io")),
ctx,
);

transfer::public_share_object(metadata);
transfer::public_share_object(treasury_cap)
}
coin::mint_and_transfer<TEST>(
&mut treasury_cap,
5,
tx_context::sender(ctx),
ctx,
);
coin::mint_and_transfer<TEST>(
&mut treasury_cap,
6,
tx_context::sender(ctx),
ctx,
);

transfer::public_share_object(metadata);
transfer::public_share_object(treasury_cap)
}
97 changes: 48 additions & 49 deletions sdk/typescript/test/e2e/data/demo-bear/sources/demo_bear.move
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module demo::demo_bear {
use std::string::{String, utf8};
module demo::demo_bear;

use sui::package;
use sui::display;
use std::string::{String, utf8};
use sui::display;
use sui::package;

/// our demo struct.
public struct DemoBear has key, store {
id: UID,
name: String
}
/// our demo struct.
public struct DemoBear has key, store {
id: UID,
name: String,
}

/// our OTW to create display.
public struct DEMO_BEAR has drop {}

// It's recommened to create Display using PTBs instead of
// directly on the contracts.
// We are only creating it here for demo purposes (one-step setup).
fun init(otw: DEMO_BEAR, ctx: &mut TxContext){
let publisher = package::claim(otw, ctx);
let keys = vector[
utf8(b"name"),
utf8(b"image_url"),
utf8(b"description"),
];


let values = vector[
// Let's add a demo name for our `DemoBear`
utf8(b"{name}"),
// Adding a happy bear image.
utf8(b"https://images.unsplash.com/photo-1589656966895-2f33e7653819?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8cG9sYXIlMjBiZWFyfGVufDB8fDB8fHww"),
// Description is static for all bears out there.
utf8(b"The greatest figure for demos"),
];

// Get a new `Display` object for the `Hero` type.
let mut display = display::new_with_fields<DemoBear>(
&publisher, keys, values, ctx
);

// Commit first version of `Display` to apply changes.
display::update_version(&mut display);

sui::transfer::public_transfer(display, ctx.sender());
sui::transfer::public_transfer(publisher, ctx.sender())
}
/// our OTW to create display.
public struct DEMO_BEAR has drop {}

// It's recommened to create Display using PTBs instead of
// directly on the contracts.
// We are only creating it here for demo purposes (one-step setup).
fun init(otw: DEMO_BEAR, ctx: &mut TxContext) {
let publisher = package::claim(otw, ctx);
let keys = vector[utf8(b"name"), utf8(b"image_url"), utf8(b"description")];

let values = vector[
// Let's add a demo name for our `DemoBear`
utf8(b"{name}"),
// Adding a happy bear image.
utf8(
b"https://images.unsplash.com/photo-1589656966895-2f33e7653819?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8cG9sYXIlMjBiZWFyfGVufDB8fDB8fHww",
),
// Description is static for all bears out there.
utf8(b"The greatest figure for demos"),
];

// Get a new `Display` object for the `Hero` type.
let mut display = display::new_with_fields<DemoBear>(
&publisher,
keys,
values,
ctx,
);

// Commit first version of `Display` to apply changes.
display::update_version(&mut display);

sui::transfer::public_transfer(display, ctx.sender());
sui::transfer::public_transfer(publisher, ctx.sender())
}

public fun new(name: String, ctx: &mut TxContext): DemoBear {
DemoBear {
id: object::new(ctx),
name: name
}
public fun new(name: String, ctx: &mut TxContext): DemoBear {
DemoBear {
id: object::new(ctx),
name: name,
}
}
Loading

0 comments on commit d5a23d7

Please sign in to comment.