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

test: compare amountProvided and amountReceived to quote #176

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 25 additions & 18 deletions validations/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ describe('/transfer', () => {
address: wallet.address,
}

it('able to transfer fiat in for crypto', async () => {
it('able to transfer in (fiat for crypto)', async () => {
const loginResult = await fiatConnectClient.login()
expect(loginResult.isOk).to.be.true

const quoteInResponse = await fiatConnectClient.createQuoteIn(
quoteInParams,
)
expect(quoteInResponse.isOk).to.be.true
const quoteId = quoteInResponse.unwrap().quote.quoteId
const { quoteId, cryptoAmount, fiatAmount } =
quoteInResponse.unwrap().quote

const addKycResult = await fiatConnectClient.addKyc(mockKYCInfo)
expect(addKycResult.isOk).to.be.true
Expand All @@ -71,7 +72,7 @@ describe('/transfer', () => {
idempotencyKey,
data: {
fiatAccountId: fiatAccountId,
quoteId: quoteId,
quoteId,
},
}

Expand All @@ -83,16 +84,19 @@ describe('/transfer', () => {
Object.values(TransferStatus),
)

const transferStatusResponse =
await fiatConnectClient.getTransferStatus({
transferId: transferInResponse.unwrap().transferId,
})
expect(transferStatusResponse.isOk).to.be.true
const transferStatusResult = await fiatConnectClient.getTransferStatus({
transferId: transferInResponse.unwrap().transferId,
})
expect(transferStatusResult.isOk).to.be.true
const transferStatusResponse = transferStatusResult.unwrap()
await checkObjectAgainstModel(
transferStatusResponse.unwrap(),
transferStatusResponse,
'TransferStatusResponse',
)

expect(transferStatusResponse.amountProvided).to.equal(fiatAmount)
expect(transferStatusResponse.amountReceived).to.equal(cryptoAmount)

const duplicateTransferResponse = await fiatConnectClient.transferIn(
transferInParams,
)
Expand Down Expand Up @@ -123,7 +127,7 @@ describe('/transfer', () => {
address: wallet.address,
}

it('able to transfer crypto in for fiat out', async () => {
it('able to transfer out (crypto for fiat)', async () => {
const loginResult = await fiatConnectClient.login()
expect(loginResult.isOk).to.be.true

Expand All @@ -140,13 +144,14 @@ describe('/transfer', () => {
quoteOutParams,
)
expect(quoteOutResponse.isOk).to.be.true
const quoteOutId = quoteOutResponse.unwrap().quote.quoteId
const { quoteId, cryptoAmount, fiatAmount } =
quoteOutResponse.unwrap().quote

const transferOutParams = {
idempotencyKey: randomUUID(),
data: {
fiatAccountId: fiatAccountId,
quoteId: quoteOutId,
quoteId,
},
}

Expand All @@ -162,15 +167,17 @@ describe('/transfer', () => {
/0x[a-fA-F0-9]{40}/,
)

const transferStatusResponse =
await fiatConnectClient.getTransferStatus({
transferId: transferOutResponse.unwrap().transferId,
})
expect(transferStatusResponse.isOk).to.be.true
const transferStatusResult = await fiatConnectClient.getTransferStatus({
transferId: transferOutResponse.unwrap().transferId,
})
expect(transferStatusResult.isOk).to.be.true
const transferStatusResponse = transferStatusResult.unwrap()
await checkObjectAgainstModel(
transferStatusResponse.unwrap(),
transferStatusResponse,
'TransferStatusResponse',
)
expect(transferStatusResponse.amountProvided).to.equal(cryptoAmount)
expect(transferStatusResponse.amountReceived).to.equal(fiatAmount)

const duplicateTransferResponse = await fiatConnectClient.transferOut(
transferOutParams,
Expand Down
51 changes: 45 additions & 6 deletions validations/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,22 @@ use(chaiPlugin({ apiDefinitionsPath }))
use(chaiAsPromised)

async function validateTransferWebhook(
params: { transferId: string; address: string; status: TransferStatus },
params: {
transferId: string
address: string
status: TransferStatus
expectedAmountProvided: string
expectedAmountReceived: string
},
retries: number,
): Promise<void> {
const { transferId, address, status } = params
const {
transferId,
address,
status,
expectedAmountReceived,
expectedAmountProvided,
} = params
if (retries < 0) {
throw new Error('Webhook event could not be found')
}
Expand All @@ -52,6 +64,27 @@ async function validateTransferWebhook(
)
return validateTransferWebhook(params, retries - 1)
}
response.data.forEach(
({
payload: { amountReceived, amountProvided },
}: WebhookRequestBodyTransferIn | WebhookRequestBodyTransferOut) => {
if (
amountProvided !== expectedAmountProvided ||
amountReceived !== expectedAmountReceived
) {
throw new Error(
`Webhook event has incorrect amountProvided or amountReceived. ${JSON.stringify(
{
amountProvided,
amountReceived,
expectedAmountProvided,
expectedAmountReceived,
},
)}`,
)
}
},
)
const correspondingWebhook = response.data.find(
(transfer: WebhookRequestBodyTransferIn | WebhookRequestBodyTransferOut) =>
transfer.payload.status === status,
Expand Down Expand Up @@ -140,7 +173,8 @@ describe('webhooks', () => {
quoteInParams,
)
expect(quoteInResponse.isOk).to.be.true
const quoteId = quoteInResponse.unwrap().quote.quoteId
const { quoteId, cryptoAmount, fiatAmount } =
quoteInResponse.unwrap().quote

const addKycResult = await fiatConnectClient.addKyc(mockKYCInfo)
expect(addKycResult.isOk).to.be.true
Expand All @@ -156,7 +190,7 @@ describe('webhooks', () => {
idempotencyKey,
data: {
fiatAccountId: fiatAccountId,
quoteId: quoteId,
quoteId,
},
}

Expand All @@ -181,6 +215,8 @@ describe('webhooks', () => {
transferId,
address: wallet.address,
status: transferStatusResponse.unwrap().status,
expectedAmountProvided: fiatAmount,
expectedAmountReceived: cryptoAmount,
},
2,
),
Expand Down Expand Up @@ -216,13 +252,14 @@ describe('webhooks', () => {
quoteOutParams,
)
expect(quoteOutResponse.isOk).to.be.true
const quoteOutId = quoteOutResponse.unwrap().quote.quoteId
const { quoteId, fiatAmount, cryptoAmount } =
quoteOutResponse.unwrap().quote

const transferOutParams = {
idempotencyKey: randomUUID(),
data: {
fiatAccountId: fiatAccountId,
quoteId: quoteOutId,
quoteId,
},
}

Expand All @@ -248,6 +285,8 @@ describe('webhooks', () => {
transferId,
address: wallet.address,
status: transferStatusResponse.unwrap().status,
expectedAmountReceived: fiatAmount,
expectedAmountProvided: cryptoAmount,
},
2,
),
Expand Down