The Chain.spend(addr, ntokens) will take from the function caller(Call.caller) wallet to send to addr. The question now is how does the contract send tokens from the Contract.Address to the function caller
Eh, no, it will most definitely not take/spend/transfer tokens from Call.caller - the contract can only spend tokens that belong to the contract itself…
If you think about it, it would be horribly insecure otherwise, any contract call you make could potentially spend all your tokens
If you want the contract to spend all your tokens you have to pass them to the contract in the contract call (the amount field in the ContractCallTx).
To explain better let me use an example of a trading system where Buy means trading Fiat to AE and Sell means trading AE to Fiat.
Sell
At this time, the Chain.spend method transfers token from the Callers address after confirmation to the stipulated address then keeps the record so the admin can send the equivalent Fiat to the user.
Buy
Then for Buy, the caller sends Fiat to get the equivalent in AE right? So how does one achieve this goal after the caller has sent the equivalent Fiat?
@Baixin.chain I hope I interpreted your question well.
It doesn’t matter what the use case is Chain.spend only takes two arguments, the account that receives the tokens, and the number of tokens to move.
To move tokens from account A to account B using contract C, account A first have to give those tokens to the contract C and then contract C can Chain.spend(B, …) - when doing a contract call you can attach tokens to the call, and likewise when you are doing a remote contract call inside a contract.
Hello @Baixin.chain, in other to backup @hanssv.chain explanation of the Chain.spend function, I created a simple contract and hope it helps. Find contract on AEStudio at AE Studio or use the below code snippet:
when calling the sell_ae function, make sure the Call.value is an AE value and not aettos or the rest
When calling the 'buy_ae` function, make sure the fiat amount is greater than 100 and less than or equals to the balance on the Contract.address when divided by 1000000000000000000 and multiplied with 100.