Skip to content
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

Trustless EVM Bridge support #792

Merged
merged 120 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
307a5c4
refactoring utils bridge folder structure & exports
Nikita-Polyakov Sep 2, 2022
1899018
Merge remote-tracking branch 'origin/develop' into evm-bridge/pw-789
Nikita-Polyakov Sep 5, 2022
13158d6
update wallet integration
Nikita-Polyakov Sep 6, 2022
8d66316
import EthBridgeStates from wallet
Nikita-Polyakov Sep 6, 2022
51c6bad
add eth hashi bridge translations
Nikita-Polyakov Sep 6, 2022
0f7981c
transfer updateEthBridgeHistory function to settings
Nikita-Polyakov Sep 7, 2022
e538ac4
wip web3 refactoring
Nikita-Polyakov Sep 7, 2022
da1e7c3
wip update registered assets
Nikita-Polyakov Sep 8, 2022
761c6d1
change network in metamask
Nikita-Polyakov Sep 9, 2022
7091c5e
wip network select & change
Nikita-Polyakov Sep 9, 2022
c65f102
select network dialog refactoring
Nikita-Polyakov Sep 9, 2022
da9b808
evmAddress evmNetwork in storage
Nikita-Polyakov Sep 12, 2022
73702af
update bridge confirm dialog
Nikita-Polyakov Sep 12, 2022
e1283a8
wip update bridge view
Nikita-Polyakov Sep 12, 2022
7682615
prepare history item creation
Nikita-Polyakov Sep 13, 2022
e227bea
wip BridgeTransaction view
Nikita-Polyakov Sep 13, 2022
60dc857
wip Bridge class
Nikita-Polyakov Sep 14, 2022
de7478a
wip evm bridge transaction handler
Nikita-Polyakov Sep 15, 2022
f66d7e4
wip outgoing reducer
Nikita-Polyakov Sep 15, 2022
520f2dd
subscribe on tx details
Nikita-Polyakov Sep 16, 2022
dcfd854
update BridgeHistory view
Nikita-Polyakov Sep 16, 2022
f59686b
add evm hash container
Nikita-Polyakov Sep 19, 2022
e8a5216
update TransferNotification
Nikita-Polyakov Sep 19, 2022
b870031
update network selection
Nikita-Polyakov Sep 19, 2022
c62ca29
update select network dialog for history usage
Nikita-Polyakov Sep 28, 2022
200cb0b
add select network to bridge history
Nikita-Polyakov Sep 29, 2022
6501a63
show history from network
Nikita-Polyakov Sep 30, 2022
afa4ecf
update transfer flow
Nikita-Polyakov Sep 30, 2022
16aafa4
update history sync logic
Nikita-Polyakov Oct 1, 2022
844a436
refactoring sync
Nikita-Polyakov Oct 1, 2022
3727d60
wip bug fixes
Nikita-Polyakov Oct 3, 2022
b4729bd
refactoring state
Nikita-Polyakov Oct 4, 2022
0aadb41
improve removing local history
Nikita-Polyakov Oct 5, 2022
7be4dfd
update Moonpay integration
Nikita-Polyakov Oct 6, 2022
0b1df76
mock incoming reducer
Nikita-Polyakov Oct 6, 2022
2ee45bf
change selected evm network on moonpay history
Nikita-Polyakov Oct 6, 2022
890fe39
fix async process
Nikita-Polyakov Oct 7, 2022
a142d98
refactoring BridgeContainer
Nikita-Polyakov Oct 7, 2022
830cce3
add some chains to list
Nikita-Polyakov Oct 10, 2022
d531b7a
refactoring chain names
Nikita-Polyakov Oct 10, 2022
f606257
fix hashi bridge history restoration
Nikita-Polyakov Oct 10, 2022
de9ef19
update network changing for hashi bridge restoration
Nikita-Polyakov Oct 10, 2022
801895d
merge origin/develop
Nikita-Polyakov Oct 20, 2022
a4b2cca
resolve SelectToken conflicts
Nikita-Polyakov Oct 20, 2022
e40b0a0
Merge branch 'develop' into evm-bridge/pw-789
Nikita-Polyakov Oct 24, 2022
00fc5e1
merge origin/develop
Nikita-Polyakov Feb 7, 2023
65abc14
remove merge conflicts
Nikita-Polyakov Feb 7, 2023
f6b53e8
fix merge conflicts
Nikita-Polyakov Feb 7, 2023
92ebeb6
add sepolia to config
Nikita-Polyakov Feb 7, 2023
31f7278
update with library imports
Nikita-Polyakov Feb 7, 2023
7d08056
merge origin/develop
Nikita-Polyakov Mar 27, 2023
3b97cb6
merge origin/develop
Nikita-Polyakov Apr 5, 2023
d8f9889
remove merge conflicts
Nikita-Polyakov Apr 7, 2023
c0feaf4
merge origin/develop
Nikita-Polyakov Apr 24, 2023
49028b4
fix messages file
Nikita-Polyakov Apr 24, 2023
94c391c
Merge remote-tracking branch 'origin/develop' into evm-bridge/pw-789
Nikita-Polyakov Apr 24, 2023
65fc971
merge origin/develop
Nikita-Polyakov Apr 27, 2023
af1859e
fix merge conflicts
Nikita-Polyakov Apr 27, 2023
867d169
fix components names
Nikita-Polyakov Apr 27, 2023
f755dd4
wip select network
Nikita-Polyakov Apr 30, 2023
82f671d
resolve ts issues
Nikita-Polyakov May 2, 2023
7592881
return eth bridge settings
Nikita-Polyakov May 2, 2023
826ceda
update registered assets for network types
Nikita-Polyakov May 3, 2023
680954e
update history params
Nikita-Polyakov May 3, 2023
d4854fc
refactoring bridge classes
Nikita-Polyakov May 4, 2023
eb70eaa
refactoring bridge interfaces
Nikita-Polyakov May 4, 2023
7d517f1
return eth bridge sign actions
Nikita-Polyakov May 4, 2023
c305713
fix select asset mixin
Nikita-Polyakov May 4, 2023
77dcf67
wip update bridge history
Nikita-Polyakov May 4, 2023
8ec7d79
update history actions
Nikita-Polyakov May 4, 2023
863dbb3
start up bridge transaction
Nikita-Polyakov May 4, 2023
2268419
update bridge getters
Nikita-Polyakov May 4, 2023
f9f8323
fix network switch
Nikita-Polyakov May 5, 2023
c793425
update history states
Nikita-Polyakov May 5, 2023
8b14fff
refactoring bridge types
Nikita-Polyakov May 5, 2023
5e6e158
refactoring eth reducers
Nikita-Polyakov May 10, 2023
7a1c3fd
return approve from dev
Nikita-Polyakov May 10, 2023
856819c
return blocks left message
Nikita-Polyakov May 10, 2023
8c09446
refactoring translations
Nikita-Polyakov May 10, 2023
4a3d1ce
update external history
Nikita-Polyakov May 10, 2023
2e911ce
update select network dialog & icons
Nikita-Polyakov May 11, 2023
9bc2697
history refactoring
Nikita-Polyakov May 11, 2023
196c64f
networks disabled state
Nikita-Polyakov May 11, 2023
e277114
improve bridge transaction view
Nikita-Polyakov May 12, 2023
09ce041
change eth bridge storage key
Nikita-Polyakov May 12, 2023
d22b630
update wallet, remove unused translations
Nikita-Polyakov May 12, 2023
051bb14
refactoring bridge types
Nikita-Polyakov May 12, 2023
f46bf68
update translations from localise
Nikita-Polyakov May 12, 2023
e45dbef
update translations
Nikita-Polyakov May 12, 2023
be70600
merge origin/develop
Nikita-Polyakov May 12, 2023
5eb76ea
remove some todo
Nikita-Polyakov May 12, 2023
7efe212
refactoring eth history class
Nikita-Polyakov May 15, 2023
04e92e0
dont restore evm address
Nikita-Polyakov May 15, 2023
fd034c4
reduce method complexity
Nikita-Polyakov May 15, 2023
71d42d0
reduce complexity
Nikita-Polyakov May 15, 2023
3f057df
fetch balances right after account connec
Nikita-Polyakov May 15, 2023
b3b2b5a
remove unnecessary code from bridge reducers
Nikita-Polyakov May 16, 2023
04ff589
fix bug when restored transaction overrides in process
Nikita-Polyakov May 16, 2023
4267131
fix history restoration
Nikita-Polyakov May 16, 2023
820fb6a
revert transactions fetching logic
Nikita-Polyakov May 16, 2023
e99a094
merge origin/develop
Nikita-Polyakov May 16, 2023
d7a8935
refactoring tx date
Nikita-Polyakov May 16, 2023
25bdab5
update prod config
Nikita-Polyakov May 16, 2023
6ec525e
refactoring getRegisteredAssets
Nikita-Polyakov May 17, 2023
f12ff48
improve getRegisteredAssets update
Nikita-Polyakov May 17, 2023
948dec0
fix bug with lost balance
Nikita-Polyakov May 17, 2023
75d03fa
remove console.log
Nikita-Polyakov May 17, 2023
4a5148b
refactoring block production time usage
Nikita-Polyakov May 18, 2023
e02f898
update wallet
Nikita-Polyakov May 19, 2023
6b8cdd2
refactoring network icon
Nikita-Polyakov May 19, 2023
77fed01
fixes after review
Nikita-Polyakov May 22, 2023
757bd31
remove unused method
Nikita-Polyakov May 22, 2023
3ffddef
fix network change detection
Nikita-Polyakov May 22, 2023
f90130c
improve evm network switch
Nikita-Polyakov May 22, 2023
da3a541
refactoring smart contracts usage
Nikita-Polyakov May 22, 2023
b25c79f
remove unused imports
Nikita-Polyakov May 22, 2023
63a8c9a
fix allowance check
Nikita-Polyakov May 22, 2023
f540b74
fix approve btn message
Nikita-Polyakov May 22, 2023
e01b80e
refactoring getRegisteredAssets
Nikita-Polyakov May 22, 2023
8533f9f
update external balances
Nikita-Polyakov May 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip Bridge class
  • Loading branch information
