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 #3

Merged
merged 38 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cdd734d
feat: add workspace for axelar gas service
canhtrinh Feb 26, 2024
b79b41e
feat: add refund impl and set up tests
canhtrinh Feb 26, 2024
5dee6ab
feat: add impl for pay_native_gas_for_contract_call
canhtrinh Feb 27, 2024
e3c403e
feat: implement collect_fees
canhtrinh Mar 1, 2024
b7d0b57
feat: update gas service unit tests
canhtrinh Mar 5, 2024
db1cc0f
chore: remove vec from and add emitted event to collect_fees api
canhtrinh Mar 7, 2024
49747d0
chore: cargo fmt
canhtrinh Mar 7, 2024
145c26b
Update contracts/axelar-gas-service/Cargo.toml
canhtrinh Mar 14, 2024
50cf1c3
Update contracts/axelar-gas-service/Cargo.toml
canhtrinh Mar 14, 2024
32a13ae
Update contracts/axelar-gas-service/src/contract.rs
canhtrinh Mar 14, 2024
8941d2a
Update contracts/axelar-gas-service/src/test.rs
canhtrinh Mar 14, 2024
42b2270
Update contracts/axelar-gas-service/src/error.rs
canhtrinh Mar 14, 2024
85260a1
Update contracts/axelar-gas-service/src/event.rs
canhtrinh Mar 14, 2024
48553de
Update contracts/axelar-gas-service/src/event.rs
canhtrinh Mar 14, 2024
ee25e75
Update contracts/axelar-gas-service/src/event.rs
canhtrinh Mar 14, 2024
4aac186
Update contracts/axelar-gas-service/src/event.rs
canhtrinh Mar 14, 2024
a016bfc
Update contracts/axelar-gas-service/src/interface.rs
canhtrinh Mar 14, 2024
e8735eb
Update contracts/axelar-gas-service/src/lib.rs
canhtrinh Mar 14, 2024
f54e251
chore: merge latest main
canhtrinh Mar 17, 2024
3a15f92
chore: addressing latest comments on gas-service PR
canhtrinh Mar 17, 2024
ac472f4
chore: fmt code
canhtrinh Mar 17, 2024
12e4837
chore: reduce parameter list size in pay_gas_for_contract_call
canhtrinh Mar 17, 2024
f68df95
Merge branch 'main' into feat/gas-service
milapsheth Mar 18, 2024
884d414
Update contracts/axelar-soroban-interfaces/src/axelar_gas_service.rs
canhtrinh Mar 18, 2024
a9184fd
Update contracts/axelar-soroban-interfaces/src/axelar_gas_service.rs
canhtrinh Mar 18, 2024
a2c9bef
Update contracts/axelar-gas-service/src/contract.rs
canhtrinh Mar 18, 2024
628bb19
Update contracts/axelar-soroban-interfaces/src/axelar_gas_service.rs
canhtrinh Mar 18, 2024
29c07ef
Update contracts/axelar-gas-service/src/contract.rs
canhtrinh Mar 18, 2024
4be6a51
Update packages/axelar-soroban-std/src/types.rs
canhtrinh Mar 18, 2024
0a2063a
Update contracts/axelar-soroban-interfaces/src/axelar_gas_service.rs
canhtrinh Mar 18, 2024
b76738b
chore: refactor gas service
canhtrinh Mar 19, 2024
cccc748
chore: update pay_gas_for_contract_call to use tranfer_from
canhtrinh Mar 19, 2024
f9d764a
Update contracts/axelar-gas-service/src/event.rs
canhtrinh Mar 19, 2024
c613025
Update contracts/axelar-gas-service/src/contract.rs
canhtrinh Mar 19, 2024
b6b56ac
Update contracts/axelar-gas-service/src/contract.rs
canhtrinh Mar 19, 2024
5299355
Update contracts/axelar-gas-service/src/test.rs
canhtrinh Mar 19, 2024
8538ef6
Update contracts/axelar-gas-service/src/test.rs
canhtrinh Mar 19, 2024
a8fb7b8
chore: fix tests with variable name change
canhtrinh Mar 19, 2024
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
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions contracts/axelar-gas-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "axelar-gas-service"
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
version = "0.1.0"
edition = { workspace = true }

[lib]
crate-type = ["cdylib"]

[dependencies]
soroban-sdk = { workspace = true, features = ["alloc"] }
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved

[dev_dependencies]
soroban-sdk = { workspace = true, features = ["testutils", "alloc"] }
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
secp256k1 = { version = "0.28.2", features = ["recovery", "rand"] }
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved

[features]
testutils = ["soroban-sdk/testutils"]
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
109 changes: 109 additions & 0 deletions contracts/axelar-gas-service/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use soroban_sdk::{contract, contractimpl, token, Address, Bytes, BytesN, Env, String, U256};

use crate::interface::AxelarGasServiceInterface;
use crate::storage_types::DataKey;
use crate::{error::Error, event};

#[contract]
pub struct AxelarGasService;

