Skip to content

Commit e9e9345

Browse files
committed
Combine analyzeToken() and interactWithPost() methods to simplify contract usage
1 parent 84a065c commit e9e9345

File tree

3 files changed

+37
-54
lines changed

3 files changed

+37
-54
lines changed

contracts/XRequestProcessor.sol

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ contract XRequestProcessor is Ownable {
2424
event NewPostInteraction(
2525
address indexed payer,
2626
XActionType indexed actionType,
27-
string indexed xPostUri
27+
string uriOrTicker
2828
);
29-
event NewTokenAnalysis(address indexed payer, string indexed tokenTicker);
3029
event PaymentTokenChanged(address newToken);
3130

3231
/**
@@ -97,33 +96,23 @@ contract XRequestProcessor is Ownable {
9796
}
9897

9998
/**
100-
* @dev This function allows users to perform XActionType for the given postUri on X (Twitter)
99+
* @dev This function allows users to perform XActionType for the given postUri on X (Twitter) or $TICKER
101100
* @param actionType A value from XActionType enum
102-
* @param postUri A URI to a post in X (Twitter)
101+
* @param uriOrTicker A URI to a post in X (Twitter) or a token $TICKER
103102
*/
104-
function interactWithPost(XActionType actionType, string memory postUri) public {
105-
require(
106-
actionType != XActionType.TokenAnalysis,
107-
'TokenAnalysis action cannot be used with this function'
108-
);
103+
function interactWithPost(XActionType actionType, string memory uriOrTicker) public {
104+
require(bytes(uriOrTicker).length > 0, 'URI or TICKER cannot be empty');
109105

110-
uint256 paymentAmount = getActionPrice(actionType);
111-
112-
// Safely transfer the required token amount from the user to this contract
113-
SafeERC20.safeTransferFrom(_paymentToken, msg.sender, address(this), paymentAmount);
106+
if (actionType == XActionType.TokenAnalysis) {
107+
require(bytes(uriOrTicker).length <= 20, 'Invalid token ticker');
108+
}
114109

115-
emit NewPostInteraction(msg.sender, actionType, postUri);
116-
}
117-
118-
function analyzeToken(string memory tokenTicker) public {
119-
require(bytes(tokenTicker).length <= 20, 'Invalid token ticker');
120-
121-
uint256 paymentAmount = getActionPrice(XActionType.TokenAnalysis);
110+
uint256 paymentAmount = getActionPrice(actionType);
122111

123112
// Safely transfer the required token amount from the user to this contract
124113
SafeERC20.safeTransferFrom(_paymentToken, msg.sender, address(this), paymentAmount);
125114

126-
emit NewTokenAnalysis(msg.sender, tokenTicker);
115+
emit NewPostInteraction(msg.sender, actionType, uriOrTicker);
127116
}
128117

129118
/**

scripts/deploy.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const { collectActionAmounts, submitActionAmounts } = require('./modules/blockch
88

99
const cliReader = readline.createInterface({ input: process.stdin, output: process.stdout })
1010

11+
async function sleep(seconds) {
12+
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
13+
}
14+
1115
async function main() {
1216
const chainId = hre.network.config.chainId
1317
const rpcUrl = hre.network.config.url
@@ -53,6 +57,7 @@ async function main() {
5357
console.log()
5458

5559
console.log('Publishing specified action prices...')
60+
await sleep(5)
5661
const txData = await submitActionAmounts(actionsDto, requestProcessorAddress, walletSigner)
5762
console.log(`TX: ${txData.hash}`)
5863
}

test/XRequestProcessor.test.js

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,25 @@ describe(`XRequestProcessor tests`, function () {
183183
expect(processorContractBalance).to.equal(repostAmount)
184184
})
185185

186-
it('interactWithPost() - User should NOT be able to invoke actions without ERC20 balance', async function () {
187-
const { user1, processorContract, tokenContract } = await loadFixture(
186+
it('interactWithPost() - User should be able to invoke XActionType.TokenAnalysis actions', async function () {
187+
const { owner, user1, processorContract, tokenContract } = await loadFixture(
188188
deployXRequestProcessorFixture
189189
)
190190

191-
const spender = await processorContract.getAddress()
192-
await tokenContract.connect(user1).approve(spender, likeAmount)
191+
await tokenContract.connect(owner).transfer(user1.address, tokenAnalysisAmount)
192+
const processorContractAddress = await processorContract.getAddress()
193+
await tokenContract.connect(user1).approve(processorContractAddress, tokenAnalysisAmount)
193194

194-
const userBalance = await tokenContract.balanceOf(user1.address)
195-
expect(userBalance).to.equal(0n)
195+
const userBalanceBefore = await tokenContract.balanceOf(user1.address)
196+
expect(userBalanceBefore).to.equal(tokenAnalysisAmount)
196197

197-
const xPostUri = 'https://x.com/SpaceX/status/1928107204931940365'
198+
const ticker = 'GIANTAI'
198199
await expect(
199-
processorContract.connect(user1).interactWithPost(XActionType.Like, xPostUri)
200-
).to.be.revertedWithCustomError(tokenContract, 'ERC20InsufficientBalance')
200+
processorContract.connect(user1).interactWithPost(XActionType.TokenAnalysis, ticker)
201+
).not.to.be.reverted
201202
})
202203

203-
it('interactWithPost() - User should NOT be able to invoke XActionType.TokenAnalysis through this method', async function () {
204+
it('interactWithPost() - User should NOT be able to invoke XActionType.TokenAnalysis with invalid TICKER', async function () {
204205
const { owner, user1, processorContract, tokenContract } = await loadFixture(
205206
deployXRequestProcessorFixture
206207
)
@@ -212,13 +213,13 @@ describe(`XRequestProcessor tests`, function () {
212213
const userBalanceBefore = await tokenContract.balanceOf(user1.address)
213214
expect(userBalanceBefore).to.equal(tokenAnalysisAmount)
214215

215-
const xPostUri = 'https://x.com/SpaceX/status/1928107204931940365'
216+
const ticker = 'SUPER_LONG_AND_INVALID_TICKER_NAME'
216217
await expect(
217-
processorContract.connect(user1).interactWithPost(XActionType.TokenAnalysis, xPostUri)
218+
processorContract.connect(user1).interactWithPost(XActionType.TokenAnalysis, ticker)
218219
).to.be.reverted
219220
})
220221

221-
it('analyzeToken() - User should be able to invoke token analysis action', async function () {
222+
it('interactWithPost() - User should NOT be able to invoke actions with empty URI or TICKER', async function () {
222223
const { owner, user1, processorContract, tokenContract } = await loadFixture(
223224
deployXRequestProcessorFixture
224225
)
@@ -230,41 +231,29 @@ describe(`XRequestProcessor tests`, function () {
230231
const userBalanceBefore = await tokenContract.balanceOf(user1.address)
231232
expect(userBalanceBefore).to.equal(tokenAnalysisAmount)
232233

233-
await expect(processorContract.connect(user1).analyzeToken('GIANTAI')).to.emit(
234-
processorContract,
235-
'NewTokenAnalysis'
236-
)
237-
238-
const userBalanceAfter = await tokenContract.balanceOf(user1.address)
239-
expect(userBalanceAfter).to.equal(0n)
240-
241-
const processorContractBalance = await tokenContract.balanceOf(processorContractAddress)
242-
expect(processorContractBalance).to.equal(tokenAnalysisAmount)
234+
const xPostUri = ''
235+
await expect(
236+
processorContract.connect(user1).interactWithPost(XActionType.TokenAnalysis, xPostUri)
237+
).to.be.reverted
243238
})
244239

245-
it('analyzeToken() - User should NOT be able to invoke token analysis without ERC20 balance', async function () {
240+
it('interactWithPost() - User should NOT be able to invoke actions without ERC20 balance', async function () {
246241
const { user1, processorContract, tokenContract } = await loadFixture(
247242
deployXRequestProcessorFixture
248243
)
249244

250245
const spender = await processorContract.getAddress()
251-
await tokenContract.connect(user1).approve(spender, tokenAnalysisAmount)
246+
await tokenContract.connect(user1).approve(spender, likeAmount)
252247

253248
const userBalance = await tokenContract.balanceOf(user1.address)
254249
expect(userBalance).to.equal(0n)
255250

251+
const xPostUri = 'https://x.com/SpaceX/status/1928107204931940365'
256252
await expect(
257-
processorContract.connect(user1).analyzeToken('GIANTAI')
253+
processorContract.connect(user1).interactWithPost(XActionType.Like, xPostUri)
258254
).to.be.revertedWithCustomError(tokenContract, 'ERC20InsufficientBalance')
259255
})
260256

261-
it('analyzeToken() - Should throw when invalid token ticker is provided', async function () {
262-
const { user1, processorContract } = await loadFixture(deployXRequestProcessorFixture)
263-
264-
const invalidTicker = 'INVALID_SUPER_LONG_TICKER_NAME'
265-
await expect(processorContract.connect(user1).analyzeToken(invalidTicker)).to.be.reverted
266-
})
267-
268257
it('getPaymentTokenAddress() - Should return the ERC20 address set during deploy time', async function () {
269258
const { processorContract, tokenContract } = await loadFixture(deployXRequestProcessorFixture)
270259

0 commit comments

Comments
 (0)