-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine interfaces for token transfer (#37)
* Implement QueryErc20TokenBalance * Implement CanQueryTokenBalance for StarknetChain * Test query balance * Implement CanTransferToken for StarknetChain * Use transfer_token in test * Use variables for addresses in test * Remove unused patch.crates-io * Implement InvokeContractMessageBuilder * Implement CanSendMessages for StarknetChain * Implement CanSubmitTx for StarknetChain * Use message senders to invoke contract * Implement QueryTransactionReceipt * Implement CanPollTxResponse for StarknetChain * Use poll_tx_response in message sender
- Loading branch information
1 parent
dfb2d7a
commit f74bae4
Showing
32 changed files
with
500 additions
and
115 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 10 additions & 35 deletions
45
relayer/crates/starknet-chain-components/src/impls/contract/invoke.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,23 @@ | ||
use hermes_relayer_components::transaction::traits::types::tx_hash::HasTransactionHashType; | ||
use starknet::accounts::{Account, Call}; | ||
use starknet::core::types::Felt; | ||
use hermes_relayer_components::chain::traits::send_message::CanSendSingleMessage; | ||
|
||
use crate::traits::account::{CanRaiseAccountErrors, HasStarknetAccount}; | ||
use crate::traits::contract::invoke::ContractInvoker; | ||
use crate::traits::provider::HasStarknetProvider; | ||
use crate::traits::types::address::HasAddressType; | ||
use crate::traits::types::blob::HasBlobType; | ||
use crate::traits::types::method::HasMethodSelectorType; | ||
use crate::traits::contract::message::CanBuildInvokeContractMessage; | ||
|
||
pub struct InvokeStarknetContract; | ||
|
||
impl<Chain> ContractInvoker<Chain> for InvokeStarknetContract | ||
where | ||
Chain: HasAddressType<Address = Felt> | ||
+ HasMethodSelectorType<MethodSelector = Felt> | ||
+ HasBlobType<Blob = Vec<Felt>> | ||
+ HasTransactionHashType<TxHash = Felt> | ||
+ HasStarknetProvider | ||
+ HasStarknetAccount | ||
+ CanRaiseAccountErrors, | ||
Chain: CanBuildInvokeContractMessage + CanSendSingleMessage, | ||
{ | ||
async fn invoke_contract( | ||
chain: &Chain, | ||
contract_address: &Felt, | ||
entry_point_selector: &Felt, | ||
calldata: &Vec<Felt>, | ||
) -> Result<Felt, Chain::Error> { | ||
let account = chain.account(); | ||
contract_address: &Chain::Address, | ||
entry_point_selector: &Chain::MethodSelector, | ||
calldata: &Chain::Blob, | ||
) -> Result<Vec<Chain::Event>, Chain::Error> { | ||
let message = | ||
chain.build_invoke_contract_message(contract_address, entry_point_selector, calldata); | ||
|
||
let call = Call { | ||
to: *contract_address, | ||
selector: *entry_point_selector, | ||
calldata: calldata.clone(), | ||
}; | ||
|
||
let execution = account.execute_v3(vec![call]); | ||
|
||
let tx_hash = execution | ||
.send() | ||
.await | ||
.map_err(Chain::raise_error)? | ||
.transaction_hash; | ||
|
||
Ok(tx_hash) | ||
chain.send_message(message).await | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
relayer/crates/starknet-chain-components/src/impls/contract/message.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use hermes_relayer_components::chain::traits::types::message::HasMessageType; | ||
use starknet::accounts::Call; | ||
use starknet::core::types::Felt; | ||
|
||
use crate::traits::contract::message::InvokeContractMessageBuilder; | ||
use crate::traits::types::address::HasAddressType; | ||
use crate::traits::types::blob::HasBlobType; | ||
use crate::traits::types::method::HasMethodSelectorType; | ||
|
||
pub struct BuildInvokeContractCall; | ||
|
||
impl<Chain> InvokeContractMessageBuilder<Chain> for BuildInvokeContractCall | ||
where | ||
Chain: HasAddressType<Address = Felt> | ||
+ HasMethodSelectorType<MethodSelector = Felt> | ||
+ HasBlobType<Blob = Vec<Felt>> | ||
+ HasMessageType<Message = Call>, | ||
{ | ||
fn build_invoke_contract_message( | ||
_chain: &Chain, | ||
contract_address: &Felt, | ||
entry_point_selector: &Felt, | ||
calldata: &Vec<Felt>, | ||
) -> Call { | ||
Call { | ||
to: *contract_address, | ||
selector: *entry_point_selector, | ||
calldata: calldata.clone(), | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
relayer/crates/starknet-chain-components/src/impls/contract/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod call; | ||
pub mod invoke; | ||
pub mod message; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
pub mod account; | ||
pub mod contract; | ||
pub mod provider; | ||
pub mod queries; | ||
pub mod send_message; | ||
pub mod submit_tx; | ||
pub mod transfer; | ||
pub mod tx_response; | ||
pub mod types; |
1 change: 1 addition & 0 deletions
1
relayer/crates/starknet-chain-components/src/impls/queries/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod token_balance; |
47 changes: 47 additions & 0 deletions
47
relayer/crates/starknet-chain-components/src/impls/queries/token_balance.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use cgp_core::error::CanRaiseError; | ||
use starknet::core::types::{Felt, U256}; | ||
use starknet::macros::selector; | ||
|
||
use crate::traits::contract::call::CanCallContract; | ||
use crate::traits::queries::token_balance::TokenBalanceQuerier; | ||
use crate::traits::types::address::HasAddressType; | ||
use crate::traits::types::amount::HasAmountType; | ||
use crate::traits::types::blob::HasBlobType; | ||
use crate::traits::types::method::HasMethodSelectorType; | ||
|
||
pub struct QueryErc20TokenBalance; | ||
|
||
pub const BALANCE_SELECTOR: Felt = selector!("balance_of"); | ||
|
||
impl<Chain> TokenBalanceQuerier<Chain> for QueryErc20TokenBalance | ||
where | ||
Chain: HasAddressType<Address = Felt> | ||
+ HasAmountType<Amount = U256> | ||
+ HasBlobType<Blob = Vec<Felt>> | ||
+ HasMethodSelectorType<MethodSelector = Felt> | ||
+ CanCallContract | ||
+ CanRaiseError<&'static str>, | ||
{ | ||
async fn query_token_balance( | ||
chain: &Chain, | ||
token_address: &Felt, | ||
account_address: &Felt, | ||
) -> Result<U256, Chain::Error> { | ||
let output = chain | ||
.call_contract(token_address, &BALANCE_SELECTOR, &vec![*account_address]) | ||
.await?; | ||
|
||
let [e1, e2]: [Felt; 2] = output.try_into().map_err(|_| { | ||
Chain::raise_error( | ||
"expect output returned from balance_of query to be consist of two felt252 values", | ||
) | ||
})?; | ||
|
||
let low = u128::from_be_bytes(e1.to_bytes_be()[16..].try_into().unwrap()); | ||
let high = u128::from_be_bytes(e2.to_bytes_be()[16..].try_into().unwrap()); | ||
|
||
let amount = U256::from_words(low, high); | ||
|
||
Ok(amount) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
relayer/crates/starknet-chain-components/src/impls/send_message.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use cgp_core::error::HasErrorType; | ||
use hermes_relayer_components::chain::traits::send_message::MessageSender; | ||
use hermes_relayer_components::chain::traits::types::event::HasEventType; | ||
use hermes_relayer_components::chain::traits::types::message::HasMessageType; | ||
use hermes_relayer_components::transaction::traits::poll_tx_response::CanPollTxResponse; | ||
use hermes_relayer_components::transaction::traits::submit_tx::CanSubmitTx; | ||
use starknet::accounts::Call; | ||
|
||
pub struct SendCallMessages; | ||
|
||
impl<Chain> MessageSender<Chain> for SendCallMessages | ||
where | ||
Chain: HasMessageType<Message = Call> | ||
+ CanSubmitTx<Transaction = Vec<Call>> | ||
+ CanPollTxResponse | ||
+ HasEventType | ||
+ HasErrorType, | ||
{ | ||
async fn send_messages( | ||
chain: &Chain, | ||
messages: Vec<Call>, | ||
) -> Result<Vec<Vec<Chain::Event>>, Chain::Error> { | ||
// stub events | ||
let events = messages.iter().map(|_| Vec::new()).collect(); | ||
|
||
let tx_hash = chain.submit_tx(&messages).await?; | ||
|
||
chain.poll_tx_response(&tx_hash).await?; | ||
|
||
Ok(events) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
relayer/crates/starknet-chain-components/src/impls/submit_tx.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use hermes_relayer_components::transaction::traits::submit_tx::TxSubmitter; | ||
use hermes_relayer_components::transaction::traits::types::transaction::HasTransactionType; | ||
use hermes_relayer_components::transaction::traits::types::tx_hash::HasTransactionHashType; | ||
use starknet::accounts::{Account, Call}; | ||
use starknet::core::types::Felt; | ||
|
||
use crate::traits::account::{CanRaiseAccountErrors, HasStarknetAccount}; | ||
use crate::traits::provider::HasStarknetProvider; | ||
|
||
pub struct SubmitCallTransaction; | ||
|
||
impl<Chain> TxSubmitter<Chain> for SubmitCallTransaction | ||
where | ||
Chain: HasTransactionType<Transaction = Vec<Call>> | ||
+ HasTransactionHashType<TxHash = Felt> | ||
+ HasStarknetProvider | ||
+ HasStarknetAccount | ||
+ CanRaiseAccountErrors, | ||
{ | ||
async fn submit_tx(chain: &Chain, messages: &Vec<Call>) -> Result<Felt, Chain::Error> { | ||
let account = chain.account(); | ||
|
||
let execution = account.execute_v3(messages.clone()); | ||
|
||
let tx_hash = execution | ||
.send() | ||
.await | ||
.map_err(Chain::raise_error)? | ||
.transaction_hash; | ||
|
||
Ok(tx_hash) | ||
} | ||
} |
Oops, something went wrong.