#[contractimpl]
impl AxelarGasService {
pub fn initialize(env: Env, gas_collector: Address) {
if env
.storage()
.instance()
.get(&DataKey::Initialized)
.unwrap_or(false)
{
panic!("Already initialized");
}

env.storage().instance().set(&DataKey::Initialized, &true);

env.storage()
.instance()
.set(&DataKey::GasCollector, &gas_collector);
}
}

#[contractimpl]
impl AxelarGasServiceInterface for AxelarGasService {
fn pay_native_gas_for_contract_call(
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
env: Env,
sender: Address,
destination_chain: String,
destination_address: String,
payload: Bytes,
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
refund_address: Address,
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
) {
event::native_gas_paid_for_contract_call(
&env,
sender,
destination_chain,
destination_address,
payload,
refund_address,
)
}

fn collect_fees(
env: Env,
receiver: Address,
token_address: Address,
amount: i128,
) -> Result<(), Error> {
let gas_collector: Address = env
.storage()
.instance()
.get(&DataKey::GasCollector)
.unwrap();

gas_collector.require_auth();

//TODO: sanity check address zero
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved

if amount == 0 {
return Err(Error::InvalidAmounts);
}

let token_client = token::Client::new(&env, &token_address);

let contract_token_balance = token_client.balance(&env.current_contract_address());

if contract_token_balance >= amount {
token_client.transfer(&env.current_contract_address(), &receiver, &amount)
} else {
return Err(Error::InsufficientBalance);
}

event::fee_collected(&env, &gas_collector, &token_address, amount);

Ok(())
}

fn refund(
env: Env,
tx_hash: BytesN<32>,
log_index: U256,
receiver: Address,
token_addr: Address,
amount: i128,
) {
let gas_collector: Address = env
.storage()
.instance()
.get(&DataKey::GasCollector)
.unwrap();

gas_collector.require_auth();

event::refunded(&env, tx_hash, log_index, &receiver, &token_addr, amount);

token::Client::new(&env, &token_addr).transfer(
&env.current_contract_address(),
&receiver,
&amount,
);
}
}
10 changes: 10 additions & 0 deletions contracts/axelar-gas-service/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use soroban_sdk::contracterror;

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
InvalidAddress = 1,
InvalidAmounts = 2,
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
InsufficientBalance = 3,
}
40 changes: 40 additions & 0 deletions contracts/axelar-gas-service/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use soroban_sdk::{symbol_short, Address, Bytes, BytesN, Env, String, U256};

pub(crate) fn native_gas_paid_for_contract_call(
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
env: &Env,
sender: Address,
destination_chain: String,
destination_address: String,
payload: Bytes,
refund_address: Address,
) {
let topics = (symbol_short!("cc_g_paid"), env.crypto().keccak256(&payload));
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
env.events().publish(
topics,
(
sender,
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
destination_chain,
destination_address,
payload,
refund_address,
),
);
}

pub(crate) fn refunded(
env: &Env,
tx_hash: BytesN<32>,
log_index: U256,
receiver: &Address,
token: &Address,
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
amount: i128,
) {
let topics = (symbol_short!("refunded"), tx_hash, log_index);
env.events().publish(topics, (receiver, token, amount));
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
}

pub(crate) fn fee_collected(env: &Env, receiver: &Address, token_address: &Address, amount: i128) {
let topics = (symbol_short!("coll_fees"),);
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
env.events()
.publish(topics, (receiver, token_address, amount));
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
}
33 changes: 33 additions & 0 deletions contracts/axelar-gas-service/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::error::Error;
use soroban_sdk::{Address, Bytes, BytesN, Env, String, U256};
/// Interface for the Axelar Gas Service.
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
// #[contractclient(crate_path = "crate", name = "AxelarGasService")]
pub trait AxelarGasServiceInterface {
/// Pay for gas using native currency for a contract call on a destination chain. This function is called on the source chain before calling the gateway to execute a remote contract.
fn pay_native_gas_for_contract_call(
env: Env,
sender: Address,
destination_chain: String,
destination_address: String,
payload: Bytes,
refund_address: Address,
);

/// Allows the gasCollector to collect accumulated fees from the contract.
fn collect_fees(
env: Env,
receiver: Address,
token_addr: Address,
amounts: i128,
) -> Result<(), Error>;

/// Refunds gas payment to the receiver in relation to a specific cross-chain transaction. Only callable by the gasCollector.
fn refund(
env: Env,
tx_hash: BytesN<32>,
log_index: U256,
receiver: Address,
token: Address,
amount: i128,
);
}
13 changes: 13 additions & 0 deletions contracts/axelar-gas-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]

pub mod contract;
mod error;
mod event;
pub mod interface;
mod storage_types;
mod types;

#[cfg(test)]
mod test;

pub use contract::AxelarGasService;
canhtrinh marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions contracts/axelar-gas-service/src/storage_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use soroban_sdk::contracttype;

#[contracttype]
#[derive(Clone, Debug)]
pub enum DataKey {
Initialized,
GasCollector,
}
Loading
Loading