본문 바로가기
WATTO 프로젝트/백엔드

web3, 컨트렉트 연결

by gun_poo 2022. 2. 15.

Eth - version

순서

  • 프론트에서 컨트렉트 호출 api를 날린다
  • 서버단에서 api를 받고 컨트렉트로 신호를 보낸다

server

  • web3와 연결
const Web3 = require('web3');
const web3 = new Web3(
	new Web3.providers.HttpProvider(
		'https://ropsten.infura.io/v3/c2cc008afe67457fb9a4ee32408bcac6'
	)
);
  • abi 읽어 오고 변환해주기
const fs = require('fs');

const WTABI = fs.readFileSync('server/abi/WTToken.json', 'utf-8');
const NWTABI = fs.readFileSync('server/abi/NWTToken.json', 'utf-8');
const NFTABI = fs.readFileSync('server/abi/NFTWT.json', 'utf8');
const SWAPABI = fs.readFileSync('server/abi/TokenSwap.json', 'utf-8');

const nftAbi = JSON.parse(NFTABI);
const wtAbi = JSON.parse(WTABI); 
const nwtAbi = JSON.parse(NWTABI);
const swapAbi = JSON.parse(SWAPABI);
  • 컨트렉트 연결
const newContract = (web3, abi, ca) => {
	return new web3.eth.Contract(abi, ca, {
		from: process.env.SERVERADDRESS,
		gas: 3000000,
	});
};

const nftContract = newContract(web3, nftAbi, process.env.NFTTOKENCA);
const wtContract = newContract(web3, wtAbi, process.env.WTTOKENCA); 
const nwtContract = newContract(web3, nwtAbi, process.env.NWTTOKENCA); 
const swapContract = newContract(web3, swapAbi, process.env.SWAPCA);

klaytn - version

  • caver 연결
const Caver = require('caver-js');
const caver = new Caver(
	new Caver.providers.HttpProvider('https://api.baobab.klaytn.net:8651')
);
  • abi 읽어오고 변환
const fs = require('fs');

const KIPWTABI = fs.readFileSync('../abi/KIP_WTToken.json', 'utf-8');
const KIPNWTABI = fs.readFileSync('../abi/KIP_NWTToken.json', 'utf-8');
const KIPNFTABI = fs.readFileSync('../abi/KIP_NFTWT.json', 'utf8');
const KIPSWAPABI = fs.readFileSync('../abi/KIP_TokenSwap.json', 'utf-8');

const nftAbi = JSON.parse(KIPNFTABI);
const wtAbi = JSON.parse(KIPWTABI);
const nwtAbi = JSON.parse(KIPNWTABI);
const swapAbi = JSON.parse(KIPSWAPABI);
  • 컨트렉트 연결
const CavernewContract = (caver, abi, ca) => {
	return new caver.klay.Contract(abi, ca, {
		from: process.env.SERVERADDRESS,
		gas: 3000000,
	});
};

const nftContract = CavernewContract(caver, nftAbi, process.env.NFTTOKENCA);
const wtContract = CavernewContract(caver, wtAbi, process.env.WTTOKENCA);
const nwtContract = CavernewContract(caver, nwtAbi, process.env.NWTTOKENCA);
const swapContract = CavernewContract(caver, swapAbi, process.env.SWAPCA);

댓글