Skip to content

Commit

Permalink
Update to the callback registration contract code and update callback…
Browse files Browse the repository at this point in the history
… test contract repo (#497)
  • Loading branch information
emperorjm authored Jul 5, 2024
1 parent 7f7f3fd commit 337200c
Showing 1 changed file with 102 additions and 31 deletions.
133 changes: 102 additions & 31 deletions content/2.developers/3.guides/7.callback/1.introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ parentSectionPath: /developers

The callback module allows smart contracts to schedule transactions at designated block heights, enhancing automation and decreasing the dependency on intermediary solutions. This feature also operates in a permissionless manner which promotes greater decentralization of dapps on Archway.

Smart contracts are typically triggered by users or other contracts. Some dapps however depend on third parties for initiating transactions, with needs ranging from price balancing arbitrage bots, vesting distribution of funds, and auto-compounding of rewards etc. The callback module addresses these dependencies by providing a minimal, trust-minimized scheduler service, allowing for autonomous operations within the Archway ecosystem.
Smart contracts are typically triggered by users or other contracts. Some dapps however depend on third parties for initiating transactions, with needs ranging from price balancing arbitrage bots, vesting distribution of funds, auto-compounding of rewards etc. The callback module addresses these dependencies by providing a minimal, trust-minimized scheduler service, allowing for autonomous operations within the Archway ecosystem.


## How does it work?
Expand Down Expand Up @@ -52,14 +52,27 @@ archwayd tx callback request-callback <contract-address> <job-id> <callback-heig

### via smart contract

Within your smart contract you will need to set up an endpoint that executes the correct stargate message for registering a callback. You can find the proto message signature [here](https://github.com/archway-network/archway/blob/main/proto/archway/callback/v1/tx.proto#L39-L51). The following is an example:
Within your smart contract you will need to set up an action that executes the correct stargate message for registering a callback. You can find the proto message signature [here](https://github.com/archway-network/archway/blob/main/proto/archway/callback/v1/tx.proto#L39-L51). The following is an example.

Add the following to your `Cargo.toml` file under `dependencies` to add the `prost` functionality:

```
[dependencies]
prost = "0.12.6"
prost-types = "0.12.6"
```

You could then add the following to your `state.rs` file:

```rust
use cosmwasm_std::{Binary, Coin, CosmosMsg, Deps, Env, Response};
use crate::error::ContractError;
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ProstCoin {
#[prost(string, tag = "1")]
pub denom: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub amount: ::prost::alloc::string::String,
}

// This could be added to the state.rs file
/// MsgRequestCallback is used to register a callback.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MsgRequestCallback {
Expand All @@ -69,35 +82,93 @@ pub struct MsgRequestCallback {
pub contract_address: ::prost::alloc::string::String,
#[prost(uint64, tag = "3")]
pub job_id: u64,
#[prost(uint64, tag = "4")]
pub callback_height: u64,
#[prost(cosmos.base.v1beta1.Coin, tag = "5")]
pub fees: Coin,
#[prost(int64, tag = "4")]
pub callback_height: i64,
#[prost(message, required, tag = "5")]
pub fees: ProstCoin,
}

impl From<cosmwasm_std::Coin> for ProstCoin {
fn from(coin: cosmwasm_std::Coin) -> Self {
ProstCoin {
denom: coin.denom,
amount: coin.amount.to_string(),
}
}
}

impl From<ProstCoin> for cosmwasm_std::Coin {
fn from(prost_coin: ProstCoin) -> Self {
cosmwasm_std::Coin {
denom: prost_coin.denom,
amount: prost_coin.amount.parse().unwrap_or_default(),
}
}
}
```

You would then add this message to the `msg.rs` file:

```rust
#[cw_serde]
pub enum ExecuteMsg {
Register { job_id: u64, callback_height: i64 },
}
```

This final piece would be added to your `contract.rs` file:

```rust

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Register { job_id, callback_height } => execute::register(env, job_id, callback_height, info.funds[0].clone()),
}
}

// This could be added as one of the execute entrypoint functions in your contract.rs file
pub fn register(deps: Deps, env: Env, job_id: u64, callback_height: u64, funds: Coin) -> Result<Response, ContractError> {
let contract_address = env.contract.address.to_string();

let regsiter_msg = MsgRequestCallback {
sender: contract_address.clone(),
contract_address: contract_address.clone(),
job_id: job_id.clone(),
callback_height: callback_height.clone(),
fees: funds.clone()
};

let register_stargate_msg = CosmosMsg::Stargate {
type_url: "/archway.callback.v1.MsgRequestCallback".to_string(),
value: Binary::from(prost::Message::encode_to_vec(&regsiter_msg)),
};

Ok(Response::new()
.add_attribute("action", "register")
.add_message(register_stargate_msg))
pub mod execute {
use super::*;

pub fn register(
env: Env,
job_id: u64,
callback_height: i64,
funds: Coin,
) -> Result<Response, ContractError> {
let contract_address = env.contract.address.to_string();

let regsiter_msg = MsgRequestCallback {
sender: contract_address.clone(),
contract_address: contract_address.clone(),
job_id: job_id.clone(),
callback_height: callback_height.clone(),
fees: ProstCoin::from(funds),
};

let register_stargate_msg = CosmosMsg::Stargate {
type_url: "/archway.callback.v1.MsgRequestCallback".to_string(),
value: Binary::from(prost::Message::encode_to_vec(&regsiter_msg)),
};

Ok(Response::new()
.add_attribute("action", "register")
.add_message(register_stargate_msg))
}
}
```

Here's an example of the command to execute the registration of the callback:

```sh
archway contracts execute callbacktest --amount 140980000000000000aconst --args '{"register": {"job_id": 1, "callback_height": 6799600}}'
```

## Accepting callbacks

### Messages
Expand Down Expand Up @@ -186,4 +257,4 @@ Additionally, you can subscribe to events from the [`cw-errors`](/developers/gui

## Callback test contract

You can find more details in the Callback test contract [here](https://github.com/archway-network/archway/tree/main/contracts/callback-test).
You can find more details in the Callback test contract [here](https://github.com/emperorjm/archway-callback).

0 comments on commit 337200c

Please sign in to comment.