본문으로 건너뛰기

주소 소유 객체

Address-owned object는 32바이트 address가 소유한다. 32바이트 address는 특정 서명 체계에서 파생된 account address이거나 object ID이다. Address-owned object는 오직 해당 소유자만 접근할 수 있다. 소유한 object는 다른 address로 전송할 수 있다.

address-owned object 생성

다음 transfer module 함수를 사용하여 address-owned object를 생성한다:

public fun transfer<T: key>(obj: T, recipient: address)
public fun public_transfer<T: key + store>(obj: T, recipient: address)

Object의 소유권은 그 object의 생애 동안 dynamic object field로 추가하거나, 다른 address로 전송하거나, immutable로 만드는 방식으로 변경될 수 있다. 그러나 object를 생성하고 소유권을 설정한 후에는 shared로 만들 수 없다.

public fun create(value: u64, recipient: address, ctx: &mut TxContext) {
transfer::public_transfer(
Object { id: object::new(ctx), value },
recipient,
)
}

address-owned object 사용 시점

다음과 같은 경우 address-owned object를 사용한다:

  • 단일 소유권이 필요한 경우
  • shared object보다 더 나은 성능이 필요한 경우
  • consensus sequencing을 피하고자 하는 경우

Interact with address-owned objects

Object의 address owner가 address에 해당하는지 object ID에 해당하는지에 따라 address-owned object에 접근하는 방법은 두 가지이다.

Object의 address owner가 account address인 경우, 해당 address가 서명한 transaction의 실행 중에 owned object로 직접 사용하고 접근할 수 있다. 다른 address는 owned object에 어떤 방식으로도 접근할 수 없다.

Object의 address owner가 object ID에 해당하는 경우, Transfer to Object에 정의된 메커니즘을 사용하여 transaction 실행 중에 동적으로 인증하고 접근해야 한다.

CLI를 통해 address-owned object와 상호작용하려면 먼저 소유한 object를 확인한다:

$ export ADDR=`sui client active-address`
$ sui client objects $ADDR

color_object example module 테스트에서 sui::transfer::public_transfer 함수가 사용되는 것을 볼 수 있다. 이 테스트는 새 address-owned ColorObject object를 생성한 다음 public_transfer를 호출해 소유자의 address로 전송한다.

color_object example code를 저장한 다음 Sui CLI를 사용해 ColorObject 코드를 온체인에 publish한다:

Beginning with the Sui v1.24.1 release, the --gas-budget option is no longer required for CLI commands.

$ sui client publish $ROOT/examples/move/color_object --gas-budget <GAS-AMOUNT>

설정해 두었다면 package object ID를 $PACKAGE 환경 변수로 설정한다. 그런 다음 새 ColorObject를 생성한다:

$ sui client call --gas-budget <GAS-AMOUNT> --package $PACKAGE --module "color_object" --function "create" --args 0 255 0

새로 생성한 object ID를 $OBJECT로 설정한다. 현재 활성 address의 object를 보려면 다음을 실행한다:

$ sui client objects $ADDR

Object 정보를 조회하고 출력의 Owner 필드를 보면 이 object가 이제 address가 소유한다는 것을 확인할 수 있다:

$ sui client object $OBJECT

Test address-owned objects

다음 테스트는 address-owned object를 생성해 owner에게 전송한 다음 owner 필드가 올바른지 확인한다:

#[test]
fun test_transfer() {
let mut ts = ts::begin(@0x0);
let sender = @0xA;
let recipient = @0xB;

// Create a ColorObject and transfer it to sender.
{
ts.next_tx(sender);
let c = new(255, 0, 255, ts.ctx());
transfer::public_transfer(c, @0xA);
};

// Transfer the object to recipient.
{
ts.next_tx(sender);
let object: ColorObject = ts.take_from_sender();
transfer::public_transfer(object, recipient);
};

// Check that sender no longer owns the object.
{
ts.next_tx(sender);
assert!(!ts.has_most_recent_for_sender<ColorObject>(), 0);
};

// Check that recipient now owns the object.
{
ts.next_tx(recipient);
assert!(ts.has_most_recent_for_sender<ColorObject>(), 0);
};

ts.end();
}