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
…ion builder
  • Loading branch information
hayes-mysten committed Nov 6, 2024
1 parent 4a6eaad commit 61d7b68
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 59 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
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
2 changes: 1 addition & 1 deletion sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"test:unit": "vitest run unit __tests__",
"test:e2e": "wait-on http://127.0.0.1:9123 -l --timeout 180000 && vitest run e2e",
"test:e2e:nowait": "vitest run e2e",
"prepare:e2e": "docker-compose down && docker-compose up -d && cargo build --bin sui --profile dev && cross-env RUST_LOG=warn,sui=error,anemo_tower=warn,consensus=off ../../target/debug/sui start --with-faucet --force-regenesis --with-indexer --pg-port 5435 --pg-db-name sui_indexer_v2 --with-graphql",
"prepare:e2e": "docker-compose down && docker-compose up -d && cargo build --bin sui --profile dev && cross-env RUST_LOG=warn,anemo_tower=warn,consensus=off ../../target/debug/sui start --with-faucet --force-regenesis --with-indexer --pg-port 5435 --pg-db-name sui_indexer_v2 --with-graphql",
"prepublishOnly": "pnpm build",
"size": "size-limit",
"analyze": "size-limit --why",
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/serializer/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 = "8C8B9ADAFF8B7267E4476A3DF08A72810194D36146AC710F0545C8843B1F1075"
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"
115 changes: 65 additions & 50 deletions sdk/typescript/test/e2e/data/serializer/sources/serializer.move
Original file line number Diff line number Diff line change
@@ -1,66 +1,81 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module serializer::serializer_tests {
use sui::clock::Clock;
use std::option::Option;
use sui::object::ID;
use std::string::String;
use std::ascii;

public struct MutableShared has key {
id: UID,
value: u64,
}
module serializer::serializer_tests;

fun init(ctx: &mut TxContext) {
transfer::share_object(MutableShared {
id: object::new(ctx),
value: 1,
})
}
use std::ascii;
use std::option::{is_some, extract};
use std::string::String;
use sui::clock::Clock;

public entry fun use_clock(_clock: &Clock) {}
public struct MutableShared has key {
id: UID,
value: u64,
}

public entry fun list<T: key + store>(
item: T,
ctx: &mut TxContext
) {
transfer::public_transfer(item, tx_context::sender(ctx))
}
fun init(ctx: &mut TxContext) {
transfer::share_object(MutableShared {
id: object::new(ctx),
value: 1,
})
}

public fun return_struct<T: key + store>(
item: T,
): T {
item
}
public entry fun use_clock(_clock: &Clock) {}

public entry fun value(clock: &MutableShared) {
assert!(clock.value > 0, 1);
}
public entry fun list<T: key + store>(item: T, ctx: &mut TxContext) {
transfer::public_transfer(item, tx_context::sender(ctx))
}

public entry fun set_value(clock: &mut MutableShared) {
clock.value = 10;
}
public fun return_struct<T: key + store>(item: T): T {
item
}

public entry fun delete_value(clock: MutableShared) {
let MutableShared { id, value: _ } = clock;
object::delete(id);
}
public entry fun value(clock: &MutableShared) {
assert!(clock.value > 0, 1);
}

public fun test_abort() {
abort 0
}
public entry fun set_value(clock: &mut MutableShared) {
clock.value = 10;
}

public entry fun delete_value(clock: MutableShared) {
let MutableShared { id, value: _ } = clock;
object::delete(id);
}

public fun test_abort() {
abort 0
}

public fun addr(_: address) {}

public fun id(_: ID) {}

public fun addr(_: address) {}
public fun id(_: ID) {}
public fun ascii_(_: ascii::String) {}

public fun ascii_(_: ascii::String) {}
public fun string(_: String) {}
public fun string(_: String) {}

public fun vec(_: vector<ascii::String>) {}
public fun opt(_: Option<ascii::String>) {}
public fun vec(_: vector<ascii::String>) {}

public fun ints(_u8: u8, _u16: u16, _u32: u32, _u64: u64, _u128: u128, _u256: u256) {}
public fun boolean(_bool: bool) {}
public fun opt(_: Option<ascii::String>) {}

public fun ints(
_u8: u8,
_u16: u16,
_u32: u32,
_u64: u64,
_u128: u128,
_u256: u256,
) {}

public fun boolean(_bool: bool) {}

public fun some<T>(opt: &mut Option<T>): T {
extract<T>(opt)
}

public fun none<T>(opt: &mut Option<T>) {
if (is_some(opt)) {
abort 1
}
}
41 changes: 41 additions & 0 deletions sdk/typescript/test/e2e/txn-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,47 @@ describe('Transaction Builders', () => {
retry: 10,
},
);

it('builds object options', async () => {
const tx = new Transaction();

tx.moveCall({
target: `${packageId}::serializer_tests::none`,
typeArguments: ['0x2::coin::Coin<0x2::sui::SUI>'],
arguments: [
tx.object.option({
type: '0x2::coin::Coin<0x2::sui::SUI>',
value: null,
}),
],
});
const coin = tx.splitCoins(tx.gas, [1])

Check failure on line 225 in sdk/typescript/test/e2e/txn-builder.test.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Insert `;`
const coin2 = tx.moveCall({
target: `${packageId}::serializer_tests::some`,
typeArguments: ['0x2::coin::Coin<0x2::sui::SUI>'],
arguments: [
tx.object.option({
type: '0x2::coin::Coin<0x2::sui::SUI>',
value: coin,
}),
],
});

const coin3 = tx.moveCall({
target: `${packageId}::serializer_tests::some`,
typeArguments: ['0x2::coin::Coin<0x2::sui::SUI>'],
arguments: [
tx.object.option({
type: '0x2::coin::Coin<0x2::sui::SUI>',
value: coin2,
}),
],
});

tx.transferObjects([coin3], toolbox.keypair.toSuiAddress());

await validateTransaction(toolbox.client, toolbox.keypair, tx);
});
});

async function validateTransaction(client: SuiClient, signer: Keypair, tx: Transaction) {
Expand Down
3 changes: 3 additions & 0 deletions sdk/typescript/test/e2e/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,15 @@ export async function publishPackage(packagePath: string, toolbox?: TestToolbox)

const tmpobj = tmp.dirSync({ unsafeCleanup: true });

console.log('building package');
const { modules, dependencies } = JSON.parse(
execSync(
`${SUI_BIN} move --client.config ${toolbox.configPath} build --dump-bytecode-as-base64 --path ${packagePath} --install-dir ${tmpobj.name}`,
{ encoding: 'utf-8' },
),
);

console.log('publishing');
const tx = new Transaction();
const cap = tx.publish({
modules,
Expand Down
Loading

0 comments on commit 61d7b68

Please sign in to comment.