diff --git a/.changeset/wild-eagles-unite.md b/.changeset/wild-eagles-unite.md new file mode 100644 index 00000000..b7f27c93 --- /dev/null +++ b/.changeset/wild-eagles-unite.md @@ -0,0 +1,5 @@ +--- +'@axelar-network/axelar-cgp-sui': minor +--- + +move all events of gas service to gas_service::events and all logic to the versioned contract. diff --git a/move/gas_service/sources/events.move b/move/gas_service/sources/events.move new file mode 100644 index 00000000..00b196db --- /dev/null +++ b/move/gas_service/sources/events.move @@ -0,0 +1,89 @@ +module gas_service::events; + +use std::ascii::String; +use sui::event; + +// ------ +// Events +// ------ +public struct GasPaid has copy, drop { + sender: address, + destination_chain: String, + destination_address: String, + payload_hash: address, + value: u64, + refund_address: address, + params: vector, +} + +public struct GasAdded has copy, drop { + message_id: String, + value: u64, + refund_address: address, + params: vector, +} + +public struct Refunded has copy, drop { + message_id: String, + value: u64, + refund_address: address, +} + +public struct GasCollected has copy, drop { + receiver: address, + value: u64, +} + +// Package Functions +public(package) fun gas_paid( + sender: address, + destination_chain: String, + destination_address: String, + payload_hash: address, + value: u64, + refund_address: address, + params: vector, +) { + event::emit(GasPaid { + sender, + destination_chain, + destination_address, + payload_hash, + value, + refund_address, + params, + }); +} + +public(package) fun gas_added( + message_id: String, + value: u64, + refund_address: address, + params: vector, +) { + event::emit(GasAdded { + message_id, + value, + refund_address, + params, + }); +} + +public(package) fun refunded( + message_id: String, + value: u64, + refund_address: address, +) { + event::emit(Refunded { + message_id, + value, + refund_address, + }); +} + +public(package) fun gas_collected(receiver: address, value: u64) { + event::emit(GasCollected { + receiver, + value, + }); +} diff --git a/move/gas_service/sources/gas_service.move b/move/gas_service/sources/gas_service.move index 12f9e3bb..9bee412a 100644 --- a/move/gas_service/sources/gas_service.move +++ b/move/gas_service/sources/gas_service.move @@ -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}; @@ -27,37 +25,6 @@ public struct GasCollectorCap has key, store { id: UID, } -// ------ -// Events -// ------ -public struct GasPaid has copy, drop { - sender: address, - destination_chain: String, - destination_address: String, - payload_hash: address, - value: u64, - refund_address: address, - params: vector, -} - -public struct GasAdded has copy, drop { - message_id: String, - value: u64, - refund_address: address, - params: vector, -} - -public struct Refunded has copy, drop { - message_id: String, - value: u64, - refund_address: address, -} - -public struct GasCollected has copy, drop { - receiver: address, - value: u64, -} - // ----- // Setup // ----- @@ -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, @@ -110,24 +79,22 @@ public fun pay_gas( refund_address: address, params: vector, ) { - let coin_value = coin.value(); - self.value_mut!(b"pay_gas").put(coin); - - let payload_hash = address::from_bytes(keccak256(&payload)); - - event::emit(GasPaid { - 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, @@ -135,15 +102,14 @@ public fun add_gas( refund_address: address, params: vector, ) { - let coin_value = coin.value(); - self.value_mut!(b"add_gas").put(coin); - - event::emit(GasAdded { - 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( @@ -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 { - receiver, - value: amount, - }); + self + .value_mut!(b"collect_gas") + .collect_gas( + receiver, + amount, + ctx, + ) } public fun refund( @@ -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 { - 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(), + ), + ]) } // ----- diff --git a/move/gas_service/sources/versioned/gas_service_v0.move b/move/gas_service/sources/versioned/gas_service_v0.move index 400ad3d4..c1bcd83a 100644 --- a/move/gas_service/sources/versioned/gas_service_v0.move +++ b/move/gas_service/sources/versioned/gas_service_v0.move @@ -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; @@ -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) { +public(package) fun pay_gas( + self: &mut GasService_v0, + coin: Coin, + sender: address, + destination_chain: String, + destination_address: String, + payload: vector, + refund_address: address, + params: vector, +) { + let coin_value = coin.value(); + self.put(coin); + + let payload_hash = address::from_bytes(keccak256(&payload)); + + events::gas_paid( + sender, + destination_chain, + destination_address, + payload_hash, + coin_value, + refund_address, + params, + ); +} + +public(package) fun add_gas( + self: &mut GasService_v0, + coin: Coin, + message_id: String, + refund_address: address, + params: vector, +) { + let coin_value = coin.value(); + self.put(coin); + + events::gas_added( + 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( + 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( + message_id, + amount, + receiver, + ); +} + +// ----------------- +// Private Functions +// ----------------- +fun put(self: &mut GasService_v0, coin: Coin) { coin::put(&mut self.balance, coin); } -public(package) fun take( +fun take( self: &mut GasService_v0, amount: u64, ctx: &mut TxContext, diff --git a/move/relayer_discovery/Move.toml b/move/relayer_discovery/Move.toml index 4b80a620..45038bd6 100644 --- a/move/relayer_discovery/Move.toml +++ b/move/relayer_discovery/Move.toml @@ -9,5 +9,5 @@ VersionControl = { local = "../version_control" } AxelarGateway = { local = "../axelar_gateway" } [addresses] -relayer_discovery = "0xa2" +relayer_discovery = "0xaa" clock = "0x6" diff --git a/test/testdata/interface_gas_service_events.json b/test/testdata/interface_gas_service_events.json new file mode 100644 index 00000000..0b162a70 --- /dev/null +++ b/test/testdata/interface_gas_service_events.json @@ -0,0 +1,4 @@ +{ + "structs": {}, + "publicFunctions": {} +}