Let's add an owner to our contract so we acn only call increase_balance from our Account
# source env/bin/activate
(env) # cp contracts/contract1.cairo contracts/contract2.cairo
In your contract first add a new state variable with the owner.
@storage_var
func owner() -> (owner_address: felt) {
}
Also add a constructor to setup the owner at deploy time.
@constructor
func constructor{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
}(owner_address: felt) {
owner.write(value=owner_address);
return ();
}
Last, modify increase_balance to check that only the owner can increase the balance
// Increases the balance by the given amount.
@external
func increase_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
amount: felt
) {
let (caller) = get_caller_address();
let (self) = owner.read();
with_attr error_message(
"Account must be the owner") {
assert self = caller;
}
let (res) = balance.read();
balance.write(res + amount);
return ();
}
(env) # nile compile
(env) # nile deploy contract2 <addr> --alias stark2
(env) # nile call stark1 get_balance
0
(env) nile send PKEY stark2 increase_balance 40
(env) # nile call stark1 get_balance
40
(env) nile send EVE stark2 increase_balance 40
(env) # nile call stark1 get_balance
40
Replace with the Address of your account (not the public key). You can find it at localhost.deployments.txt. You get the transaction Hash, and the info on the deployed contracts gets saved to localhost.deployments.txt (address:abiFile:alias).
You can add to your .env another private key and setup a new Account (EVE).