tests: example rust contracts moved from warp-wasm-templates
This commit is contained in:
56
crates/pst/deploy/scripts/deploy.js
Normal file
56
crates/pst/deploy/scripts/deploy.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { WarpFactory, defaultCacheOptions } = require('warp-contracts');
|
||||
const { mineBlock } = require('./utils/mine-block');
|
||||
const { loadWallet, walletAddress } = require('./utils/load-wallet');
|
||||
const { connectArweave } = require('./utils/connect-arweave');
|
||||
|
||||
module.exports.deploy = async function (host, port, protocol, target, walletJwk) {
|
||||
const arweave = connectArweave(host, port, protocol);
|
||||
const warp = module.exports.getWarpInstance(port, target);
|
||||
const wallet = await loadWallet(arweave, walletJwk, target);
|
||||
const walletAddr = await walletAddress(arweave, wallet);
|
||||
const contractSrc = fs.readFileSync(path.join(__dirname, '../../contract/implementation/pkg/rust-contract_bg.wasm'));
|
||||
const stateFromFile = JSON.parse(fs.readFileSync(path.join(__dirname, '../state/init-state.json'), 'utf-8'));
|
||||
|
||||
const initialState = {
|
||||
...stateFromFile,
|
||||
...{
|
||||
owner: walletAddr,
|
||||
balances: {
|
||||
...stateFromFile.balances,
|
||||
[walletAddr]: 10000000,
|
||||
},
|
||||
},
|
||||
};
|
||||
const {contractTxId} = await warp.createContract.deploy(
|
||||
{
|
||||
wallet,
|
||||
initState: JSON.stringify(initialState),
|
||||
src: contractSrc,
|
||||
wasmSrcCodeDir: path.join(__dirname, '../../contract/implementation/src'),
|
||||
wasmGlueCode: path.join(__dirname, '../../contract/implementation/pkg/rust-contract.js'),
|
||||
}
|
||||
);
|
||||
fs.writeFileSync(path.join(__dirname, `../${target}/contract-tx-id.txt`), contractTxId);
|
||||
|
||||
if (target == 'testnet' || target == 'local') {
|
||||
await mineBlock(arweave);
|
||||
}
|
||||
|
||||
if (target == 'testnet') {
|
||||
console.log(`Check contract at https://sonar.warp.cc/#/app/contract/${contractTxId}?network=testnet`);
|
||||
} else {
|
||||
console.log('Contract tx id', contractTxId);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.getWarpInstance = function (port, target) {
|
||||
if (target == 'local') {
|
||||
return WarpFactory.forLocal(port);
|
||||
} else if (target == 'testnet') {
|
||||
return WarpFactory.forTestnet();
|
||||
} else {
|
||||
return WarpFactory.forMainnet({ ...defaultCacheOptions, inMemory: true });
|
||||
}
|
||||
}
|
||||
23
crates/pst/deploy/scripts/interact-balance.js
Normal file
23
crates/pst/deploy/scripts/interact-balance.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const { loadWallet } = require('./utils/load-wallet');
|
||||
const { connectArweave } = require('./utils/connect-arweave');
|
||||
const { connectPstContract } = require('./utils/connect-pst-contract');
|
||||
const { contractTxId } = require('./utils/contract-tx-id');
|
||||
|
||||
module.exports.interactBalance = async function (
|
||||
host,
|
||||
port,
|
||||
protocol,
|
||||
target,
|
||||
walletJwk
|
||||
) {
|
||||
const arweave = connectArweave(host, port, protocol);
|
||||
const wallet = await loadWallet(arweave, walletJwk, target, true);
|
||||
|
||||
const walletAddress = await arweave.wallets.jwkToAddress(wallet);
|
||||
|
||||
const txId = contractTxId(target);
|
||||
const pst = await connectPstContract(arweave, wallet, txId, target);
|
||||
const balance = await pst.currentBalance(walletAddress);
|
||||
|
||||
console.log(balance);
|
||||
};
|
||||
29
crates/pst/deploy/scripts/interact-transfer.js
Normal file
29
crates/pst/deploy/scripts/interact-transfer.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const { loadWallet } = require('./utils/load-wallet');
|
||||
const { connectArweave } = require('./utils/connect-arweave');
|
||||
const { connectPstContract } = require('./utils/connect-pst-contract');
|
||||
const { contractTxId } = require('./utils/contract-tx-id');
|
||||
const { mineBlock } = require('./utils/mine-block');
|
||||
|
||||
module.exports.interactTransfer = async function (host, port, protocol, target, walletJwk) {
|
||||
const arweave = connectArweave(host, port, protocol);
|
||||
const wallet = await loadWallet(arweave, walletJwk, target, true);
|
||||
const txId = contractTxId(target);
|
||||
const pst = await connectPstContract(arweave, wallet, txId, target);
|
||||
|
||||
const transferId = await pst.transfer({
|
||||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
|
||||
qty: 555,
|
||||
});
|
||||
|
||||
await mineBlock(arweave);
|
||||
const state = await pst.currentState();
|
||||
|
||||
console.log('Updated state:', state);
|
||||
console.log('Contract tx id', txId);
|
||||
|
||||
if (target == 'testnet') {
|
||||
console.log(`Check transfer interaction at https://sonar.warp.cc/#/app/interaction/${transferId}?network=testnet`);
|
||||
} else {
|
||||
console.log('Transfer tx id', transferId);
|
||||
}
|
||||
};
|
||||
22
crates/pst/deploy/scripts/read-contract-state.js
Normal file
22
crates/pst/deploy/scripts/read-contract-state.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const { loadWallet } = require('./utils/load-wallet');
|
||||
const { connectArweave } = require('./utils/connect-arweave');
|
||||
const { connectContract } = require('./utils/connect-contract');
|
||||
const { contractTxId } = require(`./utils/contract-tx-id`);
|
||||
|
||||
module.exports.readContractState = async function (
|
||||
host,
|
||||
port,
|
||||
protocol,
|
||||
target,
|
||||
walletJwk
|
||||
) {
|
||||
const arweave = connectArweave(host, port, protocol);
|
||||
const wallet = await loadWallet(arweave, walletJwk, target, true);
|
||||
|
||||
const txId = contractTxId(target);
|
||||
const contract = await connectContract(arweave, wallet, txId, target);
|
||||
const { cachedValue } = await contract.readState();
|
||||
|
||||
console.log('Current state:', cachedValue.state);
|
||||
console.log('Contract tx id', txId);
|
||||
};
|
||||
4
crates/pst/deploy/scripts/utils/addFunds.js
Normal file
4
crates/pst/deploy/scripts/utils/addFunds.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports.addFunds = async function (arweave, wallet) {
|
||||
const walletAddress = await arweave.wallets.getAddress(wallet);
|
||||
await arweave.api.get(`/mint/${walletAddress}/1000000000000000`);
|
||||
};
|
||||
9
crates/pst/deploy/scripts/utils/connect-arweave.js
Normal file
9
crates/pst/deploy/scripts/utils/connect-arweave.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const Arweave = require('arweave');
|
||||
|
||||
module.exports.connectArweave = function (host, port, protocol) {
|
||||
return Arweave.init({
|
||||
host: host,
|
||||
port: port,
|
||||
protocol: protocol,
|
||||
});
|
||||
};
|
||||
15
crates/pst/deploy/scripts/utils/connect-contract.js
Normal file
15
crates/pst/deploy/scripts/utils/connect-contract.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const { getWarpInstance } = require("../deploy");
|
||||
|
||||
module.exports.connectContract = async function (
|
||||
arweave,
|
||||
wallet,
|
||||
contractTxId,
|
||||
target
|
||||
) {
|
||||
console.log('Target:', target);
|
||||
|
||||
const warp = getWarpInstance(arweave.api.config.port, target);
|
||||
return warp
|
||||
.contract(contractTxId)
|
||||
.connect(wallet);
|
||||
};
|
||||
14
crates/pst/deploy/scripts/utils/connect-pst-contract.js
Normal file
14
crates/pst/deploy/scripts/utils/connect-pst-contract.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const { getWarpInstance } = require("../deploy");
|
||||
|
||||
module.exports.connectPstContract = async function (
|
||||
arweave,
|
||||
wallet,
|
||||
contractTxId,
|
||||
target
|
||||
) {
|
||||
|
||||
const warp = getWarpInstance(arweave.api.config.port, target);
|
||||
return warp
|
||||
.pst(contractTxId)
|
||||
.connect(wallet);
|
||||
};
|
||||
19
crates/pst/deploy/scripts/utils/contract-tx-id.js
Normal file
19
crates/pst/deploy/scripts/utils/contract-tx-id.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
module.exports.contractTxId = function (target) {
|
||||
let txId;
|
||||
try {
|
||||
txId = fs
|
||||
.readFileSync(
|
||||
path.join(__dirname, `../../../deploy/${target}/contract-tx-id.txt`),
|
||||
'utf-8'
|
||||
)
|
||||
.trim();
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
'Contract tx id file not found! Please run deploy script first.'
|
||||
);
|
||||
}
|
||||
|
||||
return txId;
|
||||
};
|
||||
10
crates/pst/deploy/scripts/utils/create-testnet-wallet.js
Normal file
10
crates/pst/deploy/scripts/utils/create-testnet-wallet.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
module.exports.generateWallet = async function (arweave, target) {
|
||||
const wallet = await arweave.wallets.generate();
|
||||
fs.writeFileSync(
|
||||
path.join(__dirname, `../../${target}/wallet_${target}.json`),
|
||||
JSON.stringify(wallet)
|
||||
);
|
||||
};
|
||||
33
crates/pst/deploy/scripts/utils/load-wallet.js
Normal file
33
crates/pst/deploy/scripts/utils/load-wallet.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require('fs');
|
||||
const { addFunds } = require('./addFunds');
|
||||
const { generateWallet } = require('./create-testnet-wallet');
|
||||
|
||||
const path = require('path');
|
||||
|
||||
module.exports.loadWallet = async function (
|
||||
arweave,
|
||||
walletJwk,
|
||||
target,
|
||||
generated
|
||||
) {
|
||||
let wallet;
|
||||
if (!generated) {
|
||||
await generateWallet(arweave, target);
|
||||
}
|
||||
|
||||
try {
|
||||
wallet = JSON.parse(fs.readFileSync(path.join(walletJwk), 'utf-8'));
|
||||
} catch (e) {
|
||||
throw new Error('Wallet file not found! Please run deploy script first.');
|
||||
}
|
||||
|
||||
if (target == 'testnet' || target == 'local') {
|
||||
await addFunds(arweave, wallet);
|
||||
}
|
||||
|
||||
return wallet;
|
||||
};
|
||||
|
||||
module.exports.walletAddress = async function (arweave, wallet) {
|
||||
return arweave.wallets.getAddress(wallet);
|
||||
};
|
||||
3
crates/pst/deploy/scripts/utils/mine-block.js
Normal file
3
crates/pst/deploy/scripts/utils/mine-block.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports.mineBlock = async function (arweave) {
|
||||
await arweave.api.get('mine');
|
||||
};
|
||||
Reference in New Issue
Block a user