<commit_before>/**
 * ENS FLOW: https://docs.ens.domains/en/latest/userguide.html
 */

const Web3 = require('web3');
const web3 = new Web3();
const ENS = require('ethereum-ens');
const contracts = require('./abi/contracts.js');
const abi = require('ethereumjs-abi');
const dAppService = require('./dAppService.js');

let ens;

const setWeb3Provider = () => {
  web3.setProvider(new web3.providers.HttpProvider(process.env.PROVIDER));
};

setWeb3Provider();

/**
 * 
 * @param {*} address 
 */
export const getAddressByEns = (address) => {
  ens = new ENS(web3, process.env.ENS_ADDRESS);
  return ens.resolver(address).addr();
}

/**
 * STEP 1 確認一下該.eth狀態，回傳 tuple 多維度資訊
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L174
 * return tuple
 * @param {*} name 
 */
export const entries = (name) => {
  return contracts.ethRegistrar.entries(web3.sha3(name));
}

/**
 * STEP 2 開標，選擇網域名稱並且開標
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L296
 * return transactionHash
 * @param {*} name 
 * @param {*} privateKey
 */
export const startAuction = (name, privateKey) => {
  let fromAddress = dAppService.getAddressByPrivateKey(privateKey);
  let ethRegistrarAddress = contracts.ens.owner(contracts.namehash('eth'));
  let byteData = "0x" + 
                abi.methodID("startAuction", [ "bytes32" ]).toString("hex") + 
                abi.rawEncode([ "bytes32" ], [ name ]).toString("hex");
  const payload = {
    from: fromAddress,
    to: ethRegistrarAddress,
    value: '0x0',
    data: byteData,
    privateKey: privateKey
  };
  return dAppService.sendRawTransaction(payload);
}

/**
 * STEP 3 加密投標資訊，選定網域名稱設定混淆投標金額以及密碼
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L333
 * @param {*} name 
 * @param {*} wei 
 * @param {*} secret
 */
export const shaBid = (name, ether, secret) => {
  return contracts.ethRegistrar.shaBid(web3.sha3(name), web3.toWei(ether, "ether"), web3.sha3(secret));
}

/**
 * STEP 3 投標，將加密後的資訊發送至合約
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L350
 * @param {*} name 
 * @param {*} ether 
 * @param {*} secret 
 * @param {*} privateKey 
 */
export const newBid = (name, ether, secret, privateKey) => {
  let fromAddress = dAppService.getAddressByPrivateKey(privateKey);
  let ethRegistrarAddress = contracts.ens.owner(contracts.namehash('eth'));
  let bid = contracts.ethRegistrar.shaBid(web3.sha3(name), web3.toWei(ether, "ether"), web3.sha3(secret));
  let byteData = "0x" +
                abi.methodID("newBid", [ "bytes32" ]).toString("hex") +
                abi.rawEncode([ "bytes32" ], [ bid ]).toString("hex");
  const payload = {
    from: fromAddress,
    to: ethRegistrarAddress,
    value: '0x0',
    data: byteData,
    privateKey: privateKey
  };
  return dAppService.sendRawTransaction(payload);
}

/**
 * STEP 4 揭標，將資訊發送至合約
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L381
 * @param {*} name 
 * @param {*} ether 
 * @param {*} secret 
 * @param {*} privateKey 
 */
