Skip to content

filecoin-project/fvm-solidity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FVM Solidity

Solidity libraries for using the FVM precompiles

Installation

Forge

forge install filecoin-project/fvm-solidity

Git

git submodule add https://github.com/filecoin-project/fvm-solidity lib/fvm-solidity

Usage

FVMPay

import { FVMPay } from "fvm-solidity/FVMPay.sol";

contract BigBrain {
    using FVMPay for address;

    function payEfficiently(address recipient) external payable {
        recipient.pay(msg.value);
    }

    using FVMPay for uint256;

    function burnEfficiently() external payable {
        msg.value.burn();
    }
}

FVMActor - ResolveAddress

Resolve Filecoin or EVM (f410 / masked ID) addresses to their on-chain actor ID.

import { FVMActor } from "fvm-solidity/FVMActor.sol";

contract BigBrain {
    using FVMActor for bytes;
    using FVMActor for address;

    // Resolve a Filecoin byte address
    function resolveFilAddress(bytes calldata filAddress) external view returns (uint64 actorId) {
        return filAddress.getActorId(); // reverts with ActorNotFound if not found
    }

    // Safely attempt to resolve (no revert on missing actor)
    function tryResolveFilAddress(bytes calldata filAddress) external view returns (bool exists, uint64 actorId) {
        return filAddress.tryGetActorId();
    }

    // Resolve an EVM address (f410 delegated) or masked ID (0xff) address
    function resolveEvmAddress(address addr) external view returns (bool exists, uint64 actorId) {
        return addr.tryGetActorId();
    }
}

Testing

import {MockFVMTest} from "fvm-solidity/mocks/MockFVMTest.sol";
import {FVMActor} from "fvm-solidity/FVMActor.sol";
import {FVMAddress} from "fvm-solidity/FVMAddress.sol";

// MockFVMTest is Test
contract BigBrainTest is MockFVMTest {
    using FVMAddress for uint64;
    using FVMActor for bytes;

    function setUp() public override {
        // Mock the FVM precompiles for forge test
        super.setUp();
        /* ... */
    }

    function test_resolveAddress() public {
        uint64 actorId = 1234;
        bytes memory filAddress = actorId.f0();
        ACTOR_PRECOMPILE.mockResolveAddress(filAddress, actorId);

        (bool exists, uint64 resolved) = filAddress.tryGetActorId();
        assertTrue(exists);
        assertEq(resolved, actorId);
    }
}

Gas Profiling

These measurements were performed on the Demo contract with the gas-profile script. Note that gas costs are roughly 444x higher in the FEVM compared to the EVM.

Method Demo.sol estimateGas
Soldity payable.send(uint256) 5383103
Solidity payable.transfer(uint256) 5379173
FVMPay address.pay(uint256) 4856475
FVMPay uint64.pay(uint256) 4847666
FVMPay uint256.burn() 3561540

Support

Additional FVM support can be found in the filecoin-solidity library.

Precompiles

Supported Name Address
ResolveAddress 0xfe00000000000000000000000000000000000001
LookupDelegatedAddress 0xfe00000000000000000000000000000000000002
CallActorByAddress 0xfe00000000000000000000000000000000000003
CallActorById 0xfe00000000000000000000000000000000000005
GetBeaconRandomness 0xfe00000000000000000000000000000000000006

Methods

Supported Name Number
Send 0
Constructor 1

About

Solidity library for using the FVM precompiles

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors