-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from massalabs/smart-contract
draft smart contract
- Loading branch information
Showing
16 changed files
with
1,815 additions
and
1,119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="@as-pect/assembly/types/as-pect" /> |
Oops, something went wrong.