Skip to content

Commit

Permalink
MOD: transfer powers to user
Browse files Browse the repository at this point in the history
  • Loading branch information
cctv2206 committed Mar 25, 2024
1 parent 25838c7 commit b97a8eb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
35 changes: 28 additions & 7 deletions contracts/aime/AIMeFactoryV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ contract AIMeFactoryV2 is Ownable {
string data,
uint256 amount
);
event AIMePowerWithdraw(address aimeAddress, address to, uint256 amount);
event AIMeNFTUpdated(
address nftOwner,
address aimeAddress,
uint256 tokenId,
string data
);
event Received(address sender, uint amount);

mapping(address => uint256) public addressNonce;
Expand Down Expand Up @@ -109,13 +114,29 @@ contract AIMeFactoryV2 is Ownable {
);
}

// todo: update NFT data

function withdrawPowers(address payable aimeAddress, uint256 amount, bytes memory signature) public {
// todo: validate signature
function updateAIMeNFT(
address payable aimeAddress,
uint256 tokenId,
string memory data,
string memory image,
bytes memory signature
) public payable {
require(msg.value >= protocolFee, "Insufficient payment");
address signer = aimeSigners[aimeAddress];
if (signer != address(0)) {
bytes32 _msgHash = MessageHashUtils.toEthSignedMessageHash(
_genMessageHash(msg.sender, aimeAddress, tokenId, "", "", data, image, 0, addressNonce[msg.sender])
);
require(ECDSA.recover(_msgHash, signature) == signer, "Invalid signature");
addressNonce[msg.sender] += 1;
}

AIMeNFTV2 aime = AIMeNFTV2(aimeAddress);
aime.withdrawPowers(msg.sender, amount);
emit AIMePowerWithdraw(aimeAddress, msg.sender, amount);
aime.updateAIMeInfo(tokenId, msg.sender, data, image);

(bool success, ) = aimeAddress.call{value: msg.value}("");
require(success, "Failed to send Ether");
emit AIMeNFTUpdated(msg.sender, aimeAddress, tokenId, data);
}

function withdrawFee() public onlyOwner {
Expand Down
64 changes: 26 additions & 38 deletions contracts/aime/AIMeNFTV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
uint256 public constant AIME_POWER_TOTAL_AMOUNT = 1000000 * 1e18;
uint256 public constant AIME_POWER_SWAP_INIT_AMOUNT = 100000 * 1e18;
uint256 public constant AIME_NFT_PRICE_FACTOR = 12;
uint256 public constant AIME_POWER_TRADE_MIN_AMOUNT = 0.0001 * 1e18;
uint256 public constant NFT_HOLDING_PERIOD = 30 days;
uint256 public aimePowerReserved;
uint256 public swapPowerBalance;
Expand All @@ -23,8 +22,6 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
uint256 private _nextTokenId;
mapping(uint256 => AIMeInfo) public tokenContents;

mapping(address => uint256) public powerBalance;

error AIMeNFTUnauthorizedAccount(address account);
event Trade(
address trader,
Expand Down Expand Up @@ -116,15 +113,15 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
}

function buyPowers(uint256 amount) public payable {
require(amount < swapPowerBalance, "Insufficient power balance");
// eth amount
uint256 ethAmount = ((address(this).balance - msg.value) * amount) / (swapPowerBalance - amount);
uint256 ethAmount = ((address(this).balance - msg.value) * amount) /
(swapPowerBalance - amount);
require(msg.value == ethAmount, "Incorrect payment");

// transfer power to user
// store power in contract for AI fee
powerBalance[msg.sender] += amount;
// AIMePower power = AIMePower(aimePowerAddress);
// power.transfer(msg.sender, amount);
AIMePower power = AIMePower(aimePowerAddress);
power.transfer(msg.sender, amount);
swapPowerBalance -= amount;
emit Trade(msg.sender, true, amount, ethAmount, 0, 0);
}
Expand Down Expand Up @@ -155,13 +152,6 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
emit Trade(msg.sender, false, amount, ethAmount, 0, 0);
}

function withdrawPowers(address to, uint256 amount) public onlyFactory() {
require(powerBalance[to] >= amount, "Insufficient Power balance");
powerBalance[to] -= amount;
AIMePower power = AIMePower(aimePowerAddress);
power.transfer(to, amount);
}

function safeMint(
address to,
string memory key,
Expand Down Expand Up @@ -212,11 +202,9 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
} else {
amount = tokenContents[tokenId].amount;
}

// store user's power in contract
powerBalance[msg.sender] += amount;
// AIMePower power = AIMePower(aimePowerAddress);
// power.transfer(msg.sender, amount);

AIMePower power = AIMePower(aimePowerAddress);
power.transfer(msg.sender, amount);
emit TradeNFT(msg.sender, address(this), tokenId, amount);
}

Expand All @@ -240,12 +228,7 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
"allowance not enough"
);

// store user's power in contract
if (owner != address(this)) {
powerBalance[owner] += amount;
} else {
power.transferFrom(msg.sender, owner, amount);
}
power.transferFrom(msg.sender, owner, amount);

_safeTransfer(owner, msg.sender, tokenId);
tokenContents[tokenId].timestamp = block.timestamp;
Expand All @@ -258,11 +241,7 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {
_requireOwned(tokenId);

string memory tokenName = string(
abi.encodePacked(
name(),
" #",
tokenId.toString()
)
abi.encodePacked(name(), " #", tokenId.toString())
);
string memory imageUrl = tokenContents[tokenId].image;
string memory tradeUrl = string(
Expand All @@ -276,24 +255,33 @@ contract AIMeNFTV2 is ERC721, Ownable, ERC721Holder {

string memory attributes = string(
abi.encodePacked(
'[',
'{"trait_type": "type","value": "',tokenContents[tokenId].dataType,'"},',
'{"trait_type": "key","value": "',tokenContents[tokenId].key,'"},',
'{"trait_type": "data","value": "',tokenContents[tokenId].data,'"}',
']'
"[",
'{"trait_type": "type","value": "',
tokenContents[tokenId].dataType,
'"},',
'{"trait_type": "key","value": "',
tokenContents[tokenId].key,
'"},',
'{"trait_type": "data","value": "',
tokenContents[tokenId].data,
'"}',
"]"
)
);

string memory json = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "', tokenName,
'{"name": "',
tokenName,
'", "description": "A block of content of ',
name(),
". Trade at: ",
tradeUrl,
'", "attributes":', attributes,', "amount": "',
'", "attributes":',
attributes,
', "amount": "',
tokenContents[tokenId].amount.toString(),
'", "image": "',
imageUrl,
Expand Down

0 comments on commit b97a8eb

Please sign in to comment.