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

Prototype Smart Contract reworking #159

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions smart-contracts/as-pect.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export default {
/**
* A set of globs passed to the glob package that qualify typescript files for testing.
*/
entries: ['assembly/**/__tests__/**/*.spec.ts'],
entries: ['**/assembly/**/__tests__/**/*.spec.ts'],
/**
* A set of globs passed to the glob package that quality files to be added to each test.
*/
include: ['assembly/**/__tests__/**/*.include.ts'],
include: ['**/assembly/**/__tests__/**/*.include.ts'],
/**
* A set of regexp that will disclude source files from testing.
*/
Expand Down
27 changes: 27 additions & 0 deletions smart-contracts/erc20/assembly/__tests__/erc20.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { u256 } from "as-bignum/assembly";
import { Exportable, ERC20 } from "..";
import { Args } from "@massalabs/as-types";
import { Address } from "@massalabs/massa-as-sdk";

const totalSupply = u256.fromU32(1000);

// @eslint-ignore @typescript-eslint/no-unused-vars
// @ts-ignore
@lazy @global const FungibleToken = new ERC20(totalSupply);
Thykof marked this conversation as resolved.
Show resolved Hide resolved

describe('ERC20 - Documentation tests', () => {
it('should be simple to use - create smart contract', () => {

const totalSupplyBinary = new Args().add(totalSupply).serialize();
const addr1 = new Address('AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq');
const addr1Binary = new Args().add(addr1).serialize();
const addr2 = new Address('AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKr');
const allowanceArgs = new Args().add(addr1).add(addr2).serialize();
const u256ZeroBinary = new Args().add(u256.Zero).serialize();

expect(Exportable.totalSupply()).toStrictEqual(totalSupplyBinary);
expect(Exportable.balanceOf(addr1Binary)).toStrictEqual(u256ZeroBinary);
expect(Exportable.allowance(allowanceArgs)).toStrictEqual(u256ZeroBinary);
});
});

15 changes: 15 additions & 0 deletions smart-contracts/erc20/assembly/connectable/erc20Core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Address, call } from "@massalabs/massa-as-sdk";
import { ERC20Core } from "../interfaces/erc20Core";
import { u256 } from "as-bignum";
import { Args } from "@massalabs/as-types";

export class ERC20CoreImpl { // implements ERC20Core {
constructor(public sc: Address) {}

allowance(owner: Address, spender: Address, coins: u64 = 0): u256 {
const args = new Args().add(owner).add(spender);
const response = call(this.sc, 'allowance', args, coins);
return new Args(response).mustNext<u256>("allowance");
}

}
40 changes: 40 additions & 0 deletions smart-contracts/erc20/assembly/exportable/erc20Core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Args } from "@massalabs/as-types";
import { Address } from "@massalabs/massa-as-sdk";

export function allowance(_args: StaticArray<u8>): StaticArray<u8> {
const args = new Args(_args);
const owner = args.mustNext<Address>("owner");
Thykof marked this conversation as resolved.
Show resolved Hide resolved
const spender = args.mustNext<Address>("spender");
FungibleToken.allowance(owner, spender).serialize();
gregLibert marked this conversation as resolved.
Show resolved Hide resolved
}

export function approve(_args: StaticArray<u8>): void{
const args = new Args(_args);
const spender = args.mustNext<Address>("spender");
const amount = args.mustNext<u64>("amount");
FungibleToken.approve(spender, amount);
}

export function balanceOf(_args: StaticArray<u8>): StaticArray<u8> {
const of = new Args(_args).mustNext<Address>("of");
return new Args().add(FungibleToken.balanceOf(of)).serialize();
}

export function totalSupply(): StaticArray<u8> {
return new Args().add(FungibleToken.totalSupply()).serialize();
}

export function transfer(_args: StaticArray<u8>): void{
const args = new Args(_args);
const to = args.mustNext<Address>("to");
const amount = args.mustNext<u64>("amount");
FungibleToken.transfer(to, amount);
}

export function transferFrom(_args: StaticArray<u8>): void{
const args = new Args(_args);
const from = args.mustNext<Address>("from");
const to = args.mustNext<Address>("to");
const amount = args.mustNext<u64>("amount");
FungibleToken.transferFrom(from, to, amount);
}
69 changes: 69 additions & 0 deletions smart-contracts/erc20/assembly/implementation/erc20Full.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ERC20Core } from "../interfaces/erc20Core";
import { Args } from "@massalabs/as-types";
import { MapManager,
KeySequenceManager,
KeyIncrementer, ConstantManager, Address, Context, createEvent } from "@massalabs/massa-as-sdk";
import { u256 } from "as-bignum/assembly";

