Skip to content

Commit

Permalink
Merge pull request #24 from massalabs/smart-contract
Browse files Browse the repository at this point in the history
draft smart contract
  • Loading branch information
Thykof authored Jul 18, 2024
2 parents bb630aa + 3993d03 commit ae45cf8
Show file tree
Hide file tree
Showing 16 changed files with 1,815 additions and 1,119 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/sc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Smart contract lint and tests
on: [push]
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: smart-contract/package-lock.json

- name: Install
run: npm ci
working-directory: smart-contract

- name: Lint
run: npm run fmt:check
working-directory: smart-contract

- name: Test
run: npm run test
working-directory: smart-contract
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
}
4 changes: 1 addition & 3 deletions smart-contract/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
WALLET_SECRET_KEY=""

JSON_RPC_URL_PUBLIC=https://buildnet.massa.net/api/v2:33035
PRIVATE_KEY=
18 changes: 0 additions & 18 deletions smart-contract/.github/workflows/ci-tests.yml

This file was deleted.

2 changes: 1 addition & 1 deletion smart-contract/asconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"converge": true,
"noAssert": false,
"transform": ["@massalabs/as-transformer"]
}
Expand Down
124 changes: 124 additions & 0 deletions smart-contract/assembly/Schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Args, Serializable, Result } from '@massalabs/as-types';
import { createEvent } from '@massalabs/massa-as-sdk';
import { u256 } from 'as-bignum/assembly';

export class Schedule implements Serializable {
constructor(
public id: u64 = 0,
public tokenAddress: string = '',
public spender: string = '',
public recipient: string = '',
public amount: u256 = u256.Zero,
public interval: u64 = 0,
public occurrences: u64 = 0,
public remaining: u64 = 0,
public tolerance: u32 = 0,
public history: Transfer[] = [],
) {}

serialize(): StaticArray<u8> {
return new Args()
.add(this.id)
.add(this.tokenAddress)
.add(this.spender)
.add(this.recipient)
.add(this.amount)
.add(this.interval)
.add(this.occurrences)
.add(this.remaining)
.add(this.tolerance)
.addSerializableObjectArray(this.history)
.serialize();
}

deserialize(data: StaticArray<u8>, offset: i32): Result<i32> {
const args = new Args(data, offset);
const resultId = args.nextU64();
if (resultId.isErr()) {
return new Result(0, "Can't deserialize id.");
}
const resultTokenAddress = args.nextString();
if (resultTokenAddress.isErr()) {
return new Result(0, "Can't deserialize tokenAddress.");
}
const resultSpender = args.nextString();
if (resultSpender.isErr()) {
return new Result(0, "Can't deserialize spender.");
}
const resultRecipient = args.nextString();
if (resultRecipient.isErr()) {
return new Result(0, "Can't deserialize recipient.");
}
const resultAmount = args.nextU256();
if (resultAmount.isErr()) {
return new Result(0, "Can't deserialize amount.");
}
const resultInterval = args.nextU64();
if (resultInterval.isErr()) {
return new Result(0, "Can't deserialize interval.");
}
const resultOccurrences = args.nextU64();
if (resultOccurrences.isErr()) {
return new Result(0, "Can't deserialize occurrences.");
}
const resultRemaining = args.nextU64();
if (resultRemaining.isErr()) {
return new Result(0, "Can't deserialize times.");
}
const resultTolerance = args.nextU32();
if (resultTolerance.isErr()) {
return new Result(0, "Can't deserialize tolerance.");
}
const resultHistory = args.nextSerializableObjectArray<Transfer>();
if (resultHistory.isErr()) {
return new Result(0, "Can't deserialize history.");
}

this.id = resultId.unwrap();
this.tokenAddress = resultTokenAddress.unwrap();
this.spender = resultSpender.unwrap();
this.recipient = resultRecipient.unwrap();
this.amount = resultAmount.unwrap();
this.interval = resultInterval.unwrap();
this.occurrences = resultOccurrences.unwrap();
this.remaining = resultRemaining.unwrap();
this.tolerance = resultTolerance.unwrap();
this.history = resultHistory.unwrap();

return new Result(args.offset);
}

public createTransferEvent(period: u64, thread: u8): string {
return createEvent('Transfer', [
this.id.toString(),
this.remaining.toString(),
period.toString(),
thread.toString(),
]);
}
}

export class Transfer implements Serializable {
constructor(public period: u64 = 0, public thread: u8 = 0) {}

serialize(): StaticArray<u8> {
return new Args().add(this.period).add(this.thread).serialize();
}

deserialize(data: StaticArray<u8>, offset: i32): Result<i32> {
const args = new Args(data, offset);
const resultPeriod = args.nextU64();
if (resultPeriod.isErr()) {
return new Result(0, "Can't deserialize period.");
}
const resultThread = args.nextU8();
if (resultThread.isErr()) {
return new Result(0, "Can't deserialize thread.");
}

this.period = resultPeriod.unwrap();
this.thread = resultThread.unwrap();

return new Result(args.offset);
}
}
1 change: 1 addition & 0 deletions smart-contract/assembly/__tests__/as-pect.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@as-pect/assembly/types/as-pect" />
Loading

0 comments on commit ae45cf8

Please sign in to comment.