스마트컨트렉트 테스트에 Infinite Approve 적용하기
먼저 approve를 무한대로 해주는 infiniteApprove의 경우
Does the approve function on an ERC20 token need to be run once or before every relevant transaction?
I understand that an ERC20's approve() function has to be run before the token can be sent to another contract. But is this a one-time approval for msg.sender to be granted an approval status or do...
ethereum.stackexchange.com
해당 글에서 힌트를 얻을 수 있었다.
decimal = 18;
const infiniteVal: number = 2 ** 256 - 1;
const infiniteValBN: BigNumber = BigNumber.from(infiniteVal);
const bigIntTest: BigNumber = ethers.utils.parseUnits(
infiniteValBN.toString(),
decimal
);
그 결과 이런식으로 한번 테스트를 해봤는데 결과는
reason: 'overflow',
code: 'NUMERIC_FAULT',
fault: 'overflow',
operation: 'BigNumber.from',
value: 1.157920892373162e+77
이렇게 나왔다.
How to approve a token for spending on (Uniswap router contract)
Im trying to approve and later on swap my tokens on uniswap via web3py code. I am also using infura, not my own node. However, on both the swap and the approve I run into solidityErrors. The proble...
stackoverflow.com
보니까 web3 에선 위와 같이 되는것 같다.
https://github.com/ethers-io/ethers.js/discussions/2814
code: 'NUMERIC_FAULT', fault: 'overflow', operation: 'BigNumber.from' · Discussion #2814 · ethers-io/ethers.js
Hello! const tokenID = 96178001019380483532355422451609849948339356991030948866277126174730172245776; const someContract = new ethers.Contract(contractAddress, abi ,account) const result = await so...
github.com
보니까 bigNumber가 에러가 나는 것이므로 이때는 quote를 쓰라고 한다.
하지만 값 자체가 숫자형태로 안나오기 때문에 조금 더 찾아봤다.
https://forum.openzeppelin.com/t/using-the-maximum-integer-in-solidity/3000
Using the maximum integer in Solidity
There’s a variety of reasons you might want to know the maximum possible integer in solidity. One common use case is to approve a contract to transfer your tokens on your behalf: tokenContract.approve(exchangeContract, MAX_INT, { from: me }) Here I tell
forum.openzeppelin.com
오픈제플린 포럼에서 서치한 결과
const bigIntTest: BigNumber = ethers.utils.parseUnits(
"115792089237316195423570985008687907853269984665640564039457584007913129639935",
0
);
console.log("bigTest >>>", bigIntTest);
이렇게 하면 bignumber를 구할 수 있었다.
const bigIntTest: BigNumber = ethers.utils.parseUnits(
"115792089237316195423570985008687907853269984665640564039457584007913129639935",
0
);
it("test inifinite approve", async function () {
await erc20Token
.approve(ExampleContract.address, bigIntTest);
expect(
await erc20Token.allowance(userA.address, ExampleContract.address)
).equal(bigIntTest);
console.log("approve num", bigIntTest);
});
위와 같이 테스트 코드를 수정하니 infinite approve가 적용되었다.
어차피 고정값으로 넣으니 overflow가 나지 않게 처음부터 고정값으로 넣고 그 상태 그대로 bigNumber로 만들어서 입력값과 결과값체크에 사용하면 되는거엿다.