export const EVENT_PREFIX = "ERC20";
export const EVENT_ERR_INSUFFICIENT_BALANCE = "transfer amount exceeds balance";
export const EVENT_ERR_INSUFFICIENT_ALLOWANCE = "transfer amount exceeds allowance";
export const EVENT_TRANSFER = "Transfer";
export const EVENT_APPROVAL = "Approval";

@inline
function triggerTransferEvent(from: Address, to: Address, value: u256): void {
createEvent(`${EVENT_PREFIX}: ${EVENT_TRANSFER}`, [from.toString(), to.toString(), value.toString()]);
}

export class ERC20 implements ERC20Core {
private _totalSupply: ConstantManager<u256>;
public balances: MapManager<Address, u256>;
public allowances: MapManager<StaticArray<u8>, u256>;

constructor(totalSupply: u256, keyManager: KeySequenceManager = new KeyIncrementer<u8>(0)) {
this._totalSupply = new ConstantManager<u256>(keyManager);
this.balances = new MapManager<Address, u256>(keyManager);
this.allowances = new MapManager<StaticArray<u8>, u256>(keyManager);

this._totalSupply.set(totalSupply);
}

totalSupply(): u256 {
return this._totalSupply.mustValue();
}

balanceOf(address: Address): u256 {
return this.balances.value(address).unwrapOrDefault();
}

transfer(to: Address, value: u256): void {
const from = Context.caller();
const balance = this.balanceOf(from);
assert(balance >= value, `${EVENT_PREFIX}: ${EVENT_ERR_INSUFFICIENT_BALANCE}`);
this.balances.set(from, balance - value);
this.balances.set(to, this.balanceOf(to) + value);
triggerTransferEvent(from, to, value);
}

transferFrom(from: Address, to: Address, value: u256): void {
const caller = Context.caller();
const allowance = this.allowance(from, caller);
assert(allowance >= value, `${EVENT_PREFIX}: ${EVENT_ERR_INSUFFICIENT_ALLOWANCE}`);
this.approve(caller, allowance - value);
this.transfer(to, value);
triggerTransferEvent(from, to, value);
}

approve(spender: Address, value: u256): void {
const from = Context.caller();
const storageKey = new Args().add(from).add(spender).serialize();
this.allowances.set(storageKey, value);
createEvent(`${EVENT_PREFIX}: ${EVENT_APPROVAL}`, [from.toString(), spender.toString(), value.toString()]);
}

allowance(owner: Address, spender: Address): u256 {
const storageKey = new Args().add(owner).add(spender).serialize();
return this.allowances.value(storageKey).unwrapOrDefault();
}
}
6 changes: 6 additions & 0 deletions smart-contracts/erc20/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './implementation/erc20Full';
export * from './interfaces/erc20Core';
import * as Exportable from './exportable/erc20Core';
export { Exportable };

export * from './connectable/erc20Core';
14 changes: 14 additions & 0 deletions smart-contracts/erc20/assembly/interfaces/erc20Core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { u256 } from "as-bignum/assembly";
import { Address } from "@massalabs/massa-as-sdk";

export interface ERC20Core {
allowance(owner: Address, spender: Address): u256;
approve(spender: Address, value: u256): void;
balanceOf(address: Address): u256;
// decimals(): u8;
// name(): string;
// symbol(): string;
gregLibert marked this conversation as resolved.
Show resolved Hide resolved
totalSupply(): u256;
transfer(to: Address, value: u256): void;
transferFrom(from: Address, to: Address, value: u256): void;
}
14 changes: 14 additions & 0 deletions smart-contracts/erc20/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"**/*.ts"
],
"typedocOptions": {
"exclude": "assembly/**/__tests__",
"excludePrivate": true,
"excludeProtected": true,
"excludeExternals": true,
"includeVersion": true,
"skipErrorChecking": true
}
}
2 changes: 2 additions & 0 deletions smart-contracts/erc20/dapp/connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// todo TS code to connect a DAPP to an ERC20 smart contract
// Similar to assembly/connectable but for TS and from outside of the blockchain.
2 changes: 1 addition & 1 deletion smart-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@as-pect/cli": "^8.0.1",
"@assemblyscript/loader": "^0.25.2",
"@massalabs/as-types": "^2.0.0",
"@massalabs/as-types": "^2.0.1-dev",
"@massalabs/eslint-config": "^0.0.9",
"@massalabs/massa-as-sdk": "^2.5.5-dev",
"@massalabs/massa-sc-compiler": "^0.1.1-dev",
Expand Down
Loading