Skip to content

Commit 620b402

Browse files
authored
feat: (de/in)crease allowance (#56)
1 parent f030347 commit 620b402

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

ethers/erc20.nim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ method approve*(token: Erc20Token,
5454
amount: UInt256): ?TransactionResponse {.base, contract.}
5555
## Sets `amount` as the allowance of `spender` over the caller's tokens.
5656

57+
method increaseAllowance*(token: Erc20Token,
58+
spender: Address,
59+
addedValue: UInt256): ?TransactionResponse {.base, contract.}
60+
## Atomically increases the allowance granted to spender by the caller.
61+
## This is an alternative to approve that can be used as a mitigation for problems described in IERC20.approve.
62+
## Emits an Approval event indicating the updated allowance.
63+
##
64+
## WARNING: THIS IS NON-STANDARD ERC-20 FUNCTION, DOUBLE CHECK THAT YOUR TOKEN HAS IT!
65+
66+
method decreaseAllowance*(token: Erc20Token,
67+
spender: Address,
68+
addedValue: UInt256): ?TransactionResponse {.base, contract.}
69+
## Atomically decreases the allowance granted to spender by the caller.
70+
## This is an alternative to approve that can be used as a mitigation for problems described in IERC20.approve.
71+
## Emits an Approval event indicating the updated allowance.
72+
##
73+
## WARNING: THIS IS NON-STANDARD ERC-20 FUNCTION, DOUBLE CHECK THAT YOUR TOKEN HAS IT!
74+
5775
method transferFrom*(token: Erc20Token,
5876
spender: Address,
5977
recipient: Address,

testmodule/testErc20.nim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
6969
check (await token.balanceOf(accounts[0])) == 100.u256
7070
check (await token.balanceOf(accounts[1])) == 0.u256
7171

72+
test "increase/decrease allowance":
73+
discard await testToken.mint(accounts[0], 100.u256)
74+
75+
check (await token.allowance(accounts[0], accounts[1])) == 0.u256
76+
check (await token.balanceOf(accounts[0])) == 100.u256
77+
check (await token.balanceOf(accounts[1])) == 0.u256
78+
79+
discard await token.increaseAllowance(accounts[1], 50.u256)
80+
81+
check (await token.allowance(accounts[0], accounts[1])) == 50.u256
82+
check (await token.balanceOf(accounts[0])) == 100.u256
83+
check (await token.balanceOf(accounts[1])) == 0.u256
84+
85+
discard await token.increaseAllowance(accounts[1], 50.u256)
86+
87+
check (await token.allowance(accounts[0], accounts[1])) == 100.u256
88+
check (await token.balanceOf(accounts[0])) == 100.u256
89+
check (await token.balanceOf(accounts[1])) == 0.u256
90+
91+
discard await token.decreaseAllowance(accounts[1], 50.u256)
92+
93+
check (await token.allowance(accounts[0], accounts[1])) == 50.u256
94+
check (await token.balanceOf(accounts[0])) == 100.u256
95+
check (await token.balanceOf(accounts[1])) == 0.u256
96+
97+
7298
test "transferFrom tokens":
7399
let senderAccount = accounts[0]
74100
let receiverAccount = accounts[1]

0 commit comments

Comments
 (0)