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

fix: fix amount in createOracleFeeData #162

Merged
merged 2 commits into from
Feb 14, 2023
Merged
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
4 changes: 2 additions & 2 deletions packages/sdk/src/Sygma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ export class Sygma implements SygmaSDK {
fromDomainID,
toDomainID,
resourceID,
tokenAmount: Number(amount),
tokenAmount: amount,
recipientAddress,
});

Expand Down Expand Up @@ -665,7 +665,7 @@ export class Sygma implements SygmaSDK {
fromDomainID: parseInt(this.bridgeSetup!.chain1.domainId),
toDomainID: parseInt(this.bridgeSetup!.chain2.domainId),
resourceID,
tokenAmount: Number(amount),
tokenAmount: amount,
feeOracleBaseUrl,
feeOracleHandlerAddress,
overridedResourceId, // '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/fee/__test__/basicFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('CalculateBasicFee', () => {
fromDomainID: '1',
toDomainID: '2',
resourceID: '0x0000000000000000000000000000000000000000000000000000000000000000',
tokenAmount: 100,
tokenAmount: "0.001",
recipientAddress: '0xF4314cb9046bECe6AA54bb9533155434d0c76909',
});

Expand All @@ -54,7 +54,7 @@ describe('CalculateBasicFee', () => {
fromDomainID: '1',
toDomainID: '2',
resourceID: '0x0000000000000000000000000000000000000000000000000000000000000000',
tokenAmount: 100,
tokenAmount: "100",
recipientAddress: '0xF4314cb9046bECe6AA54bb9533155434d0c76909',
});
} catch (e) {
Expand Down
51 changes: 47 additions & 4 deletions packages/sdk/src/fee/__test__/feeOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,33 @@ describe('feeOracle', () => {
});
} catch (e) {
// @ts-ignore
expect(e.message).toMatch('Invalid fee oracle response');
expect(e.message).toMatch('Internal Error');
}
});

it('return error message from fee oracle server', async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue(
new Response(JSON.stringify({error: 'sick'}), {
url: 'url',
status: 200,
statusText: 'OK'
}),
);
await expect(
requestFeeFromFeeOracle({
feeOracleBaseUrl: 'http://localhost:8091',
fromDomainID: 1,
toDomainID: 2,
resourceID: '0x0000000000000000000000000000000000000000000000000000000000000001',
})).rejects.toThrowError("sick");
});
});

