StarkNet 是一种无需许可的去中心化 Validity-Rollup(也称为“ZK-Rollup”)零知识证明系统。它作为以太坊上的 L2 网络运行,使任何 dApp 都能实现无限的计算规模——而不会损害以太坊的可组合性和安全性,这要归功于 StarkNet 对最安全和最具可扩展性的加密证明系统——STARK 的依赖。
相较于以太坊,Starknet的一个特色就是能够在一笔交易之中同时与多个合约进行交互,这样就可以做到授权加转账放在一笔交易之中进行,节省了gas费。由于官方推荐使用的openZeppelin目前只支持Python,并且给出了使用Python实现的相关代码如下:
await signer.send_transaction(
account, [
(contract_address, 'contract_method', [arg_1]),
(contract_address, 'another_method', [arg_1, arg_2])
]
)
另外,starknet.py的官方文档中也给出了一个相关的实现方式如下:
# There are two options of executing transactions
# 1. Use contract interface
await (
await map_contract.functions["put"].invoke(key=10, value=20, max_fee=int(1e16))
).wait_for_acceptance()
# 2. Use AccountClient's execute method
call = map_contract.functions["put"].prepare(key=10, value=20)
resp = await account_client.execute(calls=call, max_fee=int(1e16))
await account_client.wait_for_tx(resp.transaction_hash)
# The advantage of using the second approach is there can be more than only one call
calls = [
map_contract.functions["put"].prepare(key=10, value=20),
map_contract.functions["put"].prepare(key=30, value=40),
map_contract.functions["put"].prepare(key=50, value=60),
]
# Executes one transaction with three calls
resp = await account_client.execute(calls=calls, max_fee=int(1e16))
await account_client.wait_for_tx(resp.transaction_hash)
但是starknet.js官方文档之中没有写如何在一笔交易之中与多个合约进行交互,经过一番摸索之后,我找到了如下办法,代码如下:
await signer.execute(
[{
contractAddress: contractAddress_1,
entrypoint: "approve",
calldata: stark.compileCalldata({
spender: contractAddress_2,
amount: {type: 'struct', low: value1, high: '0'},
})
},
{
contractAddress: contractAddress_2,
entrypoint: "transfer_ether",
calldata: [value1, '0']
}]
);
最终能够实现一笔交易内与多个合约进行交互。其中,calldata的两种形式的相关问题参见这篇文章
StarkNet是一种无需许可的去中心化L2网络,基于ZK-Rollup技术提供无限计算规模和安全性。它允许在单笔交易中与多个合约交互,从而节省gas费。文章提供了Python和JavaScript的代码示例,展示如何在StarkNet上执行这样的交互操作,特别是通过starknet.js库在一笔交易中与多个合约进行交互的方法。





