Skip to content

Commit

Permalink
Remove exchange function
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Sep 25, 2023
1 parent 1875de6 commit d97ce35
Show file tree
Hide file tree
Showing 13 changed files with 687 additions and 1,810 deletions.
80 changes: 0 additions & 80 deletions packages/contracts/contracts/Ledger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,6 @@ contract Ledger {
event Deposited(bytes32 email, uint256 depositAmount, uint256 value, uint256 balanceToken, address account);
/// @notice 토큰을 인출했을 때 발생하는 이벤트
event Withdrawn(bytes32 email, uint256 withdrawAmount, uint256 value, uint256 balanceToken, address account);
/// @notice 포인트를 토큰으로 교환했을 때 발생하는 이벤트
event ExchangedPointToToken(
bytes32 email,
uint256 amountPoint,
uint256 amountToken,
uint256 balancePoint,
uint256 balanceToken
);
/// @notice 토큰을 포인트로 교환했을 때 발생하는 이벤트
event ExchangedTokenToPoint(
bytes32 email,
uint256 amountPoint,
uint256 amountToken,
uint256 balancePoint,
uint256 balanceToken
);

/// @notice 생성자
/// @param _foundationAccount 재단의 계정
Expand Down Expand Up @@ -378,70 +362,6 @@ contract Ledger {
emit Withdrawn(email, _amount, amountPoint, tokenLedger[email], msg.sender);
}

/// @notice 포인트를 토큰으로 교환합니다
/// @dev 중계서버를 통해서 호출됩니다.
/// @param _email 사용자의 이메일 해시
/// @param _amountPoint 교환할 포인트의 량
/// @param _signer 사용자의 주소
/// @param _signature 서명
function exchangePointToToken(
bytes32 _email,
uint256 _amountPoint,
address _signer,
bytes calldata _signature
) public {
bytes32 dataHash = keccak256(abi.encode(_email, _amountPoint, _signer, nonce[_signer]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _signer, "Invalid signature");
address userAddress = linkCollection.toAddress(_email);
require(userAddress != address(0x00), "Unregistered email-address");
require(userAddress == _signer, "Invalid address");

require(pointLedger[_email] >= _amountPoint, "Insufficient balance");

uint256 amountToken = convertPointToToken(_amountPoint);
require(tokenLedger[foundationAccount] >= amountToken, "Insufficient foundation balance");

pointLedger[_email] -= _amountPoint;

tokenLedger[_email] += amountToken;
tokenLedger[foundationAccount] -= amountToken;

nonce[_signer]++;

emit ExchangedPointToToken(_email, _amountPoint, amountToken, pointLedger[_email], tokenLedger[_email]);
}

/// @notice 토큰을 포인트로 교환합니다
/// @dev 중계서버를 통해서 호출됩니다.
/// @param _email 사용자의 이메일 해시
/// @param _amountToken 교환할 토큰의 량
/// @param _signer 사용자의 주소
/// @param _signature 서명
function exchangeTokenToPoint(
bytes32 _email,
uint256 _amountToken,
address _signer,
bytes calldata _signature
) public {
bytes32 dataHash = keccak256(abi.encode(_email, _amountToken, _signer, nonce[_signer]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _signer, "Invalid signature");
address userAddress = linkCollection.toAddress(_email);
require(userAddress != address(0x00), "Unregistered email-address");
require(userAddress == _signer, "Invalid address");

require(tokenLedger[_email] >= _amountToken, "Insufficient balance");

tokenLedger[_email] -= _amountToken;
tokenLedger[foundationAccount] += _amountToken;

uint256 amountPoint = convertTokenToPoint(_amountToken);
pointLedger[_email] += amountPoint;

nonce[_signer]++;

emit ExchangedTokenToPoint(_email, amountPoint, _amountToken, pointLedger[_email], tokenLedger[_email]);
}

/// @notice 포인트의 잔고를 리턴한다
/// @param _hash 이메일의 해시
function pointBalanceOf(bytes32 _hash) public view returns (uint256) {
Expand Down
10 changes: 0 additions & 10 deletions packages/contracts/deploy/bosagora_devnet/05_ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const tx8 = await ledgerContract.connect(signer).deposit(depositAmount);
console.log(`Deposit user's amount (tx: ${tx8.hash})...`);
await tx8.wait();

const exchangeAmount = balance.div(4);
const userAccount = ContractUtils.sha256String(user.email);
const nonce = await ledgerContract.nonceOf(user.address);
const signature = await ContractUtils.signExchange(signer, userAccount, exchangeAmount, nonce);
const tx9 = await ledgerContract
.connect(await ethers.getSigner(deployer))
.exchangeTokenToPoint(userAccount, exchangeAmount, user.address, signature);
console.log(`Exchange token to point (tx: ${tx9.hash})...`);
await tx9.wait();
}
}
};
Expand Down
187 changes: 0 additions & 187 deletions packages/contracts/test/Ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,193 +926,6 @@ describe("Test for Ledger", () => {
});
});

