git.delta.rocks / unique-network / refs/commits / f461d40485f1

difftreelog

fix calibrate.ts

Daniel Shiposha2022-11-28parent: #571be54.patch.diff
in: master

1 file changed

modifiedtests/src/calibrate.tsdiffbeforeafterboth
1917
20 const nb = BigInt(n);18 const nb = BigInt(n);
2119
20 const precision = 100000000n;
21
22 // This is a workaround to beat the lack of precision of the `Number` type.
23 // We divide `BigInt`s. But since it is an integer division, we should take care of the precision on our own.
24 // After the division we can convert the result back to the `Number` and then we set the correct precision.
25 // It is crucial to have the correct slope for the regression line.
22 const a = (nb * sumxy - sumx * sumy) / (nb * sumx2 - sumx * sumx);26 const a = Number((precision * (nb * sumxy - sumx * sumy)) / (nb * sumx2 - sumx * sumx)) / Number(precision);
23 const b = (sumy - a * sumx) / nb;27 const b = (Number(sumy) - a * Number(sumx)) / Number(nb);
2428
25 return {a, b};29 return {a, b};
26}30}
68 await token.transfer(alice, {Substrate: bob.address});72 await token.transfer(alice, {Substrate: bob.address});
69 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);73 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);
7074
71 console.log(`Original price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);75 console.log(`\t[NFT Transfer] Original price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);
72 }76 }
7377
74 const api = helper.getApi();78 const api = helper.getApi();
75 const defaultCoeff = (api.consts.configuration.defaultWeightToFeeCoefficient as any).toBigInt();79 const base = (await api.query.configuration.weightToFeeCoefficientOverride() as any).toBigInt();
76 for (let i = -5; i < 5; i++) {80 for (let i = -5; i < 5; i++) {
77 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setWeightToFeeCoefficientOverride(defaultCoeff + defaultCoeff / 1000n * BigInt(i))));81 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setWeightToFeeCoefficientOverride(base + base / 1000n * BigInt(i))));
7882
79 const coefficient = (await api.query.configuration.weightToFeeCoefficientOverride() as any).toBigInt();83 const coefficient = (await api.query.configuration.weightToFeeCoefficientOverride() as any).toBigInt();
80 const collection = await helper.nft.mintCollection(alice, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});84 const collection = await helper.nft.mintCollection(alice, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});
9296
93 // console.log(`Error: ${error(dataPoints, x => a*x+b)}`);97 // console.log(`Error: ${error(dataPoints, x => a*x+b)}`);
9498
95 const perfectValue = a * helper.balance.getOneTokenNominal() / 10n + b;99 const perfectValue = BigInt(Math.ceil(a * Number(helper.balance.getOneTokenNominal()) / 10 + b));
96 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setWeightToFeeCoefficientOverride(perfectValue.toString())));100 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setWeightToFeeCoefficientOverride(perfectValue.toString())));
97101
98 {102 {
102 await token.transfer(alice, {Substrate: bob.address});106 await token.transfer(alice, {Substrate: bob.address});
103 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);107 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);
104108
105 console.log(`Calibrated price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);109 console.log(`\t[NFT Transfer] Calibrated price: ${Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal())} UNQ`);
106 }110 }
107}111}
108112
121125
122 const cost = await helper.eth.calculateFee({Ethereum: caller}, () => contract.methods.transfer(receiver, token.tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS}));126 const cost = await helper.eth.calculateFee({Ethereum: caller}, () => contract.methods.transfer(receiver, token.tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS}));
123127
124 console.log(`Original price: ${Number(cost) / Number(helper.balance.getOneTokenNominal())} UNQ`);128 console.log(`\t[ETH NFT transfer] Original price: ${Number(cost) / Number(helper.balance.getOneTokenNominal())} UNQ`);
125 }129 }
126130
127 const api = helper.getApi();131 const api = helper.getApi();
132 // const defaultCoeff = (api.consts.configuration.defaultMinGasPrice as any).toBigInt();
128 const defaultCoeff = (api.consts.configuration.defaultMinGasPrice as any).toBigInt();133 const base = (await api.query.configuration.minGasPriceOverride() as any).toBigInt();
129 for (let i = -8; i < 8; i++) {134 for (let i = -8; i < 8; i++) {
130 const gasPrice = defaultCoeff + defaultCoeff / 100000n * BigInt(i);135 const gasPrice = base + base / 100000n * BigInt(i);
131 const gasPriceStr = '0x' + gasPrice.toString(16);136 const gasPriceStr = '0x' + gasPrice.toString(16);
132 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setMinGasPriceOverride(gasPrice)));137 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setMinGasPriceOverride(gasPrice)));
133138
148 // console.log(`Error: ${error(dataPoints, x => a*x+b)}`);153 // console.log(`Error: ${error(dataPoints, x => a*x+b)}`);
149154
150 // * 0.15 = * 10000 / 66666155 // * 0.15 = * 10000 / 66666
151 const perfectValue = a * helper.balance.getOneTokenNominal() * 1000000n / 6666666n + b;156 const perfectValue = BigInt(Math.ceil(a * Number(helper.balance.getOneTokenNominal() * 1000000n / 6666666n) + b));
152 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setMinGasPriceOverride(perfectValue.toString())));157 await helper.signTransaction(alice, api.tx.sudo.sudo(api.tx.configuration.setMinGasPriceOverride(perfectValue.toString())));
153158
154 {159 {
160165
161 const cost = await helper.eth.calculateFee({Ethereum: caller}, () => contract.methods.transfer(receiver, token.tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS}));166 const cost = await helper.eth.calculateFee({Ethereum: caller}, () => contract.methods.transfer(receiver, token.tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS}));
162167
163 console.log(`Calibrated price: ${Number(cost) / Number(helper.balance.getOneTokenNominal())} UNQ`);168 console.log(`\t[ETH NFT transfer] Calibrated price: ${Number(cost) / Number(helper.balance.getOneTokenNominal())} UNQ`);
164 }169 }
165}170}
166171
167(async () => {172(async () => {
168 await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {173 await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {
169 // Second run slightly reduces error sometimes, as price line is not actually straight, this is a curve174 // Subsequent runs reduce error, as price line is not actually straight, this is a curve
175
176 const iterations = 3;
170177
171 await calibrateWeightToFee(helper, privateKey);178 console.log('[Calibrate WeightToFee]');
179 for (let i = 0; i < iterations; i++) {
172 await calibrateWeightToFee(helper, privateKey);180 await calibrateWeightToFee(helper, privateKey);
181 }
173182
174 await calibrateMinGasPrice(helper, privateKey);183 console.log();
184
185 console.log('[Calibrate MinGasPrice]');
186 for (let i = 0; i < iterations; i++) {
175 await calibrateMinGasPrice(helper, privateKey);187 await calibrateMinGasPrice(helper, privateKey);
188 }
176 });189 });
177})();190})();
178191