Skip to content

Commit

Permalink
Merge pull request #200 from near/near-contract-deletion
Browse files Browse the repository at this point in the history
NearContract deletion, init, refactoring
  • Loading branch information
volovyks authored Sep 1, 2022
2 parents 422b34b + a6e4341 commit ca8c480
Show file tree
Hide file tree
Showing 54 changed files with 442 additions and 464 deletions.
3 changes: 0 additions & 3 deletions examples/__tests__/test-clean-state.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ test.beforeEach(async t => {
// Deploy the clean-state contract.
const cleanState = await root.devDeploy('./build/clean-state.wasm');

// Init the contract
await cleanState.call(cleanState, 'init', {});

// Save state for test runs, it is unique for each test
t.context.worker = worker;
t.context.accounts = {
Expand Down
2 changes: 0 additions & 2 deletions examples/__tests__/test-counter.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ test.beforeEach(async t => {
(process.env['COUNTER_TS'] ? './build/counter-ts.wasm' : './build/counter.wasm')
);

// Init the contract
await counter.call(counter, 'init', {});

// Test users
const ali = await root.createSubAccount('ali');
Expand Down
3 changes: 0 additions & 3 deletions examples/__tests__/test-cross-contract-call.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ test.beforeEach(async t => {
'./build/status-message.wasm',
);

// Init the contract
await statusMessage.call(statusMessage, 'init', {});

// Deploy the onCall contract.
const onCall = await root.devDeploy(
'./build/cross-contract-call.wasm',
Expand Down
1 change: 0 additions & 1 deletion examples/__tests__/test-fungible-token.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test.beforeEach(async (t) => {

// Init the contracts
await ft.call(ft, 'init', { prefix: 'a', totalSupply: '1000' });
await xcc.call(xcc, 'init', {});

// Create test accounts
const ali = await root.createSubAccount('ali');
Expand Down
2 changes: 1 addition & 1 deletion examples/__tests__/test-parking-lot.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test.beforeEach(async t => {
const parkingLot = await root.devDeploy(
'build/parking-lot.wasm',
);
await parkingLot.call(parkingLot, 'init', {});


const ali = await root.createSubAccount('ali');

Expand Down
2 changes: 0 additions & 2 deletions examples/__tests__/test-status-message-collections.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ test.beforeEach(async t => {
'./build/status-message-collections.wasm',
);

// Init the contract
await statusMessage.call(statusMessage, 'init', {});

// Test users
const ali = await root.createSubAccount('ali');
Expand Down
3 changes: 0 additions & 3 deletions examples/__tests__/test-status-message.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ test.before(async t => {
'./build/status-message.wasm',
);

// Init the contract
await statusMessage.call(statusMessage, 'init', {});

// Create test users
const ali = await root.createSubAccount('ali');
const bob = await root.createSubAccount('bob');
Expand Down
17 changes: 7 additions & 10 deletions examples/src/clean-state.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { NearContract, NearBindgen, call, view, near } from 'near-sdk-js'
import { NearBindgen, call, view, near } from 'near-sdk-js'

@NearBindgen
class CleanState extends NearContract {
@NearBindgen({})
class CleanState {
@call
clean({keys}) {
clean({ keys }) {
keys.forEach(key => near.storageRemove(key))
}

@call
put({key, value}) {
put({ key, value }) {
near.storageWrite(key, value)
}

@view
get({key}) {
get({ key }) {

return near.storageRead(key)
}

default() {
return new CleanState()
}
}
6 changes: 3 additions & 3 deletions examples/src/counter-lowlevel.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// This contract implements exact same functionality as counter.js, but only use low level APIs
import {near} from 'near-sdk-js'
import { near } from 'near-sdk-js'

export function init() {
let argsRaw = near.input()
let args = JSON.parse(argsRaw || "{}")
let initial = args.initial || 0
let count = initial
let state = JSON.stringify({count})
let state = JSON.stringify({ count })
near.storageWrite('STATE', state)
}

Expand All @@ -15,7 +15,7 @@ function deserialize() {
if (state) {
return JSON.parse(state)
} else {
throw new Error("Contract state is empty")
return { count: 0 }
}
}

Expand Down
15 changes: 5 additions & 10 deletions examples/src/counter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { NearContract, NearBindgen, near, call, view } from 'near-sdk-js'
import { NearBindgen, near, call, view, initialize } from 'near-sdk-js'
import { isUndefined } from 'lodash-es'

@NearBindgen
class Counter extends NearContract {
constructor({ initial = 0 }) {
super()
this.count = initial
@NearBindgen({})
class Counter {
constructor() {
this.count = 0
}

@call
Expand All @@ -30,9 +29,5 @@ class Counter extends NearContract {
getCount() {
return this.count
}

default() {
return new Counter({ initial: 0 })
}
}

17 changes: 4 additions & 13 deletions examples/src/counter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { NearContract, NearBindgen, near, call, view } from 'near-sdk-js'
import { NearBindgen, near, call, view, initialize } from 'near-sdk-js'
import { isUndefined } from 'lodash-es'
import { log } from './log'

@NearBindgen
class Counter extends NearContract {
count: number;

constructor({ initial = 0 }: { initial: number }) {
super()
this.count = initial
}
@NearBindgen({})
class Counter {
count: number = 0;

@call
increase({ n = 1 }: { n: number }) {
Expand All @@ -34,9 +29,5 @@ class Counter extends NearContract {
getCount(): number {
return this.count
}

default() {
return new Counter({ initial: 0 })
}
}

19 changes: 10 additions & 9 deletions examples/src/cross-contract-call.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { NearContract, NearBindgen, call, view, near, bytes } from 'near-sdk-js'
import { NearBindgen, call, view, initialize, near, bytes } from 'near-sdk-js'

@NearBindgen
class OnCall extends NearContract {
constructor({ statusMessageContract }) {
super()
@NearBindgen({ requireInit: true })
class OnCall {
constructor() {
this.personOnCall = ''
this.statusMessageContract = ''
}

@initialize
init({ statusMessageContract }) {
this.personOnCall = "undefined"
this.statusMessageContract = statusMessageContract
}
Expand Down Expand Up @@ -37,8 +42,4 @@ class OnCall extends NearContract {
near.log(`Returning person on-call: ${this.personOnCall}`)
return this.personOnCall
}

default() {
return new OnCall({ statusMessageContract: '' })
}
}
11 changes: 3 additions & 8 deletions examples/src/fungible-token-helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NearContract, NearBindgen, call, view } from "near-sdk-js";
import { NearBindgen, call, view } from "near-sdk-js";

@NearBindgen
class FungibleTokenHelper extends NearContract {
@NearBindgen({})
class FungibleTokenHelper {
constructor() {
super();
this.data = "";
}

Expand All @@ -17,8 +16,4 @@ class FungibleTokenHelper extends NearContract {
getContractData() {
return this.data;
}

default() {
return new FungibleTokenHelper();
}
}
22 changes: 10 additions & 12 deletions examples/src/fungible-token-lockable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
NearContract,
NearBindgen,
call,
view,
initialize,
near,
LookupMap,
} from 'near-sdk-js'
Expand Down Expand Up @@ -49,15 +49,17 @@ class Account {
}
}

@NearBindgen
class LockableFungibleToken extends NearContract {
constructor({ prefix, totalSupply }) {
super()
this.accounts = new LookupMap(prefix) // Account ID -> Account mapping
this.totalSupply = totalSupply // Total supply of the all tokens
@NearBindgen({ initRequired: true })
class LockableFungibleToken {
constructor() {
this.accounts = new LookupMap('a') // Account ID -> Account mapping
this.totalSupply = 0 // Total supply of the all tokens
}

init() {
@initialize
init({ prefix, totalSupply }) {
this.accounts = new LookupMap(prefix)
this.totalSupply = totalSupply
let ownerId = near.signerAccountId()
let ownerAccount = this.getAccount(ownerId)
ownerAccount.balance = this.totalSupply
Expand Down Expand Up @@ -224,8 +226,4 @@ class LockableFungibleToken extends NearContract {
getLockedBalance({ ownerId, escrowAccountId }) {
return this.getAccount(ownerId).getLockedBalance(escrowAccountId)
}

default() {
return new LockableFungibleToken({ prefix: '', totalSupply: 0 })
}
}
25 changes: 12 additions & 13 deletions examples/src/fungible-token.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {
NearContract,
NearBindgen,
call,
view,
initialize,
near,
LookupMap,
assert
} from 'near-sdk-js'

@NearBindgen
class FungibleToken extends NearContract {
constructor({ prefix, totalSupply }) {
super()
this.accounts = new LookupMap(prefix)
this.totalSupply = totalSupply
// In a real world Fungible Token contract, storage management is required to denfense drain-storage attack

@NearBindgen({ initRequired: true })
class FungibleToken {
constructor() {
this.accounts = new LookupMap('a')
this.totalSupply = 0
}

init() {
@initialize
init({ prefix, totalSupply }) {
this.accounts = new LookupMap(prefix)
this.totalSupply = totalSupply
this.accounts.set(near.signerAccountId(), this.totalSupply)
// In a real world Fungible Token contract, storage management is required to denfense drain-storage attack
}

internalDeposit({ accountId, amount }) {
Expand Down Expand Up @@ -71,8 +74,4 @@ class FungibleToken extends NearContract {
ftBalanceOf({ accountId }) {
return this.accounts.get(accountId) || '0'
}

default() {
return new FungibleToken({ prefix: '', totalSupply: 0 })
}
}
18 changes: 9 additions & 9 deletions examples/src/non-fungible-token-receiver.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { NearContract, NearBindgen, call, near, assert } from 'near-sdk-js'
import { NearBindgen, call, near, assert, initialize } from 'near-sdk-js'

@NearBindgen
class NftContract extends NearContract {
constructor({ nonFungibleTokenAccountId }) {
super()
@NearBindgen({ requireInit: true })
class NftContract {
constructor() {
this.nonFungibleTokenAccountId = ''
}

@initialize
init({ nonFungibleTokenAccountId }) {
this.nonFungibleTokenAccountId = nonFungibleTokenAccountId
}

Expand All @@ -24,8 +28,4 @@ class NftContract extends NearContract {
throw Error("unsupported msg")
}
}

default() {
return new NftContract({ nonFungibleTokenAccountId: '' })
}
}
19 changes: 10 additions & 9 deletions examples/src/non-fungible-token.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NearContract, NearBindgen, call, view, near, LookupMap, bytes, assert } from 'near-sdk-js'
import { NearBindgen, call, view, initialize, near, LookupMap, bytes, assert } from 'near-sdk-js'

class Token {
constructor(token_id, owner_id) {
Expand All @@ -7,10 +7,15 @@ class Token {
}
}

@NearBindgen
class NftContract extends NearContract {
constructor({ owner_id, owner_by_id_prefix }) {
super()
@NearBindgen({ requireInit: true })
class NftContract {
constructor() {
this.owner_id = ''
this.owner_by_id = new LookupMap('a')
}

@initialize
init({ owner_id, owner_by_id_prefix }) {
this.owner_id = owner_id
this.owner_by_id = new LookupMap(owner_by_id_prefix)
}
Expand Down Expand Up @@ -85,8 +90,4 @@ class NftContract extends NearContract {

return new Token(token_id, owner_id)
}

default() {
return new NftContract({ owner_id: '', owner_by_id_prefix: '' })
}
}
Loading

0 comments on commit ca8c480

Please sign in to comment.