spender spends amount of tokens on the behalf of from_acct, spender makes a transaction of amount of tokens from from_acct to to_acct :param spender: :param from_acct: :param to_acct: :param amount: :return:
(spender,from_acct,to_acct,amount)
| 220 | |
| 221 | |
| 222 | def transferFrom(spender,from_acct,to_acct,amount): |
| 223 | """ |
| 224 | spender spends amount of tokens on the behalf of from_acct, spender makes a transaction of amount of tokens |
| 225 | from from_acct to to_acct |
| 226 | :param spender: |
| 227 | :param from_acct: |
| 228 | :param to_acct: |
| 229 | :param amount: |
| 230 | :return: |
| 231 | """ |
| 232 | if len(spender) != 20 or len(from_acct) != 20 or len(to_acct) != 20: |
| 233 | raise Exception("address length error") |
| 234 | if CheckWitness(spender) == False: |
| 235 | return False |
| 236 | |
| 237 | fromKey = concat(BALANCE_PREFIX, from_acct) |
| 238 | fromBalance = Get(ctx, fromKey) |
| 239 | if amount > fromBalance or amount < 0: |
| 240 | return False |
| 241 | |
| 242 | approveKey = concat(concat(APPROVE_PREFIX,from_acct),spender) |
| 243 | approvedAmount = Get(ctx,approveKey) |
| 244 | toKey = concat(BALANCE_PREFIX,to_acct) |
| 245 | |
| 246 | if amount > approvedAmount: |
| 247 | return False |
| 248 | elif amount == approvedAmount: |
| 249 | Delete(ctx,approveKey) |
| 250 | Put(ctx, fromKey, fromBalance - amount) |
| 251 | else: |
| 252 | Put(ctx,approveKey,approvedAmount - amount) |
| 253 | Put(ctx, fromKey, fromBalance - amount) |
| 254 | |
| 255 | toBalance = Get(ctx, toKey) |
| 256 | Put(ctx, toKey, toBalance + amount) |
| 257 | |
| 258 | # Notify(["transfer", AddressToBase58(from_acct), AddressToBase58(to_acct), amount]) |
| 259 | # TransferEvent(AddressToBase58(from_acct), AddressToBase58(to_acct), amount) |
| 260 | TransferEvent(from_acct, to_acct, amount) |
| 261 | |
| 262 | return True |
| 263 | |
| 264 | |
| 265 | def allowance(owner,spender): |