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

Seongho #3

Open
wants to merge 2 commits into
base: main
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# remark
# remark

주석이 없어도 이해가 가능한 코드가 가장 좋은 코드라고 생각합니다. 다만 개발을 진행하다보면 어느정도 복잡성을 띄는 코드는 있기 마련입니다. 따라서 훗날의 나를 위해, 또 다른 누군가를 위해 주석은 필요하다고 생각합니다.

## jdocs를 적극 활용하자.

jdocs는 기존 주석과 다르게 주석 내용을 참조할 수 있습니다. 웬만한 IDE에서 지원되고, 코드에다가 마우스를 올려놓으면 해당 코드가 있는 파일로 굳이 들어가지 않아도 주석을 볼 수 있습니다. Markdown 형식으로 작성되기 때문에, 친숙하게 읽을 수 있다는 점이 장점입니다.

다음 경우에 효과적이라 생각합니다.

+ 책임소재를 분명히 헤야할 경우
+ 라이브러리 혹은 모듈화를 시켜 공유 해야할 경우
+ (js) parameter 및 return 값의 타입을 공유해야할 경우

93 changes: 93 additions & 0 deletions contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @author Jang Seongho
*
* @constant
* @type { function }
*
* @description
* NFT를 구매하는 함수입니다. 성공적으로 구매하면 true, 실패하면 false가 반환됩니다.
* 이더리움 네트워크 특성 상, 거래 처리에 10 ~ 20초 걸립니다.
*
* @param { eth } eth - recoil에 있는 ethState를 넣어주시면 됩니다.
* @param { String } collectionName
* 컬렉션 이름을 넣어주면 됩니다. datas/enum/collection_name_enum.js에 선언되어 있는 CollectionNameEnum 객체를 사용하시면 됩니다.
* @param { String } nftId - 문자열로 nft id를 입력해주시면 됩니다.
* @param { String } owner - 문자열로 NFT 소유자의 지갑 주소를 입력해주시면 됩니다.
* @param { String } price - 문자열로 NFT 가격을 입력해주시면 됩니다.
* @returns { boolean }
* true - 거래 성공
* false - 거래 실패
*
* @example
* ```js
* const { ethState, setEthState } = useEth();
*
* buyNft(ethState, CollectionNameEnum.LACK_OF_SLEEP_LAMA, "1", "0xqweqasdz1231212", "100").then((result) => {
* if(result) {
* // 거래 성공시 처리 로직
*
* return;
* }
*
* // 거래 실패시 처리 로직
* });
* ```
*
* @throws { Error } 잔고가 부족하다던가, NFT가 없다던가 등의 이유로 거래 시도 자체가 거절된 경우
*/
export const buyNft = async (eth, collectionName, nftId, owner, price) => {
return new Promise(async (resolve, reject) => {
const ERC1155TokenContract = isExistERC1155TokenByCollectionName(eth, collectionName);


if (ERC1155TokenContract === undefined) {
reject(new Error(`Not exist nft, collection name is ${collectionName}`));

return;
}

const balanceOfNft = await ERC1155TokenContract.methods
.balanceOf(owner, nftId)
.call();

assert(balanceOfNft > 0, "The balance of nft is 0");

try {
const nonceResult = await checkNonce();
const nonce = nonceResult.data;
const ownerInfo = eth.web3.eth.accounts.privateKeyToAccount('e7a36cc7c2c6aa2c49be21b5a8516982fce19e4337f3c0b56f73a401800eb1cf');

var dataTx = await ERC1155TokenContract.methods.safeTransferFrom(owner, eth.accounts[0], nftId, 1, "0x00").encodeABI();
var estimateGas = await ERC1155TokenContract.methods.safeTransferFrom(owner, eth.accounts[0], nftId, 1, "0x00").estimateGas({ from: ownerInfo.address });

var rawTx = {
to: ERC1155TokenContract._address,
from: ownerInfo.address,
nonce: nonce,
gas: estimateGas,
gasLimit: estimateGas,
gasPrice: eth.web3.utils.toWei('10', 'gwei'),
data: dataTx,
chainId: 5,
value: 0,
};

const signTx = await eth.web3.eth.accounts.signTransaction(rawTx, ownerInfo.privateKey);
eth.web3.currentProvider.send({
method: 'eth_sendRawTransaction',
params: [signTx.rawTransaction],
jsonrpc: "2.0",
}, (err, result) => {
if (err) {
console.log("err");
return;
}

console.log(result);
})
} catch (e) {
reject(new Error(`Can't send NFT from ${owner} to ${eth.accounts[0]}.`));
}

});
}