When you are running a contract, you often want to know who is running it. For example, if someone who isn’t an account owner tries to spend their money, you need to have some way of identifying who that person is and prevent that from happening. This is where Context, or ctx inside of smart contracts, comes into play.
There are four types of ctx variables.
This is the most complex Context variable, but also the most useful. The ctx.caller is the same as the transaction signer (ctx.signer) at the beginning of execution. If the smart contract that is initially invoked calls a method on another smart contract, the ctx.caller then changes to the name of the smart contract calling that method, and so on and so forth until the end of the execution.
Assume the two contracts above exist in state space. If stu calls who_am_i on the direct contract, stu will be returned because direct does not call any methods in any other smart contracts.
However, if stu calls call_direct on the indirect contract, indirect will be returned because indirect is now the caller of this method.
A good example of how to use this would be in a token contract.
balances = Hash()
balances['stu'] = 100
balances['contract'] = 99
def send(amount: float, to: str):
assert balances[ctx.caller] >= amount
balances[ctx.caller] -= amount
balances[to] += amount
def withdraw(amount: float):
assert ctx.caller == 'stu'
In the above setup, stu has 100 tokens directly on the token contract. He can send them, because his account balance is looked up based on the ctx.caller when the send method is called.
Similarly, contract also has 99 tokens. When contract imports token and calls send, ctx.caller is changed to contract, and its balance is looked up and mutated accordingly.
This is a very simple reference to the name of the smart contract. Use cases are generally when you need to identify a smart contract itself when doing some sort of transaction, such as sending payment through an account managed by the smart contract but residing in another smart contract.
names = Hash()
def register(name: str, value: Any):
if names[name] is None:
names[name] = value
def register(value: Any):
This is the absolute signer of the transaction regardless of where the code is being executed in the call stack. This is good for creating blacklists of users from a particular contract.
not_allowed = ['stu', 'tejas']
assert ctx.signer not in not_allowed
return 'You are not blacklisted!'
In the case that stu calls the try_to_bypass method on indirect, the transaction will still fail because ctx.signer is used for gating instead of ctx.caller.
NOTE: Never use ctx.signer for account creation or identity. Only use it for security guarding and protection. ctx.caller should allow behavior based on the value. ctx.signer should block behavior based on the value.
On submission, you can specify the owner of a smart contract. This means that only the owner can call the @export methods on it. This is for advanced contract pattern types where a single controller is desired for many ‘sub-contracts’. Using ctx.owner inside of a smart contract can only be used to change the ownership of the contract itself. Be careful with this method!
def change_ownership(new_owner: str):
ctx.owner = new_owner
The above contract is not callable unless the ctx.caller is the ctx.owner. Therefore, you do not need to do additional checks to make sure that this is the case.
Telegram: t.me/lamdenchatDiscord: discord.gg/xjBgcAAUpSReddit: reddit.com/r/lamden