Skip to content

Commit 24db86e

Browse files
committed
Add burn mechanism during the invocation of interactWithPost() method
1 parent eabb863 commit 24db86e

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

contracts/XRequestProcessor.sol

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import {XActionType} from './XActionType.sol';
99

1010
contract XRequestProcessor is Ownable {
1111
IERC20 private _paymentToken;
12+
uint8 private _burnPercentage;
1213

1314
mapping(XActionType => uint256) private _actionAmounts;
1415

16+
address public constant DEAD_ADDRESS = 0x000000000000000000000000000000000000dEaD;
17+
1518
constructor(address paymentToken) Ownable(msg.sender) {
1619
_paymentToken = IERC20(paymentToken);
20+
_burnPercentage = 50;
1721
}
1822

1923
modifier nonZeroAddress(address account) {
@@ -108,10 +112,18 @@ contract XRequestProcessor is Ownable {
108112
require(bytes(uriOrTicker).length <= 20, 'Invalid token ticker');
109113
}
110114

115+
// Calculate the payment and burn amounts
111116
uint256 paymentAmount = getActionPrice(actionType);
117+
uint256 burnAmount = (paymentAmount * _burnPercentage) / 100;
118+
uint256 contractAmount = paymentAmount - burnAmount;
112119

113-
// Safely transfer the required token amount from the user to this contract
114-
SafeERC20.safeTransferFrom(_paymentToken, msg.sender, address(this), paymentAmount);
120+
// Safely transfer the payment and burn amounts
121+
if (contractAmount > 0) {
122+
SafeERC20.safeTransferFrom(_paymentToken, msg.sender, address(this), contractAmount);
123+
}
124+
if (burnAmount > 0) {
125+
SafeERC20.safeTransferFrom(_paymentToken, msg.sender, DEAD_ADDRESS, burnAmount);
126+
}
115127

116128
// Get the current day since the Unix epoch, by dividing by 24 hours (in seconds)
117129
uint128 unixDay = uint128(block.timestamp / 86400);
@@ -135,6 +147,22 @@ contract XRequestProcessor is Ownable {
135147
emit PaymentTokenChanged(newToken);
136148
}
137149

150+
/**
151+
* @dev Returns the current burn percentage
152+
*/
153+
function getBurnPercentage() public view returns (uint8) {
154+
return _burnPercentage;
155+
}
156+
157+
/**
158+
* @dev Sets the burn percentage
159+
* @param newPercentage The new burn percentage (e.g., 25 for 25%)
160+
*/
161+
function setBurnPercentage(uint8 newPercentage) public onlyOwner {
162+
require(newPercentage <= 100, 'Burn percentage cannot exceed 100');
163+
_burnPercentage = newPercentage;
164+
}
165+
138166
/**
139167
* @dev Withdraw all tokens from the contract to the specified address
140168
* @param to Address to receive the tokens held by the contract

test/XRequestProcessor.test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ const XActionType = {
2222
TokenAnalysis: 5,
2323
}
2424

25+
/**
26+
* @param {bigint} totalAmount
27+
* @param {bigint} burnPercentage
28+
* @returns {[bigint, bigint]}
29+
*/
30+
const applyBurnPercentage = (totalAmount, burnPercentage) => {
31+
const burnAmount = (totalAmount * burnPercentage) / 100n
32+
const remainingAmount = totalAmount - burnAmount
33+
return [remainingAmount, burnAmount]
34+
}
35+
2536
describe(`XRequestProcessor tests`, function () {
2637
async function deployXRequestProcessorFixture() {
2738
const [owner, user1] = await ethers.getSigners()
@@ -179,8 +190,14 @@ describe(`XRequestProcessor tests`, function () {
179190
const userBalanceAfter = await tokenContract.balanceOf(user1.address)
180191
expect(userBalanceAfter).to.equal(0n)
181192

193+
const burnPercent = await processorContract.getBurnPercentage()
194+
const [paymentAmount, burnAmount] = applyBurnPercentage(repostAmount, burnPercent)
195+
182196
const processorContractBalance = await tokenContract.balanceOf(processorContractAddress)
183-
expect(processorContractBalance).to.equal(repostAmount)
197+
expect(processorContractBalance).to.equal(paymentAmount)
198+
199+
const deadAddressBalance = await tokenContract.balanceOf(await processorContract.DEAD_ADDRESS())
200+
expect(deadAddressBalance).to.equal(burnAmount)
184201
})
185202

186203
it('interactWithPost() - User should be able to invoke XActionType.TokenAnalysis actions', async function () {
@@ -305,6 +322,28 @@ describe(`XRequestProcessor tests`, function () {
305322
.reverted
306323
})
307324

325+
it('setBurnPercentage() - Regular user should NOT be able to change the burn percentage', async function () {
326+
const { user1, processorContract } = await loadFixture(deployXRequestProcessorFixture)
327+
328+
const newBurnPercent = 0n
329+
await expect(processorContract.connect(user1).setBurnPercentage(newBurnPercent)).to.be.reverted
330+
})
331+
332+
it('setBurnPercentage() - Owner should be able to change the burn percentage between 0-100', async function () {
333+
const { owner, processorContract } = await loadFixture(deployXRequestProcessorFixture)
334+
335+
const validPercentages = [0n, 15n, 25n, 50n, 80n, 100n]
336+
for (let percent of validPercentages) {
337+
await expect(processorContract.connect(owner).setBurnPercentage(percent)).not.to.be.reverted
338+
expect(await processorContract.getBurnPercentage(), percent)
339+
}
340+
341+
const invalidPercentages = [101n, 150n, 200n]
342+
for (let percent of invalidPercentages) {
343+
await expect(processorContract.connect(owner).setBurnPercentage(percent)).to.be.reverted
344+
}
345+
})
346+
308347
it('withdrawFunds() - Owner should be able to withdraw funds to a new address', async function () {
309348
const { owner, processorContract, tokenContract } = await loadFixture(
310349
deployXRequestProcessorFixture
@@ -315,13 +354,14 @@ describe(`XRequestProcessor tests`, function () {
315354
await tokenContract.connect(owner).approve(processorContractAddress, replyToThreadAmount)
316355
const xPostUri = 'https://x.com/SpaceX/status/1928107204931940365'
317356
await processorContract.connect(owner).interactWithPost(XActionType.ReplyToThread, xPostUri)
357+
const processorContractBalance = await tokenContract.balanceOf(processorContractAddress)
318358

319359
const otherWallet = ethers.Wallet.createRandom()
320360
await expect(processorContract.connect(owner).withdrawFunds(otherWallet.address)).not.to.be
321361
.reverted
322362

323363
const otherWalletBalance = await tokenContract.balanceOf(otherWallet.address)
324-
expect(otherWalletBalance).to.equal(replyToThreadAmount)
364+
expect(otherWalletBalance).to.equal(processorContractBalance)
325365
})
326366

327367
it('withdrawFunds() - Regular user should NOT be able to withdraw funds', async function () {

0 commit comments

Comments
 (0)