-
Notifications
You must be signed in to change notification settings - Fork 91
refactor: check for contract deployment before creating pn #950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| public constructor( | ||
| public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.CONTENT_DATA, | ||
| public currentVersion: string = CURRENT_VERSION, | ||
| ) { | ||
| super(ExtensionTypes.TYPE.CONTENT_DATA, extensionId, currentVersion); | ||
| public constructor() { | ||
| super(ExtensionTypes.TYPE.CONTENT_DATA, ExtensionTypes.ID.CONTENT_DATA, CURRENT_VERSION); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type of change made for classes that never get extended
|
|
||
| export default abstract class AnyToNativeTokenPaymentNetwork extends FeeReferenceBasedPaymentNetwork { | ||
| public constructor( | ||
| protected constructor( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abstract class constructors should be protected
818873a to
9e7a533
Compare
| /** | ||
| * Handle payment detection for native token payment with conversion | ||
| */ | ||
| export abstract class AnyToNativeDetector extends AnyToAnyDetector< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New abstract class for all (future) any-to-native. Did the same for native-token
| import Erc20InfoRetriever from './address-based-info-retriever'; | ||
|
|
||
| import { PaymentDetectorBase } from '../payment-detector-base'; | ||
| const supportedNetworks = ['mainnet', 'rinkeby', 'goerli', 'private']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please challenge this. Here we're in the payment-detection package, so the context is not exactly the same. I don't think it makes sense to have a fixed array of supportedNetworks here either, but I may be wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. If there was such a limitation, it would be in the info-retriever.
Maybe throwing if no (default) provider exists for the network, within getTransferEvents(), would maintain clarity for what works and what does not? And the next step depends on what this provider returns if we query a wider range of blocks than it allows, specifically, if it allows {fromBlock: 0, toBlock: 'latest'}.
As we expect the result to be incomplete in most cases, while getTransferEvents() should be TheGraph-based, shouldn't we add a console.warn for address-based info retrieval?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Let me try to do something about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After further digging, I found out that getDefaultProvider already throws whenever a provider cannot be instantiated for the network, so that was already taken care of! This throws:
| return providers.getDefaultProvider(network, providersApiKeys); |
As you advised I added a warning every time we use
getTransferEvents from an ERC20InfoRetriever, done here: 8675519 👍
| }) { | ||
| super( | ||
| PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE, | ||
| advancedLogic.extensions.anyToNativeToken[0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to specify an index like this anymore. The routing is now done automatically in the constructor of the new parent class AnyToNativeDetector, using the new getAnyToNativeTokenExtensionForNetwork method of the AdvancedLogic class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good news
| protected readonly extension: TExtension, | ||
| protected constructor( | ||
| public readonly paymentNetworkId: PaymentTypes.PAYMENT_NETWORK_ID, | ||
| public readonly extension: TExtension, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public because now needed in the factory
| if (detector.extension && 'getDeploymentInformation' in detectorClass) { | ||
| // this throws when the contract isn't deployed and was mandatory for payment detection | ||
| (detectorClass as ContractBasedDetector).getDeploymentInformation( | ||
| network, | ||
| detector.extension.currentVersion, | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main change, please review this 😇 We let the detector choose wether we should throw or not for an unsupported network. Do you think that is OK?
| expect(paymentNetworkFactory.getPaymentNetworkFromRequest(request)).toBeInstanceOf( | ||
| NearConversionNativeTokenPaymentDetector, | ||
| ); | ||
| expect( | ||
| paymentNetworkFactory.getPaymentNetworkFromRequest({ | ||
| ...request, | ||
| currency: { ...request.currency, network: 'aurora-testnet' }, | ||
| }), | ||
| ).toBeInstanceOf(NearConversionNativeTokenPaymentDetector); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The network was missing, and since we're not mocking advancedLogic in this test file anymore, the test wasn't working
| const paymentDetector = new NearNativeTokenPaymentDetector({ | ||
| advancedLogic: mockAdvancedLogic, | ||
| network: 'aurora', | ||
| advancedLogic: advancedLogic, | ||
| currencyManager: CurrencyManager.getDefault(), | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
| // FIXME: should be Extension.PnReferenceBased.IReferenceBased<Extension.PnStreamReferenceBased.ICreationParameters> | ||
| erc777Stream: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was messing with some files if I remember well, an issue between the interface and the implementation. Will try to fix it in a consecutive PR
| getAnyToNativeTokenExtensionForNetwork: ( | ||
| network: string, | ||
| ) => Extension.IExtension<Extension.PnAnyToEth.ICreationParameters> | undefined; | ||
| extensions: IAdvancedLogicExtensions; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properly typed the extensions property. This way we can quickly know if our PN interfaces (types) match our PN implementations (classes).
| parameters: IAddRefundInstructionParameters, | ||
| ) => Extension.IAction; | ||
| createCreationAction: (parameters?: TCreationParameters) => Extension.IAction; | ||
| createCreationAction: (parameters: TCreationParameters) => Extension.IAction; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interface was not compatible with the actual class implementation. I fixed it that way, let me know if you would have done otherwise.
benjlevesque
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
amazing! great work. Happy to see those supportedNetworks go away 👍
| }) { | ||
| super( | ||
| PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE, | ||
| advancedLogic.extensions.anyToNativeToken[0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good news
yomarion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really clean, well done.
|
|
||
| private isValidMainnetAddress(address: string): boolean { | ||
| protected isValidAddress(address: string): boolean { | ||
| return this.isValidAddressForSymbolAndNetwork(address, 'NEAR', 'aurora'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we get rid of the testnet addresses validation. FYI they have different validation rules compared to mainnet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Near testnet now has its separate class:
requestNetwork/packages/advanced-logic/src/extensions/payment-network/near/any-to-near-testnet.ts
Line 17 in e57b267
| return this.isValidAddressForSymbolAndNetwork(address, 'NEAR-testnet', 'aurora-testnet'); |
| import Erc20InfoRetriever from './address-based-info-retriever'; | ||
|
|
||
| import { PaymentDetectorBase } from '../payment-detector-base'; | ||
| const supportedNetworks = ['mainnet', 'rinkeby', 'goerli', 'private']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. If there was such a limitation, it would be in the info-retriever.
Maybe throwing if no (default) provider exists for the network, within getTransferEvents(), would maintain clarity for what works and what does not? And the next step depends on what this provider returns if we query a wider range of blocks than it allows, specifically, if it allows {fromBlock: 0, toBlock: 'latest'}.
As we expect the result to be incomplete in most cases, while getTransferEvents() should be TheGraph-based, shouldn't we add a console.warn for address-based info retrieval?
| nativeToken: [new NearNative(), new NearTestnetNative()], | ||
| anyToNativeToken: [new AnyToNear(currencyManager), new AnyToNearTestnet(currencyManager)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we go down that line, can we add anyToEthProxy and feeProxyContractEth to these 2 generic PNs? And deprecate the "separate" use of these extensions? It does not add anything while we just have Near.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree! We should deprecate any-to-eth-proxy and instead use any-to-native-token. Not sure however that eth-fee-proxy-contract and native-token are compatible as the latest doesn't support fees as far as I know. I'll keep this PR a refactor without changing payment networks, but let's keep this in mind for the next steps.
ff4aac6 to
e57154b
Compare
supersedes #945 and #943
Context
The
advanced-logiclayer is a stateless singleton that contains an instance of all payment networks (PN). When created (see its class), it has no knowledge of what chain(s) it will be used on. It only needs an instance of the currency manager. It has no other dependency.Issue / Solution
It is a pain to manually update PN whenever we want them to support a new chain, especially when it could be done automatically via our smart contract artifacts. Without adding new dependencies on the
advanced-logicobject, this can be done from thepayment-detectionlayer. PN should have zero knowledge of what chain(s) they support. Routing from the chain/version to the payment network should be done during the instantiation of the payment detector.From this point:
isValidAddressin PNs now only accepts an address (no morenetworkparameter), this means:supportedNetworksparameter anymore. Classes that extendnative-tokenandany-to-nativestill have asupportedNetworksarray as they need some kind of deeper level of routing. However, those are supper specific and not supposed to change that often once they are set.advanced-logic, like we did inpayment-detectionanywhenever possible)Ideally, those changes should not impact the behavior of the library when used for payment detection.