Skip to content

Commit

Permalink
Add a loop for core.token.transfer (#362)
Browse files Browse the repository at this point in the history
* Simplify node version in .nvmrc

* Add loop for updating transitive transfer paths when transfer fails

* Update dependecy: core@2.11.0

* Add env var: DATABASE_SOURCE

* Fix multiple optional params in waitAndRetryOnFail() helper func

* Update @circles/core to testing package: v2.11.1-prerelease-5b905db

* Update node to v15 to have npm v7

* Use node v14

* Use node v16

* Update @circles/core to v2.11.1

* Update .nvmrc

* Update .nvmrc

* Update .nvmrc

* Update circles-core version

* Update package-lock.json

* Configure lint

Co-authored-by: lluna <lluna@bitspossessed.org>
  • Loading branch information
llunaCreixent and lluna authored Sep 19, 2022
1 parent 2b8153e commit 64611fe
Show file tree
Hide file tree
Showing 8 changed files with 41,439 additions and 50,573 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ API_SERVICE_EXTERNAL=http://api.circles.local
GRAPH_NODE_EXTERNAL=http://graph.circles.local
RELAY_SERVICE_EXTERNAL=http://relay.circles.local

# Database Endpoints
DATABASE_SOURCE=graph

# Smart Contract addresses
HUB_ADDRESS=0xCfEB869F69431e42cdB54A4F4f105C19C080A601
PROXY_FACTORY_ADDRESS=0xD833215cBcc3f914bD1C9ece3EE7BF8B14f841bb
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"plugin:prettier/recommended",
"plugin:react-hooks/recommended"
],
"parser": "babel-eslint",
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
Expand Down
91,832 changes: 41,300 additions & 50,532 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
"test:watch": "npm run test -- --watch"
},
"devDependencies": {
"@babel/core": "^7.17.12",
"@babel/node": "^7.17.10",
"@babel/plugin-proposal-class-properties": "^7.17.12",
"@babel/core": "^7.19.1",
"@babel/eslint-parser": "^7.19.1",
"@babel/node": "^7.19.1",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.17.12",
"@babel/plugin-transform-runtime": "^7.17.12",
"@babel/preset-env": "^7.17.12",
"@babel/preset-react": "^7.17.12",
"@babel/plugin-transform-runtime": "^7.18.9",
"@babel/preset-env": "^7.19.1",
"@babel/preset-react": "^7.18.6",
"@svgr/webpack": "^5.5.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^11.2.7",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.5",
"cross-env": "^7.0.3",
"css-loader": "^5.2.7",
Expand All @@ -57,10 +57,10 @@
"truffle": "^5.5.14",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.2"
"webpack-dev-server": "^3.11.3"
},
"dependencies": {
"@circles/core": "^2.10.10",
"@circles/core": "^2.12.0",
"@circles/timecircles": "^1.0.6",
"@material-ui/core": "^4.12.4",
"@material-ui/lab": "^4.0.0-alpha.61",
Expand Down
8 changes: 8 additions & 0 deletions src/services/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ const token = {
});
},

updateTransferSteps: async (from, to, value) => {
return await requestCore('token', 'updateTransferSteps', {
from,
to,
value,
});
},

