Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gas_service)!: move gas service logic to versioned #200

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions move/gas_service/sources/events.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module gas_service::events;

use std::ascii::String;
use sui::event;

// ------
// Events
// ------
public struct GasPaid<phantom T> has copy, drop {
Foivos marked this conversation as resolved.
Show resolved Hide resolved
sender: address,
destination_chain: String,
destination_address: String,
payload_hash: address,
value: u64,
refund_address: address,
params: vector<u8>,
}

public struct GasAdded<phantom T> has copy, drop {
message_id: String,
value: u64,
refund_address: address,
params: vector<u8>,
}

public struct Refunded<phantom T> has copy, drop {
message_id: String,
value: u64,
refund_address: address,
}

public struct GasCollected<phantom T> has copy, drop {
receiver: address,
value: u64,
}

// Package Functions
public(package) fun gas_paid<T>(
sender: address,
destination_chain: String,
destination_address: String,
payload_hash: address,
value: u64,
refund_address: address,
params: vector<u8>,
) {
event::emit(GasPaid<T> {
sender,
destination_chain,
destination_address,
payload_hash,
value,
refund_address,
params,
});
}

public(package) fun gas_added<T>(
message_id: String,
value: u64,
refund_address: address,
params: vector<u8>,
) {
event::emit(GasAdded<T> {
message_id,
value,
refund_address,
params,
});
}

public(package) fun refunded<T>(
message_id: String,
value: u64,
refund_address: address,
) {
event::emit(Refunded<T> {
message_id,
value,
refund_address,
});
}

public(package) fun gas_collected<T>(receiver: address, value: u64) {
event::emit(GasCollected<T> {
receiver,
value,
});
}
130 changes: 45 additions & 85 deletions move/gas_service/sources/gas_service.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ module gas_service::gas_service;

use gas_service::gas_service_v0::{Self, GasService_v0};
use std::ascii::{Self, String};
use sui::address;
use sui::coin::Coin;
use sui::event;
use sui::hash::keccak256;
use sui::sui::SUI;
use sui::versioned::{Self, Versioned};
Expand All @@ -27,37 +25,6 @@ public struct GasCollectorCap has key, store {
id: UID,
}

// ------
// Events
// ------
public struct GasPaid<phantom T> has copy, drop {
sender: address,
destination_chain: String,
destination_address: String,
payload_hash: address,
value: u64,
refund_address: address,
params: vector<u8>,
}

public struct GasAdded<phantom T> has copy, drop {
message_id: String,
value: u64,
refund_address: address,
params: vector<u8>,
}

public struct Refunded<phantom T> has copy, drop {
message_id: String,
value: u64,
refund_address: address,
}

public struct GasCollected<phantom T> has copy, drop {
receiver: address,
value: u64,
}

