Skip to content

Commit

Permalink
feat: (de/in)crease allowance (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau authored Oct 16, 2023
1 parent f030347 commit 620b402
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ethers/erc20.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ method approve*(token: Erc20Token,
amount: UInt256): ?TransactionResponse {.base, contract.}
## Sets `amount` as the allowance of `spender` over the caller's tokens.

method increaseAllowance*(token: Erc20Token,
spender: Address,
addedValue: UInt256): ?TransactionResponse {.base, contract.}
## Atomically increases the allowance granted to spender by the caller.
## This is an alternative to approve that can be used as a mitigation for problems described in IERC20.approve.
## Emits an Approval event indicating the updated allowance.
##
## WARNING: THIS IS NON-STANDARD ERC-20 FUNCTION, DOUBLE CHECK THAT YOUR TOKEN HAS IT!

method decreaseAllowance*(token: Erc20Token,
spender: Address,
addedValue: UInt256): ?TransactionResponse {.base, contract.}
## Atomically decreases the allowance granted to spender by the caller.
## This is an alternative to approve that can be used as a mitigation for problems described in IERC20.approve.
## Emits an Approval event indicating the updated allowance.
##
## WARNING: THIS IS NON-STANDARD ERC-20 FUNCTION, DOUBLE CHECK THAT YOUR TOKEN HAS IT!

method transferFrom*(token: Erc20Token,
spender: Address,
recipient: Address,
Expand Down
26 changes: 26 additions & 0 deletions testmodule/testErc20.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
check (await token.balanceOf(accounts[0])) == 100.u256
check (await token.balanceOf(accounts[1])) == 0.u256

test "increase/decrease allowance":
discard await testToken.mint(accounts[0], 100.u256)

check (await token.allowance(accounts[0], accounts[1])) == 0.u256
check (await token.balanceOf(accounts[0])) == 100.u256
check (await token.balanceOf(accounts[1])) == 0.u256

discard await token.increaseAllowance(accounts[1], 50.u256)

check (await token.allowance(accounts[0], accounts[1])) == 50.u256
check (await token.balanceOf(accounts[0])) == 100.u256
check (await token.balanceOf(accounts[1])) == 0.u256

discard await token.increaseAllowance(accounts[1], 50.u256)

check (await token.allowance(accounts[0], accounts[1])) == 100.u256
check (await token.balanceOf(accounts[0])) == 100.u256
check (await token.balanceOf(accounts[1])) == 0.u256

discard await token.decreaseAllowance(accounts[1], 50.u256)

check (await token.allowance(accounts[0], accounts[1])) == 50.u256
check (await token.balanceOf(accounts[0])) == 100.u256
check (await token.balanceOf(accounts[1])) == 0.u256


test "transferFrom tokens":
let senderAccount = accounts[0]
let receiverAccount = accounts[1]
Expand Down

0 comments on commit 620b402

Please sign in to comment.