checkUBIPayout: async (safeAddress) => {
return await requestCore('token', 'checkUBIPayout', {
safeAddress,
Expand Down
17 changes: 16 additions & 1 deletion src/store/token/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ export function requestUBIPayout(payout) {
};
}

async function loopTransfer(from, to, value, paymentNote) {
return await waitAndRetryOnFail(
() => {
return core.token.transfer(from, to, value, paymentNote);
},
() => {
return true;
},
{},
() => {
return core.token.updateTransferSteps(from, to, value);
},
);
}

/**
* Transfer circles to another safe
* @param {string} to Receiver safe address of Circles transfer
Expand All @@ -223,7 +238,7 @@ export function transfer(to, amount, paymentNote = '') {
const value = new web3.utils.BN(
core.utils.toFreckles(tcToCrc(Date.now(), Number(amount))),
);
const txHash = await core.token.transfer(from, to, value, paymentNote);
const txHash = await loopTransfer(from, to, value, paymentNote);

dispatch(
addPendingActivity({
Expand Down
5 changes: 5 additions & 0 deletions src/utils/stateChecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function waitAndRetryOnFail(
maxAttemptsOnFail = RETRIES_ON_FAIL_DEFAULT,
waitAfterFail = WAIT_AFTER_FAIL_DEFAULT,
} = {},
onErrorFn,
) {
// Count all attempts to retry when something failed
let attempt = 1;
Expand All @@ -91,6 +92,10 @@ export async function waitAndRetryOnFail(
// Finish when request was successful and condition arrived!
return response;
} catch (error) {
// When there is an error I want to do something
if (onErrorFn) {
await onErrorFn();
}
// Something went wrong, either the condition did not arrive or the
// request failed
if (attempt >= maxAttemptsOnFail) {
Expand Down
125 changes: 96 additions & 29 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,128 @@ describe('Format utils', () => {
it('should print a human readable TC currency string which is the same as CRC value when timestamp is Circles launch date', () => {
const { toWei } = web3.utils;
const format = formatCirclesValue;
const circlesInceptionTimestamp = new Date("2020-10-15T00:00:00.000Z").getTime();
const circlesInceptionTimestamp = new Date(
'2020-10-15T00:00:00.000Z',
).getTime();

expect(format(toWei('1.12345', 'ether'), circlesInceptionTimestamp)).toBe('3.37');
expect(format(toWei('1.12345', 'ether'), circlesInceptionTimestamp, 3)).toBe('3.370');
expect(format(toWei('1.12345', 'ether'), circlesInceptionTimestamp)).toBe(
'3.37',
);
expect(
format(toWei('1.12345', 'ether'), circlesInceptionTimestamp, 3),
).toBe('3.370');
});

it('should return a lower time circles value for a timestamp later than the Circles launch date', () => {
const { toWei } = web3.utils;
const format = formatCirclesValue;
const laterThanLaunchState = new Date("2021-11-15T00:00:00.000Z").getTime();

expect(Number(format(toWei('100', 'ether'), laterThanLaunchState))).toBeLessThan(300.00);
const laterThanLaunchState = new Date(
'2021-11-15T00:00:00.000Z',
).getTime();

expect(
Number(format(toWei('100', 'ether'), laterThanLaunchState)),
).toBeLessThan(300.0);
});

it('should round down by cutting of number by default', () => {
const { toWei } = web3.utils;
const format = formatCirclesValue;
const circlesInceptionTimestamp = new Date("2020-10-15T00:00:00.000Z").getTime();

expect(format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 0)).toBe('3');
expect(format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 1)).toBe('3.9');
expect(format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 2)).toBe('3.97');
expect(format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 4)).toBe('3.9703');
const circlesInceptionTimestamp = new Date(
'2020-10-15T00:00:00.000Z',
).getTime();

expect(
format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 0),
).toBe('3');
expect(
format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 1),
).toBe('3.9');
expect(
format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 2),
).toBe('3.97');
expect(
format(toWei('1.32345', 'ether'), circlesInceptionTimestamp, 4),
).toBe('3.9703');
});

it('should round correctly when using roundDown false', () => {
const { toWei } = web3.utils;
const format = formatCirclesValue;
const circlesInceptionTimestamp = new Date("2020-10-15T00:00:00.000Z").getTime();
const circlesInceptionTimestamp = new Date(
'2020-10-15T00:00:00.000Z',
).getTime();
const roundDown = false;

// reference time circles translation with 8 decimals 3.97366800
expect(format(toWei('1.32456', 'ether'), circlesInceptionTimestamp, 0, roundDown)).toBe('4');
expect(format(toWei('1.32456', 'ether'), circlesInceptionTimestamp, 1, roundDown)).toBe('4.0');
expect(format(toWei('1.32456', 'ether'), circlesInceptionTimestamp, 2, roundDown)).toBe('3.97');
expect(format(toWei('1.32456', 'ether'), circlesInceptionTimestamp, 4, roundDown)).toBe('3.9737');
expect(
format(
toWei('1.32456', 'ether'),
circlesInceptionTimestamp,
0,
roundDown,
),
).toBe('4');
expect(
format(
toWei('1.32456', 'ether'),
circlesInceptionTimestamp,
1,
roundDown,
),
).toBe('4.0');
expect(
format(
toWei('1.32456', 'ether'),
circlesInceptionTimestamp,
2,
roundDown,
),
).toBe('3.97');
expect(
format(
toWei('1.32456', 'ether'),
circlesInceptionTimestamp,
4,
roundDown,
),
).toBe('3.9737');
});

it('should remove decimals for exact numbers by default', () => {
const { toWei } = web3.utils;
const format = formatCirclesValue;
const circlesInceptionTimestamp = new Date("2020-10-15T00:00:00.000Z").getTime();
const circlesInceptionTimestamp = new Date(
'2020-10-15T00:00:00.000Z',
).getTime();

expect(format(toWei('100', 'ether'), circlesInceptionTimestamp)).toBe('300');
expect(format(toWei('100.00', 'ether'), circlesInceptionTimestamp)).toBe('300');
expect(format(toWei('100', 'ether'), circlesInceptionTimestamp)).toBe(
'300',
);
expect(format(toWei('100.00', 'ether'), circlesInceptionTimestamp)).toBe(
'300',
);
});

it('will not remove decimals for exact numbers when roundDown is false', () => {
const { toWei } = web3.utils;
const format = formatCirclesValue;
const circlesInceptionTimestamp = new Date("2020-10-15T00:00:00.000Z").getTime();
const circlesInceptionTimestamp = new Date(
'2020-10-15T00:00:00.000Z',
).getTime();
const roundDown = false;

expect(format(toWei('100', 'ether'), circlesInceptionTimestamp, 2, roundDown)).toBe('300.00');
expect(format(toWei('100.00', 'ether'), circlesInceptionTimestamp, 2, roundDown)).toBe('300.00');
expect(
format(toWei('100', 'ether'), circlesInceptionTimestamp, 2, roundDown),
).toBe('300.00');
expect(
format(
toWei('100.00', 'ether'),
circlesInceptionTimestamp,
2,
roundDown,
),
).toBe('300.00');
});
});
});

0 comments on commit 64611fe

Please sign in to comment.