pub struct Contract { /* private fields */ }Expand description
A deployed contract instance bound to its interface identity.
call and view encode the argument value (typically a tuple of the
method’s arguments) with the contract (CXS) codec and decode the return the
same way, so a test works in Rust values rather than raw calldata. The
argument and return types must implement xdrs WriteXDR / ReadXDR (the
method’s own argument/return types from a contract’s interface crate do).
Each method takes anything that can reach a Caller: an agent’s &ctx,
or a Caller cloned out of it with
AgentContext::caller.
Prefer an interface’s generated <Iface>Remote proxy (see the
module docs), which checks method names and argument types at
compile time. A Contract suits a contract with no interface crate to
generate one from:
use thruput_testkit::xdr::types::ContractAddress;
use thruput_testkit::{AgentContext, Contract, Result};
async fn bump(ctx: &AgentContext, address: ContractAddress) -> Result<u64> {
let counter = Contract::new(address, "Counter", "0.1.0");
// One argument is passed as itself; several, as a tuple.
counter.call(ctx, "increment", &1u64).await?;
let count: u64 = counter.call_returning(ctx, "increment", &1u64).await?;
let read: u64 = counter.view(ctx, "get", &(), Some(ctx.address())).await?;
assert_eq!(read, count);
Ok(count)
}Implementations§
Source§impl Contract
impl Contract
Sourcepub fn new(
address: ContractAddress,
interface: impl Into<String>,
version: impl Into<String>,
) -> Self
pub fn new( address: ContractAddress, interface: impl Into<String>, version: impl Into<String>, ) -> Self
Bind a handle to a deployed address and its interface identity.
Sourcepub fn address(&self) -> ContractAddress
pub fn address(&self) -> ContractAddress
The deployed contract address.
Sourcepub fn prepare_call<R, A>(
&self,
ctx: &(impl AsRef<Caller> + ?Sized),
method: &str,
args: &A,
) -> ContractCall<R>
pub fn prepare_call<R, A>( &self, ctx: &(impl AsRef<Caller> + ?Sized), method: &str, args: &A, ) -> ContractCall<R>
Prepare a state-changing call to method from ctx’s account, whose
return decodes as R. Nothing is submitted until the ContractCall
is awaited or submitted.
The arguments are encoded now, so the call borrows nothing: it can be held, moved into another task, or returned from a typed client method.
Sourcepub async fn call<A>(
&self,
ctx: &(impl AsRef<Caller> + ?Sized),
method: &str,
args: &A,
) -> Result<TransactionReceipt>
pub async fn call<A>( &self, ctx: &(impl AsRef<Caller> + ?Sized), method: &str, args: &A, ) -> Result<TransactionReceipt>
Submit a state-changing call to method through the agent’s wallet,
awaiting the receipt and requiring successful execution.
Returns TestkitError::Reverted if the transaction executed but its
status was not Ok, so callers can ? a call they expect to succeed
instead of asserting on the receipt. Add call-site context with
map_err where the method name alone does not identify the call.
Sourcepub async fn submit_call<A>(
&self,
ctx: &(impl AsRef<Caller> + ?Sized),
method: &str,
args: &A,
) -> Result<SubmittedContractCall>
pub async fn submit_call<A>( &self, ctx: &(impl AsRef<Caller> + ?Sized), method: &str, args: &A, ) -> Result<SubmittedContractCall>
Submit a state-changing call without waiting for its receipt.
Sourcepub async fn call_returning<A, R>(
&self,
ctx: &(impl AsRef<Caller> + ?Sized),
method: &str,
args: &A,
) -> Result<R>
pub async fn call_returning<A, R>( &self, ctx: &(impl AsRef<Caller> + ?Sized), method: &str, args: &A, ) -> Result<R>
Call method, requiring success, and decode the return value to R.
Sourcepub async fn view<A, R>(
&self,
ctx: &(impl AsRef<Caller> + ?Sized),
method: &str,
args: &A,
caller: Option<ContractAddress>,
) -> Result<R>
pub async fn view<A, R>( &self, ctx: &(impl AsRef<Caller> + ?Sized), method: &str, args: &A, caller: Option<ContractAddress>, ) -> Result<R>
Simulate a read-only view of method, decoding the return to R.
caller sets the simulated msg.sender; pass an agent’s own address for
state keyed by the caller.