Adjust for vesting via locks (#2396)

This commit is contained in:
Jaco Greeff
2020-06-28 16:08:02 +02:00
committed by GitHub
parent 657a2006dd
commit f1ebcddec6
14 changed files with 440 additions and 405 deletions
+1
View File
@@ -3,6 +3,7 @@
## 1.22.0-beta.x
- Improve derive accountId -> accountIndex lookups (optimize entries queries)
- Vesting balance calculations via `derive.balances.all` has been updated for locks
## 1.21.1 Jun 23, 2020
+1 -1
View File
@@ -27,7 +27,7 @@
"@babel/core": "^7.10.3",
"@babel/register": "^7.10.3",
"@babel/runtime": "^7.10.3",
"@polkadot/dev": "^0.55.12",
"@polkadot/dev": "^0.55.13",
"@polkadot/ts": "^0.3.26",
"@polkadot/typegen": "workspace:packages/typegen",
"@types/jest": "^26.0.0",
+1 -1
View File
@@ -30,7 +30,7 @@
"@polkadot/api": "1.22.0-beta.3",
"@polkadot/rpc-core": "1.22.0-beta.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"bn.js": "^5.1.2",
"rxjs": "^6.5.5"
}
+3 -3
View File
@@ -32,13 +32,13 @@
"@polkadot/rpc-core": "1.22.0-beta.3",
"@polkadot/rpc-provider": "1.22.0-beta.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util-crypto": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"@polkadot/util-crypto": "^2.16.0-beta.0",
"bn.js": "^5.1.2",
"memoizee": "^0.4.14",
"rxjs": "^6.5.5"
},
"devDependencies": {
"@polkadot/keyring": "^2.15.1"
"@polkadot/keyring": "^2.16.0-beta.0"
}
}
+11 -15
View File
@@ -20,15 +20,17 @@ type Result = [DeriveBalancesAccount, BlockNumber, ResultBalance];
function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBalance, frozenFee, frozenMisc, reservedBalance, votingBalance }, bestNumber, [vesting, locks]]: Result): DeriveBalancesAll {
let lockedBalance = api.registry.createType('Balance');
let lockedBreakdown: (BalanceLock | BalanceLockTo212)[] = [];
let vestingLocked = api.registry.createType('Balance');
let allLocked = false;
if (Array.isArray(locks)) {
// only get the locks that are valid until passed the current block
lockedBreakdown = (locks as BalanceLockTo212[]).filter(({ until }): boolean => !until || (bestNumber && until.gt(bestNumber)));
const notAll = lockedBreakdown.filter(({ amount }): boolean => !amount.isMax());
const notAll = lockedBreakdown.filter(({ amount }) => !amount.isMax());
allLocked = lockedBreakdown.some(({ amount }): boolean => amount.isMax());
allLocked = lockedBreakdown.some(({ amount }) => amount.isMax());
vestingLocked = api.registry.createType('Balance', lockedBreakdown.filter(({ id }) => id.eq('0x76657374696e6720')).reduce((result: BN, { amount }) => result.iadd(amount), new BN(0)));
// get the maximum of the locks according to https://github.com/paritytech/substrate/blob/master/srml/balances/src/lib.rs#L699
if (notAll.length) {
@@ -41,19 +43,11 @@ function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBala
// - perBlock is the unlock amount
const { locked: vestingTotal, perBlock, startingBlock } = vesting || api.registry.createType('VestingInfo');
const isStarted = bestNumber.gt(startingBlock);
const vestedBalance = api.registry.createType('Balance', isStarted ? perBlock.mul(bestNumber.sub(startingBlock)) : 0);
const isVesting = isStarted && vestedBalance.lt(vestingTotal);
// The available balance & vested has an interplay here
// "
// vesting is a guarantee that the account's balance will never go below a certain amount. so it functions in the opposite way, a bit like a lock that is monotonically decreasing rather than a liquid amount that is monotonically increasing.
// locks function as the same guarantee - that a balance will not be lower than a particular amount.
// because of this you can see that if there is a "vesting lock" that guarantees the balance cannot go below 200, and a "staking lock" that guarantees the balance cannot drop below 300, then we just have two guarantees of which the first is irrelevant.
// i.e. (balance >= 200 && balance >= 300) == (balance >= 300)
// ""
const floating = freeBalance.sub(lockedBalance);
const extraReceived = isVesting ? freeBalance.sub(vestingTotal) : new BN(0);
const availableBalance = api.registry.createType('Balance', allLocked ? 0 : bnMax(new BN(0), isVesting && floating.gt(vestedBalance) ? vestedBalance.add(extraReceived) : floating));
const vestedNow = isStarted ? perBlock.mul(bestNumber.sub(startingBlock)) : new BN(0);
const vestedBalance = vestedNow.gt(vestingTotal) ? vestingTotal : api.registry.createType('Balance', vestedNow);
const isVesting = isStarted && !vestingLocked.isZero();
const vestedClaimable = api.registry.createType('Balance', isVesting ? vestingLocked.sub(vestingTotal.sub(vestedBalance)) : 0);
const availableBalance = api.registry.createType('Balance', allLocked ? 0 : bnMax(new BN(0), freeBalance.sub(lockedBalance)));
return {
accountId,
@@ -67,6 +61,8 @@ function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBala
lockedBreakdown,
reservedBalance,
vestedBalance,
vestedClaimable,
vestingLocked,
vestingTotal,
votingBalance
};
+2
View File
@@ -31,6 +31,8 @@ export interface DeriveBalancesAll extends DeriveBalancesAccount {
availableBalance: Balance;
votingBalance: Balance;
vestedBalance: Balance;
vestedClaimable: Balance;
vestingLocked: Balance;
vestingTotal: Balance;
}
+4 -4
View File
@@ -28,19 +28,19 @@
"dependencies": {
"@babel/runtime": "^7.10.3",
"@polkadot/api-derive": "1.22.0-beta.3",
"@polkadot/keyring": "^2.15.1",
"@polkadot/keyring": "^2.16.0-beta.0",
"@polkadot/metadata": "1.22.0-beta.3",
"@polkadot/rpc-core": "1.22.0-beta.3",
"@polkadot/rpc-provider": "1.22.0-beta.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/types-known": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util-crypto": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"@polkadot/util-crypto": "^2.16.0-beta.0",
"bn.js": "^5.1.2",
"eventemitter3": "^4.0.4",
"rxjs": "^6.5.5"
},
"devDependencies": {
"@polkadot/keyring": "^2.15.1"
"@polkadot/keyring": "^2.16.0-beta.0"
}
}
+3 -3
View File
@@ -29,11 +29,11 @@
"@babel/runtime": "^7.10.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/types-known": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util-crypto": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"@polkadot/util-crypto": "^2.16.0-beta.0",
"bn.js": "^5.1.2"
},
"devDependencies": {
"@polkadot/keyring": "^2.15.1"
"@polkadot/keyring": "^2.16.0-beta.0"
}
}
+2 -2
View File
@@ -30,11 +30,11 @@
"@polkadot/metadata": "1.22.0-beta.3",
"@polkadot/rpc-provider": "1.22.0-beta.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"memoizee": "^0.4.14",
"rxjs": "^6.5.5"
},
"devDependencies": {
"@polkadot/keyring": "^2.15.1"
"@polkadot/keyring": "^2.16.0-beta.0"
}
}
+3 -3
View File
@@ -29,15 +29,15 @@
"@babel/runtime": "^7.10.3",
"@polkadot/metadata": "1.22.0-beta.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util-crypto": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"@polkadot/util-crypto": "^2.16.0-beta.0",
"bn.js": "^5.1.2",
"eventemitter3": "^4.0.4",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.31"
},
"devDependencies": {
"@polkadot/keyring": "^2.15.1",
"@polkadot/keyring": "^2.16.0-beta.0",
"mock-socket": "^9.0.3",
"nock": "^12.0.3"
}
+1 -1
View File
@@ -40,7 +40,7 @@
"@polkadot/metadata": "1.22.0-beta.3",
"@polkadot/rpc-provider": "1.22.0-beta.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"handlebars": "^4.7.6",
"websocket": "^1.0.31",
"yargs": "^15.3.1"
+1 -1
View File
@@ -28,7 +28,7 @@
"dependencies": {
"@babel/runtime": "^7.10.3",
"@polkadot/types": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"bn.js": "^5.1.2"
},
"devDependencies": {
+3 -3
View File
@@ -28,15 +28,15 @@
"dependencies": {
"@babel/runtime": "^7.10.3",
"@polkadot/metadata": "1.22.0-beta.3",
"@polkadot/util": "^2.15.1",
"@polkadot/util-crypto": "^2.15.1",
"@polkadot/util": "^2.16.0-beta.0",
"@polkadot/util-crypto": "^2.16.0-beta.0",
"@types/bn.js": "^4.11.6",
"bn.js": "^5.1.2",
"memoizee": "^0.4.14",
"rxjs": "^6.5.5"
},
"devDependencies": {
"@polkadot/keyring": "^2.15.1",
"@polkadot/keyring": "^2.16.0-beta.0",
"@types/memoizee": "^0.4.4"
}
}
+404 -368
View File
File diff suppressed because it is too large Load Diff