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

feat(PE-4388): ANT-UCM contract #20

Closed
wants to merge 4 commits into from
Closed
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: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
**.js
dist
.github
validations*
validations*
u
ucm
17 changes: 17 additions & 0 deletions initial-state-ucm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"ticker": "ANT-1",
"name": "Arweave Name Token v0.0.0",
"owner": "",
"controllers": [],
"records": {
"@": {
"transactionId": "",
"ttlSeconds": 900
}
},
"balances": {
"": 0
},
"claimable": [],
"evolve": null
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.2.0",
"private": true,
"scripts": {
"build": "yarn clean && node build.js && tsc --noEmit",
"build": "yarn clean && node build.js && node ./tools/scripts/build-ucm.js && tsc --noEmit",
"format:check": "prettier . --check --cache",
"format:fix": "prettier . --write",
"lint:check": "eslint .",
Expand Down
39 changes: 39 additions & 0 deletions schemas/allow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const allowSchema = {
$id: '#/definitions/allow',
type: 'object',
properties: {
function: {
type: 'string',
const: 'allow',
},
qty: {
type: 'number',
},
txID: {
type: 'string',
pattern: '^[a-zA-Z0-9_-]{43}$',
},
},
required: ['qty', 'txID'],
additionalProperties: false,
};

module.exports = {
allowSchema,
};
2 changes: 1 addition & 1 deletion schemas/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const balanceSchema = {
},
},
required: ['target'],
additionalProperties: false,
additionalProperties: true, // allow due to ucm passing qty
};

module.exports = {
Expand Down
39 changes: 39 additions & 0 deletions schemas/claim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const claimSchema = {
$id: '#/definitions/claim',
type: 'object',
properties: {
function: {
type: 'string',
const: 'claim',
},
qty: {
type: 'number',
},
txID: {
type: 'string',
pattern: '^[a-zA-Z0-9_-]{43}$',
},
},
required: ['qty', 'txID'],
additionalProperties: false,
};

module.exports = {
claimSchema,
};
35 changes: 35 additions & 0 deletions schemas/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const constructorSchema = {
$id: '#/definitions/constructor',
type: 'object',
properties: {
function: {
type: 'string',
const: 'constructor',
},
args: {
type: 'object',
},
},
required: ['args'],
additionalProperties: false,
};

module.exports = {
constructorSchema,
};
4 changes: 4 additions & 0 deletions schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ module.exports = {
removeControllerSchema: require('./removeController').removeControllerSchema,
balanceSchema: require('./balance').balanceSchema,
transferSchema: require('./transfer').transferSchema,
claimSchema: require('./claim').claimSchema,
allowSchema: require('./allow').allowSchema,
rejectSchema: require('./reject').rejectSchema,
constructorSchema: require('./constructor').constructorSchema,
};
40 changes: 40 additions & 0 deletions schemas/reject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const rejectSchema = {
$id: '#/definitions/reject',
type: 'object',
properties: {
function: {
type: 'string',
const: 'reject',
},
tx: {
type: 'string',
pattern: '^[a-zA-Z0-9_-]{43}$',
},
qty: {
type: 'number',
minimum: 1,
},
},
required: ['tx', 'qty'],
additionalProperties: false,
};

module.exports = {
rejectSchema,
};
2 changes: 1 addition & 1 deletion schemas/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const transferSchema = {
},
},
required: ['target'],
additionalProperties: false,
additionalProperties: true,
};

module.exports = {
Expand Down
59 changes: 59 additions & 0 deletions src/actions/write/allow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Left, Right, of } from '../../lib/either';

export const allow = (state, action) =>
of({ state, action }).chain(validate).map(update);

function update({ state, action }) {
state.balances[action.caller] -= action.input.qty;
if (!state.claimable) {
state.claimable = [];
}
state.claimable.push({
from: action.caller,
to: action.input.target,
qty: action.input.qty,
txID: SmartWeave.transaction.id,
});
return { state };
}

function validate({ state, action }) {
if (!Number.isInteger(action.input.qty) || action.input.qty === undefined) {
return Left('Invalid value for quantity. Must be an integer.');
}
if (!action?.input?.target) {
return Left('No target specified.');
}
if (action.input.target.length !== 43) {
return Left('Target is not valid!');
}
if (action.input.target === SmartWeave.transaction.id) {
return Left('Cant setup claim to transfer a balance to itself');
}
if (action.caller === action.input.target) {
return Left('Invalid balance transfer');
}
if (!state.balances[action.caller]) {
return Left('Caller does not have a balance');
}
if (state.balances[action.caller] < action.input.qty) {
return Left('Caller balance is not high enough.');
}
return Right({ state, action });
}
57 changes: 57 additions & 0 deletions src/actions/write/claim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Left, Right, of } from '../../lib/either';

export const claim = (state, action) =>
of({ state, action }).chain(validate).map(update);

function update({ state, action, idx }) {
if (!state.balances[action.caller]) {
state.balances[action.caller] = 0;
}

state.balances[action.caller] += action.input.qty;
state.claimable.splice(idx, 1);

return { state };
}

function validate({ state, action }) {
if (!action.input.txID) {
return Left('txID is not found.');
}

if (!action.input.qty) {
return Left('claim quantity is not specified.');
}

const idx = state.claimable.findIndex((c) => c.txID === action.input.txID);

if (idx < 0) {
return Left('claimable not found.');
}

if (state.claimable[idx].qty !== action.input.qty) {
return Left('claimable qty is not equal to claim qty.');
}

if (state.claimable[idx].to !== action.caller) {
return Left('claim is not addressed to caller.');
}

return Right({ state, action, idx });
}
44 changes: 44 additions & 0 deletions src/actions/write/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
export function constructor(state, action) {
if (action.input.args) {
state = {
records: state.records,
controllers: state.controllers,
...action.input.args,
};
}

if (!state.claimable) {
state.claimable = [];
}

if (!state.balances) {
state.balances = {};
}

if (!action.input?.args?.balances) {
state.balances[action.caller] = 100;
}

state.name = action.input?.args?.name
? action.input.args.name
: 'AtomicAsset';
state.ticker = action.input?.args?.ticker ? action.input.args.ticker : 'AA';

return { state };
}
Loading