# scroll alpha test bridge代码交互 **Published by:** [junjie9021](https://paragraph.com/@junjie9021-3/) **Published on:** 2023-03-03 **URL:** https://paragraph.com/@junjie9021-3/scroll-alpha-test-bridge ## Content scroll alpha 网络的交互,目前只有bridge,暂时还没看到其他应用。大家要是有找到L2层上部署的应用,可以发下哈。python3 代码样例安装依赖注意privkey 改成要交互的私钥发送后去浏览器上查下tx状态,半天没返回的,就要换下rpc节点,可在 chainlist查看有个key值暂时不要动, 要是tx 状态失败的,还得去合约上查下最新的import web3 import math import requests headers = { 'content-type': 'application/json', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', } class Rpc: """ eth rpc方法 """ def __init__(self, rpc='https://rpc.ankr.com/eth_goerli', chainid=5, proxies=None, timeout=30): self.rpc = rpc self.chainid = chainid self.proxies = proxies self.timeout = timeout def get_current_block(self): """获取最新区块""" data = {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def get_block_detail(self, number): """获取区块hash""" if isinstance(number, int): number = hex(number) data = {"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":[number,True],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def get_transaction(self, txhash): """获取的交易详情""" data = {"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":[txhash],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def get_gas_price(self): """获取gas""" data = {"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def get_transaction_count_by_address(self, address): data = {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":[address,'latest'],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def call(self, to, data): data = {"jsonrpc":"2.0","method":"eth_call","params":[{"to": to, "data": data}, "latest"],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def send_raw_transaction(self, hex): """广播交易""" data = {"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":[hex],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json() def get_balance(self, address): """获取余额""" data = {"jsonrpc":"2.0","method":"eth_getBalance","params":[address, 'latest'],"id":1} res = requests.post(self.rpc, json=data, headers=headers, proxies=self.proxies, timeout=self.timeout) return res.json()#(int(res.json()['result'], 16)) / math.pow(10,18) def transfer(self, account, to, amount, gaslimit, **kw): """离线交易 account to: 收款地址 gaslimit: 由当前区块的gaslimit获取 gasprice: get_gas_price获取 nonce: 交易总数 get_transaction_count_by_address获取 chainId: 链id """ amount = int(amount, 16) if isinstance(amount, str) else int(amount) gaslimit = int(gaslimit, 16) if not isinstance(gaslimit, int) else gaslimit gasprice = int(self.get_gas_price()['result'], 16) nonce = int(self.get_transaction_count_by_address(account.address)['result'], 16) tx = {'from': account.address, 'value': amount,'to': to, 'gas': gaslimit, 'gasPrice': gasprice, 'nonce': nonce, 'chainId': self.chainid} if kw: tx.update(**kw) signed = account.signTransaction(tx) return self.send_raw_transaction(signed.rawTransaction.hex()) if __name__ == '__main__': privkey = 'xxxxxxxxxxxx' # 这里替换成自己的私钥 value = 0.1 # 要存款的数量 account = web3.Account.from_key(privkey) BALANCE_PRECISION = math.pow(10, 18) # 主币精度,18位 rpc = Rpc('https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161') value = int(value * BALANCE_PRECISION) gaslimit = 280000 # gaslimit to = '0xe5e30e7c24e4dfcb281a682562e53154c15d3332' # 交互的合约地址 method = '0x9f8420b3' # 存款方法hash值 key = 0.0000000625 # key值有可能经常改,昨晚还是0.00000004的 amount = value + key * BALANCE_PRECISION # 计算要发送的amount data = '0x9f8420b3' + hex(value)[2:].rjust(64,'0') + '0000000000000000000000000000000000000000000000000000000000009c40' # 拼接数据 res = rpc.transfer(account, to=to, amount=amount, gaslimit=gaslimit, data=data) # 发送交易 print(res) 执行脚本后打印交易hash, 去浏览器查询状态https://goerli.etherscan.io/ 代码样例已上传: https://github.com/junjie9021/simple-demo往期代码交互教程Aave Gho稳定币项目代码交互教程Coinbase L2 Base 存款代码交互教程Sui mint nft 代码交互教程我的 推特 Lens Link3 Github ## Publication Information - [junjie9021](https://paragraph.com/@junjie9021-3/): Publication homepage - [All Posts](https://paragraph.com/@junjie9021-3/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@junjie9021-3): Subscribe to updates