Skip to content

Latest commit

 

History

History
303 lines (226 loc) · 12.6 KB

acs5.rst

File metadata and controls

303 lines (226 loc) · 12.6 KB

ACS5 - Contract Threshold Standard

If you want to raise the threshold for using contract, consider implementing ACS5.

Interface

To limit to call a method in a contract, you only need to implement the following five interfaces:

Methods

Method Name Request Type Response Type Description
SetMethodCallingThreshold acs5.SetMethodCallingThresholdInput google.protobuf.Empty Set the threshold for method calling.
GetMethodCallingThreshold google.protobuf.StringValue acs5.MethodCallingThreshold Get the threshold for method calling.

Types

acs5.MethodCallingThreshold

Field Type Description Label
symbol_to_amount MethodCallingThreshold.SymbolToAmountEntry The threshold for method calling, token symbol -> amount. repeated
threshold_check_type ThresholdCheckType The type of threshold check.

acs5.MethodCallingThreshold.SymbolToAmountEntry

Field Type Description Label
key string
value int64

acs5.SetMethodCallingThresholdInput

Field Type Description Label
method string The method name to check.
symbol_to_amount SetMethodCallingThresholdInput.SymbolToAmountEntry The threshold for method calling, token symbol -> amount. repeated
threshold_check_type ThresholdCheckType The type of threshold check.

acs5.SetMethodCallingThresholdInput.SymbolToAmountEntry

Field Type Description Label
key string
value int64

acs5.ThresholdCheckType

Name Number Description
BALANCE 0 Check balance only.
ALLOWANCE 1 Check balance and allowance at the same time.

Usage

Similar to ACS1, which uses an automatically generated pre-plugin transaction called ChargeTransactionFees to charge a transaction fee, ACS5 automatically generates a pre-plugin transaction called CheckThreshold to test whether the account that sent the transaction can invoke the corresponding method.

The implementation of CheckThreshold:

In other words, if the token balance of the sender of the transaction or the amount authorized for the target contract does not reach the set limit, the pre-plugin transaction will throw an exception, thereby it prevents the original transaction from executing.

Implementation

Just lik the GetMethodFee of ACS1, you can implement only one GetMethodCallingThreshold method.

It can also be achieved by using MappedState<string, MethodCallingThreshold> in the State class:

But at the same time, do not forget to configure the call permission of SetMethodCallingThreshold, which requires the definition of an Admin in the State (of course, you can also use ACS3):

The easiest implementation:

Test

You can test the Foo method defined above.

Make a Stub:

Before setting the threshold, check the current threshold, which should be 0:

The ELF balance of the caller of Foo should be greater than 1 ELF:

Check the threshold again:

Send the Foo transaction via an account who has sufficient balance can succeed:

Send the Foo transaction via another account without ELF fails: