// Web3 연결
async loadWeb3() {
if(window.ethereum) {
window.web3 = new Web3(window.ethereum);
await window.ethereum.enable();
}
else if(window.web3) {
window.web3 = new Web3(window.web3.currentProvider);
}
else {
window.alert('No ethereum browser detected! Check out MetaMask!');
}
}- 예금 및 출금 기능을 위한 버튼 / 오른쪽 상단의 현재 연결된 MetaMask 계정주소 / 예금 금액에 따른 Reward Tokens 보상
// staking function
function depositTokens(uint256 _amount) public {
// require staking amount to be greater than zero
require(_amount > 0, 'amount cannot be 0');
// 스테이킹할 tether tokens
tether.transferFrom(msg.sender, address(this), _amount);
// Update staking Balance
stakingBalance[msg.sender] += _amount;
if(!hasStaked[msg.sender]) {
stakers.push(msg.sender);
}
// Update staking Balance
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
} // Tokens 예금 함수
stakeTokens = (amount) => {
this.setState({loading: true});
this.state.tether.methods.approve(this.state.decentralBank._address, amount).send({from: this.state.account}).on('transactionHash', (hash) => {
this.state.decentralBank.methods.depositTokens(amount).send({from: this.state.account}).on('transactionHash', (hash) => {
this.setState({loading: false})
});
});
}// unstake tokens
function unstakeTokens() public {
uint balance = stakingBalance[msg.sender];
require(balance > 0, 'staking balance cannot be less than zero');
// 은행 주소에서 고객 주소로 언스테이킹
tether.transfer(msg.sender, balance);
// reset staking balance
stakingBalance[msg.sender] = 0;
// Update staking status
isStaking[msg.sender] = false;
} // Tokens 출금 함수
unstakeTokens = () => {
this.setState({loading: true});
this.state.decentralBank.methods.unstakeTokens().send({from: this.state.account}).on('transactionHash', (hash) => {
this.setState({loading: false})
});
}// issue rewards
function issueTokens() public {
// require the owner to issue tokens only
//require(msg.sender == owner, 'caller must be owner');
for(uint i=0; i<stakers.length; i++) {
address recipient = stakers[i];
uint balance = stakingBalance[recipient] / 10; // devide 10 to create percentage incentive
if(balance > 0){
rwd.transfer(recipient, balance);
}
}
}// Tokens Rewards 함수
issueTokens = () => {
this.setState({loading: true})
this.state.decentralBank.methods.issueTokens().send({from: this.state.account}).on('transactionHash', (hash) => {
this.setState({loading: false})
});
}



