-
Notifications
You must be signed in to change notification settings - Fork 83
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
refactor: chain manager WIP #1352
Draft
alexandre-abrioux
wants to merge
17
commits into
master
Choose a base branch
from
chain-manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d28ae8f
refactor: chain manager WIP
alexandre-abrioux 4a7dad8
fixed advanced logic and smart contracts
alexandre-abrioux 49346b9
Merge remote-tracking branch 'origin/master' into chain-manager
alexandre-abrioux 7cc2bcb
WIP
alexandre-abrioux 86d1c72
fix payment processor
alexandre-abrioux fe631c9
fix build errors
alexandre-abrioux 4a54066
Merge remote-tracking branch 'origin' into chain-manager
alexandre-abrioux 3f30469
fix build error
alexandre-abrioux fe52066
fix lint
alexandre-abrioux c6b8cee
fix test
alexandre-abrioux 5cefa05
fix type issues in tests
alexandre-abrioux 101b5e3
fix unrecognized extension
alexandre-abrioux c1621b9
fix more tests related to chain initialization
alexandre-abrioux 1ef0da3
wip fixing currency manager --> chains as objects
alexandre-abrioux 5311775
fix more build errors
alexandre-abrioux b410f47
update error messages in tests
alexandre-abrioux 05a1c9c
Merge remote-tracking branch 'origin' into chain-manager
alexandre-abrioux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { | ||
AdvancedLogicTypes, | ||
CurrencyTypes, | ||
ChainTypes, | ||
ExtensionTypes, | ||
IdentityTypes, | ||
RequestLogicTypes, | ||
} from '@requestnetwork/types'; | ||
import { ICurrencyManager, NearChains, isSameChain } from '@requestnetwork/currency'; | ||
import { ICurrencyManager } from '@requestnetwork/currency'; | ||
|
||
import ContentData from './extensions/content-data'; | ||
import AddressBasedBtc from './extensions/payment-network/bitcoin/mainnet-address-based'; | ||
|
@@ -27,11 +27,34 @@ import NativeToken from './extensions/payment-network/native-token'; | |
import AnyToNative from './extensions/payment-network/any-to-native'; | ||
import Erc20TransferableReceivablePaymentNetwork from './extensions/payment-network/erc20/transferable-receivable'; | ||
|
||
const { ECOSYSTEM, VM_ECOSYSTEMS } = ChainTypes; | ||
|
||
/** | ||
* Module to manage Advanced logic extensions | ||
* Package to route the format and parsing of extensions following their id | ||
*/ | ||
export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic { | ||
public static supportedEcosystemsForExtension: Record< | ||
ExtensionTypes.ID, | ||
readonly ChainTypes.ECOSYSTEM[] | ||
> = { | ||
[ExtensionTypes.ID.CONTENT_DATA]: [ECOSYSTEM.EVM], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.BITCOIN_ADDRESS_BASED]: [ECOSYSTEM.BTC], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.TESTNET_BITCOIN_ADDRESS_BASED]: [ECOSYSTEM.BTC], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_DECLARATIVE]: [ECOSYSTEM.BTC], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_ADDRESS_BASED]: VM_ECOSYSTEMS, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_PROXY_CONTRACT]: VM_ECOSYSTEMS, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_FEE_PROXY_CONTRACT]: VM_ECOSYSTEMS, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC777_STREAM]: [ECOSYSTEM.EVM], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ETH_INPUT_DATA]: [ECOSYSTEM.EVM], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN]: [ECOSYSTEM.NEAR], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY]: VM_ECOSYSTEMS, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ETH_FEE_PROXY_CONTRACT]: [ECOSYSTEM.EVM], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY]: [ECOSYSTEM.EVM], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE_TOKEN]: [ECOSYSTEM.NEAR], | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_TRANSFERABLE_RECEIVABLE]: [ECOSYSTEM.EVM], | ||
}; | ||
|
||
/** Give access to the functions specific of the extensions supported */ | ||
public extensions: { | ||
addressBasedBtc: AddressBasedBtc; | ||
|
@@ -108,8 +131,23 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic | |
requestState: RequestLogicTypes.IRequest, | ||
): ExtensionTypes.IExtension { | ||
const id: ExtensionTypes.ID = extensionAction.id; | ||
const network = this.getNetwork(extensionAction, requestState) || requestState.currency.network; | ||
const extension: ExtensionTypes.IExtension | undefined = { | ||
const ecosystems = AdvancedLogic.supportedEcosystemsForExtension[id]; | ||
if (!ecosystems) { | ||
throw Error(`chain ecosystem not recognized for extension: ${id}`); | ||
} | ||
const chain = this.getChainForActionAndState(extensionAction, requestState, ecosystems); | ||
const extensions = this.getExtensionsForChain(chain); | ||
const extension = extensions[id]; | ||
if (!extension) { | ||
throw Error(`extension with id: ${id} not found for network: ${chain}`); | ||
} | ||
return extension; | ||
} | ||
|
||
public getExtensionsForChain( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason this was made public? |
||
chain?: ChainTypes.IChain, | ||
): Record<ExtensionTypes.ID, ExtensionTypes.IExtension | undefined> { | ||
const extensions: Record<ExtensionTypes.ID, ExtensionTypes.IExtension | undefined> = { | ||
[ExtensionTypes.ID.CONTENT_DATA]: this.extensions.contentData, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.BITCOIN_ADDRESS_BASED]: this.extensions.addressBasedBtc, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.TESTNET_BITCOIN_ADDRESS_BASED]: | ||
|
@@ -118,36 +156,36 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic | |
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_ADDRESS_BASED]: this.extensions.addressBasedErc20, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_PROXY_CONTRACT]: this.extensions.proxyContractErc20, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_FEE_PROXY_CONTRACT]: | ||
this.getFeeProxyContractErc20ForNetwork(network), | ||
this.getFeeProxyContractErc20ForNetwork(chain), | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC777_STREAM]: this.extensions.erc777Stream, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ETH_INPUT_DATA]: this.extensions.ethereumInputData, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN]: | ||
this.getNativeTokenExtensionForNetwork(network), | ||
this.getNativeTokenExtensionForNetwork(chain), | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY]: this.extensions.anyToErc20Proxy, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ETH_FEE_PROXY_CONTRACT]: | ||
this.extensions.feeProxyContractEth, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY]: this.extensions.anyToEthProxy, | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE_TOKEN]: | ||
this.getAnyToNativeTokenExtensionForNetwork(network), | ||
this.getAnyToNativeTokenExtensionForNetwork(chain), | ||
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_TRANSFERABLE_RECEIVABLE]: | ||
this.extensions.erc20TransferableReceivable, | ||
}[id]; | ||
|
||
if (!extension) { | ||
if ( | ||
id === ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN || | ||
id === ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE_TOKEN | ||
) { | ||
throw Error(`extension with id: ${id} not found for network: ${network}`); | ||
} | ||
|
||
throw Error(`extension not recognized, id: ${id}`); | ||
} | ||
return extension; | ||
}; | ||
// filter-out unsupported extensions for this chain ecosystem | ||
return (Object.keys(extensions) as ExtensionTypes.ID[]).reduce( | ||
(filteredExtensions, extensionId) => { | ||
filteredExtensions[extensionId] = | ||
chain && | ||
AdvancedLogic.supportedEcosystemsForExtension[extensionId].includes(chain.ecosystem) | ||
? extensions[extensionId] | ||
: undefined; | ||
return filteredExtensions; | ||
}, | ||
{} as Record<ExtensionTypes.ID, ExtensionTypes.IExtension | undefined>, | ||
); | ||
} | ||
|
||
public getNativeTokenExtensionForNetwork( | ||
network?: CurrencyTypes.ChainName, | ||
network?: ChainTypes.IChain, | ||
): ExtensionTypes.IExtension<ExtensionTypes.PnReferenceBased.ICreationParameters> | undefined { | ||
return network | ||
? this.extensions.nativeToken.find((nativeTokenExtension) => | ||
|
@@ -157,7 +195,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic | |
} | ||
|
||
public getAnyToNativeTokenExtensionForNetwork( | ||
network?: CurrencyTypes.ChainName, | ||
network?: ChainTypes.IChain, | ||
): AnyToNative | undefined { | ||
return network | ||
? this.extensions.anyToNativeToken.find((anyToNativeTokenExtension) => | ||
|
@@ -166,30 +204,46 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic | |
: undefined; | ||
} | ||
|
||
public getFeeProxyContractErc20ForNetwork(network?: string): FeeProxyContractErc20 { | ||
return NearChains.isChainSupported(network) | ||
public getFeeProxyContractErc20ForNetwork(network?: ChainTypes.IChain): FeeProxyContractErc20 { | ||
return this.currencyManager.chainManager.ecosystems[ECOSYSTEM.NEAR].isChainSupported(network) | ||
? new FeeProxyContractErc20(this.currencyManager, undefined, undefined, network) | ||
: this.extensions.feeProxyContractErc20; | ||
} | ||
|
||
protected getNetwork( | ||
private getChainForActionAndState( | ||
extensionAction: ExtensionTypes.IAction, | ||
requestState: RequestLogicTypes.IRequest, | ||
): CurrencyTypes.ChainName | undefined { | ||
ecosystems: readonly ChainTypes.ECOSYSTEM[], | ||
): ChainTypes.IChain | undefined { | ||
// This check is only used for the "native-token" extension | ||
// (notice the "extensionAction.parameters.paymentNetworkName" property). | ||
// This is important because "isSameChain" throws an error | ||
// when "extensionAction.parameters.paymentNetworkName" | ||
// and the chain supporting "requestState.currency" | ||
// are not part of the same ecosystem. | ||
// This should not happen in this case. | ||
if ( | ||
requestState.currency.network && | ||
extensionAction.parameters.paymentNetworkName && | ||
!isSameChain(requestState.currency.network, extensionAction.parameters.paymentNetworkName) | ||
!this.currencyManager.chainManager.isSameChain( | ||
requestState.currency.network, | ||
extensionAction.parameters.paymentNetworkName, | ||
ecosystems, | ||
) | ||
) { | ||
throw new Error( | ||
`Cannot apply action for network ${extensionAction.parameters.paymentNetworkName} on state with payment network: ${requestState.currency.network}`, | ||
`Cannot apply action for extension ${extensionAction.parameters.paymentNetworkName} on state with chain: ${requestState.currency.network}`, | ||
); | ||
} | ||
const network = | ||
extensionAction.action === 'create' | ||
|
||
const chainName = | ||
(extensionAction.action === 'create' | ||
? extensionAction.parameters.network | ||
: requestState.extensions[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE_TOKEN]?.values | ||
?.network; | ||
return network; | ||
?.network) || requestState.currency.network; | ||
|
||
if (!chainName) return; | ||
|
||
return this.currencyManager.chainManager.fromName(chainName, ecosystems); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
strange... CONTENT_DATA isn't a payment network...