Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/solidity compat doc 1 #365

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions assembly/std/solidity_compat/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Serializable, stringToBytes } from '@massalabs/as-types';

// An implementation of Solidity Bytes32:
// https://docs.soliditylang.org/en/latest/types.html#fixed-size-byte-arrays
export class Bytes32 {
private MAX_LEN: i32 = 32;
// private _offset: i32 = 0;
Expand Down Expand Up @@ -39,6 +41,9 @@ export class Bytes32 {
}
}

// An abstract implementation of Solidity BytesXX:
// https://docs.soliditylang.org/en/latest/types.html#fixed-size-byte-arrays
// See Bytes4 implementation
abstract class BytesLen implements Serializable {
private serialized: StaticArray<u8> = new StaticArray<u8>(0);
private offset_ser: i32 = 0;
Expand Down Expand Up @@ -91,6 +96,8 @@ abstract class BytesLen implements Serializable {
*/
}

// An implementation of Solidity Bytes4
// https://docs.soliditylang.org/en/latest/types.html#fixed-size-byte-arrays
export class Bytes4 extends BytesLen {
@inline
max_len(): i32 {
Expand Down
12 changes: 8 additions & 4 deletions assembly/std/solidity_compat/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { u128, u256 } from 'as-bignum/assembly';
import { toBytes, stringToBytes } from '@massalabs/as-types';
import { Bytes4, Bytes32 } from './bytes';

// Emulate abi.encode
// Note: in order to add a class (a struct in Solidity) you need to add each field one by one
// Implementation of abi.encode
// https://docs.soliditylang.org/en/develop/abi-spec.html
// Note: in order to abi.encode a class (e.g. a struct in Solidity):
// add each field one by one in correct order
export class AbiEncode {
private serialized: StaticArray<u8> = new StaticArray<u8>(0);
constructor(serialized: StaticArray<u8> = []) {
Expand Down Expand Up @@ -65,7 +67,8 @@ export class AbiEncode {
}
}

// Emulate abi.encodePacked
// Implementation of abi.encodePacked:
// https://docs.soliditylang.org/en/develop/abi-spec.html#non-standard-packed-mode
export class AbiEncodePacked {
private _offset: i32 = 0;
private serialized: StaticArray<u8> = new StaticArray<u8>(0);
Expand Down Expand Up @@ -97,7 +100,8 @@ export class AbiEncodePacked {
}
}

// Emulate abi.encodeWithSelector
// Implementation of abi.encodeWithSelector:
// https://docs.soliditylang.org/en/develop/abi-spec.html#function-selector-and-argument-encoding
export class AbiEncodeWithSelector {
private serialized: StaticArray<u8> = new StaticArray<u8>(8);

Expand Down
27 changes: 27 additions & 0 deletions assembly/std/solidity_compat/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { u256 } from 'as-bignum/assembly';
import { wrapStaticArray } from '@massalabs/as-types';
import { Address } from '../address';
import { env } from '../../env';
// import setBytecode = env.setBytecode;
// import getKeys = env.getKeys;
import { Storage } from '../index';

// Convert bytes array (length 32) to u256
// In Solidity, you can see code like:
Expand All @@ -10,3 +15,25 @@ import { wrapStaticArray } from '@massalabs/as-types';
export function bytes32ToU256(a: StaticArray<u8>): u256 {
return u256.fromUint8ArrayBE(wrapStaticArray(a));
}

// implementation Solidity selfdestruct:
// https://www.infuy.com/blog/using-self-destruct-in-solidity-contracts/
export function selfDestruct(transferToAddr: Address): void {
// 1- empty the SC
let emptySc = new StaticArray<u8>(0);
env.setBytecode(emptySc);

// 2- delete everything in Storage
let keys = env.getKeys(new StaticArray<u8>(0));
for (let i = 0; i < keys.length; i++) {
Storage.del(keys[i]);
}

// 3- transfer back coins if any
let scBalance = env.balance();
// Balance will most likely be > 0 as we deleted some keys from the Storage
// but if there is nothing in the Storage, no need to call transferCoins
if (scBalance > 0) {
env.transferCoins(transferToAddr.toString(), scBalance);
}
}