Skip to content

Commit

Permalink
fix: remove unneccessary dependencies (#163)
Browse files Browse the repository at this point in the history
* fix: remove unneccessary dependencies

* fix oracle and dependencies

* update fee oracle and test

---------

Co-authored-by: Nikolay Topkaridi <[email protected]>
  • Loading branch information
mpetrunic and enemycnt authored Feb 22, 2023
1 parent 324d77c commit dec73cd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 287 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/integration/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */

import { Bridge__factory } from '@chainsafe/chainbridge-contracts'
import { Bridge__factory } from '@buildwithsygma/sygma-contracts'
import { Erc20DetailedFactory } from '../../src/Contracts/Erc20DetailedFactory'
import { BridgeData, ChainbridgeContracts, Signer } from '../../src/types'
import { computeBridgeEvents, computeProposalVoteEvents, computeProvidersAndSigners } from '../../src/utils'
Expand Down
6 changes: 2 additions & 4 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
],
"dependencies": {
"@buildwithsygma/sygma-contracts": "1.0.0",
"eth-crypto": "2.3.0",
"node-fetch": "2.6.1",
"react-hook-form": "^7.30.0",
"ethers": "5.6.2"
"ethers": "5.6.2",
"node-fetch": "2.6.1"
}
}
19 changes: 3 additions & 16 deletions packages/sdk/src/Sygma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,19 +555,14 @@ export class Sygma implements SygmaSDK {
* @name fetchFeeData
* @description it fetches the fee data according to bridge setup
* @param {object} params
* @param {string} params.amount
* @param {string} params.amount - the amount of token to transfer
* @param {string} params.recipientAddress - receiver of the deposit
* @param {string} params.overridedResourceId - matches local token in local setup with address of the token from where the price data is being fetched.
* Only used when doing bridge with fee oracle service
* @param {string} params.oraclePrivateKey
*/
public async fetchFeeData(params: {
amount: string;
recipientAddress: string;
overridedResourceId?: string;
oraclePrivateKey?: string;
}): Promise<FeeDataResult | Error | undefined> {
const { amount, overridedResourceId, oraclePrivateKey, recipientAddress } = params;
const { amount, recipientAddress } = params;
const {
feeSettings: { type },
} = this.getSelectedToken();
Expand All @@ -581,8 +576,6 @@ export class Sygma implements SygmaSDK {
return await this.fetchFeeOracleData({
amount,
recipientAddress,
overridedResourceId,
oraclePrivateKey,
});
} else {
return await this.fetchBasicFeeData({
Expand Down Expand Up @@ -640,14 +633,12 @@ export class Sygma implements SygmaSDK {
private async fetchFeeOracleData(params: {
amount: string;
recipientAddress: string;
overridedResourceId?: string;
oraclePrivateKey?: string;
}): Promise<FeeDataResult | undefined> {
if (!this.feeOracleSetup && !this.getSelectedToken().feeSettings.address) {
console.log('No feeOracle config');
return;
}
const { amount, recipientAddress, overridedResourceId, oraclePrivateKey } = params;
const { amount, recipientAddress } = params;
const provider = this.providers!.chain1!;
const {
resourceId: resourceID,
Expand All @@ -668,12 +659,8 @@ export class Sygma implements SygmaSDK {
tokenAmount: amount,
feeOracleBaseUrl,
feeOracleHandlerAddress,
overridedResourceId, // '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
oraclePrivateKey, //'0x6937d1d0b52f2fa7f4e071c7e64934ad988a8f21c6bf4f323fc19af4c77e3c5e'
});

// feeOracleBaseUrl: 'http://localhost:8091',
// FeeHandlerWithOracleAddress: '0xa9ddD97e1762920679f3C20ec779D79a81903c0B',
return feeData;
}

Expand Down
36 changes: 3 additions & 33 deletions packages/sdk/src/fee/__test__/feeOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,45 +97,17 @@ describe('feeOracle', () => {
it('builds feeData', () => {
const feeData = createOracleFeeData(
oracleResponse.response,
"10",
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x6937d1d0b52f2fa7f4e071c7e64934ad988a8f21c6bf4f323fc19af4c77e3c5e',
"10"
);
expect(feeData).toBe(
'0x000000000000000000000000000000000000000000000000000194b9a2ecd000000000000000000000000000000000000000000000000000dd55bf4eab04000000000000000000000000000000000000000000000000000000000000773594000000000000000000000000000000000000000000000000000000000069b26b140000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000f7d44d4684d34972ca45bb3a1c19ecff830e652d6c7c3885ace35db60f74e1bb338a28389d033e479734c0afc0b894255c51af33eb7666c195255709efef93d11c000000000000000000000000000000000000000000000000000000000000000a',
'0x000000000000000000000000000000000000000000000000000194b9a2ecd000000000000000000000000000000000000000000000000000dd55bf4eab04000000000000000000000000000000000000000000000000000000000000773594000000000000000000000000000000000000000000000000000000000069b26b140000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000ffdd02c9aaf691e70dcbb69f9e6ec558c3e078c1ec75a5beec0ec46d452c505d3a616a5d6dc738da57ce1ffb6c16fb7f51cfbea6017fa029cd95005a8eaefef31b000000000000000000000000000000000000000000000000000000000000000a',
);
});
});

describe('calculateFeeData', () => {
it('get the fee data', 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',
overridedResourceId: '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
oraclePrivateKey: '0x6937d1d0b52f2fa7f4e071c7e64934ad988a8f21c6bf4f323fc19af4c77e3c5e',
});
expect(feeData).toMatchObject({
calculatedRate: '0.00000000000000001',
erc20TokenAddress: '0x141F8690A87A7E57C2E270ee77Be94935970c035',
feeData:
'0x000000000000000000000000000000000000000000000000000194b9a2ecd000000000000000000000000000000000000000000000000000dd55bf4eab04000000000000000000000000000000000000000000000000000000000000773594000000000000000000000000000000000000000000000000000000000069b26b140000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000f7d44d4684d34972ca45bb3a1c19ecff830e652d6c7c3885ace35db60f74e1bb338a28389d033e479734c0afc0b894255c51af33eb7666c195255709efef93d11c0000000000000000000000000000000000000000000000000000000000000064',
});
});

it('get the fee data with no oracle key', async () => {
it('get the fee data with no oracle private key', async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue(
new Response(JSON.stringify(oracleResponse), { url: 'url', status: 200, statusText: 'OK' }),
);
Expand Down Expand Up @@ -175,8 +147,6 @@ describe('feeOracle', () => {
tokenAmount: "100",
feeOracleBaseUrl: 'http://localhost:8091',
feeOracleHandlerAddress: '0xa9ddD97e1762920679f3C20ec779D79a81903c0B',
overridedResourceId: '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
oraclePrivateKey: '0x6937d1d0b52f2fa7f4e071c7e64934ad988a8f21c6bf4f323fc19af4c77e3c5e',
});
} catch (e) {
expect(e).toMatch('Err');
Expand Down
38 changes: 7 additions & 31 deletions packages/sdk/src/fee/feeOracle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FeeHandlerWithOracle__factory } from '@buildwithsygma/sygma-contracts';
import { ethers, utils } from 'ethers';
import fetch from 'node-fetch';
import EthCrypto from 'eth-crypto';

import { OracleResource, FeeDataResult } from '../types';
import { toHex, createERCDepositData } from '../utils/helpers';
Expand All @@ -17,15 +16,10 @@ type OracleResponse = {
* @param oracleResponse - OracleResource object
* @param amount - amount to send
* @param tokenResource - the resource Id of the token
* @param oraclePrivateKey - private key of the oracle
* @returns {string} - hex of the fee data constructed
*/
export const createOracleFeeData = (
oracleResponse: OracleResource,
amount: string,
tokenResource: string,
oraclePrivateKey?: string,
): string => {

export const createOracleFeeData = (oracleResponse: OracleResource, amount: string): string => {
/*
feeData structure:
ber*10^18: uint256
Expand Down Expand Up @@ -62,16 +56,8 @@ export const createOracleFeeData = (
],
);

let signature;
if (oraclePrivateKey) {
// temprorary signature generated with sign by private key
const messageHash = EthCrypto.hash.keccak256([{ type: 'bytes', value: oracleMessage }]);
signature = EthCrypto.sign(oraclePrivateKey, messageHash);
return oracleMessage + signature.substring(2) + toHex(amount, 32).substring(2);
} else {
signature = oracleResponse.signature;
return oracleMessage + signature + toHex(amount, 32).substring(2);
}
const signature = oracleResponse.signature;
return oracleMessage + signature + toHex(amount, 32).substring(2);
};

/**
Expand All @@ -90,8 +76,6 @@ export const calculateFeeData = async ({
tokenAmount,
feeOracleBaseUrl,
feeOracleHandlerAddress,
overridedResourceId,
oraclePrivateKey,
}: {
provider: ethers.providers.Provider;
sender: string;
Expand All @@ -102,8 +86,6 @@ export const calculateFeeData = async ({
tokenAmount: string;
feeOracleBaseUrl: string;
feeOracleHandlerAddress: string;
overridedResourceId?: string;
oraclePrivateKey?: string;
}): Promise<FeeDataResult | undefined> => {
const depositData = createERCDepositData(utils.parseUnits(tokenAmount, 18), 20, recipientAddress);
let oracleResponse;
Expand All @@ -112,18 +94,12 @@ export const calculateFeeData = async ({
feeOracleBaseUrl,
fromDomainID,
toDomainID,
resourceID: overridedResourceId ? overridedResourceId : resourceID, // dirty hack for localsetup
resourceID,
});
} catch (e) {
//@ts-ignore-line
return e;
return Promise.reject(e);
}
const feeData = createOracleFeeData(
oracleResponse as OracleResource,
tokenAmount,
resourceID,
oraclePrivateKey,
);
const feeData = createOracleFeeData(oracleResponse as OracleResource, tokenAmount);
const FeeHandlerWithOracleInstance = FeeHandlerWithOracle__factory.connect(
feeOracleHandlerAddress,
provider,
Expand Down
Loading

0 comments on commit dec73cd

Please sign in to comment.