-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
238 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,144 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import test from 'ava'; | ||
|
||
|
||
test.before(async t => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
const highlevelPromise = await root.createSubAccount('highlevel-promise', {initialBalance: '100100N'}); | ||
await highlevelPromise.deploy('build/highlevel-promise.wasm'); | ||
|
||
// Create and deploy callee contract | ||
const calleeContract = await root.createSubAccount('callee-contract'); | ||
await calleeContract.deploy('build/promise_api.wasm'); | ||
|
||
// Test users | ||
const ali = await root.createSubAccount('ali'); | ||
const bob = await root.createSubAccount('bob'); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, highlevelPromise, ali, bob, calleeContract }; | ||
}); | ||
|
||
test.after(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('highlevel promise create account, transfer', async t => { | ||
const { bob, highlevelPromise } = t.context.accounts; | ||
|
||
let r = await bob.callRaw(highlevelPromise, 'test_promise_batch_create_transfer', '', {gas: '100 Tgas'}); | ||
t.is(r.result.receipts_outcome[1].outcome.executor_id, highlevelPromise.getSubAccount('a').accountId); | ||
t.is(r.result.receipts_outcome[1].outcome.status.SuccessValue, ''); | ||
|
||
let balance = await highlevelPromise.getSubAccount('a').balance() | ||
t.is(balance.total.toString(), '10000000000000000000000000') | ||
}); | ||
|
||
test('highlevel promise stake', async t => { | ||
const { highlevelPromise } = t.context.accounts; | ||
await highlevelPromise.callRaw(highlevelPromise, 'test_promise_batch_stake', '', {gas: '100 Tgas'}); | ||
let balance = await highlevelPromise.balance(); | ||
t.is(balance.staked.toString(), '100000000000000000000000000000'); | ||
}); | ||
|
||
test('highlevel promise add full access key', async t => { | ||
const { bob, highlevelPromise } = t.context.accounts; | ||
let r = await bob.callRaw(highlevelPromise, 'test_promise_add_full_access_key', '', {gas: '100 Tgas'}); | ||
t.is(r.result.status.SuccessValue, ''); | ||
}); | ||
|
||
test('highlevel promise add function call key', async t => { | ||
const { bob, highlevelPromise } = t.context.accounts; | ||
let r = await bob.callRaw(highlevelPromise, 'test_promise_add_function_call_access_key', '', {gas: '100 Tgas'}); | ||
t.is(r.result.status.SuccessValue, ''); | ||
}); | ||
|
||
test('highlevel promise delete account', async t => { | ||
const { bob, highlevelPromise } = t.context.accounts; | ||
let r = await bob.callRaw(highlevelPromise, 'test_delete_account', '', {gas: '100 Tgas'}); | ||
t.is(r.result.status.SuccessValue, ''); | ||
t.is(await highlevelPromise.getSubAccount('e').exists(), false); | ||
}); | ||
|
||
test('highlevel promise then', async t => { | ||
const { ali, highlevelPromise, calleeContract } = t.context.accounts; | ||
let r = await ali.callRaw(highlevelPromise, 'test_promise_then', '', {gas: '70 Tgas'}); | ||
// call the callee | ||
t.is(r.result.receipts_outcome[1].outcome.executor_id, calleeContract.accountId); | ||
t.deepEqual(JSON.parse(Buffer.from(r.result.receipts_outcome[1].outcome.status.SuccessValue, 'base64')), { | ||
currentAccountId: calleeContract.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: 'abc', | ||
}); | ||
|
||
// the callback scheduled by promise_then | ||
t.is(r.result.receipts_outcome[3].outcome.executor_id, highlevelPromise.accountId); | ||
t.deepEqual(JSON.parse(Buffer.from(r.result.receipts_outcome[3].outcome.status.SuccessValue, 'base64')), { | ||
currentAccountId: highlevelPromise.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: '{"callbackArg1":"def"}', | ||
promiseResults: [JSON.stringify({ | ||
currentAccountId: calleeContract.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: 'abc', | ||
})], | ||
callbackArg1: 'def' | ||
}); | ||
}); | ||
|
||
test('highlevel promise and', async t => { | ||
const { ali, highlevelPromise, calleeContract } = t.context.accounts; | ||
let r = await ali.callRaw(highlevelPromise, 'test_promise_and', '', {gas: '150 Tgas'}); | ||
|
||
// console.log(JSON.stringify(r, null, 2)) | ||
// promise and schedule to call the callee | ||
t.is(r.result.receipts_outcome[1].outcome.executor_id, calleeContract.accountId); | ||
t.deepEqual(JSON.parse(Buffer.from(r.result.receipts_outcome[1].outcome.status.SuccessValue, 'base64')), { | ||
currentAccountId: calleeContract.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: 'abc', | ||
}); | ||
|
||
// promise and schedule to call the callee, with different args | ||
t.is(r.result.receipts_outcome[3].outcome.executor_id, calleeContract.accountId); | ||
t.deepEqual(JSON.parse(Buffer.from(r.result.receipts_outcome[3].outcome.status.SuccessValue, 'base64')), { | ||
currentAccountId: calleeContract.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: 'def', | ||
}); | ||
|
||
// the callback scheduled by promise_then on the promise created by promise_and | ||
t.is(r.result.receipts_outcome[5].outcome.executor_id, highlevelPromise.accountId); | ||
t.deepEqual(JSON.parse(Buffer.from(r.result.receipts_outcome[5].outcome.status.SuccessValue, 'base64')), { | ||
currentAccountId: highlevelPromise.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: '{"callbackArg1":"ghi"}', | ||
promiseResults: [JSON.stringify({ | ||
currentAccountId: calleeContract.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: 'abc', | ||
}), JSON.stringify({ | ||
currentAccountId: calleeContract.accountId, | ||
signerAccountId: ali.accountId, | ||
predecessorAccountId: highlevelPromise.accountId, | ||
input: 'def', | ||
})], | ||
callbackArg1: 'ghi', | ||
}); | ||
}); | ||
|
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,87 @@ | ||
import {NearBindgen, call, view, NearPromise, near, bytes} from 'near-sdk-js' | ||
import { PublicKey } from 'near-sdk-js/lib/types'; | ||
|
||
function callingData() { | ||
return { | ||
currentAccountId: near.currentAccountId(), | ||
signerAccountId: near.signerAccountId(), | ||
predecessorAccountId: near.predecessorAccountId(), | ||
input: near.input(), | ||
} | ||
} | ||
|
||
function arrayN(n) { | ||
return [...Array(Number(n)).keys()] | ||
} | ||
|
||
@NearBindgen({}) | ||
class HighlevelPromiseContract { | ||
@call | ||
test_promise_batch_stake() { | ||
let promise = NearPromise.new('highlevel-promise.test.near') | ||
.stake(100000000000000000000000000000n, new PublicKey(near.signerAccountPk())) | ||
|
||
return promise; | ||
} | ||
|
||
@call | ||
test_promise_batch_create_transfer() { | ||
let promise = NearPromise.new('a.highlevel-promise.test.near') | ||
.createAccount() | ||
.transfer(10000000000000000000000000n) | ||
return promise; | ||
} | ||
|
||
@call | ||
test_promise_add_full_access_key() { | ||
let promise = NearPromise.new('c.highlevel-promise.test.near') | ||
.createAccount() | ||
.transfer(10000000000000000000000000n) | ||
.addFullAccessKey(new PublicKey(near.signerAccountPk())) | ||
return promise; | ||
} | ||
|
||
@call | ||
test_promise_add_function_call_access_key() { | ||
let promise = NearPromise.new('d.highlevel-promise.test.near') | ||
.createAccount() | ||
.transfer(10000000000000000000000000n) | ||
.addAccessKey(new PublicKey(near.signerAccountPk()), 250000000000000000000000n, 'highlevel-promise.test.near', 'test_promise_batch_create_transfer') | ||
return promise; | ||
} | ||
|
||
@call | ||
test_delete_account() { | ||
let promise = NearPromise.new('e.highlevel-promise.test.near') | ||
.createAccount() | ||
.transfer(10000000000000000000000000n) | ||
.deleteAccount(near.signerAccountId()) | ||
return promise; | ||
} | ||
|
||
@call | ||
test_promise_then() { | ||
let promise = NearPromise.new('callee-contract.test.near') | ||
.functionCall('cross_contract_callee', bytes('abc'), 0, 2 * Math.pow(10, 13)) | ||
.then(NearPromise.new('highlevel-promise.test.near').functionCall('cross_contract_callback', bytes(JSON.stringify({callbackArg1: 'def'})), 0, 2 * Math.pow(10, 13))) | ||
return promise; | ||
} | ||
|
||
@call | ||
test_promise_and() { | ||
let promise = NearPromise.new('callee-contract.test.near') | ||
.functionCall('cross_contract_callee', bytes('abc'), 0, 2 * Math.pow(10, 13)) | ||
let promise2 = NearPromise.new('callee-contract.test.near') | ||
.functionCall('cross_contract_callee', bytes('def'), 0, 2 * Math.pow(10, 13)) | ||
let retPromise = promise.and(promise2).then( | ||
NearPromise.new('highlevel-promise.test.near') | ||
.functionCall('cross_contract_callback', bytes(JSON.stringify({callbackArg1: 'ghi'})), 0, 3 * Math.pow(10, 13))) | ||
|
||
return retPromise; | ||
} | ||
|
||
@call | ||
cross_contract_callback({callbackArg1}) { | ||
return {...callingData(), promiseResults: arrayN(near.promiseResultsCount()).map(i => near.promiseResult(i)), callbackArg1} | ||
} | ||
} |