Skip to content

Instantly share code, notes, and snippets.

@mariano-aguero
Created May 6, 2020 18:11
Show Gist options
  • Save mariano-aguero/4d9e37034fd36c307f0aa0be1f09d880 to your computer and use it in GitHub Desktop.
Save mariano-aguero/4d9e37034fd36c307f0aa0be1f09d880 to your computer and use it in GitHub Desktop.
Approve
const emptyOps : list(operation) = list end;
function approve (const addressSpender : address; const value : nat; var store : store) : return is
block {
// If sender is the spender approving is not necessary
if sender = addressSpender then skip;
else block {
const senderAccount: account = getAccount(sender, store.accounts);
var allowance: nat := getAllowance(addressSpender, senderAccount.allowances);
// Changing allowance value from non-zero value to a non-zero value is forbidden to prevent the corresponding attack vector.
if allowance =/= 0n then failwith("UnsafeAllowanceChange");
else block {
senderAccount.allowances[addressSpender] := value;
store.accounts[sender] := senderAccount;
}
}
} with (emptyOps, store);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment