Skip to content

Commit

Permalink
[Sui Framework] Clean up TxContext API's
Browse files Browse the repository at this point in the history
* Rename the rather wordy `get_signer_address` to `sender`. I think this should also be more intuitive, especially for folks coming from Eth.
* Rename `get_signer` to `signer_` (can't use `signer` because it's a keyword). This actually isn't used anywhere yet, but is valuable for code that wants to authenticate the sender of transaction, but doesn't need to create new ID's--such code can ask for a `&signer` rather than a `&mut TxContext`.
* Hide `ids_created`, which we probably don't want folks outside the module calling because it is difficult to reason about.
  • Loading branch information
sblackshear committed Feb 28, 2022
1 parent f72128e commit d115a1d
Show file tree
Hide file tree
Showing 29 changed files with 96 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module Examples::CustomObjectTemplate {
write_field(to_write, v + int_input);
transfer(to_consume, recipient);
// demonstrate creating a new object for the sender
let sender = TxContext::get_signer_address(ctx);
let sender = TxContext::sender(ctx);
Transfer::transfer(create(ctx), sender)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module Examples::TicTacToe {
x_address: x_address,
o_address: o_address,
};
Transfer::transfer(game, TxContext::get_signer_address(ctx));
Transfer::transfer(game, TxContext::sender(ctx));
let cap = MarkMintCap {
id: TxContext::new_id(ctx),
game_id: copy game_id,
Expand Down Expand Up @@ -174,7 +174,7 @@ module Examples::TicTacToe {
cap.remaining_supply = cap.remaining_supply - 1;
Mark {
id: TxContext::new_id(ctx),
player: TxContext::get_signer_address(ctx),
player: TxContext::sender(ctx),
row,
col,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module Examples::TrustedCoin {
// Get a treasury cap for the coin and give it to the transaction
// sender
let treasury_cap = Coin::create_currency<EXAMPLE>(EXAMPLE{}, ctx);
Transfer::transfer(treasury_cap, TxContext::get_signer_address(ctx))
Transfer::transfer(treasury_cap, TxContext::sender(ctx))
}

public fun mint(treasury_cap: &mut TreasuryCap<EXAMPLE>, amount: u64, ctx: &mut TxContext) {
let coin = Coin::mint<EXAMPLE>(amount, treasury_cap, ctx);
Coin::transfer(coin, TxContext::get_signer_address(ctx));
Coin::transfer(coin, TxContext::sender(ctx));
}

public fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address, _ctx: &mut TxContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ module Test::M1 {
// initializer that should be executed upon publishing this module
fun init(ctx: &mut TxContext, value: u64) {
let singleton = Object { id: TxContext::new_id(ctx), value };
Transfer::transfer(singleton, TxContext::get_signer_address(ctx))
Transfer::transfer(singleton, TxContext::sender(ctx))
}
}
6 changes: 3 additions & 3 deletions sui_core/src/unit_tests/data/hero/sources/Hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module Examples::Hero {
fun init(ctx: &mut TxContext) {
let admin = admin();
// ensure this is being initialized by the expected admin authenticator
assert!(&TxContext::get_signer_address(ctx) == &admin, ENOT_ADMIN);
assert!(&TxContext::sender(ctx) == &admin, ENOT_ADMIN);
Transfer::transfer(
GameAdmin {
id: TxContext::new_id(ctx),
Expand Down Expand Up @@ -136,7 +136,7 @@ module Examples::Hero {
};
// let the world know about the hero's triumph by emitting an event!
Event::emit(BoarSlainEvent {
slayer_address: TxContext::get_signer_address(ctx),
slayer_address: TxContext::sender(ctx),
hero: *ID::inner(&hero.id),
boar: *ID::inner(&boar_id),
});
Expand Down Expand Up @@ -222,7 +222,7 @@ module Examples::Hero {
public fun acquire_hero(payment: Coin<EXAMPLE>, ctx: &mut TxContext) {
let sword = create_sword(payment, ctx);
let hero = create_hero(sword, ctx);
Transfer::transfer(hero, TxContext::get_signer_address(ctx))
Transfer::transfer(hero, TxContext::sender(ctx))
}

/// Anyone can create a hero if they have a sword. All heros start with the
Expand Down
4 changes: 2 additions & 2 deletions sui_core/src/unit_tests/data/hero/sources/TrustedCoin.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module Examples::TrustedCoin {
// Get a treasury cap for the coin and give it to the transaction
// sender
let treasury_cap = Coin::create_currency<EXAMPLE>(EXAMPLE{}, ctx);
Transfer::transfer(treasury_cap, TxContext::get_signer_address(ctx))
Transfer::transfer(treasury_cap, TxContext::sender(ctx))
}

public fun mint(treasury_cap: &mut TreasuryCap<EXAMPLE>, amount: u64, ctx: &mut TxContext) {
let coin = Coin::mint<EXAMPLE>(amount, treasury_cap, ctx);
Coin::transfer(coin, TxContext::get_signer_address(ctx));
Coin::transfer(coin, TxContext::sender(ctx));
}

public fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address, _ctx: &mut TxContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module Test::M1 {
fun init(ctx: &mut TxContext) {
let value = 42;
let singleton = Object { id: TxContext::new_id(ctx), value };
Transfer::transfer(singleton, TxContext::get_signer_address(ctx))
Transfer::transfer(singleton, TxContext::sender(ctx))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ module Test::M1 {
// initializer that should be executed upon publishing this module
fun init(ctx: &mut TxContext, value: u64) {
let singleton = Object { id: TxContext::new_id(ctx), value };
Transfer::transfer(singleton, TxContext::get_signer_address(ctx))
Transfer::transfer(singleton, TxContext::sender(ctx))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module Test::M1 {
public fun init(ctx: &mut TxContext) {
let value = 42;
let singleton = Object { id: TxContext::new_id(ctx), value };
Transfer::transfer(singleton, TxContext::get_signer_address(ctx))
Transfer::transfer(singleton, TxContext::sender(ctx))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Test::M1 {
fun init(ctx: &mut TxContext): u64 {
let value = 42;
let singleton = Object { id: TxContext::new_id(ctx), value };
Transfer::transfer(singleton, TxContext::get_signer_address(ctx));
Transfer::transfer(singleton, TxContext::sender(ctx));
value
}
}
6 changes: 3 additions & 3 deletions sui_programmability/examples/sources/CombinableObjects.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Examples::CombinableObjects {
}

struct Sandwich has key {
id: VersionedID
id: VersionedID,
}

/// Address selling ham, bread, etc
Expand Down Expand Up @@ -46,12 +46,12 @@ module Examples::CombinableObjects {
/// Combine the `ham` and `bread` into a delicious sandwich
public fun make_sandwich(
ham: Ham, bread: Bread, ctx: &mut TxContext
): Sandwich {
) {
let Ham { id: ham_id } = ham;
let Bread { id: bread_id } = bread;
ID::delete(ham_id);
ID::delete(bread_id);
Sandwich { id: TxContext::new_id(ctx) }
Transfer::transfer(Sandwich { id: TxContext::new_id(ctx) }, TxContext::sender(ctx))
}

fun admin(): address {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module Examples::CustomObjectTemplate {
write_field(to_write, v + int_input);
transfer(to_consume, recipient);
// demonstrate creating a new object for the sender
let sender = TxContext::get_signer_address(ctx);
let sender = TxContext::sender(ctx);
Transfer::transfer(create(ctx), sender)
}

Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/examples/sources/EconMod.move
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Examples::EconMod {
HelpMeSlayThisMonster {
id: TxContext::new_id(ctx),
monster,
monster_owner: TxContext::get_signer_address(ctx),
monster_owner: TxContext::sender(ctx),
helper_reward
},
helper
Expand Down
6 changes: 3 additions & 3 deletions sui_programmability/examples/sources/Hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module Examples::Hero {
fun init(ctx: &mut TxContext) {
let admin = admin();
// ensure this is being initialized by the expected admin authenticator
assert!(&TxContext::get_signer_address(ctx) == &admin, ENOT_ADMIN);
assert!(&TxContext::sender(ctx) == &admin, ENOT_ADMIN);
Transfer::transfer(
GameAdmin {
id: TxContext::new_id(ctx),
Expand Down Expand Up @@ -136,7 +136,7 @@ module Examples::Hero {
};
// let the world know about the hero's triumph by emitting an event!
Event::emit(BoarSlainEvent {
slayer_address: TxContext::get_signer_address(ctx),
slayer_address: TxContext::sender(ctx),
hero: *ID::inner(&hero.id),
boar: *ID::inner(&boar_id),
});
Expand Down Expand Up @@ -222,7 +222,7 @@ module Examples::Hero {
public fun acquire_hero(payment: Coin<EXAMPLE>, ctx: &mut TxContext) {
let sword = create_sword(payment, ctx);
let hero = create_hero(sword, ctx);
Transfer::transfer(hero, TxContext::get_signer_address(ctx))
Transfer::transfer(hero, TxContext::sender(ctx))
}

/// Anyone can create a hero if they have a sword. All heros start with the
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/examples/sources/HeroMod.move
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module Examples::HeroMod {
token_supply_max,
monster_max,
},
TxContext::get_signer_address(ctx)
TxContext::sender(ctx)
)
}

Expand Down
4 changes: 2 additions & 2 deletions sui_programmability/examples/sources/TicTacToe.move
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module Examples::TicTacToe {
x_address: x_address,
o_address: o_address,
};
Transfer::transfer(game, TxContext::get_signer_address(ctx));
Transfer::transfer(game, TxContext::sender(ctx));
let cap = MarkMintCap {
id: TxContext::new_id(ctx),
game_id: copy game_id,
Expand Down Expand Up @@ -174,7 +174,7 @@ module Examples::TicTacToe {
cap.remaining_supply = cap.remaining_supply - 1;
Mark {
id: TxContext::new_id(ctx),
player: TxContext::get_signer_address(ctx),
player: TxContext::sender(ctx),
row,
col,
}
Expand Down
4 changes: 2 additions & 2 deletions sui_programmability/examples/sources/TrustedCoin.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module Examples::TrustedCoin {
// Get a treasury cap for the coin and give it to the transaction
// sender
let treasury_cap = Coin::create_currency<EXAMPLE>(EXAMPLE{}, ctx);
Transfer::transfer(treasury_cap, TxContext::get_signer_address(ctx))
Transfer::transfer(treasury_cap, TxContext::sender(ctx))
}

public fun mint(treasury_cap: &mut TreasuryCap<EXAMPLE>, amount: u64, ctx: &mut TxContext) {
let coin = Coin::mint<EXAMPLE>(amount, treasury_cap, ctx);
Coin::transfer(coin, TxContext::get_signer_address(ctx));
Coin::transfer(coin, TxContext::sender(ctx));
}

public fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address, _ctx: &mut TxContext) {
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/framework/sources/Coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module Sui::Coin {
/// and the remaining balance is left is `self`.
public fun split<T>(self: &mut Coin<T>, split_amount: u64, ctx: &mut TxContext) {
let new_coin = withdraw(self, split_amount, ctx);
Transfer::transfer(new_coin, TxContext::get_signer_address(ctx));
Transfer::transfer(new_coin, TxContext::sender(ctx));
}

/// Split coin `self` into multiple coins, each with balance specified
Expand Down
4 changes: 2 additions & 2 deletions sui_programmability/framework/sources/Collection.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Sui::Collection {

/// Create a new Collection and transfer it to the signer.
public fun create(ctx: &mut TxContext) {
Transfer::transfer(new(ctx), TxContext::get_signer_address(ctx))
Transfer::transfer(new(ctx), TxContext::sender(ctx))
}

/// Returns the size of the collection.
Expand Down Expand Up @@ -84,7 +84,7 @@ module Sui::Collection {
/// Remove the object from the collection, and then transfer it to the signer.
public fun remove_and_take<T: key>(c: &mut Collection, object: T, ctx: &mut TxContext) {
let object = remove(c, object);
Transfer::transfer(object, TxContext::get_signer_address(ctx));
Transfer::transfer(object, TxContext::sender(ctx));
}

/// Transfer the entire collection to `recipient`.
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/framework/sources/Escrow.move
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Sui::Escrow {
escrowed: T,
ctx: &mut TxContext
) {
let sender = TxContext::get_signer_address(ctx);
let sender = TxContext::sender(ctx);
let id = TxContext::new_id(ctx);
// escrow the object with the trusted third party
Transfer::transfer(
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/framework/sources/GAS.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Sui::GAS {
fun init(ctx: &mut TxContext) {
// Get a treasury cap for the coin and give it to the transaction sender
let treasury_cap = Coin::create_currency(GAS{}, ctx);
Transfer::transfer(treasury_cap, TxContext::get_signer_address(ctx))
Transfer::transfer(treasury_cap, TxContext::sender(ctx))
}

/// Transfer to a recipient
Expand Down
6 changes: 3 additions & 3 deletions sui_programmability/framework/sources/Geniteam.move
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module Sui::Geniteam {
) {
let farm = create_farm_(farm_name, farm_img_id, total_monster_slots, ctx);
let player = create_player_(player_name, farm, ctx);
Transfer::transfer(player, TxContext::get_signer_address(ctx))
Transfer::transfer(player, TxContext::sender(ctx))
}

/// Update the attributes of a player
Expand Down Expand Up @@ -178,7 +178,7 @@ module Sui::Geniteam {
monster_description,
ctx
);
Transfer::transfer(monster, TxContext::get_signer_address(ctx))
Transfer::transfer(monster, TxContext::sender(ctx))
}

/// Add a monster to a farm
Expand All @@ -192,7 +192,7 @@ module Sui::Geniteam {
public fun remove_monster(self: &mut Farm, monster_id: vector<u8>, ctx: &mut TxContext) {
// TODO: monster_id should be probably be `address`, but leaving this as-is to avoid breaking Geniteam
let monster = remove_monster_(self, &ID::new_from_bytes(monster_id));
Transfer::transfer(monster, TxContext::get_signer_address(ctx))
Transfer::transfer(monster, TxContext::sender(ctx))
}

/// Update the attributes of a farm
Expand Down
8 changes: 4 additions & 4 deletions sui_programmability/framework/sources/ID.move
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Sui::ID {
version: u64
}

// --- constructors ---
// === constructors ===

/// Create an `ID` from an address
public fun new(a: address): ID {
Expand All @@ -74,7 +74,7 @@ module Sui::ID {
VersionedID { id: UniqueID { id: ID { bytes } }, version: INITIAL_VERSION }
}

// --- reads ---
// === reads ===

/// Get the underyling `ID` of `obj`
public fun id<T: key>(obj: &T): &ID {
Expand Down Expand Up @@ -131,7 +131,7 @@ module Sui::ID {
// Private for now, but may expose in the future.
native fun get_versioned_id<T: key>(obj: &T): &VersionedID;

// --- destructors ---
// === destructors ===

/// Delete `id`. This is the only way to eliminate a `VersionedID`.
// This exists to inform Sui of object deletions. When an object
Expand All @@ -145,7 +145,7 @@ module Sui::ID {

native fun delete_id(id: UniqueID);

// --- internal functions ---
// === internal functions ===

/// Convert raw bytes into an address
native fun bytes_to_address(bytes: vector<u8>): address;
Expand Down
4 changes: 2 additions & 2 deletions sui_programmability/framework/sources/ObjectBasics.move
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ module Sui::ObjectBasics {
}

public fun wrap(o: Object, ctx: &mut TxContext) {
Transfer::transfer(Wrapper { id: TxContext::new_id(ctx), o }, TxContext::get_signer_address(ctx))
Transfer::transfer(Wrapper { id: TxContext::new_id(ctx), o }, TxContext::sender(ctx))
}

public fun unwrap(w: Wrapper, ctx: &mut TxContext) {
let Wrapper { id, o } = w;
ID::delete(id);
Transfer::transfer(o, TxContext::get_signer_address(ctx))
Transfer::transfer(o, TxContext::sender(ctx))
}
}
10 changes: 5 additions & 5 deletions sui_programmability/framework/sources/TestScenario.move
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module Sui::TestScenario {
/// only succeeds when the object to choose is unambiguous. In cases where there are multiple `T`'s,
/// the caller should resolve the ambiguity by using `remove_object_by_id`.
public fun remove_object<T: key>(scenario: &mut Scenario): T {
let sender = get_signer_address(scenario);
let sender = sender(scenario);
remove_unique_object(scenario, sender)
}

Expand Down Expand Up @@ -175,13 +175,13 @@ module Sui::TestScenario {
// to do (e.g.) `delete_object_for_testing(t)` instead.
// TODO: do this with a special test-only event to enable writing tests that look directly at system events
// like transfers. the current scheme will perturb the count of transfer events.
Transfer::transfer(t, get_signer_address(scenario))
Transfer::transfer(t, sender(scenario))
}

/// Return `true` if a call to `remove_object<T>(scenario)` will succeed
public fun can_remove_object<T: key>(scenario: &Scenario): bool {
let objects: vector<T> = get_inventory<T>(
get_signer_address(scenario),
sender(scenario),
last_tx_start_index(scenario)
);
let res = !Vector::is_empty(&objects);
Expand All @@ -200,8 +200,8 @@ module Sui::TestScenario {
}

/// Return the sender of the current tx in this `scenario`
public fun get_signer_address(scenario: &Scenario): address {
TxContext::get_signer_address(&scenario.ctx)
public fun sender(scenario: &Scenario): address {
TxContext::sender(&scenario.ctx)
}

/// Return the number of concluded transactions in this scenario.
Expand Down
Loading

0 comments on commit d115a1d

Please sign in to comment.