Skip to content

Commit

Permalink
Merge branch 'feature/agnostic'
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Mar 18, 2024
2 parents 7e95191 + befff71 commit 667c5fe
Show file tree
Hide file tree
Showing 105 changed files with 1,824 additions and 1,225 deletions.
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/dist
**/build
dist/
build/
temp/
73 changes: 47 additions & 26 deletions integration/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,39 @@ import Web3 from 'web3';

import { TestConfig } from './testConfig';
import {
createDefaultTokenBridge,
loggerProvider,
TokenBridge,
TaquitoContractTezosBridgeBlockchainService,
Web3EtherlinkBridgeBlockchainService,
DefaultDataProvider,
BridgeTokenTransferKind, BridgeTokenTransferStatus, LogLevel,
type TokenBridge, type DefaultTokenBridgeOptions,
type TezosToken, type EtherlinkToken,
type BridgeTokenTransfer,
type PendingBridgeTokenDeposit, type CreatedBridgeTokenDeposit, type FinishedBridgeTokenDeposit,
type PendingBridgeTokenWithdrawal, type CreatedBridgeTokenWithdrawal,
type SealedBridgeTokenWithdrawal, type FinishedBridgeTokenWithdrawal
type SealedBridgeTokenWithdrawal, type FinishedBridgeTokenWithdrawal,
type DefaultDataProviderOptions,
} from '../src';

const tezosOperationRegex = /^o/;
const depositIdRegex = /^o[0-9a-zA-Z]{50}_\d+_\d+$/;
const withdrawalIdRegex = /^0x[0-9a-f]{64}_\d+$/;
const tezosOperationRegex = /^o[0-9a-zA-Z]{50}$/;
const etherlinkOperationRegex = /^0x[0-9a-f]{64}$/;

interface CreateTestTokenBridgeParams {
testConfig: TestConfig,
tezosToolkit?: TezosToolkit,
etherlinkToolkit?: Web3,
overrideOptions?: Partial<DefaultTokenBridgeOptions>
overriddenDefaultDataProviderOptions?: Partial<DefaultDataProviderOptions>
}