context("Exchange token & point", () => {
before("Deploy", async () => {
await deployAllContract();
});

before("Prepare Token", async () => {
for (const elem of users) {
await tokenContract.connect(deployer).transfer(elem.address, amount.value);
}
});

context("Prepare shop data", () => {
it("Add Shop Data", async () => {
for (const elem of shopData) {
const email = ContractUtils.sha256String(elem.email);
await expect(shopCollection.connect(validator1).add(elem.shopId, elem.timestamp, email))
.to.emit(shopCollection, "AddedShop")
.withArgs(elem.shopId, elem.timestamp, email);
}
expect(await shopCollection.shopsLength()).to.equal(shopData.length);
});
});

context("Prepare email-address", () => {
it("Link email-address", async () => {
const nonce = await linkCollectionContract.nonceOf(users[0].address);
const hash = emailHashes[0];
const signature = await ContractUtils.sign(users[0], hash, nonce);
requestId = ContractUtils.getRequestId(hash, users[0].address, nonce);
await expect(
linkCollectionContract.connect(relay).addRequest(requestId, hash, users[0].address, signature)
)
.to.emit(linkCollectionContract, "AddedRequestItem")
.withArgs(requestId, hash, users[0].address);
await linkCollectionContract.connect(validator1).voteRequest(requestId);
await linkCollectionContract.connect(validator1).countVote(requestId);
});
});

context("Exchange token to point", () => {
const amountToken = BigNumber.from(amount.value);
const amountPoint = amountToken.mul(price).div(multiple);

before("Deposit token", async () => {
await tokenContract.connect(users[0]).approve(ledgerContract.address, amountToken);
await expect(ledgerContract.connect(users[0]).deposit(amountToken)).to.emit(
ledgerContract,
"Deposited"
);
});

it("Invalid signature", async () => {
const nonce = await ledgerContract.nonceOf(users[0].address);
const signature = await ContractUtils.signExchange(users[0], emailHashes[0], amountToken, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangeTokenToPoint(emailHashes[0], amountToken, users[1].address, signature)
).to.revertedWith("Invalid signature");
});

it("Unregistered email-address", async () => {
const nonce = await ledgerContract.nonceOf(users[1].address);
const signature = await ContractUtils.signExchange(users[1], emailHashes[1], amountToken, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangeTokenToPoint(emailHashes[1], amountToken, users[1].address, signature)
).to.revertedWith("Unregistered email-address");
});

it("Invalid address", async () => {
const nonce = await ledgerContract.nonceOf(users[1].address);
const signature = await ContractUtils.signExchange(users[1], emailHashes[0], amountToken, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangeTokenToPoint(emailHashes[0], amountToken, users[1].address, signature)
).to.revertedWith("Invalid address");
});

it("Insufficient balance", async () => {
const nonce = await ledgerContract.nonceOf(users[0].address);
const signature = await ContractUtils.signExchange(
users[0],
emailHashes[0],
amountToken.mul(100),
nonce
);
await expect(
ledgerContract
.connect(relay)
.exchangeTokenToPoint(emailHashes[0], amountToken.mul(100), users[0].address, signature)
).to.revertedWith("Insufficient balance");
});

it("Success", async () => {
const oldFoundationTokenBalance = await ledgerContract.tokenBalanceOf(foundationAccount);
const nonce = await ledgerContract.nonceOf(users[0].address);
const signature = await ContractUtils.signExchange(users[0], emailHashes[0], amountToken, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangeTokenToPoint(emailHashes[0], amountToken, users[0].address, signature)
)
.to.emit(ledgerContract, "ExchangedTokenToPoint")
.withNamedArgs({
email: emailHashes[0],
amountToken,
amountPoint,
});
expect(await ledgerContract.tokenBalanceOf(foundationAccount)).to.deep.equal(
oldFoundationTokenBalance.add(amountToken)
);
});
});

context("Exchange point to token", () => {
const amountToken = BigNumber.from(amount.value);
const amountPoint = amountToken.mul(price).div(multiple);
it("Invalid signature", async () => {
const nonce = await ledgerContract.nonceOf(users[0].address);
const signature = await ContractUtils.signExchange(users[0], emailHashes[0], amountPoint, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangePointToToken(emailHashes[0], amountPoint, users[1].address, signature)
).to.revertedWith("Invalid signature");
});

it("Unregistered email-address", async () => {
const nonce = await ledgerContract.nonceOf(users[1].address);
const signature = await ContractUtils.signExchange(users[1], emailHashes[1], amountPoint, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangePointToToken(emailHashes[1], amountPoint, users[1].address, signature)
).to.revertedWith("Unregistered email-address");
});

it("Invalid address", async () => {
const nonce = await ledgerContract.nonceOf(users[1].address);
const signature = await ContractUtils.signExchange(users[1], emailHashes[0], amountPoint, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangePointToToken(emailHashes[0], amountPoint, users[1].address, signature)
).to.revertedWith("Invalid address");
});

it("Insufficient balance", async () => {
const nonce = await ledgerContract.nonceOf(users[0].address);
const signature = await ContractUtils.signExchange(
users[0],
emailHashes[0],
amountPoint.mul(100),
nonce
);
await expect(
ledgerContract
.connect(relay)
.exchangePointToToken(emailHashes[0], amountPoint.mul(100), users[0].address, signature)
).to.revertedWith("Insufficient balance");
});

it("Success", async () => {
const oldFoundationTokenBalance = await ledgerContract.tokenBalanceOf(foundationAccount);
const nonce = await ledgerContract.nonceOf(users[0].address);
const signature = await ContractUtils.signExchange(users[0], emailHashes[0], amountPoint, nonce);
await expect(
ledgerContract
.connect(relay)
.exchangePointToToken(emailHashes[0], amountPoint, users[0].address, signature)
)
.to.emit(ledgerContract, "ExchangedPointToToken")
.withNamedArgs({
email: emailHashes[0],
amountPoint,
amountToken,
});
expect(await ledgerContract.tokenBalanceOf(foundationAccount)).to.deep.equal(
oldFoundationTokenBalance.sub(amountToken)
);
});
});
});

context("Clearing for shops", () => {
const purchaseData: PurchaseData[] = [
{
Expand Down
Loading

0 comments on commit d97ce35

Please sign in to comment.