🆗Approve
The function approves a new allowance for the spender.
There are three types of approve:
1. function approve(address _spender, ctUint64 _itCT, bytes calldata _itSignature) public returns (bool success)
2. function approve(address _spender, gtUint64 _value) public returns (bool success)
3. function approveClear(address _spender, uint64 _value) public returns (bool success)
1: Validates the inputText to get the required amount to allow.
3: Allow an encrypted amount. This version is used by a contract that already has the value as encrypted gtUint64.
Parameters:
_spender: The address to allow the given amount
_itCT, _itSignature: An encrypted value to allow as an InputText
_value: The value to allow (clear or encrypted depends on the type)
Return value:
Always true
Full Solidity Code
1.
// Sets the new allowance given inside the IT (encrypted and signed value) of the
// spender
function approve(address _spender, ctUint64 _itCT, bytes calldata _itSignature)
public returns (bool success){
// Create IT using the given CT and signature
itUint64 memory it;
it.ciphertext = _itCT;
it.signature = _itSignature;
return approve(_spender, MpcCore.validateCiphertext(it));
}
2.
// Sets the new encrypted allowance of the spender
function approve(address _spender, gtUint64 _value) public returns (bool success){
address owner = msg.sender;
setApproveValue(owner, _spender, MpcCore.offBoard(_value));
emit Approval(owner, _spender);
return true;
}
// Sets the new allowance of the spender
function approveClear(address _spender, uint64 _value) public returns (bool success){
address owner = msg.sender;
gtUint64 gt = MpcCore.setPublic64(_value);
setApproveValue(owner, _spender, MpcCore.offBoard(gt));
emit Approval(owner, _spender, _value);
return true;
}
Example Usage:
print("************* Approve ", plaintext_integer*10, " to my address *************")
# Set allowance for this account
function = contract.functions.approveClear(account.address, plaintext_integer*10)
soda_helper.call_contract_function_transaction("private_erc20", function)
print("************* Check my allowance *************")
# Get my allowance to check that the allowance is correct
check_allowance(account, contract, user_key, plaintext_integer*10)
print("************* Approve IT 50 to my address *************")
# Approve 50 SOD to this account
function = contract.functions.approve(account.address, dummyCT, dummySignature) # Dummy function
func_sig = get_function_signature(function.abi) # Get the function signature
# Prepare the input text for the function
ct, signature = prepare_IT(50, user_key, account, contract, func_sig, bytes.fromhex(private_key[2:]))
function = contract.functions.approve(account.address, ct, signature)
soda_helper.call_contract_function_transaction("private_erc20", function)
print("************* Check my allowance *************")
# Check that the allowance has changed to 50 SOD
check_allowance(account, contract, user_key, 50)
Last updated