From 620b402a7d33385ae8e2cb5677e9f6dce9724acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Mon, 16 Oct 2023 10:23:58 +0200 Subject: [PATCH] feat: (de/in)crease allowance (#56) --- ethers/erc20.nim | 18 ++++++++++++++++++ testmodule/testErc20.nim | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/ethers/erc20.nim b/ethers/erc20.nim index eaaa79c..57f0cbf 100644 --- a/ethers/erc20.nim +++ b/ethers/erc20.nim @@ -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, diff --git a/testmodule/testErc20.nim b/testmodule/testErc20.nim index 4944efc..3643820 100644 --- a/testmodule/testErc20.nim +++ b/testmodule/testErc20.nim @@ -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]