Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove alchemica refund #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,11 @@ contract TestInstallationFacet is Modifiers {
//next level
InstallationType memory nextInstallation = s.installationTypes[prevInstallation.nextLevelId];

//take the required alchemica
address[4] memory alchemicaAddresses = realm.getAlchemicaAddresses();
// LibItems._splitAlchemica(nextInstallation.alchemicaCost, alchemicaAddresses);

require(prevInstallation.nextLevelId > 0, "TestInstallationFacet: Maximum upgrade reached");
require(prevInstallation.installationType == nextInstallation.installationType, "TestInstallationFacet: Wrong installation type");
require(prevInstallation.alchemicaType == nextInstallation.alchemicaType, "TestInstallationFacet: Wrong alchemicaType");
require(prevInstallation.level == nextInstallation.level - 1, "TestInstallationFacet: Wrong installation level");

uint256 gltrAmount = uint256(_gltr) * 1e18;
// IERC20(s.gltr).transferFrom(msg.sender, 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, gltrAmount); //should revert if user doesnt have enough GLTR

//prevent underflow if user sends too much GLTR
if (_gltr > nextInstallation.craftTime) revert("TestInstallationFacet: Too much GLTR");

Expand Down
28 changes: 0 additions & 28 deletions contracts/RealmDiamond/facets/RealmFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,38 +129,10 @@ contract RealmFacet is Modifiers {
"RealmFacet: Invalid signature"
);

InstallationDiamondInterface installationsDiamond = InstallationDiamondInterface(s.installationsDiamond);
InstallationDiamondInterface.InstallationType memory installation = installationsDiamond.getInstallationType(_installationId);

LibRealm.removeInstallation(_realmId, _installationId, _x, _y);
InstallationDiamondInterface(s.installationsDiamond).unequipInstallation(msg.sender, _realmId, _installationId);
LibAlchemica.reduceTraits(_realmId, _installationId, false);

//Process refund
if (installationsDiamond.getInstallationUnequipType(_installationId) == 0) {
//Loop through each level of the installation.
//@todo: For now we can use the ID order to get the cost of previous upgrades. But in the future we'll need to add some data redundancy.
uint256 currentLevel = installation.level;
uint256[] memory alchemicaRefund = new uint256[](4);
for (uint256 index = 0; index < currentLevel; index++) {
InstallationDiamondInterface.InstallationType memory prevInstallation = installationsDiamond.getInstallationType(_installationId - index);

//Loop through each Alchemica cost
for (uint256 i; i < prevInstallation.alchemicaCost.length; i++) {
//Only half of the cost is refunded
alchemicaRefund[i] += prevInstallation.alchemicaCost[i] / 2;
}
}

for (uint256 j = 0; j < alchemicaRefund.length; j++) {
//don't send 0 refunds
if (alchemicaRefund[j] > 0) {
IERC20 alchemica = IERC20(s.alchemicaAddresses[j]);
alchemica.transfer(msg.sender, alchemicaRefund[j]);
}
}
}

emit UnequipInstallation(_realmId, _installationId, _x, _y);
}

Expand Down
39 changes: 2 additions & 37 deletions test/realm/refundInstallationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,14 @@ describe("Testing Installation Refund", async function () {
)) as ERC20;
const currentFud = await fud.balanceOf(testAddress);
const currentFomo = await fomo.balanceOf(testAddress);
console.log(currentFud);
console.log(currentFomo);

const lvl1 = installationTypes[0]; //id 10
const lvl2 = installationTypes[1]; //id 11
const lvl3 = installationTypes[2]; //id 12
const lvl4 = installationTypes[3]; //id 13

const lvls = [lvl1, lvl2, lvl3, lvl4];

const costs: BigNumber[] = [
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
];

const refund: BigNumber[] = [
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
];

lvls.forEach((lvl) => {
lvl.alchemicaCost.forEach((amt, index) => {
costs[index] = costs[index].add(amt);

const refundAmt = BigNumber.from(amt).div(2);
refund[index] = refund[index].add(refundAmt);
});
});

const sig = genEquipInstallationSignature(testParcelId, 13, 8, 8);

await realmFacet.unequipInstallation(testParcelId, 13, 8, 8, sig);
const fudAfterBal = await fud.balanceOf(testAddress);
const fomoAfterBal = await fomo.balanceOf(testAddress);

const fudDiff = ethers.utils.formatEther(fudAfterBal.sub(currentFud));
const fomoDiff = ethers.utils.formatEther(fomoAfterBal.sub(currentFomo));

expect(Number(fudDiff)).to.equal(refund[0].toNumber());
expect(Number(fomoDiff)).to.equal(refund[1].toNumber());
expect(fudAfterBal).to.equal(currentFud);
expect(fomoAfterBal).to.equal(currentFomo);
});
});