export const createTestTokenBridge = ({ testConfig, tezosToolkit, etherlinkToolkit, overrideOptions }: CreateTestTokenBridgeParams): TokenBridge => {
export const createTestTokenBridge = ({ testConfig, tezosToolkit, etherlinkToolkit, overriddenDefaultDataProviderOptions }: CreateTestTokenBridgeParams) => {
tezosToolkit = tezosToolkit || createTezosToolkitWithSigner(testConfig.tezosRpcUrl, testConfig.tezosAccountPrivateKey);
etherlinkToolkit = etherlinkToolkit || createEtherlinkToolkitWithSigner(testConfig.etherlinkRpcUrl, testConfig.etherlinkAccountPrivateKey);

return createDefaultTokenBridge({
logging: {
logLevel: LogLevel.Debug
},
tezos: {
toolkit: tezosToolkit,
rollupAddress: testConfig.tezosRollupAddress
},
etherlink: {
toolkit: etherlinkToolkit
},
loggerProvider.setLogLevel(LogLevel.Debug);

const defaultDataProvider = new DefaultDataProvider({
dipDup: {
baseUrl: testConfig.dipDupBaseUrl,
webSocketApiBaseUrl: testConfig.dipDupBaseUrl.replace('https', 'wss'),
Expand Down Expand Up @@ -76,7 +74,22 @@ export const createTestTokenBridge = ({ testConfig, tezosToolkit, etherlinkToolk
}
},
],
...overrideOptions
...overriddenDefaultDataProviderOptions
});

return new TokenBridge({
tezosBridgeBlockchainService: new TaquitoContractTezosBridgeBlockchainService({
tezosToolkit,
smartRollupAddress: testConfig.tezosRollupAddress
}),
etherlinkBridgeBlockchainService: new Web3EtherlinkBridgeBlockchainService({
web3: etherlinkToolkit
}),
bridgeDataProviders: {
transfers: defaultDataProvider,
balances: defaultDataProvider,
tokens: defaultDataProvider,
}
});
};

Expand Down Expand Up @@ -135,16 +148,18 @@ export const expectCreatedDeposit = (
}
) => {
expect(createdBridgeTokenDeposit).toMatchObject<CreatedBridgeTokenDeposit>({
id: expect.stringMatching(depositIdRegex),
kind: BridgeTokenTransferKind.Deposit,
status: BridgeTokenTransferStatus.Created,
source: params.source,
receiver: params.receiver,
tezosOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(tezosOperationRegex),
counter: expect.any(Number),
nonce: expect.any(Number),
amount: params.amount,
token: params.tezosToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
}
});
Expand All @@ -162,24 +177,26 @@ export const expectFinishedDeposit = (
}
) => {
expect(finishedBridgeTokenDeposit).toMatchObject<FinishedBridgeTokenDeposit>({
id: expect.stringMatching(depositIdRegex),
kind: BridgeTokenTransferKind.Deposit,
status: BridgeTokenTransferStatus.Finished,
source: params.source,
receiver: params.receiver,
tezosOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(tezosOperationRegex),
counter: expect.any(Number),
nonce: expect.any(Number),
amount: params.inAmount,
token: params.tezosToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
},
etherlinkOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(etherlinkOperationRegex),
logIndex: expect.any(Number),
amount: params.outAmount,
token: params.etherlinkToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
}
});
Expand Down Expand Up @@ -218,16 +235,17 @@ export const expectCreatedWithdrawal = (
}
) => {
expect(createdBridgeTokenWithdrawal).toMatchObject<CreatedBridgeTokenWithdrawal>({
id: expect.stringMatching(withdrawalIdRegex),
kind: BridgeTokenTransferKind.Withdrawal,
status: BridgeTokenTransferStatus.Created,
source: params.source,
receiver: params.receiver,
etherlinkOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(etherlinkOperationRegex),
logIndex: expect.any(Number),
amount: params.amount,
token: params.etherlinkToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
},
rollupData: {
Expand All @@ -247,23 +265,24 @@ export const expectSealedWithdrawal = (
}
) => {
expect(sealedBridgeTokenWithdrawal).toMatchObject<SealedBridgeTokenWithdrawal>({
id: expect.stringMatching(withdrawalIdRegex),
kind: BridgeTokenTransferKind.Withdrawal,
status: BridgeTokenTransferStatus.Sealed,
source: params.source,
receiver: params.receiver,
etherlinkOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(etherlinkOperationRegex),
logIndex: expect.any(Number),
amount: params.amount,
token: params.etherlinkToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
},
rollupData: {
outboxMessageIndex: expect.any(Number),
outboxMessageLevel: expect.any(Number),
commitment: expect.any(String),
proof: expect.any(String)
proof: expect.any(String),
}
});
};
Expand All @@ -280,24 +299,26 @@ export const expectFinishedWithdrawal = (
}
) => {
expect(finishedBridgeTokenWithdrawal).toMatchObject<FinishedBridgeTokenWithdrawal>({
id: expect.stringMatching(withdrawalIdRegex),
kind: BridgeTokenTransferKind.Withdrawal,
status: BridgeTokenTransferStatus.Finished,
source: params.source,
receiver: params.receiver,
etherlinkOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(etherlinkOperationRegex),
logIndex: expect.any(Number),
amount: params.inAmount,
token: params.etherlinkToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
},
tezosOperation: {
blockId: expect.any(Number),
hash: expect.stringMatching(tezosOperationRegex),
counter: expect.any(Number),
nonce: null,
amount: params.outAmount,
token: params.tezosToken,
fee: expect.any(BigInt),
timestamp: expect.any(String),
},
rollupData: {
Expand Down
12 changes: 6 additions & 6 deletions integration/tests/balances.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TezosToolkit } from '@taquito/taquito';
import Web3 from 'web3';

import { type TokenBridge, type AccountTokenBalance, type AccountTokenBalances } from '../../src';
import type { AccountTokenBalance, AccountTokenBalances } from '../../src';
import { getTestConfig, type TestConfig, type TestTokens } from '../testConfig';
import {
createTezosToolkitWithSigner, createEtherlinkToolkitWithSigner, createTestTokenBridge
Expand All @@ -12,7 +12,7 @@ describe('Balances', () => {
let tokens: TestTokens;
let tezosToolkit: TezosToolkit;
let etherlinkToolkit: Web3;
let tokenBridge: TokenBridge;
let tokenBridge: ReturnType<typeof createTestTokenBridge>;
let testTezosAccountAddress: string;
let testEtherlinkAccountAddress: string;

Expand All @@ -27,11 +27,11 @@ describe('Balances', () => {
tokenBridge = createTestTokenBridge({ testConfig, tezosToolkit, etherlinkToolkit });

const connectedAddresses = await Promise.all([
await tokenBridge.getTezosConnectedAddress(),
await tokenBridge.getEtherlinkConnectedAddress()
await tokenBridge.getTezosSignerAddress(),
await tokenBridge.getEtherlinkSignerAddress()
]);
testTezosAccountAddress = connectedAddresses[0];
testEtherlinkAccountAddress = connectedAddresses[1];
testTezosAccountAddress = connectedAddresses[0]!;
testEtherlinkAccountAddress = connectedAddresses[1]!;
});

afterEach(() => {
Expand Down
35 changes: 16 additions & 19 deletions integration/tests/deposit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Web3 from 'web3';

import {
BridgeTokenTransferStatus,
type NativeEtherlinkToken, type NativeTezosToken, type TokenBridge
type NativeEtherlinkToken, type NativeTezosToken
} from '../../src';
import { bridgeUtils } from '../../src/utils';
import { getTestConfig, type TestConfig, type TestTokens } from '../testConfig';
Expand All @@ -16,15 +16,12 @@ import {
createTestTokenBridge
} from '../testHelpers';

// The Taquito Wallet API does not close some handles after tests complete.
const useWalletApi = false;

describe('Deposit', () => {
let testConfig: TestConfig;
let tokens: TestTokens;
let tezosToolkit: TezosToolkit;
let etherlinkToolkit: Web3;
let tokenBridge: TokenBridge;
let tokenBridge: ReturnType<typeof createTestTokenBridge>;
let testTezosAccountAddress: string;
let testEtherlinkAccountAddress: string;

Expand All @@ -39,11 +36,11 @@ describe('Deposit', () => {
tokenBridge = createTestTokenBridge({ testConfig, tezosToolkit, etherlinkToolkit });

const connectedAddresses = await Promise.all([
await tokenBridge.getTezosConnectedAddress(),
await tokenBridge.getEtherlinkConnectedAddress()
await tokenBridge.getTezosSignerAddress(),
await tokenBridge.getEtherlinkSignerAddress()
]);
testTezosAccountAddress = connectedAddresses[0];
testEtherlinkAccountAddress = connectedAddresses[1];
testTezosAccountAddress = connectedAddresses[0]!;
testEtherlinkAccountAddress = connectedAddresses[1]!;
});

afterEach(() => {
Expand All @@ -54,7 +51,7 @@ describe('Deposit', () => {
const amount = 1_000_000n;
const [tezosToken, etherlinkToken]: [NativeTezosToken, NativeEtherlinkToken] = [tokens.tezos.tez, tokens.etherlink.tez];

const depositResult = await tokenBridge.deposit(amount, tezosToken, { useWalletApi });
const depositResult = await tokenBridge.deposit(amount, tezosToken);
expectPendingDeposit(depositResult.tokenTransfer, {
amount,
source: testTezosAccountAddress,
Expand All @@ -81,7 +78,7 @@ describe('Deposit', () => {
const amount = 7n;
const [tezosToken, etherlinkToken] = [tokens.tezos.ctez, tokens.etherlink.ctez];

const depositResult = await tokenBridge.deposit(amount, tezosToken, { useWalletApi });
const depositResult = await tokenBridge.deposit(amount, tezosToken);
expectPendingDeposit(depositResult.tokenTransfer, {
amount,
source: testTezosAccountAddress,
Expand All @@ -108,7 +105,7 @@ describe('Deposit', () => {
const amount = 20n;
const [tezosToken, etherlinkToken] = [tokens.tezos.usdt, tokens.etherlink.usdt];

const depositResult = await tokenBridge.deposit(amount, tezosToken, { useWalletApi });
const depositResult = await tokenBridge.deposit(amount, tezosToken);
expectPendingDeposit(depositResult.tokenTransfer, {
amount,
source: testTezosAccountAddress,
Expand Down Expand Up @@ -172,8 +169,8 @@ describe('Deposit', () => {
}
});

tokenBridge.deposit(amount, tezosToken, { useWalletApi })
.then(result => tokenBridge.stream.subscribeToTokenTransfer(result.tokenTransfer));
tokenBridge.deposit(amount, tezosToken)
.then(result => tokenBridge.stream.subscribeToOperationTokenTransfers(result.tokenTransfer));
});

test('Deposit FA1.2 token, check the transfer status using events (subscribeToAccountTransfers)', done => {
Expand All @@ -183,7 +180,7 @@ describe('Deposit', () => {
let tokenTransferOperationHash: string | undefined;

tokenBridge.addEventListener('tokenTransferCreated', tokenTransfer => {
if (bridgeUtils.getInitialOperationHash(tokenTransfer) !== tokenTransferOperationHash)
if (bridgeUtils.getInitialOperation(tokenTransfer).hash !== tokenTransferOperationHash)
return;

expectPendingDeposit(tokenTransfer, {
Expand All @@ -196,7 +193,7 @@ describe('Deposit', () => {
});

tokenBridge.addEventListener('tokenTransferUpdated', tokenTransfer => {
if (bridgeUtils.getInitialOperationHash(tokenTransfer) !== tokenTransferOperationHash)
if (bridgeUtils.getInitialOperation(tokenTransfer).hash !== tokenTransferOperationHash)
return;

if (tokenTransfer.status === BridgeTokenTransferStatus.Created) {
Expand All @@ -217,7 +214,7 @@ describe('Deposit', () => {
etherlinkToken
});
if (!readyForDone) {
done.fail('The tokenTransferCreated event has not been fired.');
done('The tokenTransferCreated event has not been fired.');
}

done();
Expand All @@ -226,9 +223,9 @@ describe('Deposit', () => {

tokenBridge.stream.subscribeToAccountTokenTransfers([testTezosAccountAddress, testEtherlinkAccountAddress]);

tokenBridge.deposit(amount, tezosToken, { useWalletApi })
tokenBridge.deposit(amount, tezosToken)
.then(depositResult => {
tokenTransferOperationHash = depositResult.depositOperation.hash;
tokenTransferOperationHash = depositResult.operationResult.hash;
});
});
});
12 changes: 6 additions & 6 deletions integration/tests/withdrawal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ describe('Withdrawal', () => {
tokenBridge = createTestTokenBridge({ testConfig, tezosToolkit, etherlinkToolkit });

const connectedAddresses = await Promise.all([
await tokenBridge.getTezosConnectedAddress(),
await tokenBridge.getEtherlinkConnectedAddress()
await tokenBridge.getTezosSignerAddress(),
await tokenBridge.getEtherlinkSignerAddress()
]);
testTezosAccountAddress = connectedAddresses[0];
testEtherlinkAccountAddress = connectedAddresses[1];
testTezosAccountAddress = connectedAddresses[0]!;
testEtherlinkAccountAddress = connectedAddresses[1]!;
});

afterEach(() => {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Withdrawal', () => {
});

const sealedBridgeTokenWithdrawal = await tokenBridge.waitForStatus(
startWithdrawResult.tokenTransfer,
createdBridgeTokenWithdrawal,
BridgeTokenTransferStatus.Sealed
);
expectSealedWithdrawal(sealedBridgeTokenWithdrawal, {
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('Withdrawal', () => {
});

const sealedBridgeTokenWithdrawal = await tokenBridge.waitForStatus(
startWithdrawResult.tokenTransfer,
createdBridgeTokenWithdrawal,
BridgeTokenTransferStatus.Sealed
);
expectSealedWithdrawal(sealedBridgeTokenWithdrawal, {
Expand Down
Loading

0 comments on commit 667c5fe

Please sign in to comment.