export const unsealBid = (name, ether, secret, privateKey) => {
  let fromAddress = dAppService.getAddressByPrivateKey(privateKey);
  let ethRegistrarAddress = contracts.ens.owner(contracts.namehash('eth'));
  let byteData = "0x" +
                abi.methodID("unsealBid", [ "bytes32", "uint", "bytes32" ]).toString("hex") +
                abi.rawEncode([ "bytes32" ], [ web3.sha3(name) ]).toString("hex") +
                abi.rawEncode([ "uint" ], [ web3.toWei(ether, "ether") ]).toString("hex") + 
                abi.rawEncode([ "bytes32" ], [ web3.sha3(secret) ]).toString("hex");
  const payload = {
    from: fromAddress,
    to: ethRegistrarAddress,
    value: '0x0',
    data: byteData,
    privateKey: privateKey
  };
  return dAppService.sendRawTransaction(payload);<commit_msg>Fix newBid for payable ether value<commit_after>/**
 * ENS FLOW: https://docs.ens.domains/en/latest/userguide.html
 */

const Web3 = require('web3');
const web3 = new Web3();
const ENS = require('ethereum-ens');
const contracts = require('./abi/contracts.js');
const abi = require('ethereumjs-abi');
const dAppService = require('./dAppService.js');

let ens;

const setWeb3Provider = () => {
  web3.setProvider(new web3.providers.HttpProvider(process.env.PROVIDER));
};

setWeb3Provider();

/**
 * 
 * @param {*} address 
 */
export const getAddressByEns = (address) => {
  ens = new ENS(web3, process.env.ENS_ADDRESS);
  return ens.resolver(address).addr();
}

/**
 * STEP 1 確認一下該.eth狀態，回傳 tuple 多維度資訊
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L174
 * return tuple
 * @param {*} name 
 */
export const entries = (name) => {
  return contracts.ethRegistrar.entries(web3.sha3(name));
}

/**
 * STEP 2 開標，選擇網域名稱並且開標
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L296
 * return transactionHash
 * @param {*} name 
 * @param {*} privateKey
 */
export const startAuction = (name, privateKey) => {
  let fromAddress = dAppService.getAddressByPrivateKey(privateKey);
  let ethRegistrarAddress = contracts.ens.owner(contracts.namehash('eth'));
  let byteData = "0x" + 
                abi.methodID("startAuction", [ "bytes32" ]).toString("hex") + 
                abi.rawEncode([ "bytes32" ], [ name ]).toString("hex");
  const payload = {
    from: fromAddress,
    to: ethRegistrarAddress,
    value: '0x0',
    data: byteData,
    privateKey: privateKey
  };
  return dAppService.sendRawTransaction(payload);
}

/**
 * STEP 3 加密投標資訊，選定網域名稱設定混淆投標金額以及密碼
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L333
 * @param {*} name 
 * @param {*} wei 
 * @param {*} secret
 */
export const shaBid = (name, ether, secret) => {
  return contracts.ethRegistrar.shaBid(web3.sha3(name), web3.toWei(ether, "ether"), web3.sha3(secret));
}

/**
 * STEP 3 投標，將加密後的資訊發送至合約
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L350
 * @param {*} name 
 * @param {*} ether 
 * @param {*} secret 
 * @param {*} privateKey 
 */
export const newBid = (name, ether, secret, privateKey) => {
  let fromAddress = dAppService.getAddressByPrivateKey(privateKey);
  let ethRegistrarAddress = contracts.ens.owner(contracts.namehash('eth'));
  let bid = contracts.ethRegistrar.shaBid(web3.sha3(name), web3.toWei(ether, "ether"), web3.sha3(secret));
  let byteData = "0x" +
                abi.methodID("newBid", [ "bytes32" ]).toString("hex") +
                abi.rawEncode([ "bytes32" ], [ bid ]).toString("hex");
  const payload = {
    from: fromAddress,
    to: ethRegistrarAddress,
    value: web3.toHex(web3.toWei(ether, "ether")),
    data: byteData,
    privateKey: privateKey
  };
  return dAppService.sendRawTransaction(payload);
}

/**
 * STEP 4 揭標，將資訊發送至合約
 * https://github.com/ethereum/ens/blob/master/contracts/HashRegistrarSimplified.sol#L381
 * @param {*} name 
 * @param {*} ether 
 * @param {*} secret 
 * @param {*} privateKey 
 */
export const unsealBid = (name, ether, secret, privateKey) => {
  let fromAddress = dAppService.getAddressByPrivateKey(privateKey);
  let ethRegistrarAddress = contracts.ens.owner(contracts.namehash('eth'));
  let byteData = "0x" +
                abi.methodID("unsealBid", [ "bytes32", "uint", "bytes32" ]).toString("hex") +
                abi.rawEncode([ "bytes32" ], [ web3.sha3(name) ]).toString("hex") +
                abi.rawEncode([ "uint" ], [ web3.toWei(ether, "ether") ]).toString("hex") + 
                abi.rawEncode([ "bytes32" ], [ web3.sha3(secret) ]).toString("hex");
  const payload = {
    from: fromAddress,
    to: ethRegistrarAddress,
    value: '0x0',
    data: byteData,
    privateKey: privateKey
  };
  return dAppService.sendRawTransaction(payload);
{'fraction_non_alphanumeric': 0.16466836734693877, 'fraction_numerical': 0.018112244897959182, 'mean_word_length': 3.831176833025262, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5357223', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2889, 'n_words': 483}