-
Notifications
You must be signed in to change notification settings - Fork 115
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
PassAll
gas option for program to program calls
#1417
base: main
Are you sure you want to change the base?
Changes from all commits
31b96ec
ee4711a
035c514
73fbf1d
aba66b3
6df5c21
caf12bc
f5ab9a8
6c68c82
d177208
b5a40dc
8b6e6b9
20878be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,6 +251,10 @@ impl Context { | |
borsh::from_slice(&bytes).expect("failed to deserialize") | ||
} | ||
|
||
pub fn call_contract_builder(&mut self, address: Address) -> CallContractBuilder<'_> { | ||
CallContractBuilder::with_address(self, address) | ||
} | ||
|
||
#[cfg(feature = "bindings")] | ||
#[must_use] | ||
pub fn to_extern(&mut self, args: ExternalCallArgs) -> ExternalCallContext<'_, Self> { | ||
|
@@ -299,7 +303,7 @@ impl Context { | |
function_name, | ||
args, | ||
value, | ||
max_units: 0, | ||
max_units: Gas::PassAll, | ||
}; | ||
|
||
let contract_args = borsh::to_vec(&(CALL_FUNCTION_PREFIX, contract_args)) | ||
|
@@ -334,6 +338,58 @@ impl Context { | |
} | ||
} | ||
|
||
pub struct CallContractBuilder<'a> { | ||
ctx: &'a mut Context, | ||
address: Address, | ||
max_units: Gas, | ||
value: u64, | ||
} | ||
|
||
impl<'a> CallContractBuilder<'a> { | ||
/// Creates a new context with an address. | ||
fn with_address(ctx: &'a mut Context, address: Address) -> Self { | ||
CallContractBuilder { | ||
ctx, | ||
address, | ||
max_units: Gas::PassAll, | ||
value: 0, | ||
} | ||
} | ||
|
||
/// Specifies the amount of units that should be passed to the context. | ||
pub fn with_max_units(mut self, units: u64) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great if this function could only accept units that are > 0, there might be some semantic changes to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise, replicate the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might also want to replace the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep being explicit on the gas forwarding, having a function named |
||
self.max_units = units.into(); | ||
self | ||
} | ||
|
||
/// Sets the gas forwarding rule to be "pass-all". This is the default behaviour. | ||
pub fn with_pass_all(mut self) -> Self { | ||
self.max_units = Gas::PassAll; | ||
self | ||
} | ||
|
||
/// Sets the value passed to the context. Defaults to 0. | ||
pub fn with_value(mut self, value: u64) -> Self { | ||
self.value = value; | ||
self | ||
} | ||
|
||
/// Consumes the builder and call the specified function with the args. | ||
pub fn call_function<T: BorshDeserialize>( | ||
self, | ||
function_name: &str, | ||
args: &[u8], | ||
) -> Result<T, ExternalCallError> { | ||
self.ctx.call_contract( | ||
self.address, | ||
function_name, | ||
args, | ||
self.max_units, | ||
self.value, | ||
) | ||
} | ||
} | ||
|
||
/// An error that is returned from call to public functions. | ||
#[derive(Debug, Display, BorshSerialize, BorshDeserialize, PartialEq, Eq)] | ||
#[repr(u8)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be interested to know if there could be a cleaner solution. Currently there is a bool and the uint64 that tells the amount of fuel passed, but since having
PassAll = true
andFuel > 0
is inconsistent state, this has to be validated through the execution. Is there a better way to simulate this rust enum: