Skip to content

Commit

Permalink
Merge branch 'main' into WT-2017
Browse files Browse the repository at this point in the history
  • Loading branch information
imx-mikhala authored Feb 12, 2024
2 parents c565840 + 81d6f43 commit d5fc5d2
Show file tree
Hide file tree
Showing 83 changed files with 1,541 additions and 815 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.husky @immutable/sdk
/sdk @immutable/sdk
/packages/x-client @immutable/sdk
/packages/config @immutable/sdk
Expand Down
2 changes: 1 addition & 1 deletion .github/scripts/push-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fi
# Do not remove the trailing period!!!
# https://dev.to/ackshaey/macos-vs-linux-the-cp-command-will-trip-you-up-2p00
INPUT_SOURCE_FOLDER="./docs/."
INPUT_DESTINATION_REPO="immutable/imx-docs"
INPUT_DESTINATION_REPO="immutable/docs"
INPUT_DESTINATION_HEAD_BRANCH="ts-immutable-sdk-docs-$VERSION"
INPUT_DESTINATION_FOLDER="$CLONE_DIR/api-docs/sdk-references/ts-immutable-sdk/$VERSION"

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Checkout Docs Repo
uses: actions/checkout@v3
with:
repository: immutable/imx-docs
repository: immutable/docs
token: ${{ secrets.TS_IMMUTABLE_SDK_GITHUB_TOKEN }}
path: imx-docs
ref: main
Expand Down Expand Up @@ -67,8 +67,8 @@ jobs:
- name: Update version link
run: ./.github/scripts/update-docs-link.sh

- name: Push SDK Docs to imx-docs
id: docs_push
- name: Push SDK Docs to docs
id: docs_push
run: ./.github/scripts/push-docs.sh
shell: bash

Expand Down
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
yarn test --watchAll=false
33 changes: 31 additions & 2 deletions packages/checkout/sdk-sample-app/src/components/Cancel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Web3Provider } from '@ethersproject/providers';
import LoadingButton from './LoadingButton';
import { useEffect, useState } from 'react';
import { SuccessMessage, ErrorMessage } from './messages';
import { Box, FormControl, TextInput } from '@biom3/react';
import { Box, Checkbox, FormControl, TextInput } from '@biom3/react';

interface CancelProps {
checkout: Checkout;
Expand All @@ -13,7 +13,8 @@ interface CancelProps {
export default function Cancel({ checkout, provider }: CancelProps) {
const [orderId, setOrderId] = useState<string>('');
const [orderIdError, setOrderIdError] = useState<any>(null);
const [signActions, setSignActions] = useState<boolean>(false);
const [useGaslessCancel, setUseGaslessCancel] = useState<boolean>(false);
const [waitFulfillmentSettlements, setWaitFulfillmentSettlements] = useState<boolean>(true);
const [error, setError] = useState<any>(null);
const [loading, setLoading] = useState<boolean>(false);

Expand All @@ -36,6 +37,10 @@ export default function Cancel({ checkout, provider }: CancelProps) {
const result = await checkout.cancel({
provider,
orderIds: [orderId],
overrides: {
useGaslessCancel,
waitFulfillmentSettlements,
}
});
console.log('Cancel result', result);
setLoading(false);
Expand All @@ -54,6 +59,14 @@ export default function Cancel({ checkout, provider }: CancelProps) {
setOrderIdError('');
}

const updateUseGasless = (event: any) => {
setUseGaslessCancel(event.target.checked);
}

const updateWaitFulfillmentSettlements = (event: any) => {
setWaitFulfillmentSettlements(event.target.checked);
}

useEffect(() => {
setError(null);
setLoading(false);
Expand All @@ -69,6 +82,22 @@ export default function Cancel({ checkout, provider }: CancelProps) {
)}
</FormControl>
<br />
<FormControl>
<FormControl.Label>Use gasless cancel</FormControl.Label>
<Checkbox
checked={useGaslessCancel}
onChange={updateUseGasless}
/>
</FormControl>
<br />
<FormControl>
<FormControl.Label>Wait for fullfilment settlements</FormControl.Label>
<Checkbox
checked={waitFulfillmentSettlements}
onChange={updateWaitFulfillmentSettlements}
/>
</FormControl>
<br />
<LoadingButton onClick={cancelClick} loading={loading}>
Cancel
</LoadingButton>
Expand Down
4 changes: 4 additions & 0 deletions packages/checkout/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type {
CancelOverrides,
CancelResult,
CancelResultFailed,
CancelResultGasless,
CancelResultSuccess,
CancelResultFulfillmentsUnsettled,
CheckConnectionParams,
Expand All @@ -65,6 +66,7 @@ export type {
ERC20ItemRequirement,
ERC721Balance,
ERC721ItemRequirement,
FailedGaslessCancellation,
Fee,
FeePercentage,
FeeToken,
Expand Down Expand Up @@ -100,6 +102,7 @@ export type {
OnRampFundingStep,
OrderFee,
OnRampProviderFees,
PendingGaslessCancellation,
RemoteConfiguration,
RoutesFound,
RoutingOutcome,
Expand All @@ -117,6 +120,7 @@ export type {
SmartCheckoutResult,
SmartCheckoutRouter,
SmartCheckoutSufficient,
SuccessfulGaslessCancellation,
SwapFundingStep,
SwitchNetworkParams,
SwitchNetworkResult,
Expand Down
145 changes: 118 additions & 27 deletions packages/checkout/sdk/src/smartCheckout/cancel/cancel.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Web3Provider } from '@ethersproject/providers';
import { Environment } from '@imtbl/config';
import { OrderStatusName } from '@imtbl/orderbook';
import { PopulatedTransaction } from 'ethers';
import { PopulatedTransaction, TypedDataDomain } from 'ethers';
import { CheckoutConfiguration } from '../../config';
import { CheckoutError, CheckoutErrorType } from '../../errors';
import { CheckoutErrorType } from '../../errors';
import { cancel } from './cancel';
import { createOrderbookInstance } from '../../instance';
import { signFulfillmentTransactions } from '../actions';
Expand All @@ -17,25 +18,26 @@ jest.mock('../actions');
jest.mock('../../transaction');

describe('cancel', () => {
describe('cancel', () => {
let config: CheckoutConfiguration;
let mockProvider: Web3Provider;

beforeEach(() => {
mockProvider = {
getSigner: jest.fn().mockReturnValue({
getAddress: jest.fn().mockResolvedValue('0xADDRESS'),
}),
} as unknown as Web3Provider;
let config: CheckoutConfiguration;
let mockProvider: Web3Provider;

const mockedHttpClient = new HttpClient() as jest.Mocked<HttpClient>;
config = new CheckoutConfiguration({
baseConfig: { environment: Environment.SANDBOX },
}, mockedHttpClient);
beforeEach(() => {
mockProvider = {
getSigner: jest.fn().mockReturnValue({
getAddress: jest.fn().mockResolvedValue('0xADDRESS'),
_signTypedData: jest.fn().mockResolvedValue('0xSIGNED'),
}),
} as unknown as Web3Provider;

jest.spyOn(console, 'info').mockImplementation(() => {});
});
const mockedHttpClient = new HttpClient() as jest.Mocked<HttpClient>;
config = new CheckoutConfiguration({
baseConfig: { environment: Environment.SANDBOX },
}, mockedHttpClient);

jest.spyOn(console, 'info').mockImplementation(() => {});
});

describe('on chain cancel', () => {
it('should sign the cancel transaction', async () => {
const orderId = '1';
(createOrderbookInstance as jest.Mock).mockReturnValue({
Expand Down Expand Up @@ -193,14 +195,7 @@ describe('cancel', () => {

(createOrderbookInstance as jest.Mock).mockReturnValue({
cancelOrdersOnChain: jest.fn().mockRejectedValue(
new CheckoutError(
'An error occurred while cancelling the order listing',
CheckoutErrorType.CANCEL_ORDER_LISTING_ERROR,
{
orderId: '1',
message: 'An error occurred while cancelling the order listing',
},
),
new Error('Error from orderbook'),
),
});

Expand All @@ -218,8 +213,104 @@ describe('cancel', () => {

expect(message).toEqual('An error occurred while cancelling the order listing');
expect(type).toEqual(CheckoutErrorType.CANCEL_ORDER_LISTING_ERROR);
expect(data.error).toBeDefined();
expect(data.error.message).toEqual('Error from orderbook');
expect(data.orderId).toEqual('1');
});
});

describe('gasless cancel', () => {
it('should call gasless cancel and get the cancellations', async () => {
const orderId = '1';
(createOrderbookInstance as jest.Mock).mockReturnValue({
prepareOrderCancellations: jest.fn().mockResolvedValue({
signableAction: {
message: {
domain: {} as TypedDataDomain,
types: { types: [] },
value: { values: '' },
},
},
}),
cancelOrders: jest.fn().mockResolvedValue({
result: {
successful_cancellations: [
'018a8c71-d7e4-e303-a2ef-318871ef7756',
'458a8c71-d7e4-e303-a2ef-318871ef7778',
],
failed_cancellations: [
{
order: '458a8c71-d7e4-e303-a2ef-318871ef7790',
reason_code: 'FILLED',
},
{
order: '338a8c71-d7e4-e303-a2ef-318871ef7342',
reason_code: 'FILLED',
},
],
pending_cancellations: [
'238a8c71-d7e4-e303-a2ef-318871ef7778',
'898a8c71-d7e4-e303-a2ef-318871ef7735',
],
},
}),
});

const result = await cancel(config, mockProvider, [orderId], { useGaslessCancel: true });
expect(result).toEqual({
successfulCancellations: [
{
orderId: '018a8c71-d7e4-e303-a2ef-318871ef7756',
},
{
orderId: '458a8c71-d7e4-e303-a2ef-318871ef7778',
},
],
failedCancellations: [
{
orderId: '458a8c71-d7e4-e303-a2ef-318871ef7790',
reason: 'FILLED',
},
{
orderId: '338a8c71-d7e4-e303-a2ef-318871ef7342',
reason: 'FILLED',
},
],
pendingCancellations: [
{
orderId: '238a8c71-d7e4-e303-a2ef-318871ef7778',
},
{
orderId: '898a8c71-d7e4-e303-a2ef-318871ef7735',
},
],
});
});

it('should handle errors from orderbook', async () => {
const orderId = '1';

(createOrderbookInstance as jest.Mock).mockReturnValue({
prepareOrderCancellations: jest.fn().mockRejectedValue(
new Error('Error from orderbook'),
),
});

let message;
let type;
let data;

try {
await cancel(config, mockProvider, [orderId], { useGaslessCancel: true });
} catch (err: any) {
message = err.message;
type = err.type;
data = err.data;
}

expect(message).toEqual('An error occurred while cancelling the order listing');
expect(type).toEqual(CheckoutErrorType.CANCEL_ORDER_LISTING_ERROR);
expect(data.error.message).toEqual('Error from orderbook');
expect(data.orderIds).toEqual([orderId]);
});
});
});
Loading

0 comments on commit d5fc5d2

Please sign in to comment.