diff --git a/src/RelayServer.ts b/src/RelayServer.ts index 0a8afd0..3d82899 100644 --- a/src/RelayServer.ts +++ b/src/RelayServer.ts @@ -60,8 +60,9 @@ import { convertGasToTokenAndNative, calculateFee, validateExpirationTime, - callVerifierMethod, queryVerifiers, + getAcceptedTokensFromVerifier, + getAcceptedContractsFromVerifier, } from './relayServerUtils'; import { getPastEventsForHub } from './getPastEventsForHub'; import type { PastEventOptions } from './definitions'; @@ -215,9 +216,8 @@ export class RelayServer extends EventEmitter { const res: HandlerResponse = {}; for (const verifier of verifiers) { - res[utils.getAddress(verifier)] = await callVerifierMethod( - verifier, - 'Token' + res[utils.getAddress(verifier)] = await getAcceptedTokensFromVerifier( + verifier ); } @@ -231,9 +231,8 @@ export class RelayServer extends EventEmitter { const res: HandlerResponse = {}; for (const verifier of verifiers) { - res[utils.getAddress(verifier)] = await callVerifierMethod( - verifier, - 'Contract' + res[utils.getAddress(verifier)] = await getAcceptedContractsFromVerifier( + verifier ); } diff --git a/src/relayServerUtils.ts b/src/relayServerUtils.ts index 29e308e..2f66cd4 100644 --- a/src/relayServerUtils.ts +++ b/src/relayServerUtils.ts @@ -396,28 +396,38 @@ async function validateExpirationTime( } } -type VerifierQueries = 'Token' | 'Contract'; +async function getAcceptedContractsFromVerifier( + verifier: string +): Promise { + try { + const provider = getProvider(); + + const handler = DestinationContractHandler__factory.connect( + verifier, + provider + ); + + return await handler.getAcceptedContracts(); + } catch (error) { + log.warn( + `Couldn't get accepted contracts from verifier ${verifier}`, + error + ); + } -async function callVerifierMethod( - verifier: string, - type: VerifierQueries + return []; +} + +async function getAcceptedTokensFromVerifier( + verifier: string ): Promise { try { const provider = getProvider(); - if (type === 'Token') { - const handler = TokenHandler__factory.connect(verifier, provider); - - return await handler.getAcceptedTokens(); - } else { - const handler = DestinationContractHandler__factory.connect( - verifier, - provider - ); - - return await handler.getAcceptedContracts(); - } + const handler = TokenHandler__factory.connect(verifier, provider); + + return await handler.getAcceptedTokens(); } catch (error) { - log.warn(`Verifier ${verifier} failed while query ${type} `, error); + log.warn(`Couldn't get accepted tokens from verifier ${verifier}`, error); } return []; @@ -446,6 +456,7 @@ export { TRANSFER_HASH, TRANSFER_FROM_HASH, validateExpirationTime, - callVerifierMethod, queryVerifiers, + getAcceptedContractsFromVerifier, + getAcceptedTokensFromVerifier, }; diff --git a/test/unit/RelayServer.test.ts b/test/unit/RelayServer.test.ts index 8904572..3ec9a8b 100644 --- a/test/unit/RelayServer.test.ts +++ b/test/unit/RelayServer.test.ts @@ -350,7 +350,7 @@ describe('RelayServer tests', function () { queryVerifiersSpy = sinon.spy(relayServerUtils, 'queryVerifiers'); callVerifierMehodStub = sinon.stub( relayServerUtils, - 'callVerifierMethod' + 'getAcceptedTokensFromVerifier' ); callVerifierMehodStub.returns(addressArray); trustedVerifiers = new Set(); @@ -379,7 +379,7 @@ describe('RelayServer tests', function () { expect(result).to.be.deep.equal(expectedResult); expect(queryVerifiersSpy).to.be.calledOnce; for (const v of trustedVerifiers) { - expect(callVerifierMehodStub).to.be.calledWithExactly(v, 'Token'); + expect(callVerifierMehodStub).to.be.calledWithExactly(v); } }); @@ -391,7 +391,7 @@ describe('RelayServer tests', function () { expect(result).to.be.deep.equal(expectedResult); expect(queryVerifiersSpy).to.be.calledOnce; - expect(callVerifierMehodStub).to.be.calledWithExactly(verifier, 'Token'); + expect(callVerifierMehodStub).to.be.calledWithExactly(verifier); }); }); @@ -406,7 +406,7 @@ describe('RelayServer tests', function () { queryVerifiersSpy = sinon.spy(relayServerUtils, 'queryVerifiers'); callVerifierMehodStub = sinon.stub( relayServerUtils, - 'callVerifierMethod' + 'getAcceptedContractsFromVerifier' ); callVerifierMehodStub.returns(addressArray); trustedVerifiers = new Set(); @@ -435,7 +435,7 @@ describe('RelayServer tests', function () { expect(result).to.be.deep.equal(expectedResult); expect(queryVerifiersSpy).to.be.calledOnce; for (const v of trustedVerifiers) { - expect(callVerifierMehodStub).to.be.calledWithExactly(v, 'Contract'); + expect(callVerifierMehodStub).to.be.calledWithExactly(v); } }); @@ -447,10 +447,7 @@ describe('RelayServer tests', function () { expect(result).to.be.deep.equal(expectedResult); expect(queryVerifiersSpy).to.be.calledOnce; - expect(callVerifierMehodStub).to.be.calledWithExactly( - verifier, - 'Contract' - ); + expect(callVerifierMehodStub).to.be.calledWithExactly(verifier); }); }); }); diff --git a/test/unit/relayServerUtils.test.ts b/test/unit/relayServerUtils.test.ts index 3a9e47b..e919514 100644 --- a/test/unit/relayServerUtils.test.ts +++ b/test/unit/relayServerUtils.test.ts @@ -620,7 +620,7 @@ describe('relayServerUtils tests', function () { }); }); - describe('Function callVerifierMethod()', function () { + describe('', function () { const addressArray = ['0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7']; const verifier = '0x155845fd06c85B7EA1AA2d030E1a747B3d8d15D7'; let getAcceptedTokens: SinonStub; @@ -642,46 +642,44 @@ describe('relayServerUtils tests', function () { .returns(contractHandler); }); - it('should return accepted tokens', async function () { - const tokens = await relayServerUtils.callVerifierMethod( - verifier, - 'Token' - ); + describe('getAcceptedTokensFromVerifier', function () { + it('should return accepted tokens', async function () { + const tokens = await relayServerUtils.getAcceptedTokensFromVerifier( + verifier + ); - expect(getAcceptedTokens).to.be.calledOnce; - expect(tokens).to.be.equal(addressArray); - }); + expect(getAcceptedTokens).to.be.calledOnce; + expect(tokens).to.be.equal(addressArray); + }); - it('should return accepted contracts', async function () { - const contracts = await relayServerUtils.callVerifierMethod( - verifier, - 'Contract' - ); + it('should return empty if it fails while retrieving accepted tokens', async function () { + getAcceptedTokens.throws(); + const tokens = await relayServerUtils.getAcceptedTokensFromVerifier( + verifier + ); - expect(getAcceptedContracts).to.be.calledOnce; - expect(contracts).to.be.equal(addressArray); + expect(getAcceptedTokens).to.be.calledOnce; + expect(tokens).to.be.empty; + }); }); - it('should return empty if it fails while retrieving accepted tokens', async function () { - getAcceptedTokens.throws(); - const tokens = await relayServerUtils.callVerifierMethod( - verifier, - 'Token' - ); + describe('getAcceptedContractsFromVerifier', function () { + it('should return accepted contracts', async function () { + const contracts = + await relayServerUtils.getAcceptedContractsFromVerifier(verifier); - expect(getAcceptedTokens).to.be.calledOnce; - expect(tokens).to.be.empty; - }); + expect(getAcceptedContracts).to.be.calledOnce; + expect(contracts).to.be.equal(addressArray); + }); - it('should return empty if it fails while retrieving accepted contracts', async function () { - getAcceptedContracts.throws(); - const contracts = await relayServerUtils.callVerifierMethod( - verifier, - 'Contract' - ); + it('should return empty if it fails while retrieving accepted contracts', async function () { + getAcceptedContracts.throws(); + const contracts = + await relayServerUtils.getAcceptedContractsFromVerifier(verifier); - expect(getAcceptedContracts).to.be.calledOnce; - expect(contracts).to.be.empty; + expect(getAcceptedContracts).to.be.calledOnce; + expect(contracts).to.be.empty; + }); }); });