Skip to content

Instantly share code, notes, and snippets.

@apow2
apow2 / contractBase.sol
Last active April 30, 2021 00:35
Basic Contract
contract Base{
event Transfer(address from, address to, uint256 tokenId);
mapping (uint256 => address) public IndexToOwner;
mapping (address => uint256) public ownershipTokenCount;
mapping (uint256 => address) public IndexToApproved;
function getMyTokens(address myaddress) view public returns(uint[] memory){
uint num = ownershipTokenCount[myaddress];
uint found = 0;
@apow2
apow2 / signAndSendTransaction.js
Created April 30, 2021 00:43
Signing and sending a transaction
const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider('https://api.avax-test.network/ext/bc/C/rpc'));
let account = web3.eth.accounts.privateKeyToAccount(yourPrivateKey);
let address = account["address"];
let accsign = account["signTransaction"]; // function for signing transactions
chain = {name: "fuji", networkId: 1, chainId: 43113}; //chain information
mycommon = {customChain:chain};
@apow2
apow2 / tipjar.sol
Last active April 30, 2021 01:00
a tip jar
contract TipJar{
address owner;
constructor() public{
owner = msg.sender;
}
receive() external payable{
}
fallback() external payable {
@apow2
apow2 / sink.sol
Created April 30, 2021 01:00
sink
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;
// This contract keeps all Ether sent to it with no way
// to get it back.
contract Sink {
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
const tx = {
from: address,
to: contractAddress,
gas: 20000,
value: 1000,
data: myContract.methods.setAddresses(MetaDataAddress, BINAddress).encodeABI(),
common: {customChain: {networkId:1, chainId: 43113}} //chain information
};
@apow2
apow2 / pinata_ipfs.js
Last active April 30, 2021 01:41
pin to ipfs
const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK(yourKey, yourKey2);
const fs = require('fs');
function getStream(){
return fs.createReadStream(PATH_TO_IMAGES+files[i]);
}
let readableStreamForFile = getStream();
//pin on IPFS
cur_promise = await new Promise((resolve, reject) => {
@apow2
apow2 / axios.js
Last active June 27, 2023 06:20
use axios to get data from IPFS
import axios from "axios";
let myContract;
let init = async () => {
await ethStore.setProvider("https://api.avax-test.network/ext/bc/C/rpc");
await ethStore.setBrowserProvider();
let contract = await new $web3.eth.Contract(abi, contractAddress);
if ($chainId != 43113) {
alert("Warning: You are not connected to Avalanche Fuji test-net!");
}
import {web3, selectedAccount, chainId, connected} from "svelte-web3";
import {contractAddress, abi} from "./utils/contract.js";
let myContract;
let init = async () => {
await ethStore.setProvider("https://api.avax-test.network/ext/bc/C/rpc");
await ethStore.setBrowserProvider();
let contract = await new $web3.eth.Contract(abi, contractAddress);
if ($chainId != 43113) {
alert("Warning: You are not connected to Avalanche Fuji test-net!");
}
export const transfer= (contract) => (to) => async (tokenId) => {
let myContract = await contract;
try {
let tx = await get(web3).eth.sendTransaction({
gasPrice: get(web3).utils.toHex(get(web3).utils.toWei("225", "gwei")),
gasLimit: get(web3).utils.toHex("600000"),
from: get(selectedAccount),
to: contractAddress,
data: myContract.methods.transfer(to, tokenId).encodeABI(),
});
<script>
import {get} from "svelte/store";
export const getBalance = (contract) => async (e) => {
await contract; // ensure $web3.eth is defined
return get(web3).eth.getBalance(get(selectedAccount));
};
export let checkAccount = get(selectedAccount) || "0x0000000000000000000000000000000000000000";
let myContract = getContext("myContract");
</script>