Skip to content

Commit

Permalink
Merge pull request #75 from DarkFlorist/token-returning
Browse files Browse the repository at this point in the history
return only created and received names minus the final child
  • Loading branch information
KillariDev authored Oct 26, 2024
2 parents eb4c28c + 91e8f20 commit da5fd74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/ts/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const ethSimulateTransactions = async (rpc: string, transactions: readonly Block
const runTests = async () => {
console.log(`Reneval manager: ${ getOpenRenewalManagerAddress() }`)
const testOpenRenewalManagerAddressIsConstant = async () => {
const openRenewalManagerAddress = 0x094f4b81679aaB4aD7538c2A4aB1343294864F6Fn
const openRenewalManagerAddress = 0x55B75C29834DFd71ef30E2C828a938394564f0C0n
const calculatedAddress = BigInt(getOpenRenewalManagerAddress())
if (calculatedAddress !== openRenewalManagerAddress) throw new Error(`The Address of Open Renewal Manager has changed to ${ addressString(calculatedAddress) }.`)
}
Expand Down
1 change: 1 addition & 0 deletions solidity/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const compilePetalLock = async () => {
'OpenRenewalManager.sol': { content: await fs.readFile('contracts/OpenRenewalManager.sol', 'utf8') },
},
settings: {
viaIR: true,
optimizer: {
enabled: true,
runs: 500,
Expand Down
37 changes: 26 additions & 11 deletions solidity/contracts/PetalLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ address constant ENS_REGISTRY_WITH_FALLBACK = 0x00000000000C2E074eC69A0dFb2997BA
address constant ENS_ETH_REGISTRAR_CONTROLLER = 0x253553366Da8546fC250F225fe3d25d0C782303b;

// Open Renewal Manager contract address
address constant OPEN_RENEWAL_MANAGER = 0x094f4b81679aaB4aD7538c2A4aB1343294864F6F;
address constant OPEN_RENEWAL_MANAGER = 0x55B75C29834DFd71ef30E2C828a938394564f0C0;

// ENS Fuses
uint16 constant CANNOT_UNWRAP = 1;
Expand Down Expand Up @@ -131,12 +131,16 @@ contract PetalLock {

// CREATE SUBDOMAINS //
// create nodes and approve open renewal manager. Do not create the first domain, it needs to be created already
uint256 createdSubdomains = 0;
uint256[] memory createdSubDomainNodesAndZeros = new uint256[](pathLength);
for (uint256 i = 1; i < pathLength; i++) {
bytes32 node = nodePathToChild[i];
// check that the record exists, if not, lets create it
if (!ensRegistry.recordExists(node)) {
ensNameWrapper.setSubnodeRecord(nodePathToChild[i - 1], labelPathToChild[i], address(this), ENS_PUBLIC_RESOLVER, 0, 0, MAX_UINT64);
ensNameWrapper.approve(OPEN_RENEWAL_MANAGER, uint256(node));
createdSubDomainNodesAndZeros[createdSubdomains] = uint256(node);
createdSubdomains++;
} else {
// the node needs to be wrapped
require(ensNameWrapper.isWrapped(node), 'PetalLock: Child node is not wrapped');
Expand All @@ -145,13 +149,13 @@ contract PetalLock {
}
}
}

bytes32 finalChildNode = nodePathToChild[finalChildIndex];
// set content hash and address
if (contenthash.length != 0) { ensPublicResolver.setContenthash(nodePathToChild[finalChildIndex], contenthash); }
if (resolutionAddress != address(0x0)) { ensPublicResolver.setAddr(nodePathToChild[finalChildIndex], resolutionAddress); }
if (contenthash.length != 0) { ensPublicResolver.setContenthash(finalChildNode, contenthash); }
if (resolutionAddress != address(0x0)) { ensPublicResolver.setAddr(finalChildNode, resolutionAddress); }

if (finalChildIndex == 0) { // the second level domain is made immutable
(, uint32 finalChildFuses,) = ensNameWrapper.getData(uint256(nodePathToChild[finalChildIndex]));
(, uint32 finalChildFuses,) = ensNameWrapper.getData(uint256(finalChildNode));
if (finalChildFuses & ONLY_CHILD_FUSES != ONLY_CHILD_FUSES) {
ensNameWrapper.setFuses(nodePathToChild[0], ONLY_CHILD_FUSES);
}
Expand All @@ -170,22 +174,33 @@ contract PetalLock {
}

// burn the final child fuses
(, uint32 finalChildFuses,) = ensNameWrapper.getData(uint256(nodePathToChild[finalChildIndex]));
(, uint32 finalChildFuses,) = ensNameWrapper.getData(uint256(finalChildNode));
if (finalChildFuses & FINAL_CHILD_FUSES_TO_BURN != FINAL_CHILD_FUSES_TO_BURN) {
ensNameWrapper.setChildFuses(nodePathToChild[finalChildIndex - 1], keccak256(abi.encodePacked(labelPathToChild[finalChildIndex])), FINAL_CHILD_FUSES_TO_BURN, MAX_UINT64);
}
}

// move the final child to renewal manager, so it can be renewed by anyone (otherwise its technically burned)
ensNameWrapper.safeTransferFrom(address(this), OPEN_RENEWAL_MANAGER, uint256(nodePathToChild[finalChildIndex]), 1, bytes(''));
ensNameWrapper.safeTransferFrom(address(this), OPEN_RENEWAL_MANAGER, uint256(finalChildNode), 1, bytes(''));

// return rest of the tokens to the sender
uint256 returnableTokensLength = pathLength - 1;
uint256 returnableTokensLength = createdSubdomains + idsLength - 1; // return all except the final child
uint256[] memory returnableTokens = new uint256[](returnableTokensLength);
uint256[] memory returnableAmounts = new uint256[](returnableTokensLength);
for (uint256 i = 0; i < returnableTokensLength; i++) {
returnableTokens[i] = uint256(nodePathToChild[i]);
returnableAmounts[i] = 1;
uint256 returnableTokenIndex = 0;

for (uint256 i = 0; i < pathLength; i++) {
if (createdSubDomainNodesAndZeros[i] == 0) continue;
if (createdSubDomainNodesAndZeros[i] == uint256(finalChildNode)) continue;
returnableTokens[returnableTokenIndex] = createdSubDomainNodesAndZeros[i];
returnableAmounts[returnableTokenIndex] = 1;
returnableTokenIndex++;
}
for (uint256 idIndex = 0; idIndex < idsLength; idIndex++) {
if (tokenIds[idIndex] == uint256(finalChildNode)) continue;
returnableTokens[returnableTokenIndex] = tokenIds[idIndex];
returnableAmounts[returnableTokenIndex] = 1;
returnableTokenIndex++;
}

ensNameWrapper.safeBatchTransferFrom(address(this), originalOwner, returnableTokens, returnableAmounts, bytes(''));
Expand Down

0 comments on commit da5fd74

Please sign in to comment.