-
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.
Implement components to invoke Starknet contract (#35)
* Add HasStarknetProvider trait * Add HasStarknetAccount trait * Add account field to chain context * Implement InvokeStarknetContract * Implement CanInvokeContract for StarknetChain * Test call to invoke * Ad hoc check that transfer is successful
- Loading branch information
1 parent
0a5933c
commit dfdd0e8
Showing
17 changed files
with
268 additions
and
14 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
29 changes: 29 additions & 0 deletions
29
relayer/crates/starknet-chain-components/src/impls/account.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,29 @@ | ||
use std::marker::PhantomData; | ||
|
||
use cgp_core::prelude::*; | ||
use starknet::accounts::ConnectedAccount; | ||
|
||
use crate::traits::account::{ | ||
HasStarknetAccountType, ProvideStarknetAccountType, StarknetAccountGetter, | ||
}; | ||
|
||
pub struct GetStarknetAccountField<Tag>(pub PhantomData<Tag>); | ||
|
||
impl<Chain, Tag> ProvideStarknetAccountType<Chain> for GetStarknetAccountField<Tag> | ||
where | ||
Chain: Async + HasField<Tag>, | ||
Tag: Async, | ||
Chain::Field: Async + ConnectedAccount, | ||
{ | ||
type Account = Chain::Field; | ||
} | ||
|
||
impl<Chain, Tag> StarknetAccountGetter<Chain> for GetStarknetAccountField<Tag> | ||
where | ||
Chain: Async + HasStarknetAccountType + HasField<Tag, Field = Chain::Account>, | ||
Tag: Async, | ||
{ | ||
fn account(chain: &Chain) -> &Chain::Account { | ||
chain.get_field(PhantomData) | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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::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; | ||
|
||
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, | ||
{ | ||
async fn invoke_contract( | ||
chain: &Chain, | ||
contract_address: &Felt, | ||
entry_point_selector: &Felt, | ||
calldata: &Vec<Felt>, | ||
) -> Result<Felt, Chain::Error> { | ||
let account = chain.account(); | ||
|
||
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) | ||
} | ||
} |
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 +1,2 @@ | ||
pub mod call; | ||
pub mod invoke; |
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,4 @@ | ||
pub mod account; | ||
pub mod contract; | ||
pub mod provider; | ||
pub mod types; |
29 changes: 29 additions & 0 deletions
29
relayer/crates/starknet-chain-components/src/impls/provider.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,29 @@ | ||
use std::marker::PhantomData; | ||
|
||
use cgp_core::prelude::*; | ||
use starknet::providers::Provider; | ||
|
||
use crate::traits::provider::{ | ||
HasStarknetProviderType, ProvideStarknetProviderType, StarknetProviderGetter, | ||
}; | ||
|
||
pub struct GetStarknetProviderField<Tag>(pub PhantomData<Tag>); | ||
|
||
impl<Chain, Tag> ProvideStarknetProviderType<Chain> for GetStarknetProviderField<Tag> | ||
where | ||
Chain: Async + HasField<Tag>, | ||
Tag: Async, | ||
Chain::Field: Async + Provider, | ||
{ | ||
type Provider = Chain::Field; | ||
} | ||
|
||
impl<Chain, Tag> StarknetProviderGetter<Chain> for GetStarknetProviderField<Tag> | ||
where | ||
Chain: Async + HasStarknetProviderType + HasField<Tag, Field = Chain::Provider>, | ||
Tag: Async, | ||
{ | ||
fn provider(chain: &Chain) -> &Chain::Provider { | ||
chain.get_field(PhantomData) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
relayer/crates/starknet-chain-components/src/impls/types/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,3 +1,4 @@ | ||
pub mod address; | ||
pub mod blob; | ||
pub mod method; | ||
pub mod tx_hash; |
9 changes: 9 additions & 0 deletions
9
relayer/crates/starknet-chain-components/src/impls/types/tx_hash.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,9 @@ | ||
use cgp_core::Async; | ||
use hermes_relayer_components::transaction::traits::types::tx_hash::ProvideTransactionHashType; | ||
use starknet::core::types::Felt; | ||
|
||
pub struct ProvideFeltTxHash; | ||
|
||
impl<Chain: Async> ProvideTransactionHashType<Chain> for ProvideFeltTxHash { | ||
type TxHash = Felt; | ||
} |
26 changes: 26 additions & 0 deletions
26
relayer/crates/starknet-chain-components/src/traits/account.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,26 @@ | ||
use cgp_core::prelude::*; | ||
use starknet::accounts::{Account, AccountError, ConnectedAccount}; | ||
|
||
#[derive_component(StarknetAccountTypeComponent, ProvideStarknetAccountType<Chain>)] | ||
pub trait HasStarknetAccountType: Async { | ||
type Account: Async + ConnectedAccount; | ||
} | ||
|
||
#[derive_component(StarknetAccountGetterComponent, StarknetAccountGetter<Chain>)] | ||
pub trait HasStarknetAccount: HasStarknetAccountType { | ||
fn account(&self) -> &Self::Account; | ||
} | ||
|
||
pub trait CanRaiseAccountErrors: | ||
HasStarknetAccountType | ||
+ CanRaiseError<<Self::Account as Account>::SignError> | ||
+ CanRaiseError<AccountError<<Self::Account as Account>::SignError>> | ||
{ | ||
} | ||
|
||
impl<Chain> CanRaiseAccountErrors for Chain where | ||
Chain: HasStarknetAccountType | ||
+ CanRaiseError<<Chain::Account as Account>::SignError> | ||
+ CanRaiseError<AccountError<<Self::Account as Account>::SignError>> | ||
{ | ||
} |
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,3 +1,5 @@ | ||
pub mod account; | ||
pub mod client; | ||
pub mod contract; | ||
pub mod provider; | ||
pub mod types; |
12 changes: 12 additions & 0 deletions
12
relayer/crates/starknet-chain-components/src/traits/provider.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,12 @@ | ||
use cgp_core::prelude::*; | ||
use starknet::providers::Provider; | ||
|
||
#[derive_component(StarknetProviderTypeComponent, ProvideStarknetProviderType<Chain>)] | ||
pub trait HasStarknetProviderType: Async { | ||
type Provider: Async + Provider; | ||
} | ||
|
||
#[derive_component(StarknetProviderGetterComponent, StarknetProviderGetter<Chain>)] | ||
pub trait HasStarknetProvider: HasStarknetProviderType { | ||
fn provider(&self) -> &Self::Provider; | ||
} |
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
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
Oops, something went wrong.