// -----
// Setup
// -----
Expand Down Expand Up @@ -98,8 +65,10 @@ macro fun value_mut(
// Public Functions
// ----------------
/// Pay gas for a contract call.
/// This function is called by the channel that wants to pay gas for a contract call.
/// It can also be called by the user to pay gas for a contract call, while setting the sender as the channel ID.
/// This function is called by the channel that wants to pay gas for a contract
/// call.
/// It can also be called by the user to pay gas for a contract call, while
/// setting the sender as the channel ID.
public fun pay_gas(
self: &mut GasService,
coin: Coin<SUI>,
Expand All @@ -110,40 +79,37 @@ public fun pay_gas(
refund_address: address,
params: vector<u8>,
) {
let coin_value = coin.value();
self.value_mut!(b"pay_gas").put(coin);

let payload_hash = address::from_bytes(keccak256(&payload));

event::emit(GasPaid<SUI> {
sender,
destination_chain,
destination_address,
payload_hash,
value: coin_value,
refund_address,
params,
})
self
.value_mut!(b"pay_gas")
.pay_gas(
coin,
sender,
destination_chain,
destination_address,
payload,
refund_address,
params,
);
}

/// Add gas for an existing cross-chain contract call.
/// This function can be called by a user who wants to add gas for a contract call with insufficient gas.
/// This function can be called by a user who wants to add gas for a contract
/// call with insufficient gas.
public fun add_gas(
self: &mut GasService,
coin: Coin<SUI>,
message_id: String,
refund_address: address,
params: vector<u8>,
) {
let coin_value = coin.value();
self.value_mut!(b"add_gas").put(coin);

event::emit(GasAdded<SUI> {
message_id,
value: coin_value,
refund_address,
params,
});
self
.value_mut!(b"add_gas")
.add_gas(
coin,
message_id,
refund_address,
params,
);
}

public fun collect_gas(
Expand All @@ -153,15 +119,13 @@ public fun collect_gas(
amount: u64,
ctx: &mut TxContext,
) {
transfer::public_transfer(
self.value_mut!(b"collect_gas").take(amount, ctx),
receiver,
);

event::emit(GasCollected<SUI> {
receiver,
value: amount,
});
self
.value_mut!(b"collect_gas")
.collect_gas(
receiver,
amount,
ctx,
)
}

public fun refund(
Expand All @@ -172,29 +136,25 @@ public fun refund(
amount: u64,
ctx: &mut TxContext,
) {
transfer::public_transfer(
self.value_mut!(b"refund").take(amount, ctx),
receiver,
);

event::emit(Refunded<SUI> {
message_id,
value: amount,
refund_address: receiver,
});
self
.value_mut!(b"refund")
.refund(
message_id,
receiver,
amount,
ctx,
);
}

// -----------------
// Private Functions
// -----------------
fun version_control(): VersionControl {
version_control::new(
vector[
vector[b"pay_gas", b"add_gas", b"collect_gas", b"refund"].map!(
|function_name| function_name.to_ascii_string(),
),
],
)
version_control::new(vector[
vector[b"pay_gas", b"add_gas", b"collect_gas", b"refund"].map!(
|function_name| function_name.to_ascii_string(),
),
])
}

// -----
Expand Down
91 changes: 89 additions & 2 deletions move/gas_service/sources/versioned/gas_service_v0.move
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module gas_service::gas_service_v0;

use gas_service::events;
use std::ascii::String;
use sui::address;
use sui::balance::{Self, Balance};
use sui::coin::{Self, Coin};
use sui::hash::keccak256;
use sui::sui::SUI;
use version_control::version_control::VersionControl;

Expand All @@ -27,11 +31,94 @@ public(package) fun version_control(self: &GasService_v0): &VersionControl {
&self.version_control
}

public(package) fun put(self: &mut GasService_v0, coin: Coin<SUI>) {
public(package) fun pay_gas(
self: &mut GasService_v0,
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
coin: Coin<SUI>,
sender: address,
destination_chain: String,
destination_address: String,
payload: vector<u8>,
refund_address: address,
params: vector<u8>,
) {
let coin_value = coin.value();
self.put(coin);

let payload_hash = address::from_bytes(keccak256(&payload));

events::gas_paid<SUI>(
sender,
destination_chain,
destination_address,
payload_hash,
coin_value,
refund_address,
params,
);
}

public(package) fun add_gas(
self: &mut GasService_v0,
coin: Coin<SUI>,
message_id: String,
refund_address: address,
params: vector<u8>,
) {
let coin_value = coin.value();
self.put(coin);

events::gas_added<SUI>(
message_id,
coin_value,
refund_address,
params,
);
}

public(package) fun collect_gas(
self: &mut GasService_v0,
receiver: address,
amount: u64,
ctx: &mut TxContext,
) {
transfer::public_transfer(
self.take(amount, ctx),
receiver,
);

events::gas_collected<SUI>(
receiver,
amount,
);
}

public(package) fun refund(
self: &mut GasService_v0,
message_id: String,
receiver: address,
amount: u64,
ctx: &mut TxContext,
) {
transfer::public_transfer(
self.take(amount, ctx),
receiver,
);

events::refunded<SUI>(
message_id,
amount,
receiver,
);
}

// -----------------
// Private Functions
// -----------------
fun put(self: &mut GasService_v0, coin: Coin<SUI>) {
coin::put(&mut self.balance, coin);
}

public(package) fun take(
fun take(
self: &mut GasService_v0,
amount: u64,
ctx: &mut TxContext,
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/interface_gas_service_events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"structs": {},
"publicFunctions": {}
}