describe('createOracleFeeData', () => {
it('builds feeData', () => {
const feeData = createOracleFeeData(
oracleResponse.response,
10,
"10",
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x6937d1d0b52f2fa7f4e071c7e64934ad988a8f21c6bf4f323fc19af4c77e3c5e',
);
Expand All @@ -104,7 +121,7 @@ describe('feeOracle', () => {
fromDomainID: 1,
toDomainID: 2,
resourceID: '0x0000000000000000000000000000000000000000000000000000000000000001',
tokenAmount: 100,
tokenAmount: "100",
feeOracleBaseUrl: 'http://localhost:8091',
feeOracleHandlerAddress: '0xa9ddD97e1762920679f3C20ec779D79a81903c0B',
overridedResourceId: '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
Expand All @@ -118,6 +135,32 @@ describe('feeOracle', () => {
});
});

it('get the fee data with no oracle key', async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue(
new Response(JSON.stringify(oracleResponse), { url: 'url', status: 200, statusText: 'OK' }),
);

const provider = new ethers.providers.JsonRpcProvider();
const feeData = await calculateFeeData({
provider,
sender: ethers.constants.AddressZero,
recipientAddress: '0x74d2946319bEEe4A140068eb83F9ee3a90B06F4f',
fromDomainID: 1,
toDomainID: 2,
resourceID: '0x0000000000000000000000000000000000000000000000000000000000000001',
tokenAmount: "100",
feeOracleBaseUrl: 'http://localhost:8091',
feeOracleHandlerAddress: '0xa9ddD97e1762920679f3C20ec779D79a81903c0B',
});
expect(feeData?.feeData).toContain(oracleResponse.response.signature)
expect(feeData).toMatchObject({
calculatedRate: '0.00000000000000001',
erc20TokenAddress: '0x141F8690A87A7E57C2E270ee77Be94935970c035',
feeData:
'0x000000000000000000000000000000000000000000000000000194b9a2ecd000000000000000000000000000000000000000000000000000dd55bf4eab04000000000000000000000000000000000000000000000000000000000000773594000000000000000000000000000000000000000000000000000000000069b26b140000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000ffdd02c9aaf691e70dcbb69f9e6ec558c3e078c1ec75a5beec0ec46d452c505d3a616a5d6dc738da57ce1ffb6c16fb7f51cfbea6017fa029cd95005a8eaefef31b0000000000000000000000000000000000000000000000000000000000000064',
});
});

it('get error', async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockRejectedValue('Err');
try {
Expand All @@ -129,7 +172,7 @@ describe('feeOracle', () => {
fromDomainID: 1,
toDomainID: 2,
resourceID: '0x0000000000000000000000000000000000000000000000000000000000000001',
tokenAmount: 100,
tokenAmount: "100",
feeOracleBaseUrl: 'http://localhost:8091',
feeOracleHandlerAddress: '0xa9ddD97e1762920679f3C20ec779D79a81903c0B',
overridedResourceId: '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
Expand Down
7 changes: 4 additions & 3 deletions packages/sdk/src/fee/basicFee.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasicFeeHandler__factory as BasicFeeHandler } from '@buildwithsygma/sygma-contracts';
import { ethers } from 'ethers';
import { ethers, utils } from 'ethers';
import { FeeDataResult } from '../types';
import { createERCDepositData } from '../utils/helpers';

Expand All @@ -25,10 +25,11 @@ export const calculateBasicfee = async ({
fromDomainID: string;
toDomainID: string;
resourceID: string;
tokenAmount: number;
tokenAmount: string;
recipientAddress: string;
}): Promise<FeeDataResult | Error> => {
const depositData = createERCDepositData(tokenAmount, 20, recipientAddress);
const convertedAmount = utils.parseUnits(tokenAmount, 18);
const depositData = createERCDepositData(convertedAmount, 20, recipientAddress);
// WHY 0X00 AND NOT 0X0?
const feeData = '0x00';
const BasicFeeHandlerInstance = BasicFeeHandler.connect(basicFeeHandlerAddress, provider);
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk/src/fee/feeOracle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FeeHandlerWithOracle__factory } from '@buildwithsygma/sygma-contracts';
import { ethers } from 'ethers';
import { ethers, utils } from 'ethers';
import fetch from 'node-fetch';
import EthCrypto from 'eth-crypto';

Expand All @@ -22,7 +22,7 @@ type OracleResponse = {
*/
export const createOracleFeeData = (
oracleResponse: OracleResource,
amount: number,
amount: string,
tokenResource: string,
oraclePrivateKey?: string,
): string => {
Expand Down Expand Up @@ -70,7 +70,7 @@ export const createOracleFeeData = (
return oracleMessage + signature.substring(2) + toHex(amount, 32).substring(2);
} else {
signature = oracleResponse.signature;
return oracleMessage + signature + toHex(0, 32).substring(2);
return oracleMessage + signature + toHex(amount, 32).substring(2);
MakMuftic marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down Expand Up @@ -99,13 +99,13 @@ export const calculateFeeData = async ({
fromDomainID: number;
toDomainID: number;
resourceID: string;
tokenAmount: number;
tokenAmount: string;
feeOracleBaseUrl: string;
feeOracleHandlerAddress: string;
overridedResourceId?: string;
oraclePrivateKey?: string;
}): Promise<FeeDataResult | undefined> => {
const depositData = createERCDepositData(tokenAmount, 20, recipientAddress);
const depositData = createERCDepositData(utils.parseUnits(tokenAmount, 18), 20, recipientAddress);
let oracleResponse;
try {
oracleResponse = await requestFeeFromFeeOracle({
Expand Down Expand Up @@ -185,6 +185,6 @@ export const requestFeeFromFeeOracle = async ({
return data.response;
}
} catch (e) {
return Promise.reject(new Error('Invalid fee oracle response'));
return Promise.reject(e);
}
};
3 changes: 2 additions & 1 deletion packages/sdk/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { utils, BigNumber } from 'ethers';
* @returns {string}
*/
export const toHex = (covertThis: string | number | BigNumber, padding: number): string => {
return utils.hexZeroPad(utils.hexlify(BigNumber.from(covertThis)), padding);
const amount = covertThis instanceof BigNumber ? covertThis : BigNumber.from(covertThis);
return utils.hexZeroPad(utils.hexlify(amount), padding);
};

/**
Expand Down