Nikita-Polyakov committed Sep 14, 2022
commit 60dc8578e3d44d8858d89f595656801774fcb2ce
Empty file.
Empty file.
91 changes: 52 additions & 39 deletions src/utils/bridge/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,35 @@ type HandleTransactionPayload = {
type SignedEvmTxResult = SignTxResult;

type AddAsset = (address: string) => Promise<void>;
type SignEvm = (id: string) => Promise<SignedEvmTxResult>;
type GetAssetByAddress = (address: string) => Nullable<RegisteredAccountAssetWithDecimals>;
type GetActiveHistoryItem = () => Nullable<BridgeHistory>;
type GetBridgeHistoryInstance = () => Promise<EthBridgeHistory>;
type GetTransaction = (id: string) => BridgeHistory;
type ShowNotification = (tx: BridgeHistory) => void;
type SignEvm = (id: string) => Promise<SignedEvmTxResult>;

interface Constructable<T> {
new (...args: any): T;
}

type BridgeOperations =
| Operation.EthBridgeOutgoing
| Operation.EthBridgeIncoming
| Operation.EvmOutgoing
| Operation.EvmIncoming;

interface BridgeCommonOptions {
addAsset: AddAsset;
updateHistory: VoidFunction;
showNotification: ShowNotification;
getAssetByAddress: GetAssetByAddress;
getActiveHistoryItem: GetActiveHistoryItem;
getBridgeHistoryInstance: GetBridgeHistoryInstance;
getTransaction: GetTransaction;
showNotification: ShowNotification;
updateHistory: VoidFunction;
}

interface BridgeOptions extends BridgeCommonOptions {
interface BridgeConstructorOptions<BridgeReducer> extends BridgeCommonOptions {
reducers: Partial<Record<BridgeOperations, Constructable<BridgeReducer>>>;
sign: {
[Operation.EthBridgeOutgoing]: SignEvm;
[Operation.EthBridgeIncoming]: SignEvm;
Expand All @@ -66,6 +79,7 @@ class BridgeTransactionStateHandler {
protected readonly getAssetByAddress!: GetAssetByAddress;
protected readonly getActiveHistoryItem!: GetActiveHistoryItem;
protected readonly getBridgeHistoryInstance!: GetBridgeHistoryInstance;
protected readonly getTransaction!: GetTransaction;

constructor({
signEvm,
Expand All @@ -75,6 +89,7 @@ class BridgeTransactionStateHandler {
getAssetByAddress,
getActiveHistoryItem,
getBridgeHistoryInstance,
getTransaction,
}: BridgeReducerOptions) {
this.signEvm = signEvm;
this.addAsset = addAsset;
Expand All @@ -83,11 +98,16 @@ class BridgeTransactionStateHandler {
this.getAssetByAddress = getAssetByAddress;
this.getActiveHistoryItem = getActiveHistoryItem;
this.getBridgeHistoryInstance = getBridgeHistoryInstance;
this.getTransaction = getTransaction;
}

async changeState(tx: any): Promise<void> {
console.info('changeState implementation is required!');
}

async handleState(id: string, { status, nextState, rejectState, handler }: HandleTransactionPayload): Promise<void> {
try {
const transaction = getTransaction(id);
const transaction = this.getTransaction(id);

if (transaction.status === BridgeTxStatus.Done) return;
if (status && transaction.status !== status) {
Expand All @@ -102,7 +122,7 @@ class BridgeTransactionStateHandler {
} catch (error) {
console.error(error);

const transaction = getTransaction(id);
const transaction = this.getTransaction(id);
const failed = transaction.status === BridgeTxStatus.Failed;

this.updateTransactionParams(id, {
Expand All @@ -121,7 +141,7 @@ class BridgeTransactionStateHandler {

onComplete(id: string): void {
this.updateTransactionParams(id, { endTime: Date.now() });
const tx = getTransaction(id);
const tx = this.getTransaction(id);
const { type, assetAddress } = tx;
if (type === Operation.EthBridgeIncoming && assetAddress) {
const asset = this.getAssetByAddress(assetAddress);
Expand All @@ -148,7 +168,7 @@ class BridgeTransactionStateHandler {
async onEvmPending(id: string): Promise<void> {
await waitForEvmTransaction(id);

const tx = getTransaction(id);
const tx = this.getTransaction(id);
const { evmNetworkFee, blockHeight } = (await getEvmTransactionRecieptByHash(tx.ethereumHash as string)) || {};

if (!evmNetworkFee || !blockHeight) {
Expand All @@ -163,7 +183,7 @@ class BridgeTransactionStateHandler {
async onEvmSubmitted(id: string): Promise<void> {
this.updateTransactionParams(id, { transactionState: ETH_BRIDGE_STATES.EVM_PENDING });

const tx = getTransaction(id);
const tx = this.getTransaction(id);

if (!tx.ethereumHash) {
await this.beforeSubmit(id);
Expand Down Expand Up @@ -219,7 +239,7 @@ class EthBridgeOutgoingStateReducer extends BridgeTransactionStateHandler {
await this.beforeSubmit(id);
this.updateTransactionParams(id, { transactionState: ETH_BRIDGE_STATES.SORA_PENDING });

const { txId, blockId, to, amount, assetAddress } = getTransaction(id);
const { txId, blockId, to, amount, assetAddress } = this.getTransaction(id);

if (!amount) throw new Error('[Bridge]: TX "amount" cannot be empty');
if (!assetAddress) throw new Error('[Bridge]: TX "assetAddress" cannot be empty');
Expand Down Expand Up @@ -263,7 +283,7 @@ class EthBridgeOutgoingStateReducer extends BridgeTransactionStateHandler {

this.updateTransactionParams(id, { hash });

const tx = getTransaction(id);
const tx = this.getTransaction(id);

const { to } = await waitForApprovedRequest(tx);

Expand Down Expand Up @@ -383,7 +403,7 @@ class EthBridgeIncomingStateReducer extends BridgeTransactionStateHandler {
nextState: ETH_BRIDGE_STATES.SORA_COMMITED,
rejectState: ETH_BRIDGE_STATES.SORA_REJECTED,
handler: async (id: string) => {
const tx = getTransaction(id);
const tx = this.getTransaction(id);
const { hash, blockId } = await waitForIncomingRequest(tx);
this.updateTransactionParams(id, { hash, blockId });
},
Expand All @@ -410,35 +430,20 @@ class EthBridgeIncomingStateReducer extends BridgeTransactionStateHandler {
}
}

type BridgeReducer = EthBridgeOutgoingStateReducer | EthBridgeIncomingStateReducer;
class Bridge<BridgeReducer extends BridgeTransactionStateHandler> {
protected reducers!: Partial<Record<BridgeOperations, BridgeReducer>>;
protected readonly getTransaction!: GetTransaction;

type BridgeReducers = {
[Operation.EthBridgeOutgoing]: BridgeReducer;
[Operation.EthBridgeIncoming]: BridgeReducer;
};

class Bridge {
protected reducers!: BridgeReducers;

static readonly OPERATION_REDUCERS = {
[Operation.EthBridgeOutgoing]: EthBridgeOutgoingStateReducer,
[Operation.EthBridgeIncoming]: EthBridgeIncomingStateReducer,
};

constructor({ sign, ...rest }: BridgeOptions) {
this.reducers = Object.entries(Bridge.OPERATION_REDUCERS).reduce((acc, [operation, Reducer]) => {
if (!(operation in acc)) {
const reducer = new Reducer({ ...rest, signEvm: sign[operation] });

acc[operation] = reducer;
}
constructor({ reducers, sign, getTransaction, ...rest }: BridgeConstructorOptions<BridgeReducer>) {
this.getTransaction = getTransaction;
this.reducers = Object.entries(reducers).reduce((acc, [operation, Reducer]) => {
acc[operation] = new Reducer({ ...rest, getTransaction, signEvm: sign[operation] });
return acc;
}, {} as BridgeReducers);
}, {});
}

async handleTransaction(id: string) {
const transaction = getTransaction(id);

const transaction = this.getTransaction(id);
const { type } = transaction;

if (!(type in this.reducers)) {
Expand All @@ -447,21 +452,28 @@ class Bridge {

const reducer = this.reducers[type];

await this.process(transaction, reducer);
if (reducer) {
await this.process(transaction, reducer);
}
}

private async process(transaction: BridgeHistory, reducer: BridgeReducer) {
await reducer.changeState(transaction);

const tx = getTransaction(transaction.id as string);
const tx = this.getTransaction(transaction.id as string);

// TODO [EVM] pass stop states
if (![BridgeTxStatus.Done, BridgeTxStatus.Failed].includes(tx.status as BridgeTxStatus)) {
await this.process(tx, reducer);
}
}
}

// const appBridge = new Bridge({
// const appBridge = new Bridge<EthBridgeIncomingStateReducer | EthBridgeOutgoingStateReducer>({
// reducers: {
// [Operation.EthBridgeIncoming]: EthBridgeIncomingStateReducer,
// [Operation.EthBridgeOutgoing]: EthBridgeOutgoingStateReducer,
// },
// sign: {
// [Operation.EthBridgeIncoming]: (id: string) => store.dispatch.bridge.signEvmTransactionEvmToSora(id),
// [Operation.EthBridgeOutgoing]: (id: string) => store.dispatch.bridge.signEvmTransactionSoraToEvm(id),
Expand All @@ -472,6 +484,7 @@ class Bridge {
// getAssetByAddress: (address: string) => store.getters.assets.assetDataByAddress(address),
// getActiveHistoryItem: () => store.getters.bridge.historyItem,
// getBridgeHistoryInstance: () => store.dispatch.bridge.getEthBridgeHistoryInstance(),
// getTransaction,
// });

// export default appBridge;