Skip to content

Commit

Permalink
Solving issue CryptoBlades#1905: Aggregate Fights into a single txn t…
Browse files Browse the repository at this point in the history
…o reduce gas fees
  • Loading branch information
NindoK committed Mar 29, 2023
1 parent a20a889 commit d5358f1
Show file tree
Hide file tree
Showing 4 changed files with 981 additions and 494 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ This code is open, but not open source. It is not licensed, which means you cann
## Currency Setup

1. Install [Ganache](https://www.trufflesuite.com/ganache).
1. For Ganache, choose Quickstart Ethereum.
1. Increase the gas limit in the workspace to `99999999` (or some other high number so you can deploy).
1. Install [MetaMask](https://metamask.io/).
1. Create a new connection to connect to Ganache with these settings: http://localhost:7545, any name, any chain id
1. In Ganache, click the key icon on the right side of any address and grab the private key.
1. In MetaMask, create a new account, import from private key, and paste the key in there.
2. For Ganache, choose Quickstart Ethereum.
3. Increase the gas limit in the workspace to `99999999` (or some other high number so you can deploy).
4. Install [MetaMask](https://metamask.io/).
5. Create a new connection to connect to Ganache with these settings: http://localhost:7545, any name, any chain id
6. In Ganache, click the key icon on the right side of any address and grab the private key. Make sure that chainID is 5777.
7. In MetaMask, create a new account, import from private key, and paste the key in there.

You should now have 100 fake eth! You're now fake rich.

Expand Down Expand Up @@ -118,7 +118,6 @@ If you get any issues during deployment, run:
- Language is loaded on startup and added to the language drop-down of the Options page.
- The value for the drop-down is "name" at the root of the json map.


### i18n Manager App

- Adding translations is easier with the use of [i18n-manager](https://www.electronjs.org/apps/i18n-manager)
Expand Down
72 changes: 59 additions & 13 deletions contracts/TokensManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ contract TokensManager is Initializable, AccessControlUpgradeable {
require(hasRole(GAME_ADMIN, msg.sender));
}

function initialize(
address gameContract
) public initializer {
function initialize(address gameContract) public initializer {
__AccessControl_init_unchained();
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);

Expand All @@ -39,25 +37,73 @@ contract TokensManager is Initializable, AccessControlUpgradeable {
offsetSlippage = 5;
}

receive() external payable restricted {
}
receive() external payable restricted {}

function fight(
uint256 char,
uint32 target,
uint8 fightMultiplier
) external payable {
uint256 totalTokens;
uint256 totalExpectedTokens;
for (uint256 i = 0; i < char.length; i++) {
(uint256 tokens, uint256 expectedTokens) = game.fight(
msg.sender,
char[i],
target[i],
fightMultiplier[i]
);
totalTokens += tokens;
totalExpectedTokens += expectedTokens;
}
//The following can be put outside the loop? I'm not 100% sure this is correct
uint256 offset = ABDKMath64x64.mulu(
getSkillToNativeRatio(),
totalExpectedTokens.mul(combatTokenChargePercent).div(100)
);

function fight(uint256 char, uint32 target, uint8 fightMultiplier) external payable {
(uint256 tokens, uint256 expectedTokens) = game.fight(msg.sender, char, target, fightMultiplier);
require(
msg.value >= offset.mul(100 - offsetSlippage).div(100) &&
msg.value <= offset.mul(100 + offsetSlippage).div(100),
"Offset error"
);
if (totalTokens == 0) {
payable(msg.sender).transfer(msg.value);
}
}

uint256 offset = ABDKMath64x64.mulu(getSkillToNativeRatio(), expectedTokens.mul(combatTokenChargePercent).div(100));
function singleFight(
uint256 char,
uint32 target,
uint8 fightMultiplier
) external payable {
(uint256 tokens, uint256 expectedTokens) = game.fight(
msg.sender,
char,
target,
fightMultiplier
);

uint256 offset = ABDKMath64x64.mulu(
getSkillToNativeRatio(),
expectedTokens.mul(combatTokenChargePercent).div(100)
);

require(
msg.value >= offset.mul(100 - offsetSlippage).div(100) && msg.value <= offset.mul(100 + offsetSlippage).div(100),
'Offset error'
);
msg.value >= offset.mul(100 - offsetSlippage).div(100) &&
msg.value <= offset.mul(100 + offsetSlippage).div(100),
"Offset error"
);

if (tokens == 0) {
payable(msg.sender).transfer(msg.value);
}
}

function retrieve(address addressToTransferTo, uint256 amount) external restricted {
function retrieve(
address addressToTransferTo,
uint256 amount
) external restricted {
payable(addressToTransferTo).transfer(amount);
}

Expand All @@ -80,4 +126,4 @@ contract TokensManager is Initializable, AccessControlUpgradeable {
function setOffsetSlippage(uint8 slippage) external restricted {
offsetSlippage = slippage;
}
}
}
Loading

0 comments on commit d5358f1

Please sign in to comment.