以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

文章前言

智能合约是一个运行在安全环境下的计算机程序,它可以直接控制数字资产,本篇文章我们主要介绍如何在公链环境中编译、部署、运行合约~

合约部署

合约创建

以太坊支持使用solidity编写的智能合约,用户可以使用Solidity编写一个自己所需要的之智能合约,例如:

pragma solidity ^0.8.4;
contract test{ function multiply(uint a) public returns(uint d){ return a *7; }}
合约编译

关于合约的编译我们可以使用solc也可以使用在线编译工具Remix来对合约进行编译操作:

Bytecode:

{  \\\"generatedSources\\\": [],  \\\"linkReferences\\\": {},  \\\"object\\\": \\\"608060405234801561001057600080fd5b506101be806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c3565b60405180910390f35b600060078261006f91906100de565b9050919050565b60008135905061008581610171565b92915050565b60006020828403121561009d57600080fd5b60006100ab84828501610076565b91505092915050565b6100bd81610138565b82525050565b60006020820190506100d860008301846100b4565b92915050565b60006100e982610138565b91506100f483610138565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561012d5761012c610142565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b61017a81610138565b811461018557600080fd5b5056fea2646970667358221220491d8f5c845eedc25e0382e27b3a1b760fe85cf6d03ce32f6c956adfa3d81b5864736f6c63430008040033\\\",  \\\"opcodes\\\": \\\"PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x1BE DUP1 PUSH2 0x20 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x2B JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0xC6888FA1 EQ PUSH2 0x30 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0x4A PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 PUSH2 0x45 SWAP2 SWAP1 PUSH2 0x8B JUMP JUMPDEST PUSH2 0x60 JUMP JUMPDEST PUSH1 0x40 MLOAD PUSH2 0x57 SWAP2 SWAP1 PUSH2 0xC3 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 PUSH1 0x7 DUP3 PUSH2 0x6F SWAP2 SWAP1 PUSH2 0xDE JUMP JUMPDEST SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 DUP2 CALLDATALOAD SWAP1 POP PUSH2 0x85 DUP2 PUSH2 0x171 JUMP JUMPDEST SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH1 0x0 PUSH1 0x20 DUP3 DUP5 SUB SLT ISZERO PUSH2 0x9D JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x0 PUSH2 0xAB DUP5 DUP3 DUP6 ADD PUSH2 0x76 JUMP JUMPDEST SWAP2 POP POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH2 0xBD DUP2 PUSH2 0x138 JUMP JUMPDEST DUP3 MSTORE POP POP JUMP JUMPDEST PUSH1 0x0 PUSH1 0x20 DUP3 ADD SWAP1 POP PUSH2 0xD8 PUSH1 0x0 DUP4 ADD DUP5 PUSH2 0xB4 JUMP JUMPDEST SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH1 0x0 PUSH2 0xE9 DUP3 PUSH2 0x138 JUMP JUMPDEST SWAP2 POP PUSH2 0xF4 DUP4 PUSH2 0x138 JUMP JUMPDEST SWAP3 POP DUP2 PUSH32 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF DIV DUP4 GT DUP3 ISZERO ISZERO AND ISZERO PUSH2 0x12D JUMPI PUSH2 0x12C PUSH2 0x142 JUMP JUMPDEST JUMPDEST DUP3 DUP3 MUL SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH1 0x0 DUP2 SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH32 0x4E487B7100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 MSTORE PUSH1 0x11 PUSH1 0x4 MSTORE PUSH1 0x24 PUSH1 0x0 REVERT JUMPDEST PUSH2 0x17A DUP2 PUSH2 0x138 JUMP JUMPDEST DUP2 EQ PUSH2 0x185 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP JUMP INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 0x49 SAR DUP16 0x5C DUP5 0x5E 0xED 0xC2 0x5E SUB DUP3 0xE2 PUSH28 0x3A1B760FE85CF6D03CE32F6C956ADFA3D81B5864736F6C6343000804 STOP CALLER \\\",  \\\"sourceMap\\\": \\\"27:101:0:-:0;;;;;;;;;;;;;;;;;;;\\\"}

ABI:

    [  {    \\\"inputs\\\": [      {        \\\"internalType\\\": \\\"uint256\\\",        \\\"name\\\": \\\"a\\\",        \\\"type\\\": \\\"uint256\\\"      }    ],    \\\"name\\\": \\\"multiply\\\",    \\\"outputs\\\": [      {        \\\"internalType\\\": \\\"uint256\\\",        \\\"name\\\": \\\"d\\\",        \\\"type\\\": \\\"uint256\\\"      }    ],    \\\"stateMutability\\\": \\\"nonpayable\\\",    \\\"type\\\": \\\"function\\\"  }]

    之后在bejson中转义成字符串http://www.bejson.com/jsonviewernew/

    [{\\\\\\\"constant\\\\\\\":false,\\\\\\\"inputs\\\\\\\":[{\\\\\\\"name\\\\\\\":\\\\\\\"a\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"uint256\\\\\\\"}],\\\\\\\"name\\\\\\\":\\\\\\\"multiply\\\\\\\",\\\\\\\"outputs\\\\\\\":[{\\\\\\\"name\\\\\\\":\\\\\\\"d\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"uint256\\\\\\\"}],\\\\\\\"payable\\\\\\\":false,\\\\\\\"type\\\\\\\":\\\\\\\"function\\\\\\\",\\\\\\\"stateMutability\\\\\\\":\\\\\\\"nonpayable\\\\\\\"}]

    合约部署

    启动公链的交互模式:

    sudo geth --networkid 666 --datadir /home/ubuntu/Private_eth/eth1 --identity \\\"node1\\\" --rpc --rpcport \\\"8545\\\" --rpcaddr \\\"192.168.174.212\\\" --nodiscover --rpcapi \\\"eth,net,web3,txpool,debug,miner\\\" --allow-insecure-unlock console

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后我们创建一个对象:

    var abi=JSON.parse(\\\'[{\\\\\\\"constant\\\\\\\":false,\\\\\\\"inputs\\\\\\\":[{\\\\\\\"name\\\\\\\":\\\\\\\"a\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"uint256\\\\\\\"}],\\\\\\\"name\\\\\\\":\\\\\\\"multiply\\\\\\\",\\\\\\\"outputs\\\\\\\":[{\\\\\\\"name\\\\\\\":\\\\\\\"d\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"uint256\\\\\\\"}],\\\\\\\"payable\\\\\\\":false,\\\\\\\"type\\\\\\\":\\\\\\\"function\\\\\\\",\\\\\\\"stateMutability\\\\\\\":\\\\\\\"nonpayable\\\\\\\"}]\\\')

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    myContract=web3.eth.contract(abi)

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后检查coinbase账户余额(如果这里的余额大于0,则继续,否则,开始挖矿,等你觉得差不多的时候再停止就OK)

    > eth.coinbase\\\"0x8b52dc34762a2a951406d3189bdba1b920e7cde9\\\"> eth.accounts[0]\\\"0x8b52dc34762a2a951406d3189bdba1b920e7cde9\\\"> web3.fromWei(eth.getBalance(eth.accounts[0]),\\\"ether\\\")20.000021>

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后解锁coinbase账户,并使用coinbase账户来部署合约

    personal.unlockAccount(eth.accounts[0],\\\"123456\\\",0)

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后预估手续费——149143 wei:

    bytecode=\\\"0x608060405234801561001057600080fd5b506101be806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c3565b60405180910390f35b600060078261006f91906100de565b9050919050565b60008135905061008581610171565b92915050565b60006020828403121561009d57600080fd5b60006100ab84828501610076565b91505092915050565b6100bd81610138565b82525050565b60006020820190506100d860008301846100b4565b92915050565b60006100e982610138565b91506100f483610138565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561012d5761012c610142565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b61017a81610138565b811461018557600080fd5b5056fea2646970667358221220491d8f5c845eedc25e0382e27b3a1b760fe85cf6d03ce32f6c956adfa3d81b5864736f6c63430008040033\\\"web3.eth.estimateGas({data:bytecode})

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后开始部署合约(这里增加一个回调函数为了看效果)

    var testContract = web3.eth.contract([{\\\"constant\\\":false,\\\"inputs\\\":[{\\\"name\\\":\\\"a\\\",\\\"type\\\":\\\"uint256\\\"}],\\\"name\\\":\\\"multiply\\\",\\\"outputs\\\":[{\\\"name\\\":\\\"d\\\",\\\"type\\\":\\\"uint256\\\"}],\\\"payable\\\":false,\\\"type\\\":\\\"function\\\",\\\"stateMutability\\\":\\\"nonpayable\\\"}]);var test = testContract.new(   {     from: web3.eth.accounts[0],      data: \\\'0x608060405234801561001057600080fd5b506101be806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c3565b60405180910390f35b600060078261006f91906100de565b9050919050565b60008135905061008581610171565b92915050565b60006020828403121561009d57600080fd5b60006100ab84828501610076565b91505092915050565b6100bd81610138565b82525050565b60006020820190506100d860008301846100b4565b92915050565b60006100e982610138565b91506100f483610138565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561012d5761012c610142565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b61017a81610138565b811461018557600080fd5b5056fea2646970667358221220491d8f5c845eedc25e0382e27b3a1b760fe85cf6d03ce32f6c956adfa3d81b5864736f6c63430008040033\\\',      gas: \\\'4700000\\\'   }, function (e, contract){    console.log(e, contract);    if (typeof contract.address !== \\\'undefined\\\') {         console.log(\\\'Contract mined! address: \\\' + contract.address + \\\' transactionHash: \\\' + contract.transactionHash);    } })

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后开启挖矿(这里我们将log输出到了geth.log文件中去并未在控制台中显示,所以需要访问geth.log来查看)

    miner.start()

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    交易信息如下:

    eth.getTransaction(\\\"0x0c6b2e83df41ff12036743c5dbfd587086255ab715ac807a0f02f2df4a25ac01\\\")

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后停止挖矿:

    miner.stop()

    以太坊智能合约是什么意思(以太坊智能合约的开发语言是)

    之后验证合约部署成功与否:

    eth.getCode(test.address)

    合约调用

    之后调用test合约的multiply方法:

    test.multiply.call(8)

    至此,合约在链上的部署流程演示完毕~

    文末小结

    本篇文章只是对如何在公链环境中部署智能合约做了一个简单的介绍,而我们知道智能合约是运行在EVM上的,后续我们将对以太坊虚拟机源码进行分析,这里暂且不对此展开分析~

    原创文章,作者:七芒星实验室,如若转载,请注明出处:https://www.sudun.com/ask/34282.html

    Like (0)
    七芒星实验室的头像七芒星实验室
    Previous 2024年4月4日
    Next 2024年4月4日

    相关推荐

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注